s4:web_server/wsgi.c - add missing Python compatibility code
[metze/samba/wip.git] / source4 / web_server / wsgi.c
index 762de55f695c2ede4cfe7a8000c0d9f73a7ffd9b..84252458f03a2864ae08b0f1bfcb0a4d9f68e0e9 100644 (file)
@@ -5,27 +5,39 @@
 
    Implementation of the WSGI interface described in PEP0333 
    (http://www.python.org/dev/peps/pep-0333)
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <Python.h>
 #include "includes.h"
 #include "web_server/web_server.h"
 #include "../lib/util/dlinklist.h"
 #include "../lib/util/data_blob.h"
 #include "lib/tls/tls.h"
-#include <Python.h>
+#include "lib/tsocket/tsocket.h"
+
+/* There's no Py_ssize_t in 2.4, apparently */
+#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
+typedef int Py_ssize_t;
+typedef inquiry lenfunc;
+typedef intargfunc ssizeargfunc;
+#endif
+
+#ifndef Py_RETURN_NONE
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#endif
 
 typedef struct {
        PyObject_HEAD
@@ -188,7 +200,7 @@ static PyObject *py_input_read(PyObject *_self, PyObject *args, PyObject *kwargs
 
        ret = PyString_FromStringAndSize((char *)self->web->input.partial.data+self->offset, size);
        self->offset += size;
-       
+
        return ret;
 }
 
@@ -299,7 +311,12 @@ static PyObject *create_environ(bool tls, int content_length, struct http_header
                if (!strcasecmp(hdr->name, "Content-Type")) {
                        PyDict_SetItemString(env, "CONTENT_TYPE", PyString_FromString(hdr->value));
                } else { 
-                       asprintf(&name, "HTTP_%s", hdr->name);
+                       if (asprintf(&name, "HTTP_%s", hdr->name) < 0) {
+                               Py_DECREF(env);
+                               Py_DECREF(inputstream);
+                               PyErr_NoMemory();
+                               return NULL;
+                       }
                        PyDict_SetItemString(env, name, PyString_FromString(hdr->value));
                        free(name);
                }
@@ -319,19 +336,24 @@ static void wsgi_process_http_input(struct web_server_data *wdata,
                                    struct websrv_context *web)
 {
        PyObject *py_environ, *result, *item, *iter;
-       PyObject *request_handler = wdata->private;
-       struct socket_address *socket_address;
-
+       PyObject *request_handler = (PyObject *)wdata->private_data;
+       struct tsocket_address *my_address = web->conn->local_address;
+       const char *addr = "0.0.0.0";
+       uint16_t port = 0;
        web_request_Object *py_web = PyObject_New(web_request_Object, &web_request_Type);
        py_web->web = web;
 
-       socket_address = socket_get_my_addr(web->conn->socket, web);
+       if (tsocket_address_is_inet(my_address, "ip")) {
+               addr = tsocket_address_inet_addr_string(my_address, wdata);
+               port = tsocket_address_inet_port(my_address);
+       }
+
        py_environ = create_environ(tls_enabled(web->conn->socket),
                                    web->input.content_length, 
                                    web->input.headers, 
                                    web->input.post_request?"POST":"GET",
-                                   socket_address->addr,
-                                   socket_address->port, 
+                                   addr,
+                                   port,
                                    Py_InputHttpStream(web),
                                    web->input.url
                                    );
@@ -382,6 +404,6 @@ bool wsgi_initialize(struct web_server_data *wdata)
                DEBUG(0, ("Unable to find SWAT\n"));
                return false;
        }
-       wdata->private = py_swat;
+       wdata->private_data = py_swat;
        return true;
 }