The branch, master has been updated
       via  2618d67 wsgi: Serve '500 Internal Server Error' page when errors 
occur.
       via  ad252fb web_server: Make second argument to websrv_output const.
       via  12c72a3 wsgi: When encountering error in Python code, print 
traceback to logs.
      from  2ad5620 BUG 9459: Install manpages only if we install the target.

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


- Log -----------------------------------------------------------------
commit 2618d67fe5eda1e86f90de2fcde90c048fbcd50b
Author: Jelmer Vernooij <[email protected]>
Date:   Sat Nov 24 20:44:23 2012 +0100

    wsgi: Serve '500 Internal Server Error' page when errors occur.
    
    Autobuild-User(master): Jelmer Vernooij <[email protected]>
    Autobuild-Date(master): Wed Dec  5 18:40:25 CET 2012 on sn-devel-104

commit ad252fb2f5b403001a283aff58773b8b429be145
Author: Jelmer Vernooij <[email protected]>
Date:   Sat Nov 24 20:44:08 2012 +0100

    web_server: Make second argument to websrv_output const.

commit 12c72a38359cb81328c5c57a8d6b6fb9920aebe6
Author: Jelmer Vernooij <[email protected]>
Date:   Sat Nov 24 19:35:33 2012 +0100

    wsgi: When encountering error in Python code, print traceback to logs.
    
    Signed-off-by: Jelmer Vernooij <[email protected]>

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

Summary of changes:
 source4/web_server/web_server.c |    2 +-
 source4/web_server/web_server.h |    2 +-
 source4/web_server/wsgi.c       |   80 ++++++++++++++++++++++++++++++++++----
 3 files changed, 73 insertions(+), 11 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source4/web_server/web_server.c b/source4/web_server/web_server.c
index 99a6b65..239612a 100644
--- a/source4/web_server/web_server.c
+++ b/source4/web_server/web_server.c
@@ -93,7 +93,7 @@ void websrv_output_headers(struct websrv_context *web, const 
char *status, struc
        data_blob_free(&b);
 }
 
-void websrv_output(struct websrv_context *web, void *data, size_t length)
+void websrv_output(struct websrv_context *web, const void *data, size_t length)
 {
        data_blob_append(web, &web->output.content, data, length);
        TEVENT_FD_NOT_READABLE(web->conn->event.fde);
diff --git a/source4/web_server/web_server.h b/source4/web_server/web_server.h
index 274a54a..9f21359 100644
--- a/source4/web_server/web_server.h
+++ b/source4/web_server/web_server.h
@@ -67,7 +67,7 @@ struct websrv_context {
 bool wsgi_initialize(struct web_server_data *wdata);
 void http_error(struct websrv_context *web, const char *status, const char 
*info);
 void websrv_output_headers(struct websrv_context *web, const char *status, 
struct http_header *headers);
-void websrv_output(struct websrv_context *web, void *data, size_t length);
+void websrv_output(struct websrv_context *web, const void *data, size_t 
length);
 NTSTATUS http_parse_header(struct websrv_context *web, const char *line);
 
 #endif /* __WEB_SERVER_H__ */
diff --git a/source4/web_server/wsgi.c b/source4/web_server/wsgi.c
index 00c9535..17ad65a 100644
--- a/source4/web_server/wsgi.c
+++ b/source4/web_server/wsgi.c
@@ -134,7 +134,7 @@ static PyObject *py_error_write(PyObject *self, PyObject 
*args, PyObject *kwargs
                return NULL;
        }
 
-       DEBUG(0, ("WSGI App: %s", str));
+       DEBUG(0, ("%s", str));
 
        Py_RETURN_NONE;
 }
@@ -147,11 +147,11 @@ static PyObject *py_error_writelines(PyObject *self, 
PyObject *args, PyObject *k
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:writelines", 
discard_const_p(char *, kwnames), &seq)) {
                return NULL;
        }
-       
+
        while ((item = PyIter_Next(seq))) {
                char *str = PyString_AsString(item);
 
-               DEBUG(0, ("WSGI App: %s", str));
+               DEBUG(0, ("%s", str));
        }
 
        Py_RETURN_NONE;
@@ -260,6 +260,49 @@ static PyObject *Py_ErrorHttpStream(void)
        return (PyObject *)ret;
 }
 
