2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
6 Implementation of the WSGI interface described in PEP0333
7 (http://www.python.org/dev/peps/pep-0333)
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.
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.
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/>.
24 #include "web_server/web_server.h"
25 #include "lib/util/dlinklist.h"
26 #include "lib/util/data_blob.h"
31 struct websrv_context *web;
34 static PyObject *start_response(PyObject *self, PyObject *args, PyObject *kwargs)
36 PyObject *response_header, *exc_info = NULL;
39 const char *kwnames[] = {
40 "status", "response_header", "exc_info", NULL
42 web_request_Object *py_web = (web_request_Object *)self;
43 struct websrv_context *web = py_web->web;
44 struct http_header *headers = NULL;
46 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|O:start_response", discard_const_p(char *, kwnames), &status, &response_header, &exc_info)) {
52 if (!PyList_Check(response_header)) {
53 PyErr_SetString(PyExc_TypeError, "response_header should be list");
57 for (i = 0; i < PyList_Size(response_header); i++) {
58 struct http_header *hdr = talloc_zero(web, struct http_header);
59 PyObject *item = PyList_GetItem(response_header, i);
60 PyObject *py_name, *py_value;
62 if (!PyTuple_Check(item)) {
63 PyErr_SetString(PyExc_TypeError, "Expected tuple");
67 if (PyTuple_Size(item) != 2) {
68 PyErr_SetString(PyExc_TypeError, "header tuple has invalid size, expected 2");
72 py_name = PyTuple_GetItem(item, 0);
74 if (!PyString_Check(py_name)) {
75 PyErr_SetString(PyExc_TypeError, "header name should be string");
79 py_value = PyTuple_GetItem(item, 1);
80 if (!PyString_Check(py_value)) {
81 PyErr_SetString(PyExc_TypeError, "header value should be string");
85 hdr->name = talloc_strdup(hdr, PyString_AsString(py_name));
86 hdr->value = talloc_strdup(hdr, PyString_AsString(py_value));
87 DLIST_ADD(headers, hdr);
90 websrv_output_headers(web, status, headers);
95 static PyMethodDef web_request_methods[] = {
96 { "start_response", (PyCFunction)start_response, METH_VARARGS|METH_KEYWORDS, NULL },
101 PyTypeObject web_request_Type = {
102 PyObject_HEAD_INIT(NULL) 0,
103 .tp_name = "wsgi.Request",
104 .tp_methods = web_request_methods,
105 .tp_basicsize = sizeof(web_request_Object),
106 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
111 } error_Stream_Object;
113 static PyObject *py_error_flush(PyObject *self, PyObject *args, PyObject *kwargs)
115 /* Nothing to do here */
119 static PyObject *py_error_write(PyObject *self, PyObject *args, PyObject *kwargs)
121 const char *kwnames[] = { "str", NULL };
124 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:write", discard_const_p(char *, kwnames), &str)) {
128 DEBUG(0, ("WSGI App: %s", str));
133 static PyObject *py_error_writelines(PyObject *self, PyObject *args, PyObject *kwargs)
135 const char *kwnames[] = { "seq", NULL };
136 PyObject *seq = NULL, *item;
138 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:writelines", discard_const_p(char *, kwnames), &seq)) {
142 while ((item = PyIter_Next(seq))) {
143 char *str = PyString_AsString(item);
145 DEBUG(0, ("WSGI App: %s", str));
151 static PyMethodDef error_Stream_methods[] = {
152 { "flush", (PyCFunction)py_error_flush, METH_VARARGS|METH_KEYWORDS, NULL },
153 { "write", (PyCFunction)py_error_write, METH_VARARGS|METH_KEYWORDS, NULL },
154 { "writelines", (PyCFunction)py_error_writelines, METH_VARARGS|METH_KEYWORDS, NULL },
155 { NULL, NULL, 0, NULL }
158 PyTypeObject error_Stream_Type = {
159 PyObject_HEAD_INIT(NULL) 0,
160 .tp_name = "wsgi.ErrorStream",
161 .tp_basicsize = sizeof(error_Stream_Object),
162 .tp_methods = error_Stream_methods,
163 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
168 } input_Stream_Object;
170 static PyObject *py_input_read(PyObject *self, PyObject *args, PyObject *kwargs)
176 static PyObject *py_input_readline(PyObject *self, PyObject *args, PyObject *kwargs)
182 static PyObject *py_input_readlines(PyObject *self, PyObject *args, PyObject *kwargs)
188 static PyObject *py_input___iter__(PyObject *self, PyObject *args, PyObject *kwargs)
194 static PyMethodDef input_Stream_methods[] = {
195 { "read", (PyCFunction)py_input_read, METH_VARARGS|METH_KEYWORDS, NULL },
196 { "readline", (PyCFunction)py_input_readline, METH_VARARGS|METH_KEYWORDS, NULL },
197 { "readlines", (PyCFunction)py_input_readlines, METH_VARARGS|METH_KEYWORDS, NULL },
198 { "__iter__", (PyCFunction)py_input___iter__, METH_VARARGS|METH_KEYWORDS, NULL },
199 { NULL, NULL, 0, NULL }
202 PyTypeObject input_Stream_Type = {
203 PyObject_HEAD_INIT(NULL) 0,
204 .tp_name = "wsgi.InputStream",
205 .tp_basicsize = sizeof(input_Stream_Object),
206 .tp_methods = input_Stream_methods,
207 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
210 static PyObject *Py_InputHttpStream(void *foo)
212 input_Stream_Object *ret = PyObject_New(input_Stream_Object, &input_Stream_Type);
213 return (PyObject *)ret;
216 static PyObject *Py_ErrorHttpStream(void)
218 error_Stream_Object *ret = PyObject_New(error_Stream_Object, &error_Stream_Type);
219 return (PyObject *)ret;
222 static PyObject *create_environ(bool tls, int content_length, const char *content_type, struct http_header *headers, const char *request_method)
225 PyObject *inputstream, *errorstream;
227 struct http_header *hdr;
234 inputstream = Py_InputHttpStream(NULL);
235 if (inputstream == NULL) {
240 errorstream = Py_ErrorHttpStream();
241 if (errorstream == NULL) {
243 Py_DECREF(inputstream);
247 PyDict_SetItemString(env, "wsgi.input", inputstream);
248 PyDict_SetItemString(env, "wsgi.errors", errorstream);
249 PyDict_SetItemString(env, "wsgi.version", Py_BuildValue("(i,i)", 1, 0));
250 PyDict_SetItemString(env, "wsgi.multithread", Py_False);
251 PyDict_SetItemString(env, "wsgi.multiprocess", Py_True);
252 PyDict_SetItemString(env, "wsgi.run_once", Py_False);
253 PyDict_SetItemString(env, "SERVER_PROTOCOL", PyString_FromString("HTTP/1.0"));
254 if (content_type != NULL) {
255 PyDict_SetItemString(env, "CONTENT_TYPE", PyString_FromString(content_type));
257 if (content_length > 0) {
258 PyDict_SetItemString(env, "CONTENT_LENGTH", PyLong_FromLong(content_length));
260 PyDict_SetItemString(env, "REQUEST_METHOD", PyString_FromString(request_method));
262 /* FIXME: SCRIPT_NAME */
263 /* FIXME: PATH_INFO */
264 /* FIXME: QUERY_STRING */
265 /* FIXME: SERVER_NAME, SERVER_PORT */
267 for (hdr = headers; hdr; hdr = hdr->next) {
269 asprintf(&name, "HTTP_%s", hdr->name);
270 PyDict_SetItemString(env, name, PyString_FromString(hdr->value));
275 py_scheme = PyString_FromString("https");
277 py_scheme = PyString_FromString("http");
279 PyDict_SetItemString(env, "wsgi.url_scheme", py_scheme);
284 static void wsgi_process_http_input(struct web_server_data *wdata,
285 struct websrv_context *web)
287 PyObject *py_environ, *result, *item, *iter;
288 PyObject *request_handler = wdata->private;
290 web_request_Object *py_web = PyObject_New(web_request_Object, &web_request_Type);
293 py_environ = create_environ(false /* FIXME: Figure out whether we're using tls */,
294 web->input.content_length,
295 web->input.content_type,
297 web->input.post_request?"POST":"GET");
298 if (py_environ == NULL) {
299 DEBUG(0, ("Unable to create WSGI environment object\n"));
303 result = PyObject_CallMethod(request_handler, discard_const_p(char, "__call__"), discard_const_p(char, "OO"),
304 py_environ, PyObject_GetAttrString((PyObject *)py_web, "start_response"));
306 if (result == NULL) {
307 DEBUG(0, ("error while running WSGI code\n"));
311 iter = PyObject_GetIter(result);
314 /* Now, iter over all the data returned */
316 while ((item = PyIter_Next(iter))) {
317 data_blob_append(web, &web->output.content,
318 PyString_AsString(item), PyString_Size(item));
325 bool wsgi_initialize(struct web_server_data *wdata)
331 if (PyType_Ready(&web_request_Type) < 0)
334 if (PyType_Ready(&input_Stream_Type) < 0)
337 if (PyType_Ready(&error_Stream_Type) < 0)
340 wdata->http_process_input = wsgi_process_http_input;
341 py_swat = PyImport_Import(PyString_FromString("swat"));
342 if (py_swat == NULL) {
343 DEBUG(0, ("Unable to find SWAT\n"));
346 wdata->private = py_swat;