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"
27 static PyObject *start_response(PyObject *self, PyObject *args, PyObject *kwargs)
29 PyObject *response_header, *exc_info;
31 const char *kwnames[] = {
32 "status", "response_header", "exc_info", NULL
35 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sOO:start_response", discard_const_p(char *, kwnames), &status, &response_header, &exc_info)) {
39 /* FIXME: response_header, exc_info */
41 /* FIXME: Wrap stdout */
47 } error_Stream_Object;
49 static PyObject *py_error_flush(PyObject *self, PyObject *args, PyObject *kwargs)
51 /* Nothing to do here */
55 static PyObject *py_error_write(PyObject *self, PyObject *args, PyObject *kwargs)
57 const char *kwnames[] = { "str", NULL };
60 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:write", discard_const_p(char *, kwnames), &str)) {
64 DEBUG(0, ("WSGI App: %s", str));
69 static PyObject *py_error_writelines(PyObject *self, PyObject *args, PyObject *kwargs)
71 const char *kwnames[] = { "seq", NULL };
72 PyObject *seq = NULL, *item;
74 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:writelines", discard_const_p(char *, kwnames), &seq)) {
78 while ((item = PyIter_Next(seq))) {
79 char *str = PyString_AsString(item);
81 DEBUG(0, ("WSGI App: %s", str));
87 static PyMethodDef error_Stream_methods[] = {
88 { "flush", (PyCFunction)py_error_flush, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
89 { "write", (PyCFunction)py_error_write, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
90 { "writelines", (PyCFunction)py_error_writelines, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
91 { NULL, NULL, 0, NULL }
94 PyTypeObject error_Stream_Type = {
95 PyObject_HEAD_INIT(NULL) 0,
96 .tp_name = "wsgi.ErrorStream",
97 .tp_basicsize = sizeof(error_Stream_Object),
98 .tp_methods = error_Stream_methods,
99 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
104 } input_Stream_Object;
106 static PyObject *py_input_read(PyObject *self, PyObject *args, PyObject *kwargs)
111 static PyObject *py_input_readline(PyObject *self, PyObject *args, PyObject *kwargs)
116 static PyObject *py_input_readlines(PyObject *self, PyObject *args, PyObject *kwargs)
121 static PyObject *py_input___iter__(PyObject *self, PyObject *args, PyObject *kwargs)
126 static PyMethodDef input_Stream_methods[] = {
127 { "read", (PyCFunction)py_input_read, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
128 { "readline", (PyCFunction)py_input_readline, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
129 { "readlines", (PyCFunction)py_input_readlines, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
130 { "__iter__", (PyCFunction)py_input___iter__, METH_O|METH_VARARGS|METH_KEYWORDS, NULL },
131 { NULL, NULL, 0, NULL }
134 PyTypeObject input_Stream_Type = {
135 PyObject_HEAD_INIT(NULL) 0,
136 .tp_name = "wsgi.InputStream",
137 .tp_basicsize = sizeof(input_Stream_Object),
138 .tp_methods = input_Stream_methods,
139 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
142 static PyObject *Py_InputHttpStream(void *foo)
144 input_Stream_Object *ret = PyObject_New(input_Stream_Object, &input_Stream_Type);
145 return (PyObject *)ret;
148 static PyObject *Py_ErrorHttpStream(void)
150 error_Stream_Object *ret = PyObject_New(error_Stream_Object, &error_Stream_Type);
151 return (PyObject *)ret;
154 static PyObject *create_environ(void)
156 PyObject *env, *osmodule, *osenviron;
157 PyObject *inputstream, *errorstream;
159 osmodule = PyImport_ImportModule("os");
160 if (osmodule == NULL)
163 osenviron = PyObject_CallMethod(osmodule, "environ", NULL);
165 env = PyDict_Copy(osenviron);
169 inputstream = Py_InputHttpStream(NULL);
170 if (inputstream == NULL) {
175 errorstream = Py_ErrorHttpStream();
176 if (errorstream == NULL) {
178 Py_DECREF(inputstream);
182 PyDict_SetItemString(env, "wsgi.input", inputstream);
183 PyDict_SetItemString(env, "wsgi.errors", errorstream);
184 PyDict_SetItemString(env, "wsgi.version", Py_BuildValue("(i,i)", 1, 0));
185 PyDict_SetItemString(env, "wsgi.multithread", Py_False);
186 PyDict_SetItemString(env, "wsgi.multiprocess", Py_True);
187 PyDict_SetItemString(env, "wsgi.run_once", Py_False);
190 PyDict_SetItemString(env, "wsgi.url_scheme", "http");
191 PyDict_SetItemString(env, "wsgi.url_scheme", "https");
197 void wsgi_process_http_input(struct websrv_context *web)