+static void DEBUG_Print_PyError(int level, const char *message)
+{
+       PyObject *old_stderr, *new_stderr;
+       PyObject *sys_module;
+       PyObject *ptype, *pvalue, *ptb;
+
+       PyErr_Fetch(&ptype, &pvalue, &ptb);
+
+       DEBUG(0, ("WSGI: Server exception occurred: %s\n", message));
+
+       sys_module = PyImport_ImportModule("sys");
+       if (sys_module == NULL) {
+               DEBUG(0, ("Unable to obtain sys module while printing error"));
+               return;
+       }
+
+       old_stderr = PyObject_GetAttrString(sys_module, "stderr");
+       if (old_stderr == NULL) {
+               DEBUG(0, ("Unable to obtain old stderr"));
+               Py_DECREF(sys_module);
+               return;
+       }
+
+       new_stderr = Py_ErrorHttpStream();
+       if (new_stderr == NULL) {
+               DEBUG(0, ("Unable to create error stream"));
+               Py_DECREF(sys_module);
+               Py_DECREF(old_stderr);
+               return;
+       }
+
+       PyObject_SetAttrString(sys_module, "stderr", new_stderr);
+       Py_DECREF(new_stderr);
+
+       PyErr_Restore(ptype, pvalue, ptb);
+       PyErr_Print();
+
+       PyObject_SetAttrString(sys_module, "stderr", old_stderr);
+       Py_DECREF(old_stderr);
+
+       Py_DECREF(sys_module);
+}
+
 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;
@@ -367,6 +410,22 @@ error:
        return NULL;
 }
 
+static void wsgi_serve_500(struct websrv_context *web)
+{
+       struct http_header *headers = NULL;
+       const char *contents[] = {
+               "An internal server error occurred while handling this request. 
",
+               "Please refer to the server logs for more details. ",
+               NULL
+       };
+       int i;
+
+       websrv_output_headers(web, "500 Internal Server Error", headers);
+       for (i = 0; contents[i]; i++) {
+               websrv_output(web, contents[i], strlen(contents[i]));
+       }
+}
+
 static void wsgi_process_http_input(struct web_server_data *wdata,
                                    struct websrv_context *web)
 {
@@ -380,7 +439,7 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
 
        py_web = PyObject_New(web_request_Object, &web_request_Type);
        if (py_web == NULL) {
-               DEBUG(0, ("Unable to allocate web request"));
+               DEBUG_Print_PyError(0, "Unable to allocate web request");
                return;
        }
        py_web->web = web;
@@ -392,7 +451,7 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
 
        py_input_stream = Py_InputHttpStream(web);
        if (py_input_stream == NULL) {
-               DEBUG(0, ("unable to create python input stream"));
+               DEBUG_Print_PyError(0, "unable to create python input stream");
                return;
        }
 
@@ -409,7 +468,8 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
        Py_DECREF(py_input_stream);
 
        if (py_environ == NULL) {
-               DEBUG(0, ("Unable to create WSGI environment object\n"));
+               DEBUG_Print_PyError(0, "Unable to create WSGI environment 
object");
+               wsgi_serve_500(web);
                return;
        }
 
@@ -417,7 +477,8 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
                                       py_environ, 
PyObject_GetAttrString((PyObject *)py_web, "start_response"));
 
        if (result == NULL) {
-               DEBUG(0, ("error while running WSGI code\n"));
+               DEBUG_Print_PyError(0, "error while handling request");
+               wsgi_serve_500(web);
                return;
        }
 
@@ -425,7 +486,8 @@ static void wsgi_process_http_input(struct web_server_data 
*wdata,
        Py_DECREF(result);
 
        if (iter == NULL) {
-               DEBUG(0, ("wsgi application did not return iterable\n"));
+               DEBUG_Print_PyError(0, "application did not return iterable");
+               wsgi_serve_500(web);
                return;
        }
 
@@ -460,7 +522,7 @@ bool wsgi_initialize(struct web_server_data *wdata)
        wdata->http_process_input = wsgi_process_http_input;
        py_web_server = PyImport_ImportModule("samba.web_server");
        if (py_web_server == NULL) {
-               DEBUG(0, ("Unable to find web server\n"));
+               DEBUG_Print_PyError(0, "Unable to find web server");
                return false;
        }
        wdata->private_data = py_web_server;


-- 
Samba Shared Repository

Reply via email to