The branch, master has been updated
       via  0d9bdcf web_server: Load SWAT if it is available.
       via  bfc6a9e web_server: the web server is not multi-process, indicate 
so in WSGI.
       via  b89e7f6 web_server: Properly decrement reference counters for 
python objects in wsgi.
       via  ac635d0 web_server: Properly set SCRIPT_NAME and PATH_INFO.
       via  3bffb58 web_server: Create a string object for SERVER_PORT variable.
       via  31f0e24 web_server/wsgi: Don't segfault when wsgi app doesn't 
return iterable.
      from  f22e15d build: Do not install testing binaries

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 0d9bdcf834cbe9ec4e1354bc7e912f197bf72d7b
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:47:00 2012 +0000

    web_server: Load SWAT if it is available.
    
    Reviewed-by: Matthieu Patou <[email protected]>
    
    Autobuild-User(master): Matthieu Patou <[email protected]>
    Autobuild-Date(master): Fri Nov 23 01:39:38 CET 2012 on sn-devel-104

commit bfc6a9e21d2955d0662e4179448c7031806ad6d7
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:46:59 2012 +0000

    web_server: the web server is not multi-process, indicate so in WSGI.
    
    This is a requirement for some of the paster middleware used by SWAT2.
    
    Reviewed-by: Matthieu Patou <[email protected]>

commit b89e7f6e51011cce264a9a0c8796a5d4811007d7
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:46:58 2012 +0000

    web_server: Properly decrement reference counters for python objects in 
wsgi.
    
    Reviewed-by: Matthieu Patou <[email protected]>

commit ac635d02de202523d8921ce752d7f93e2377bb07
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:46:57 2012 +0000

    web_server: Properly set SCRIPT_NAME and PATH_INFO.
    
    Reviewed-by: Matthieu Patou <[email protected]>

commit 3bffb585ba36753cf1ebd5c00e7a47f1974b0d85
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:46:56 2012 +0000

    web_server: Create a string object for SERVER_PORT variable.
    
    This matches the behaviour of other wsgi server implementations.
    
    Reviewed-by: Matthieu Patou <[email protected]>

commit 31f0e24fbe5f3da7e1a3e4b0ae234c0803123501
Author: Jelmer Vernooij <[email protected]>
Date:   Thu Nov 22 00:46:55 2012 +0000

    web_server/wsgi: Don't segfault when wsgi app doesn't return iterable.
    
    There is a bug in the application if this happens, but invalid Python
    code shouldn't cause segfaults.
    
    Reviewed-by: Matthieu Patou <[email protected]>

-----------------------------------------------------------------------

Summary of changes:
 .../scripting/python/samba/web_server/__init__.py  |   34 +++++-
 source4/web_server/wsgi.c                          |  120 +++++++++++++++-----
 2 files changed, 121 insertions(+), 33 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source4/scripting/python/samba/web_server/__init__.py 
b/source4/scripting/python/samba/web_server/__init__.py
index ed3c7de..78ce953 100644
--- a/source4/scripting/python/samba/web_server/__init__.py
+++ b/source4/scripting/python/samba/web_server/__init__.py
@@ -19,9 +19,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-
-
 def render_placeholder(environ, start_response):
+    """Send the user a simple placeholder about missing SWAT."""
     status = '200 OK'
     response_headers = [('Content-type', 'text/html')]
     start_response(status, response_headers)
@@ -41,7 +40,36 @@ def render_placeholder(environ, start_response):
     yield "</html>\n"
 
 
-__call__ = render_placeholder
+def __call__(environ, start_response):
+    """Handle a HTTP request."""
+    from wsgiref.util import application_uri, shift_path_info
+    from urlparse import urljoin
+
+    try:
+        import swat
+    except ImportError, e:
+        print "NO SWAT: %r" % e
+        have_swat = False
+    else:
+        have_swat = True
+
+    orig_path = environ['PATH_INFO']
+    name = shift_path_info(environ)
+
+    if name == "":
+        if have_swat:
+            start_response('301 Redirect',
+                [('Location', urljoin(application_uri(environ), 'swat')),])
+            return []
+        else:
+            return render_placeholder(environ, start_response)
+    elif have_swat and name == "swat":
+        return swat.__call__(environ, start_response)
+    else:
+        status = '404 Not found'
+        response_headers = [('Content-type', 'text/html')]
+        start_response(status, response_headers)
+        return ["The path %s (%s) was not found" % (orig_path, name)]
 
 
 if __name__ == '__main__':
