3391065dae0df4fd67e2f9917681dc8b76ea96d4
[bbaumbach/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 "includes.h"
24 #include "web_server/web_server.h"
25 #include <Python.h>
26
27 static PyObject *start_response(PyObject *self, PyObject *args, PyObject *kwargs)
28 {
29         PyObject *response_header, *exc_info;
30         char *status;
31         const char *kwnames[] = {
32                 "status", "response_header", "exc_info", NULL
33         };
34
35         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sOO:start_response", discard_const_p(char *, kwnames), &status, &response_header, &exc_info)) {
36                 return NULL;
37         }
38
39         /* FIXME: response_header, exc_info */
40
41         /* FIXME: Wrap stdout */
42         return NULL;
43 }
44
45 typedef struct {
46         PyObject_HEAD
47 } error_Stream_Object;
48
49 static PyObject *py_error_flush(PyObject *self, PyObject *args, PyObject *kwargs)
50 {
51         /* Nothing to do here */
52         return Py_None;
53 }
54
55 static PyObject *py_error_write(PyObject *self, PyObject *args, PyObject *kwargs)
56 {
57         const char *kwnames[] = { "str", NULL };
58         char *str = NULL;
59
60         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:write", discard_const_p(char *, kwnames), &str)) {
61                 return NULL;
62         }
63
64         DEBUG(0, ("WSGI App: %s", str));
65
66         return Py_None;
67 }
68
69 static PyObject *py_error_writelines(PyObject *self, PyObject *args, PyObject *kwargs)
70 {
71         const char *kwnames[] = { "seq", NULL };
72         PyObject *seq = NULL, *item;
73
74         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:writelines", discard_const_p(char *, kwnames), &seq)) {
75                 return NULL;
76         }
77         
78         while ((item = PyIter_Next(seq))) {
79                 char *str = PyString_AsString(item);
80
81                 DEBUG(0, ("WSGI App: %s", str));
82         }
83
84         return Py_None;
85 }
86
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 }
92 };
93
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,
100 };
101
102 typedef struct {
103         PyObject_HEAD
104 } input_Stream_Object;
105
106 static PyObject *py_input_read(PyObject *self, PyObject *args, PyObject *kwargs)
107 {
108         return NULL;
109 }
110
111 static PyObject *py_input_readline(PyObject *self, PyObject *args, PyObject *kwargs)
112 {
113         return NULL;
114 }
115
116 static PyObject *py_input_readlines(PyObject *self, PyObject *args, PyObject *kwargs)
117 {
118         return NULL;
119 }
120
121 static PyObject *py_input___iter__(PyObject *self, PyObject *args, PyObject *kwargs)
122 {
123         return NULL;
124 }
125
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 }
132 };
133
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,
140 };
141
142 static PyObject *Py_InputHttpStream(void *foo)
143 {
144         input_Stream_Object *ret = PyObject_New(input_Stream_Object, &input_Stream_Type);
145         return (PyObject *)ret;
146 }
147
148 static PyObject *Py_ErrorHttpStream(void)
149 {
150         error_Stream_Object *ret = PyObject_New(error_Stream_Object, &error_Stream_Type);
151         return (PyObject *)ret;
152 }
153
154 static PyObject *create_environ(void)
155 {
156         PyObject *env, *osmodule, *osenviron;
157         PyObject *inputstream, *errorstream;
158
159         osmodule = PyImport_ImportModule("os"); 
160         if (osmodule == NULL)
161                 return NULL;
162
163         osenviron = PyObject_CallMethod(osmodule, "environ", NULL);
164
165         env = PyDict_Copy(osenviron);
166
167         Py_DECREF(env);
168
169         inputstream = Py_InputHttpStream(NULL);
170         if (inputstream == NULL) {
171                 Py_DECREF(env);
172                 return NULL;
173         }
174
175         errorstream = Py_ErrorHttpStream();
176         if (errorstream == NULL) {
177                 Py_DECREF(env);
178                 Py_DECREF(inputstream);
179                 return NULL;
180         }
181
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);
188
189         /* FIXME: 
190         PyDict_SetItemString(env, "wsgi.url_scheme", "http");
191         PyDict_SetItemString(env, "wsgi.url_scheme", "https");
192         */
193
194         return env;
195 }
196
197 void wsgi_process_http_input(struct websrv_context *web)
198 {
199
200 }