s4:web_server/*.c - optimise includes
[sfrench/samba-autobuild/.git] / source4 / web_server / wsgi.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5
6    Implementation of the WSGI interface described in PEP0333 
7    (http://www.python.org/dev/peps/pep-0333)
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <Python.h>
24 #include "includes.h"
25 #include "web_server/web_server.h"
26 #include "../lib/util/dlinklist.h"
27 #include "lib/tls/tls.h"
28 #include "lib/tsocket/tsocket.h"
29
30 /* There's no Py_ssize_t in 2.4, apparently */
31 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
32 typedef int Py_ssize_t;
33 typedef inquiry lenfunc;
34 typedef intargfunc ssizeargfunc;
35 #endif
36
37 #ifndef Py_RETURN_NONE
38 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
39 #endif
40
41 typedef struct {
42         PyObject_HEAD
43         struct websrv_context *web;
44 } web_request_Object;
45
46 static PyObject *start_response(PyObject *self, PyObject *args, PyObject *kwargs)
47 {
48         PyObject *response_header, *exc_info = NULL;
49         char *status;
50         Py_ssize_t i;
51         const char *kwnames[] = {
52                 "status", "response_header", "exc_info", NULL
53         };
54         web_request_Object *py_web = (web_request_Object *)self;
55         struct websrv_context *web = py_web->web;
56         struct http_header *headers = NULL;
57
58         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|O:start_response", discard_const_p(char *, kwnames), &status, &response_header, &exc_info)) {
59                 return NULL;
60         }
61
62         /* FIXME: exc_info */
63
64         if (!PyList_Check(response_header)) {
65                 PyErr_SetString(PyExc_TypeError, "response_header should be list");
66                 return NULL;
67         }
68
69         for (i = 0; i < PyList_Size(response_header); i++) {
70                 struct http_header *hdr = talloc_zero(web, struct http_header);
71                 PyObject *item = PyList_GetItem(response_header, i);
72                 PyObject *py_name, *py_value;
73
74                 if (!PyTuple_Check(item)) {
75                         PyErr_SetString(PyExc_TypeError, "Expected tuple");
76                         return NULL;
77                 }
78
79                 if (PyTuple_Size(item) != 2) {
80                         PyErr_SetString(PyExc_TypeError, "header tuple has invalid size, expected 2");
81                         return NULL;
82                 }
83
84                 py_name = PyTuple_GetItem(item, 0);
85
86                 if (!PyString_Check(py_name)) {
87                         PyErr_SetString(PyExc_TypeError, "header name should be string");
88                         return NULL;
89                 }
90
91                 py_value = PyTuple_GetItem(item, 1);
92                 if (!PyString_Check(py_value)) {
93                         PyErr_SetString(PyExc_TypeError, "header value should be string");
94                         return NULL;
95                 }
96
97                 hdr->name = talloc_strdup(hdr, PyString_AsString(py_name));
98                 hdr->value = talloc_strdup(hdr, PyString_AsString(py_value));
99                 DLIST_ADD(headers, hdr);
100         }
101
102         websrv_output_headers(web, status, headers);
103
104         Py_RETURN_NONE;
105 }
106
107 static PyMethodDef web_request_methods[] = {
108         { "start_response", (PyCFunction)start_response, METH_VARARGS|METH_KEYWORDS, NULL },
109         { NULL }
110 };
111
112
113 PyTypeObject web_request_Type = {
114         PyObject_HEAD_INIT(NULL) 0,
115         .tp_name = "wsgi.Request",
116         .tp_methods = web_request_methods,
117         .tp_basicsize = sizeof(web_request_Object),
118         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
119 };
120
121 typedef struct {
122         PyObject_HEAD
123 } error_Stream_Object;
124
125 static PyObject *py_error_flush(PyObject *self, PyObject *args, PyObject *kwargs)
126 {
127         /* Nothing to do here */
128         Py_RETURN_NONE;
129 }
130
131 static PyObject *py_error_write(PyObject *self, PyObject *args, PyObject *kwargs)
132 {
133         const char *kwnames[] = { "str", NULL };
134         char *str = NULL;
135
136         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:write", discard_const_p(char *, kwnames), &str)) {
137                 return NULL;
138         }
139
140         DEBUG(0, ("WSGI App: %s", str));
141
142         Py_RETURN_NONE;
143 }
144
145 static PyObject *py_error_writelines(PyObject *self, PyObject *args, PyObject *kwargs)
146 {
147         const char *kwnames[] = { "seq", NULL };
148         PyObject *seq = NULL, *item;
149
150         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:writelines", discard_const_p(char *, kwnames), &seq)) {
151                 return NULL;
152         }
153         
154         while ((item = PyIter_Next(seq))) {
155                 char *str = PyString_AsString(item);
156
157                 DEBUG(0, ("WSGI App: %s", str));
158         }
159
160         Py_RETURN_NONE;
161 }
162
163 static PyMethodDef error_Stream_methods[] = {
164         { "flush", (PyCFunction)py_error_flush, METH_VARARGS|METH_KEYWORDS, NULL },
165         { "write", (PyCFunction)py_error_write, METH_VARARGS|METH_KEYWORDS, NULL },
166         { "writelines", (PyCFunction)py_error_writelines, METH_VARARGS|METH_KEYWORDS, NULL },
167         { NULL, NULL, 0, NULL }
168 };
169
170 PyTypeObject error_Stream_Type = {
171         PyObject_HEAD_INIT(NULL) 0,
172         .tp_name = "wsgi.ErrorStream",
173         .tp_basicsize = sizeof(error_Stream_Object),
174         .tp_methods = error_Stream_methods,
175         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
176 };
177
178 typedef struct {
179         PyObject_HEAD
180         struct websrv_context *web;
181         size_t offset;
182 } input_Stream_Object;
183
184 static PyObject *py_input_read(PyObject *_self, PyObject *args, PyObject *kwargs)
185 {
186         const char *kwnames[] = { "size", NULL };
187         PyObject *ret;
188         input_Stream_Object *self = (input_Stream_Object *)_self;
189         int size = -1;
190
191         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", discard_const_p(char *, kwnames), &size))
192                 return NULL;
193         
194         /* Don't read beyond buffer boundaries */
195         if (size == -1)
196                 size = self->web->input.partial.length-self->offset;
197         else
198                 size = MIN(size, self->web->input.partial.length-self->offset);
199
200         ret = PyString_FromStringAndSize((char *)self->web->input.partial.data+self->offset, size);
201         self->offset += size;
202
203         return ret;
204 }
205
206 static PyObject *py_input_readline(PyObject *_self)
207 {
208         /* FIXME */
209         PyErr_SetString(PyExc_NotImplementedError, 
210                         "readline() not yet implemented");
211         return NULL;
212 }
213
214 static PyObject *py_input_readlines(PyObject *_self, PyObject *args, PyObject *kwargs)
215 {
216         const char *kwnames[] = { "hint", NULL };
217         int hint;
218
219         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", discard_const_p(char *, kwnames), &hint))
220                 return NULL;
221         
222         /* FIXME */
223         PyErr_SetString(PyExc_NotImplementedError, 
224                         "readlines() not yet implemented");
225         return NULL;
226 }
227
228 static PyObject *py_input___iter__(PyObject *_self)
229 {
230         /* FIXME */
231         PyErr_SetString(PyExc_NotImplementedError, 
232                         "__iter__() not yet implemented");
233         return NULL;
234 }
235
236 static PyMethodDef input_Stream_methods[] = {
237         { "read", (PyCFunction)py_input_read, METH_VARARGS|METH_KEYWORDS, NULL },
238         { "readline", (PyCFunction)py_input_readline, METH_NOARGS, NULL },
239         { "readlines", (PyCFunction)py_input_readlines, METH_VARARGS|METH_KEYWORDS, NULL },
240         { "__iter__", (PyCFunction)py_input___iter__, METH_NOARGS, NULL },
241         { NULL, NULL, 0, NULL }
242 };
243
244 PyTypeObject input_Stream_Type = {
245         PyObject_HEAD_INIT(NULL) 0,
246         .tp_name = "wsgi.InputStream",
247         .tp_basicsize = sizeof(input_Stream_Object),
248         .tp_methods = input_Stream_methods,
249         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
250 };
251
252 static PyObject *Py_InputHttpStream(struct websrv_context *web)
253 {
254         input_Stream_Object *ret = PyObject_New(input_Stream_Object, &input_Stream_Type);
255         ret->web = web;
256         ret->offset = 0;
257         return (PyObject *)ret;
258 }
259
260 static PyObject *Py_ErrorHttpStream(void)
261 {
262         error_Stream_Object *ret = PyObject_New(error_Stream_Object, &error_Stream_Type);
263         return (PyObject *)ret;
264 }
265
266 static PyObject *create_environ(bool tls, int content_length, struct http_header *headers, const char *request_method, const char *servername, int serverport, PyObject *inputstream, const char *request_string)
267 {
268         PyObject *env;
269         PyObject *errorstream;
270         PyObject *py_scheme;
271         struct http_header *hdr;
272         char *questionmark;
273         
274         env = PyDict_New();
275         if (env == NULL) {
276                 return NULL;
277         }
278
279         errorstream = Py_ErrorHttpStream();
280         if (errorstream == NULL) {
281                 Py_DECREF(env);
282                 Py_DECREF(inputstream);
283                 return NULL;
284         }
285
286         PyDict_SetItemString(env, "wsgi.input", inputstream);
287         PyDict_SetItemString(env, "wsgi.errors", errorstream);
288         PyDict_SetItemString(env, "wsgi.version", Py_BuildValue("(i,i)", 1, 0));
289         PyDict_SetItemString(env, "wsgi.multithread", Py_False);
290         PyDict_SetItemString(env, "wsgi.multiprocess", Py_True);
291         PyDict_SetItemString(env, "wsgi.run_once", Py_False);
292         PyDict_SetItemString(env, "SERVER_PROTOCOL", PyString_FromString("HTTP/1.0"));
293         if (content_length > 0) {
294                 PyDict_SetItemString(env, "CONTENT_LENGTH", PyLong_FromLong(content_length));
295         }
296         PyDict_SetItemString(env, "REQUEST_METHOD", PyString_FromString(request_method));
297
298         questionmark = strchr(request_string, '?');
299         if (questionmark == NULL) {
300                 PyDict_SetItemString(env, "SCRIPT_NAME", PyString_FromString(request_string));
301         } else {
302                 PyDict_SetItemString(env, "QUERY_STRING", PyString_FromString(questionmark+1));
303                 PyDict_SetItemString(env, "SCRIPT_NAME", PyString_FromStringAndSize(request_string, questionmark-request_string));
304         }
305         
306         PyDict_SetItemString(env, "SERVER_NAME", PyString_FromString(servername));
307         PyDict_SetItemString(env, "SERVER_PORT", PyInt_FromLong(serverport));
308         for (hdr = headers; hdr; hdr = hdr->next) {
309                 char *name;
310                 if (!strcasecmp(hdr->name, "Content-Type")) {
311                         PyDict_SetItemString(env, "CONTENT_TYPE", PyString_FromString(hdr->value));
312                 } else { 
313                         if (asprintf(&name, "HTTP_%s", hdr->name) < 0) {
314                                 Py_DECREF(env);
315                                 Py_DECREF(inputstream);
316                                 PyErr_NoMemory();
317                                 return NULL;
318                         }
319                         PyDict_SetItemString(env, name, PyString_FromString(hdr->value));
320                         free(name);
321                 }
322         }
323
324         if (tls) {
325                 py_scheme = PyString_FromString("https");
326         } else {
327                 py_scheme = PyString_FromString("http");
328         }
329         PyDict_SetItemString(env, "wsgi.url_scheme", py_scheme);
330
331         return env;
332 }
333
334 static void wsgi_process_http_input(struct web_server_data *wdata,
335                                     struct websrv_context *web)
336 {
337         PyObject *py_environ, *result, *item, *iter;
338         PyObject *request_handler = (PyObject *)wdata->private_data;
339         struct tsocket_address *my_address = web->conn->local_address;
340         const char *addr = "0.0.0.0";
341         uint16_t port = 0;
342         web_request_Object *py_web = PyObject_New(web_request_Object, &web_request_Type);
343         py_web->web = web;
344
345         if (tsocket_address_is_inet(my_address, "ip")) {
346                 addr = tsocket_address_inet_addr_string(my_address, wdata);
347                 port = tsocket_address_inet_port(my_address);
348         }
349
350         py_environ = create_environ(tls_enabled(web->conn->socket),
351                                     web->input.content_length, 
352                                     web->input.headers, 
353                                     web->input.post_request?"POST":"GET",
354                                     addr,
355                                     port,
356                                     Py_InputHttpStream(web),
357                                     web->input.url
358                                     );
359         if (py_environ == NULL) {
360                 DEBUG(0, ("Unable to create WSGI environment object\n"));
361                 return;
362         }
363
364         result = PyObject_CallMethod(request_handler, discard_const_p(char, "__call__"), discard_const_p(char, "OO"),
365                                        py_environ, PyObject_GetAttrString((PyObject *)py_web, "start_response"));
366
367         if (result == NULL) {
368                 DEBUG(0, ("error while running WSGI code\n"));
369                 return;
370         }
371
372         iter = PyObject_GetIter(result);
373         Py_DECREF(result);
374
375         /* Now, iter over all the data returned */
376
377         while ((item = PyIter_Next(iter))) {
378                 websrv_output(web, PyString_AsString(item), PyString_Size(item));
379                 Py_DECREF(item);
380         }
381
382         Py_DECREF(iter);
383 }
384
385 bool wsgi_initialize(struct web_server_data *wdata)
386 {
387         PyObject *py_swat;
388
389         Py_Initialize();
390
391         if (PyType_Ready(&web_request_Type) < 0)
392                 return false;
393
394         if (PyType_Ready(&input_Stream_Type) < 0)
395                 return false;
396
397         if (PyType_Ready(&error_Stream_Type) < 0)
398                 return false;
399
400         wdata->http_process_input = wsgi_process_http_input;
401         py_swat = PyImport_Import(PyString_FromString("swat"));
402         if (py_swat == NULL) {
403                 DEBUG(0, ("Unable to find SWAT\n"));
404                 return false;
405         }
406         wdata->private_data = py_swat;
407         return true;
408 }