From 62b24fcd2a00b06b33f9ff28a382c1eef6472bb2 Mon Sep 17 00:00:00 2001 From: "roberto@precise64" Date: Sat, 5 May 2012 09:00:52 +0200 Subject: [PATCH] another python headers management optimization --- plugins/python/web3_subhandler.c | 7 ++++- plugins/python/wsgi_headers.c | 49 ++++++++++++++++++++------------ plugins/python/wsgi_subhandler.c | 2 +- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/plugins/python/web3_subhandler.c b/plugins/python/web3_subhandler.c index e833fb4a..4911a46e 100644 --- a/plugins/python/web3_subhandler.c +++ b/plugins/python/web3_subhandler.c @@ -158,11 +158,16 @@ int uwsgi_response_subhandler_web3(struct wsgi_request *wsgi_req) { Py_INCREF(headers); PyTuple_SetItem(spit_args, 1, headers); - if (py_uwsgi_spit(NULL, spit_args) == Py_None) { + if (py_uwsgi_spit(Py_None, spit_args) == NULL) { + PyErr_Print(); Py_DECREF(spit_args); goto clear; } + // send the headers if not already sent + if (!wsgi_req->headers_sent && wsgi_req->headers_hvec > 0) { + uwsgi_python_do_send_headers(wsgi_req); + } Py_DECREF(spit_args); diff --git a/plugins/python/wsgi_headers.c b/plugins/python/wsgi_headers.c index 57033f6b..51765686 100644 --- a/plugins/python/wsgi_headers.c +++ b/plugins/python/wsgi_headers.c @@ -33,8 +33,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { PyObject *exc_tb = PyTuple_GetItem(exc_info, 2); if (!exc_type || !exc_val || !exc_tb) { - PyErr_Print(); - goto clear; + return NULL; } Py_INCREF(exc_type); @@ -44,7 +43,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { PyErr_Restore(exc_type, exc_val, exc_tb); // the error is reported, let's continue... - //goto clear; + // return NULL } } @@ -54,7 +53,8 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { } #ifdef PYTHREE - if (!PyUnicode_Check(head)) { + // check for web3 + if ((self != Py_None && !PyUnicode_Check(head)) || (self == Py_None && !PyBytes_Check(head))) { #else if (!PyString_Check(head)) { #endif @@ -77,7 +77,12 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { wsgi_req->hvec[1].iov_base = " "; wsgi_req->hvec[1].iov_len = 1; #ifdef PYTHREE - wsgi_req->hvec[2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); + if (self != Py_None) { + wsgi_req->hvec[2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); + } + else { + wsgi_req->hvec[2].iov_base = PyBytes_AsString(head); + } wsgi_req->hvec[2].iov_len = strlen(wsgi_req->hvec[2].iov_base); #else wsgi_req->hvec[2].iov_base = PyString_AsString(head); @@ -93,7 +98,12 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { wsgi_req->hvec[0].iov_base = "Status: "; wsgi_req->hvec[0].iov_len = 8; #ifdef PYTHREE - wsgi_req->hvec[1].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); + if (self != Py_None) { + wsgi_req->hvec[1].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); + } + else { + wsgi_req->hvec[1].iov_base = PyBytes_AsString(head); + } wsgi_req->hvec[1].iov_len = strlen(wsgi_req->hvec[1].iov_base); #else wsgi_req->hvec[1].iov_base = PyString_AsString(head); @@ -122,8 +132,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { j = (i * 4) + base; head = PyList_GetItem(headers, i); if (!head) { - PyErr_Print(); - goto clear; + return NULL; } if (!PyTuple_Check(head)) { return PyErr_Format(PyExc_TypeError, "http header must be defined in a tuple"); @@ -133,7 +142,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { return PyErr_Format(PyExc_TypeError, "http header must be a 2-item tuple"); } #ifdef PYTHREE - if (!PyUnicode_Check(h_key)) { + if ((self != Py_None && !PyUnicode_Check(h_key)) || (self == Py_None && !PyBytes_Check(h_key))) { #else if (!PyString_Check(h_key)) { #endif @@ -144,7 +153,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { return PyErr_Format(PyExc_TypeError, "http header must be a 2-item tuple"); } #ifdef PYTHREE - if (!PyUnicode_Check(h_value)) { + if ((self != Py_None && !PyUnicode_Check(h_value)) || (self == Py_None && !PyBytes_Check(h_value))) { #else if (!PyString_Check(h_value)) { #endif @@ -154,7 +163,12 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { #ifdef PYTHREE - wsgi_req->hvec[j].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_key)); + if (self != Py_None) { + wsgi_req->hvec[j].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_key)); + } + else { + wsgi_req->hvec[j].iov_base = PyBytes_AsString(h_key); + } wsgi_req->hvec[j].iov_len = strlen(wsgi_req->hvec[j].iov_base); #else wsgi_req->hvec[j].iov_base = PyString_AsString(h_key); @@ -163,7 +177,12 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { wsgi_req->hvec[j + 1].iov_base = h_sep; wsgi_req->hvec[j + 1].iov_len = H_SEP_SIZE; #ifdef PYTHREE - wsgi_req->hvec[j + 2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_value)); + if (self != Py_None) { + wsgi_req->hvec[j + 2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_value)); + } + else { + wsgi_req->hvec[j + 2].iov_base = PyBytes_AsString(h_value); + } wsgi_req->hvec[j + 2].iov_len = strlen(wsgi_req->hvec[j + 2].iov_base); #else wsgi_req->hvec[j + 2].iov_base = PyString_AsString(h_value); @@ -212,13 +231,7 @@ PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { //uwsgi_log("%d %p\n", wsgi_req->poll.fd, up.wsgi_writeout); Py_INCREF(up.wsgi_writeout); - return up.wsgi_writeout; - -clear: - - Py_INCREF(Py_None); - return Py_None; } int uwsgi_python_do_send_headers(struct wsgi_request *wsgi_req) { diff --git a/plugins/python/wsgi_subhandler.c b/plugins/python/wsgi_subhandler.c index 34de1297..62b9f9af 100644 --- a/plugins/python/wsgi_subhandler.c +++ b/plugins/python/wsgi_subhandler.c @@ -292,7 +292,7 @@ clear: Py_XDECREF((PyObject *)wsgi_req->async_placeholder); clear2: - // sent the headers if not already sent + // send the headers if not already sent if (!wsgi_req->headers_sent && wsgi_req->headers_hvec > 0) { uwsgi_python_do_send_headers(wsgi_req); }