forked from jonashaag/bjoern
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest.c
355 lines (312 loc) · 8.95 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <Python.h>
#include "bytesio.h"
#include "request.h"
#include "filewrapper.h"
static inline void PyDict_ReplaceKey(PyObject* dict, PyObject* k1, PyObject* k2);
static PyObject* wsgi_http_header(string header);
static http_parser_settings parser_settings;
static PyObject* wsgi_base_dict = NULL;
Request* Request_new(int client_fd, const char* client_addr)
{
Request* request = malloc(sizeof(Request));
#ifdef DEBUG
static unsigned long request_id = 0;
request->id = request_id++;
#endif
request->client_fd = client_fd;
request->client_addr = PyUnicode_FromString(client_addr);
http_parser_init((http_parser*)&request->parser, HTTP_REQUEST);
request->parser.parser.data = request;
Request_reset(request);
return request;
}
void Request_reset(Request* request)
{
memset(&request->state, 0, sizeof(Request) - (size_t)&((Request*)NULL)->state);
request->state.response_length_unknown = true;
request->parser.body = (string){NULL, 0};
}
void Request_free(Request* request)
{
Request_clean(request);
Py_DECREF(request->client_addr);
free(request);
}
void Request_clean(Request* request)
{
if(request->iterable) {
/* Call 'iterable.close()' if available */
PyObject* close_method = PyObject_GetAttr(request->iterable, _close);
if(close_method == NULL) {
if(PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
} else {
PyObject_CallObject(close_method, NULL);
Py_DECREF(close_method);
}
if(PyErr_Occurred()) PyErr_Print();
Py_DECREF(request->iterable);
}
Py_XDECREF(request->iterator);
Py_XDECREF(request->headers);
Py_XDECREF(request->status);
}
/* Parse stuff */
void Request_parse(Request* request, const char* data, const size_t data_len)
{
assert(data_len);
size_t nparsed = http_parser_execute((http_parser*)&request->parser,
&parser_settings, data, data_len);
if(nparsed != data_len)
request->state.error_code = HTTP_BAD_REQUEST;
}
#define REQUEST ((Request*)parser->data)
#define PARSER ((bj_parser*)parser)
#define UPDATE_LENGTH(name) \
/* Update the len of a header field/value.
*
* Short explaination of the pointer arithmetics fun used here:
*
* [old header data ] ...stuff... [ new header data ]
* ^-------------- A -------------^--------B--------^
*
* A = XXX- PARSER->XXX.data
* B = len
* A + B = old header start to new header end
*/ \
do { PARSER->name.len = (name - PARSER->name.data) + len; } while(0)
#define _set_header(k, v) PyDict_SetItem(REQUEST->headers, k, v);
#define _set_header_free_value(k, v) \
do { \
PyObject* val = (v); \
_set_header(k, val); \
Py_DECREF(val); \
} while(0)
#define _set_header_free_both(k, v) \
do { \
PyObject* key = (k); \
PyObject* val = (v); \
_set_header(key, val); \
Py_DECREF(key); \
Py_DECREF(val); \
} while(0)
static int
on_message_begin(http_parser* parser)
{
REQUEST->headers = PyDict_New();
PARSER->field = (string){NULL, 0};
PARSER->value = (string){NULL, 0};
return 0;
}
static int
on_path(http_parser* parser, const char* path, size_t len)
{
if(!(len = unquote_url_inplace((char*)path, len)))
return 1;
_set_header_free_value(_PATH_INFO, PyUnicode_FromStringAndSize(path, len));
return 0;
}
static int
on_query_string(http_parser* parser, const char* query, size_t len)
{
_set_header_free_value(_QUERY_STRING, PyUnicode_FromStringAndSize(query, len));
return 0;
}
static int
on_header_field(http_parser* parser, const char* field, size_t len)
{
if(PARSER->value.data) {
/* Store previous header and start a new one */
_set_header_free_both(
wsgi_http_header(PARSER->field),
PyUnicode_FromStringAndSize(PARSER->value.data, PARSER->value.len)
);
} else if(PARSER->field.data) {
UPDATE_LENGTH(field);
return 0;
}
PARSER->field = (string){(char*)field, len};
PARSER->value = (string){NULL, 0};
return 0;
}
static int
on_header_value(http_parser* parser, const char* value, size_t len)
{
if(PARSER->value.data) {
UPDATE_LENGTH(value);
} else {
/* Start a new value */
PARSER->value = (string){(char*)value, len};
}
return 0;
}
static int
on_headers_complete(http_parser* parser)
{
if(PARSER->field.data) {
_set_header_free_both(
wsgi_http_header(PARSER->field),
PyUnicode_FromStringAndSize(PARSER->value.data, PARSER->value.len)
);
}
return 0;
}
static int
on_body(http_parser* parser, const char* data, const size_t len)
{
bytesio *body;
body = (bytesio*)PyDict_GetItem(REQUEST->headers, _wsgi_input);
if(body == NULL) {
if(!parser->content_length) {
REQUEST->state.error_code = HTTP_LENGTH_REQUIRED;
return 1;
}
PyObject *arglist = Py_BuildValue("(y#)", NULL, parser->content_length);
body = (bytesio*)PyObject_CallObject((PyObject *) &BytesIO_Type, arglist);
Py_XDECREF(arglist);
if(body == NULL)
return 1;
_set_header(_wsgi_input, (PyObject*)body);
Py_DECREF(body);
}
bytesio_write_bytes(body, data, len);
return 0;
}
static int
on_message_complete(http_parser* parser)
{
/* HTTP_CONTENT_{LENGTH,TYPE} -> CONTENT_{LENGTH,TYPE} */
PyDict_ReplaceKey(REQUEST->headers, _HTTP_CONTENT_LENGTH, _CONTENT_LENGTH);
PyDict_ReplaceKey(REQUEST->headers, _HTTP_CONTENT_TYPE, _CONTENT_TYPE);
/* SERVER_PROTOCOL (REQUEST_PROTOCOL) */
_set_header(_SERVER_PROTOCOL, parser->http_minor == 1 ? _HTTP_1_1 : _HTTP_1_0);
/* REQUEST_METHOD */
if(parser->method == HTTP_GET) {
/* I love useless micro-optimizations. */
_set_header(_REQUEST_METHOD, _GET);
} else {
_set_header_free_value(_REQUEST_METHOD,
PyUnicode_FromString(http_method_str(parser->method)));
}
/* REMOTE_ADDR */
_set_header(_REMOTE_ADDR, REQUEST->client_addr);
PyObject* body = PyDict_GetItem(REQUEST->headers, _wsgi_input);
if(body) {
/* We abused the `pos` member for tracking the amount of data copied from
* the buffer in on_body, so reset it to zero here. */
((bytesio*)body)->pos = 0;
} else {
/* Request has no body */
// XXX create an empty bytesio
//_set_header_free_value(_wsgi_input, (_empty_string));
}
PyDict_Update(REQUEST->headers, wsgi_base_dict);
REQUEST->state.parse_finished = true;
return 0;
}
static PyObject*
wsgi_http_header(string header)
{
int size = header.len+strlen("HTTP_");
char dest[size];
int i = 0;
dest[i++] = 'H';
dest[i++] = 'T';
dest[i++] = 'T';
dest[i++] = 'P';
dest[i++] = '_';
while(header.len--) {
char c = *header.data++;
if(c == '-')
dest[i++] = '_';
else if(c >= 'a' && c <= 'z')
dest[i++] = c - ('a'-'A');
else
dest[i++] = c;
}
return (PyObject *)PyUnicode_FromStringAndSize(dest, size);
}
static inline void
PyDict_ReplaceKey(PyObject* dict, PyObject* old_key, PyObject* new_key)
{
PyObject* value = PyDict_GetItem(dict, old_key);
if(value) {
Py_INCREF(value);
PyDict_DelItem(dict, old_key);
PyDict_SetItem(dict, new_key, value);
Py_DECREF(value);
}
}
static http_parser_settings
parser_settings = {
on_message_begin, on_path, on_query_string, NULL, NULL, on_header_field,
on_header_value, on_headers_complete, on_body, on_message_complete
};
void _initialize_request_module(const char* server_host, const int server_port)
{
if(wsgi_base_dict == NULL) {
wsgi_base_dict = PyDict_New();
/* dct['wsgi.file_wrapper'] = FileWrapper */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.file_wrapper",
(PyObject*)&FileWrapper_Type
);
/* dct['SCRIPT_NAME'] = '' */
PyDict_SetItemString(
wsgi_base_dict,
"SCRIPT_NAME",
_empty_string
);
/* dct['wsgi.version'] = (1, 0) */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.version",
PyTuple_Pack(2, PyLong_FromLong(1), PyLong_FromLong(0))
);
/* dct['wsgi.url_scheme'] = 'http'
* (This can be hard-coded as there is no TLS support in bjoern.) */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.url_scheme",
PyUnicode_FromString("http")
);
/* dct['wsgi.errors'] = sys.stderr */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.errors",
PySys_GetObject("stderr")
);
/* dct['wsgi.multithread'] = True
* If I correctly interpret the WSGI specs, this means
* "Can the server be ran in a thread?" */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.multithread",
Py_True
);
/* dct['wsgi.multiprocess'] = True
* ... and this one "Can the server process be forked?" */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.multiprocess",
Py_True
);
/* dct['wsgi.run_once'] = False (bjoern is no CGI gateway) */
PyDict_SetItemString(
wsgi_base_dict,
"wsgi.run_once",
Py_False
);
}
PyDict_SetItemString(
wsgi_base_dict,
"SERVER_NAME",
PyUnicode_FromString(server_host)
);
PyDict_SetItemString(
wsgi_base_dict,
"SERVER_PORT",
server_port ? PyUnicode_FromFormat("%d", server_port) : _empty_string
);
}