Klaus Demo bjoern / 6ef8f62
Fix #138 Jonas Haag 1 year, 11 months ago
5 changed file(s) with 58 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
2929 void _init_common()
3030 {
3131
32 #define _(name) _##name = _Unicode_FromString(#name)
32 #define _(name) _##name = _UnicodeUTF8_FromString(#name)
3333 _(REMOTE_ADDR);
3434 _(PATH_INFO);
3535 _(QUERY_STRING);
4444 _(CONTENT_LENGTH);
4545 _(HTTP_CONTENT_TYPE);
4646 _(CONTENT_TYPE);
47 _(HTTP_);
48 _(http);
4749
4850 _(BytesIO);
4951 _(write);
5153 _(seek);
5254 #undef _
5355
54 _HTTP_1_1 = _Unicode_FromString("HTTP/1.1");
55 _HTTP_1_0 = _Unicode_FromString("HTTP/1.0");
56 _wsgi_input = _Unicode_FromString("wsgi.input");
57 _empty_string = _Unicode_FromString("");
56 _HTTP_1_1 = _UnicodeUTF8_FromString("HTTP/1.1");
57 _HTTP_1_0 = _UnicodeUTF8_FromString("HTTP/1.0");
58 _wsgi_input = _UnicodeUTF8_FromString("wsgi.input");
59 _empty_string = _UnicodeUTF8_FromString("");
5860 _empty_bytes = _Bytes_FromString("");
5961 }
2121 PyObject *_REMOTE_ADDR, *_PATH_INFO, *_QUERY_STRING, *_REQUEST_METHOD, *_GET,
2222 *_HTTP_CONTENT_LENGTH, *_CONTENT_LENGTH, *_HTTP_CONTENT_TYPE,
2323 *_CONTENT_TYPE, *_SERVER_PROTOCOL, *_SERVER_NAME, *_SERVER_PORT,
24 *_HTTP_1_1, *_HTTP_1_0, *_wsgi_input, *_close, *_empty_string,
25 *_empty_bytes, *_BytesIO, *_write, *_read, *_seek;
24 *_http, *_HTTP_, *_HTTP_1_1, *_HTTP_1_0, *_wsgi_input, *_close,
25 *_empty_string, *_empty_bytes, *_BytesIO, *_write, *_read, *_seek;
2626
2727 #ifdef DEBUG
2828 #define DBG_REQ(request, ...) \
44 #define NOP do{}while(0)
55 #define PyFile_IncUseCount(file) NOP
66 #define PyFile_DecUseCount(file) NOP
7 #define _Bytes_AS_DATA(obj) PyBytes_AS_STRING(obj)
8 #define _Bytes_FromString(name) PyBytes_FromString(name)
9 #define _Unicode_FromString(name) PyUnicode_FromString(name)
7 #define _Bytes_AS_DATA(bytes) PyBytes_AS_STRING(bytes)
8 #define _Bytes_FromString(bytes) PyBytes_FromString(bytes)
109 #define _Bytes_FromStringAndSize(data, len) PyBytes_FromStringAndSize(data, len)
11 #define _Unicode_FromStringAndSize(data, len) PyUnicode_FromStringAndSize(data, len)
12 #define _Bytes_GET_SIZE(obj) PyBytes_GET_SIZE(obj)
13 #define _Unicode_GET_SIZE(obj) PyUnicode_GET_LENGTH(obj)
14 #define _Bytes_Check(obj) PyBytes_Check(obj)
15 #define _Bytes_Resize(obj, len) _PyBytes_Resize(obj, len)
10 #define _Bytes_GET_SIZE(bytes) PyBytes_GET_SIZE(bytes)
11 #define _Bytes_Check(bytes) PyBytes_Check(bytes)
12 #define _Bytes_Resize(bytes, len) _PyBytes_Resize(bytes, len)
13 #define _BytesLatin1_FromUnicode(u) PyUnicode_AsLatin1String(u)
14 #define _UnicodeUTF8_FromString(data) PyUnicode_FromString(data)
15 #define _Unicode_GET_SIZE(u) PyUnicode_GET_LENGTH(u)
16 #define _UnicodeLatin1_FromStringAndSize(data, len) PyUnicode_DecodeLatin1(data, len, "replace")
17 #define _Unicode_Concat(u1, u2) PyUnicode_Concat(u1, u2)
1618 #define _FromLong(n) PyLong_FromLong(n)
17 #define _Unicode_EncodeLatin1(u) PyUnicode_AsLatin1String(u)
18 #define _Unicode_Concat(a, b) PyUnicode_Concat(a, b)
1919
2020 #else
2121
22 #define _Bytes_AS_DATA(obj) PyString_AS_STRING(obj)
23 #define _Bytes_FromString(name) PyString_FromString(name)
24 #define _Unicode_FromString(name) PyString_FromString(name)
22 #define _Bytes_AS_DATA(bytes) PyString_AS_STRING(bytes)
23 #define _Bytes_FromString(bytes) PyString_FromString(bytes)
2524 #define _Bytes_FromStringAndSize(data, len) PyString_FromStringAndSize(data, len)
26 #define _Unicode_FromStringAndSize(data, len) PyString_FromStringAndSize(data, len)
27 #define _Bytes_GET_SIZE(obj) PyString_GET_SIZE(obj)
28 #define _Unicode_GET_SIZE(obj) PyString_GET_SIZE(obj)
29 #define _Bytes_Check(obj) PyString_Check(obj)
30 #define _Bytes_Resize(obj, len) _PyString_Resize(obj, len)
25 #define _Bytes_GET_SIZE(bytes) PyString_GET_SIZE(bytes)
26 #define _Bytes_Check(bytes) PyString_Check(bytes)
27 #define _Bytes_Resize(bytes, len) _PyString_Resize(bytes, len)
28 #define _BytesLatin1_FromUnicode(u) (Py_INCREF(u),u)
3129 #define _FromLong(n) PyInt_FromLong(n)
32 #define _Unicode_EncodeLatin1(u) (Py_INCREF(u),u)
3330
34 static PyObject *_Unicode_Concat(PyObject *l, PyObject *r) {
35 PyObject *ret = l;
31 #define _UnicodeUTF8_FromString(data) PyString_FromString(data)
32 #define _UnicodeLatin1_FromString(name) PyString_FromString(name)
33 #define _Unicode_GET_SIZE(u) PyString_GET_SIZE(u)
3634
37 Py_INCREF(l); /* reference to old left will be stolen */
38 PyString_Concat(&ret, r);
35 static PyObject* _UnicodeLatin1_FromStringAndSize(const char* data, Py_ssize_t len)
36 {
37 PyObject* tmp = PyUnicode_DecodeLatin1(data, len, "replace");
38 if (tmp == NULL) {
39 return NULL;
40 }
41 PyObject* tmp2 = PyUnicode_AsLatin1String(tmp);
42 Py_DECREF(tmp);
43 return tmp2;
44 }
3945
40 return ret;
46 static PyObject *_Unicode_Concat(PyObject *l, PyObject *r)
47 {
48 PyObject *ret = l;
49
50 Py_INCREF(l); /* reference to old left will be stolen */
51 PyString_Concat(&ret, r);
52
53 return ret;
4154 }
4255 #endif
4356
1818 #endif
1919 request->server_info = server_info;
2020 request->client_fd = client_fd;
21 request->client_addr = _Unicode_FromString(client_addr);
21 request->client_addr = _UnicodeUTF8_FromString(client_addr);
2222 http_parser_init((http_parser*)&request->parser, HTTP_REQUEST);
2323 request->parser.parser.data = request;
2424 Request_reset(request);
9292
9393 static void
9494 _set_or_append_header(PyObject* headers, PyObject* k, const char* val, size_t len) {
95 PyObject *py_val = _Unicode_FromStringAndSize(val, len);
95 PyObject *py_val = _UnicodeLatin1_FromStringAndSize(val, len);
9696 PyObject *py_val_old = PyDict_GetItem(headers, k);
9797
9898 if (py_val_old) {
135135 if(PARSER->last_call_was_header_value) {
136136 /* We are starting a new header */
137137 Py_XDECREF(PARSER->field);
138 PARSER->field = _Unicode_FromStringAndSize("HTTP_", 5);
138 Py_INCREF(_HTTP_);
139 PARSER->field = _HTTP_;
139140 PARSER->last_call_was_header_value = false;
140141 PARSER->invalid_header = false;
141142 }
163164
164165 /* Append field name to the part we got from previous call */
165166 PyObject *field_old = PARSER->field;
166 PyObject *field_new = _Unicode_FromStringAndSize(field_processed, len);
167 PyObject *field_new = _UnicodeLatin1_FromStringAndSize(field_processed, len);
167168 PARSER->field = _Unicode_Concat(field_old, field_new);
168169 Py_DECREF(field_old);
169170 Py_DECREF(field_new);
170171
171 return 0;
172 return PARSER->field == NULL;
172173 }
173174
174175 static int
222223 _set_header(_REQUEST_METHOD, _GET);
223224 } else {
224225 _set_header_free_value(_REQUEST_METHOD,
225 _Unicode_FromString(http_method_str(parser->method))
226 _UnicodeUTF8_FromString(http_method_str(parser->method))
226227 );
227228 }
228229
299300
300301 /* dct['wsgi.url_scheme'] = 'http'
301302 * (This can be hard-coded as there is no TLS support in bjoern.) */
303 Py_INCREF(_http);
302304 PyDict_SetItemString(
303305 wsgi_base_dict,
304306 "wsgi.url_scheme",
305 _Unicode_FromString("http")
307 _http
306308 );
307309
308310 /* dct['wsgi.errors'] = sys.stderr */
183183 PyObject* unicode_field = PyTuple_GET_ITEM(tuple, 0);
184184 PyObject* unicode_value = PyTuple_GET_ITEM(tuple, 1);
185185
186 PyObject* bytes_field = _Unicode_EncodeLatin1(unicode_field);
187 PyObject* bytes_value = _Unicode_EncodeLatin1(unicode_value);
186 PyObject* bytes_field = _BytesLatin1_FromUnicode(unicode_field);
187 PyObject* bytes_value = _BytesLatin1_FromUnicode(unicode_value);
188188
189189 if (bytes_field == NULL || bytes_value == NULL) {
190190 Py_XDECREF(bytes_field);
344344 return NULL;
345345 }
346346
347 request->status = _Unicode_EncodeLatin1(status_unicode);
347 request->status = _BytesLatin1_FromUnicode(status_unicode);
348348 if (request->status == NULL) {
349349 return NULL;
350350 }