diff --git a/source4/web_server/wsgi.c b/source4/web_server/wsgi.c
index 37ded29..00c9535 100644
--- a/source4/web_server/wsgi.c
+++ b/source4/web_server/wsgi.c
@@ -263,57 +263,91 @@ static PyObject *Py_ErrorHttpStream(void)
 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)
 {
        PyObject *env;
-       PyObject *errorstream;
        PyObject *py_scheme;
+       PyObject *py_val;
        struct http_header *hdr;
        char *questionmark;
-       
+
        env = PyDict_New();
        if (env == NULL) {
                return NULL;
        }
 
-       errorstream = Py_ErrorHttpStream();
-       if (errorstream == NULL) {
-               Py_DECREF(env);
-               Py_DECREF(inputstream);
-               return NULL;
-       }
-
        PyDict_SetItemString(env, "wsgi.input", inputstream);
-       PyDict_SetItemString(env, "wsgi.errors", errorstream);
-       PyDict_SetItemString(env, "wsgi.version", Py_BuildValue("(i,i)", 1, 0));
+
+       py_val = Py_ErrorHttpStream();
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "wsgi.errors", py_val);
+       Py_DECREF(py_val);
+
+       py_val = Py_BuildValue("(i,i)", 1, 0);
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "wsgi.version", py_val);
+       Py_DECREF(py_val);
        PyDict_SetItemString(env, "wsgi.multithread", Py_False);
-       PyDict_SetItemString(env, "wsgi.multiprocess", Py_True);
+       PyDict_SetItemString(env, "wsgi.multiprocess", Py_False);
        PyDict_SetItemString(env, "wsgi.run_once", Py_False);
-       PyDict_SetItemString(env, "SERVER_PROTOCOL", 
PyString_FromString("HTTP/1.0"));
+       py_val = PyString_FromString("HTTP/1.0");
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "SERVER_PROTOCOL", py_val);
+       Py_DECREF(py_val);
        if (content_length > 0) {
-               PyDict_SetItemString(env, "CONTENT_LENGTH", 
PyLong_FromLong(content_length));
+               py_val = PyLong_FromLong(content_length);
+               if (py_val == NULL) goto error;
+               PyDict_SetItemString(env, "CONTENT_LENGTH", py_val);
+               Py_DECREF(py_val);
        }
-       PyDict_SetItemString(env, "REQUEST_METHOD", 
PyString_FromString(request_method));
-
+       py_val = PyString_FromString(request_method);
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "REQUEST_METHOD", py_val);
+       Py_DECREF(py_val);
+
+       /* There is always a single wsgi app to which all requests are 
redirected,
+        * so SCRIPT_NAME will be / */
+       py_val = PyString_FromString("/");
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "SCRIPT_NAME", py_val);
+       Py_DECREF(py_val);
        questionmark = strchr(request_string, '?');
        if (questionmark == NULL) {
-               PyDict_SetItemString(env, "SCRIPT_NAME", 
PyString_FromString(request_string));
+               py_val = PyString_FromString(request_string);
+               if (py_val == NULL) goto error;
+               PyDict_SetItemString(env, "PATH_INFO", py_val);
+               Py_DECREF(py_val);
        } else {
-               PyDict_SetItemString(env, "QUERY_STRING", 
PyString_FromString(questionmark+1));
-               PyDict_SetItemString(env, "SCRIPT_NAME", 
PyString_FromStringAndSize(request_string, questionmark-request_string));
+               py_val = PyString_FromString(questionmark+1);
+               if (py_val == NULL) goto error;
+               PyDict_SetItemString(env, "QUERY_STRING", py_val);
+               Py_DECREF(py_val);
+               py_val = PyString_FromStringAndSize(request_string, 
questionmark-request_string);
+               if (py_val == NULL) goto error;
+               PyDict_SetItemString(env, "PATH_INFO", py_val);
+               Py_DECREF(py_val);
        }
