forked from cherokee/webserver
/
request.c
226 lines (182 loc) · 5.77 KB
/
request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "request.h"
ret_t
cherokee_request_header_init (cherokee_request_header_t *request)
{
ret_t ret;
/* Init the node list information
*/
INIT_LIST_HEAD (LIST(&request->list_node));
/* Set default values
*/
request->method = http_get;
request->version = http_version_11;
request->auth = http_auth_nothing;
request->proxy = false;
request->keepalive = true;
request->pipeline = 1;
request->post_len = 0;
ret = cherokee_url_init (&request->url);
if (unlikely(ret < ret_ok)) return ret;
cherokee_buffer_init (&request->extra_headers);
cherokee_buffer_init (&request->user);
cherokee_buffer_init (&request->password);
return ret_ok;
}
ret_t
cherokee_request_header_mrproper (cherokee_request_header_t *request)
{
cherokee_buffer_mrproper (&request->user);
cherokee_buffer_mrproper (&request->password);
cherokee_buffer_mrproper (&request->extra_headers);
cherokee_url_mrproper (&request->url);
return ret_ok;
}
ret_t
cherokee_request_header_clean (cherokee_request_header_t *request)
{
request->method = http_get;
request->version = http_version_11;
request->keepalive = true;
request->pipeline = 1;
request->post_len = 0;
cherokee_url_clean (&request->url);
return ret_ok;
}
ret_t
cherokee_request_header_uses_proxy (cherokee_request_header_t *request, cherokee_boolean_t proxy)
{
request->proxy = proxy;
return ret_ok;
}
ret_t
cherokee_request_header_parse_string (cherokee_request_header_t *request, cherokee_buffer_t *url_string)
{
return cherokee_url_parse (&request->url, url_string, &request->user, &request->password);
}
ret_t
cherokee_request_header_build_string (cherokee_request_header_t *request, cherokee_buffer_t *buf, cherokee_buffer_t *tmp1, cherokee_buffer_t *tmp2)
{
ret_t ret;
const char *ptr;
cuint_t len;
cherokee_url_t *url = REQUEST_URL(request);
/* 200 bytes should be enough for a small header
*/
cherokee_buffer_ensure_size (buf, 200);
/* Add main request line:
* GET /dir/object HTTP/1.1
*/
ret = cherokee_http_method_to_string (request->method, &ptr, &len);
if (ret != ret_ok) return ret;
ret = cherokee_buffer_add (buf, ptr, len);
if (ret != ret_ok) return ret;
cherokee_buffer_add_str (buf, " ");
/* Check if the requests goes through a proxy
*/
if (request->proxy) {
cherokee_buffer_add_str (buf, "http://");
cherokee_buffer_add_buffer (buf, URL_HOST(url));
}
cherokee_buffer_add_buffer (buf, URL_REQUEST(url));
switch (REQUEST_VERSION(request)) {
case http_version_11:
cherokee_buffer_add_str (buf, " HTTP/1.1" CRLF);
break;
case http_version_10:
cherokee_buffer_add_str (buf, " HTTP/1.0" CRLF);
break;
default:
SHOULDNT_HAPPEN;
return ret_error;
}
/* Add "Host:" header - in HTTP/1.1
*/
if (REQUEST_VERSION(request) == http_version_11) {
cherokee_buffer_add_str (buf, "Host: ");
cherokee_buffer_add_buffer (buf, URL_HOST(url));
cherokee_buffer_add_str (buf, CRLF);
}
/* Post information
*/
if (request->post_len != 0) {
/* "Content-Length: " FMT_OFFSET CRLF, request->post_len;
*/
cherokee_buffer_add_str (buf, "Content-Length: ");
cherokee_buffer_add_ullong10 (buf, (cullong_t) request->post_len);
cherokee_buffer_add_str (buf, CRLF);
}
/* Add "Connection:" header
*/
if (REQUEST_KEEPALIVE(request)) {
cherokee_buffer_add_str (buf, "Connection: Keep-Alive"CRLF);
} else {
cherokee_buffer_add_str (buf, "Connection: close"CRLF);
}
/* Authentication
*/
if (! cherokee_buffer_is_empty (&request->user) ||
! cherokee_buffer_is_empty (&request->password)) {
cherokee_buffer_clean (tmp1);
cherokee_buffer_clean (tmp2);
cherokee_buffer_add_buffer (tmp1, &request->user);
cherokee_buffer_add_char (tmp1, ':');
cherokee_buffer_add_buffer (tmp1, &request->password);
cherokee_buffer_encode_base64 (tmp1, tmp2);
cherokee_buffer_add_str (buf, "Authorization: Basic ");
cherokee_buffer_add_buffer (buf, tmp2);
cherokee_buffer_add_str (buf, CRLF);
}
/* Extra headers
*/
if (! cherokee_buffer_is_empty (&request->extra_headers)) {
cherokee_buffer_add_buffer (buf, &request->extra_headers);
}
/* Finish the header
*/
cherokee_buffer_add_str (buf, CRLF);
return ret_ok;
}
ret_t
cherokee_request_header_set_auth (cherokee_request_header_t *request,
cherokee_http_auth_t auth,
cherokee_buffer_t *user,
cherokee_buffer_t *password)
{
request->auth = auth;
cherokee_buffer_clean (&request->user);
cherokee_buffer_clean (&request->password);
cherokee_buffer_add_buffer (&request->user, user);
cherokee_buffer_add_buffer (&request->password, password);
return ret_ok;
}
ret_t
cherokee_request_header_add_header (cherokee_request_header_t *request, char *ptr, cuint_t len)
{
cherokee_buffer_ensure_addlen (&request->extra_headers, len + CSZLEN(CRLF));
cherokee_buffer_add (&request->extra_headers, ptr, len);
cherokee_buffer_add_str (&request->extra_headers, CRLF);
return ret_ok;
}