|
| 1 | +#include "common.h" |
1 | 2 | #include "server.h"
|
| 3 | +#include "bjoernmodule.h" |
2 | 4 | #include "wsgi.h"
|
3 | 5 |
|
| 6 | +static PyKeywordFunc start_response; |
| 7 | +static bool wsgi_sendfile(Request*); |
| 8 | +static bool wsgi_senditer(Request*); |
| 9 | + |
| 10 | +typedef struct { |
| 11 | + PyObject_HEAD |
| 12 | + Request* request; |
| 13 | +} StartResponse; |
| 14 | +static PyTypeObject StartResponse_Type; |
| 15 | + |
| 16 | + |
4 | 17 | bool
|
5 | 18 | wsgi_call_application(Request* request)
|
6 | 19 | {
|
7 |
| - request->response = PyString_FromString("Hello World!\n"); |
8 |
| - request->state = REQUEST_WSGI_RESPONSE; |
| 20 | + StartResponse* start_response = PyObject_NEW(StartResponse, &StartResponse_Type); |
| 21 | + start_response->request = request; |
| 22 | + PyObject* args = PyTuple_Pack(/* size */ 2, request->headers, start_response); |
| 23 | + |
| 24 | + GIL_LOCK(0); |
| 25 | + PyObject* retval = PyObject_CallObject(wsgi_app, args); |
| 26 | + GIL_UNLOCK(0); |
| 27 | + |
| 28 | + Py_DECREF(start_response); |
| 29 | + if(retval == NULL) |
| 30 | + return false; |
| 31 | + |
| 32 | + if(PyFile_Check(retval)) { |
| 33 | + request->state = REQUEST_WSGI_FILE_RESPONSE; |
| 34 | + request->response = retval; |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + if(PyString_Check(retval)) { |
| 39 | + request->state = REQUEST_WSGI_STRING_RESPONSE; |
| 40 | + request->response = retval; |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + PyObject* iter = PyObject_GetIter(retval); |
| 45 | + TYPECHECK2(iter, PyIter, PySeqIter, "wsgi application return value", false); |
| 46 | + |
| 47 | + Py_DECREF(retval); |
| 48 | + request->state = REQUEST_WSGI_ITER_RESPONSE; |
| 49 | + request->response = iter; |
| 50 | + /* Get the first item of the iterator, because that may execute code that |
| 51 | + * invokes `start_response` (which might not have been invoked yet). |
| 52 | + * Think of the following scenario: |
| 53 | + * |
| 54 | + * def app(environ, start_response): |
| 55 | + * start_response('200 Ok', ...) |
| 56 | + * yield 'Hello World' |
| 57 | + * |
| 58 | + * That would make `app` return an iterator (more precisely, a generator). |
| 59 | + * Unfortunately, `start_response` wouldn't be called until the first item |
| 60 | + * of that iterator is requested; `start_response` however has to be called |
| 61 | + * _before_ the wsgi body is sent, because it passes the HTTP headers. |
| 62 | + */ |
| 63 | + request->response_curiter = PyIter_Next(iter); |
| 64 | + |
9 | 65 | return true;
|
10 | 66 | }
|
11 | 67 |
|
12 | 68 | bool
|
13 | 69 | wsgi_send_response(Request* request)
|
14 | 70 | {
|
15 |
| - sendall(request, PyString_AS_STRING(request->response), PyString_GET_SIZE(request->response)); |
16 |
| - request->state = REQUEST_WSGI_DONE; |
17 |
| - return true; |
| 71 | + switch(request->state) { |
| 72 | + case REQUEST_WSGI_STRING_RESPONSE: |
| 73 | + sendall( |
| 74 | + request, |
| 75 | + PyString_AS_STRING(request->response), |
| 76 | + PyString_GET_SIZE(request->response) |
| 77 | + ); |
| 78 | + return true; |
| 79 | + |
| 80 | + case REQUEST_WSGI_FILE_RESPONSE: |
| 81 | + return wsgi_sendfile(request); |
| 82 | + |
| 83 | + case REQUEST_WSGI_ITER_RESPONSE: |
| 84 | + return wsgi_senditer(request); |
| 85 | + |
| 86 | + default: |
| 87 | + assert(0); |
| 88 | + } |
18 | 89 | }
|
| 90 | + |
| 91 | +static bool |
| 92 | +wsgi_sendfile(Request* request) |
| 93 | +{ |
| 94 | + assert(0); |
| 95 | +} |
| 96 | + |
| 97 | +static bool |
| 98 | +wsgi_senditer(Request* request) |
| 99 | +{ |
| 100 | + if(request->response_curiter == NULL) |
| 101 | + return true; |
| 102 | + |
| 103 | + TYPECHECK(request->response_curiter, PyString, "wsgi iterable items", true); |
| 104 | + sendall( |
| 105 | + request, |
| 106 | + PyString_AS_STRING(request->response_curiter), |
| 107 | + PyString_GET_SIZE(request->response_curiter) |
| 108 | + ); |
| 109 | + |
| 110 | + GIL_LOCK(0); |
| 111 | + request->response_curiter = PyIter_Next(request->response); |
| 112 | + GIL_UNLOCK(0); |
| 113 | + return request->response_curiter != NULL; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +static PyObject* |
| 119 | +start_response(PyObject* self, PyObject* args, PyObject* kwargs) |
| 120 | +{ |
| 121 | + Request* req = ((StartResponse*)self)->request; |
| 122 | + |
| 123 | + if(!PyArg_UnpackTuple(args, |
| 124 | + /* Python function name */ "start_response", |
| 125 | + /* min args */ 2, /* max args */ 2, |
| 126 | + &req->status, |
| 127 | + &req->response_headers |
| 128 | + )) |
| 129 | + return NULL; |
| 130 | + |
| 131 | + TYPECHECK(req->status, PyString, "start_response argument 1", NULL); |
| 132 | + TYPECHECK(req->response_headers, PyList, "start_response argument 2", NULL); |
| 133 | + |
| 134 | + Py_RETURN_NONE; |
| 135 | +} |
| 136 | + |
| 137 | +static PyTypeObject StartResponse_Type = { |
| 138 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 139 | + "start_response", |
| 140 | + sizeof(StartResponse), |
| 141 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | + start_response /* __call__ */ |
| 143 | +}; |
0 commit comments