-       
-       PyDict_SetItemString(env, "SERVER_NAME", 
PyString_FromString(servername));
-       PyDict_SetItemString(env, "SERVER_PORT", PyInt_FromLong(serverport));
+
+       py_val = PyString_FromString(servername);
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "SERVER_NAME", py_val);
+       Py_DECREF(py_val);
+       py_val = PyString_FromFormat("%d", serverport);
+       if (py_val == NULL) goto error;
+       PyDict_SetItemString(env, "SERVER_PORT", py_val);
+       Py_DECREF(py_val);
+
        for (hdr = headers; hdr; hdr = hdr->next) {
                char *name;
                if (!strcasecmp(hdr->name, "Content-Type")) {
-                       PyDict_SetItemString(env, "CONTENT_TYPE", 
PyString_FromString(hdr->value));
-               } else { 
+                       py_val = PyString_FromString(hdr->value);
+                       PyDict_SetItemString(env, "CONTENT_TYPE", py_val);
+                       Py_DECREF(py_val);
+               } else {
                        if (asprintf(&name, "HTTP_%s", hdr->name) < 0) {
-                               Py_DECREF(env);
-                               Py_DECREF(inputstream);
                                PyErr_NoMemory();
-                               return NULL;
+                               goto error;
                        }
-                       PyDict_SetItemString(env, name, 
PyString_FromString(hdr->value));
+                       py_val = PyString_FromString(hdr->value);
+                       PyDict_SetItemString(env, name, py_val);
+                       Py_DECREF(py_val);
                        free(name);
                }
        }
@@ -323,9 +357,14 @@ static PyObject *create_environ(bool tls, int 
content_length, struct http_header
        } else {
                py_scheme = PyString_FromString("http");
        }
+       if (py_scheme == NULL) goto error;
        PyDict_SetItemString(env, "wsgi.url_scheme", py_scheme);
+       Py_DECREF(py_scheme);
 
        return env;
+error:
+       Py_DECREF(env);
+       return NULL;
 }
 
 static void wsgi_process_http_input(struct web_server_data *wdata,
@@ -336,7 +375,14 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
        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);
+       web_request_Object *py_web;
+       PyObject *py_input_stream;
+
+       py_web = PyObject_New(web_request_Object, &web_request_Type);
+       if (py_web == NULL) {
+               DEBUG(0, ("Unable to allocate web request"));
+               return;
+       }
        py_web->web = web;
 
        if (tsocket_address_is_inet(my_address, "ip")) {
@@ -344,15 +390,24 @@ static void wsgi_process_http_input(struct 
web_server_data *wdata,
                port = tsocket_address_inet_port(my_address);
        }
 
+       py_input_stream = Py_InputHttpStream(web);
+       if (py_input_stream == NULL) {
+               DEBUG(0, ("unable to create python input stream"));
+               return;
+       }
+
        py_environ = create_environ(tls_enabled(web->conn->socket),
                                    web->input.content_length, 
                                    web->input.headers, 
                                    web->input.post_request?"POST":"GET",
                                    addr,
                                    port,
-                                   Py_InputHttpStream(web),
+                                   py_input_stream,
                                    web->input.url
                                    );
+
+       Py_DECREF(py_input_stream);
+
        if (py_environ == NULL) {
                DEBUG(0, ("Unable to create WSGI environment object\n"));
                return;
@@ -369,6 +424,11 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
        iter = PyObject_GetIter(result);
        Py_DECREF(result);
 
+       if (iter == NULL) {
+               DEBUG(0, ("wsgi application did not return iterable\n"));
+               return;
+       }
+
        /* Now, iter over all the data returned */
 
        while ((item = PyIter_Next(iter))) {


-- 
Samba Shared Repository

Reply via email to