Klaus Demo ~jonashaag/bjoern / 8773d32
Fast file transfers, part 1: Provide a file wrapper. Jonas Haag 9 years ago
7 changed file(s) with 84 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
11 #include "server.h"
22 #include "wsgi.h"
33 #include "bjoernmodule.h"
4 #include "filewrapper.h"
45
56
67 PyDoc_STRVAR(listen_doc,
7778
7879 PyMODINIT_FUNC initbjoern()
7980 {
81 _init_common();
82 _init_filewrapper();
83
84 PyType_Ready(&FileWrapper_Type);
85 assert(FileWrapper_Type.tp_flags & Py_TPFLAGS_READY);
8086 PyType_Ready(&StartResponse_Type);
8187 assert(StartResponse_Type.tp_flags & Py_TPFLAGS_READY);
82 _initialize_static_strings();
8388
8489 PyObject* bjoern_module = Py_InitModule("bjoern", Bjoern_FunctionTable);
8590 PyModule_AddObject(bjoern_module, "version", Py_BuildValue("(iii)", 1, 1, 1));
2525 return len;
2626 }
2727
28 void _initialize_static_strings()
28 void _init_common()
2929 {
3030 #define _(name) _##name = PyString_FromString(#name)
3131 _(REMOTE_ADDR); _(PATH_INFO); _(QUERY_STRING); _(close);
66 #include <stdbool.h>
77 #include <string.h>
88
9 #define TYPE_ERROR_INNER(what, expected, ...) \
10 PyErr_Format(PyExc_TypeError, what " must be " expected " " __VA_ARGS__)
11 #define TYPE_ERROR(what, expected, got) \
12 TYPE_ERROR_INNER(what, expected, "(got '%.200s' object instead)", Py_TYPE(got)->tp_name)
13
914 typedef struct { char* data; size_t len; } string;
1015
1116 enum http_status { HTTP_BAD_REQUEST = 1, HTTP_LENGTH_REQUIRED, HTTP_SERVER_ERROR };
1217
1318 size_t unquote_url_inplace(char* url, size_t len);
14 void _initialize_static_strings();
19 void (_init_common)();
1520
1621 PyObject *_REMOTE_ADDR, *_PATH_INFO, *_QUERY_STRING, *_REQUEST_METHOD, *_GET,
1722 *_HTTP_CONTENT_LENGTH, *_CONTENT_LENGTH, *_HTTP_CONTENT_TYPE, *_CONTENT_TYPE,
0 #include "filewrapper.h"
1
2 static PyObject*
3 FileWrapper_New(PyObject* self, PyObject* args, PyObject* kwargs)
4 {
5 PyObject* file;
6 if(!PyArg_ParseTuple(args, "O:FileWrapper", &file))
7 return NULL;
8 if(!PyFile_Check(file)) {
9 TYPE_ERROR("FileWrapper argument", "file", file);
10 return NULL;
11 }
12 Py_INCREF(file);
13 FileWrapper* wrapper = PyObject_NEW(FileWrapper, &FileWrapper_Type);
14 wrapper->file = file;
15 return (PyObject*)wrapper;
16 }
17
18 static PyObject*
19 FileWrapper_GetAttrO(PyObject* self, PyObject* name)
20 {
21 return PyObject_GetAttr(((FileWrapper*)self)->file, name);
22 }
23
24 static PyObject*
25 FileWrapper_Iter(PyObject* self)
26 {
27 return PyObject_GetIter(((FileWrapper*)self)->file);
28 }
29
30 static void
31 FileWrapper_dealloc(PyObject* self)
32 {
33 Py_DECREF(((FileWrapper*)self)->file);
34 PyObject_FREE(self);
35 }
36
37 PyTypeObject FileWrapper_Type = {
38 PyVarObject_HEAD_INIT(NULL, 0)
39 "FileWrapper", /* tp_name (__name__) */
40 sizeof(FileWrapper), /* tp_basicsize */
41 0, /* tp_itemsize */
42 (destructor)FileWrapper_dealloc, /* tp_dealloc */
43 };
44
45 void _init_filewrapper()
46 {
47 FileWrapper_Type.tp_new = FileWrapper_New;
48 FileWrapper_Type.tp_iter = FileWrapper_Iter;
49 FileWrapper_Type.tp_getattro = FileWrapper_GetAttrO;
50 FileWrapper_Type.tp_flags |= Py_TPFLAGS_DEFAULT;
51 }
0 #include "common.h"
1
2 PyTypeObject FileWrapper_Type;
3
4 typedef struct {
5 PyObject_HEAD
6 PyObject* file;
7 } FileWrapper;
8
9 void (_init_filewrapper)();
00 #include <Python.h>
11 #include <cStringIO.h>
22 #include "request.h"
3 #include "filewrapper.h"
34
45 static inline void PyDict_ReplaceKey(PyObject* dict, PyObject* k1, PyObject* k2);
56 static PyObject* wsgi_http_header(string header);
286287 PycString_IMPORT;
287288 wsgi_base_dict = PyDict_New();
288289
290 /* dct['wsgi.file_wrapper'] = FileWrapper */
291 PyDict_SetItemString(
292 wsgi_base_dict,
293 "wsgi.file_wrapper",
294 (PyObject*)&FileWrapper_Type
295 );
296
289297 /* dct['SCRIPT_NAME'] = '' */
290298 PyDict_SetItemString(
291299 wsgi_base_dict,
00 #include "common.h"
11 #include "bjoernmodule.h"
22 #include "wsgi.h"
3
4 #define TYPE_ERROR_INNER(what, expected, ...) \
5 PyErr_Format(PyExc_TypeError, what " must be " expected " " __VA_ARGS__)
6 #define TYPE_ERROR(what, expected, got) \
7 TYPE_ERROR_INNER(what, expected, "(got '%.200s' object instead)", Py_TYPE(got)->tp_name)
83
94 static PyObject* (start_response)(PyObject* self, PyObject* args, PyObject *kwargs);
105 static size_t wsgi_getheaders(Request*, PyObject* buf);
326321 }
327322
328323 PyTypeObject StartResponse_Type = {
329 PyObject_HEAD_INIT(NULL)
330 0, /* ob_size (deprecated) */
324 PyVarObject_HEAD_INIT(NULL, 0)
331325 "start_response", /* tp_name (__name__) */
332326 sizeof(StartResponse), /* tp_basicsize */
333327 0, /* tp_itemsize */