From 19cf592e77980e239446a443927e22b940708489 Mon Sep 17 00:00:00 2001 From: "roberto@natty32" Date: Wed, 20 Apr 2011 09:32:37 +0200 Subject: [PATCH] fastcgi optimization --- plugins/python/wsgi_handlers.c | 36 ++++++++++++++++++++++------------ proto/fastcgi.c | 17 ++-------------- proto/zeromq.c | 3 +++ uwsgi.h | 4 ++-- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index 205e5ecd..3af24a51 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -10,12 +10,16 @@ typedef struct uwsgi_Input { struct wsgi_request *wsgi_req; } uwsgi_Input; -PyObject *uwsgi_Input_iter(PyObject * self) { +PyObject *uwsgi_Input_iter(PyObject *self) { Py_INCREF(self); return self; } -PyObject *uwsgi_Input_next(PyObject * self) { +PyObject *uwsgi_Input_next(PyObject* self) { + + if (!((uwsgi_Input *)self)->wsgi_req->post_cl) { + return PyString_FromString(""); + } return PyErr_Format(PyExc_NotImplementedError, "wsgi.input __iter__() is not implemented"); @@ -97,12 +101,18 @@ static PyObject *uwsgi_Input_read(uwsgi_Input *self, PyObject *args) { static PyObject *uwsgi_Input_readline(uwsgi_Input *self, PyObject *args) { + if (!self->wsgi_req->post_cl) { + return PyString_FromString(""); + } return PyErr_Format(PyExc_NotImplementedError, "wsgi.input readline() is not implemented"); } static PyObject *uwsgi_Input_readlines(uwsgi_Input *self, PyObject *args) { + if (!self->wsgi_req->post_cl) { + return PyString_FromString(""); + } return PyErr_Format(PyExc_NotImplementedError, "wsgi.input readlines() is not implemented"); } @@ -443,26 +453,28 @@ int uwsgi_request_wsgi(struct wsgi_request *wsgi_req) { UWSGI_GET_GIL } else { - wsgi_req->async_post = fdopen(wsgi_req->poll.fd, "r"); + if (wsgi_req->post_cl > 0) { + wsgi_req->async_post = fdopen(wsgi_req->poll.fd, "r"); + } } } - if (!up.pep3333_input) { + if (up.pep3333_input || !wsgi_req->post_cl) { + wsgi_socket = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType); + ((uwsgi_Input*)wsgi_socket)->wsgi_req = wsgi_req; + ((uwsgi_Input*)wsgi_socket)->pos = 0; + } + else { #ifdef PYTHREE wsgi_socket = PyFile_FromFd(fileno(wsgi_req->async_post), "wsgi_input", "rb", 0, NULL, NULL, NULL, 0); #else wsgi_socket = PyFile_FromFile(wsgi_req->async_post, "wsgi_input", "r", NULL); #endif - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.input", wsgi_socket); - Py_DECREF(wsgi_socket); - } - else { - wsgi_socket = (PyObject *) PyObject_New(uwsgi_Input, &uwsgi_InputType); - ((uwsgi_Input*)wsgi_socket)->wsgi_req = wsgi_req; - ((uwsgi_Input*)wsgi_socket)->pos = 0; - PyDict_SetItemString(wsgi_req->async_environ, "wsgi.input", wsgi_socket); } + PyDict_SetItemString(wsgi_req->async_environ, "wsgi.input", wsgi_socket); + Py_DECREF(wsgi_socket); + wsgi_req->async_result = wi->request_subhandler(wsgi_req, wi); diff --git a/proto/fastcgi.c b/proto/fastcgi.c index 29debc5f..3fddfc8f 100644 --- a/proto/fastcgi.c +++ b/proto/fastcgi.c @@ -212,25 +212,12 @@ ssize_t uwsgi_proto_fastcgi_write_header(struct wsgi_request *wsgi_req, char *bu } void uwsgi_proto_fastcgi_close(struct wsgi_request *wsgi_req) { - struct fcgi_record fr; - fr.version = 1; - fr.type = 6; - fr.req1 = 0; - fr.req0 = 1; - fr.pad = 0; - fr.reserved = 0; - fr.cl = 0; - - if (write(wsgi_req->poll.fd, &fr, 8) <= 0) { + if (write(wsgi_req->poll.fd, FCGI_END_REQUEST, 24) <= 0) { uwsgi_error("write()"); } - if (write(wsgi_req->poll.fd, FCGI_END_REQUEST, 16) <= 0) { - uwsgi_error("write()"); - } - - close(wsgi_req->poll.fd); + uwsgi_proto_base_close(wsgi_req); } ssize_t uwsgi_proto_fastcgi_sendfile(struct wsgi_request *wsgi_req) { diff --git a/proto/zeromq.c b/proto/zeromq.c index 7d4a841f..4ee6464c 100644 --- a/proto/zeromq.c +++ b/proto/zeromq.c @@ -2,7 +2,9 @@ extern struct uwsgi_server uwsgi; +#ifdef UWSGI_JSON #include +#endif static uint16_t http_add_uwsgi_var(struct wsgi_request *wsgi_req, char *key, uint16_t keylen, char *val, uint16_t vallen) { @@ -364,6 +366,7 @@ void uwsgi_proto_zeromq_close(struct wsgi_request *wsgi_req) { zmq_msg_close(&reply); if (wsgi_req->async_post && wsgi_req->body_as_file) { + uwsgi_log("closing...\n"); fclose(wsgi_req->async_post); } diff --git a/uwsgi.h b/uwsgi.h index 41f87b2e..058113cc 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1643,7 +1643,7 @@ char *uwsgi_get_last_char(char *, char); struct uwsgi_twobytes { uint8_t cl1; uint8_t cl0; -}; +} __attribute__((__packed__)); struct fcgi_record { uint8_t version; @@ -1659,7 +1659,7 @@ struct fcgi_record { } __attribute__((__packed__)); #define FCGI_BEGIN_REQUEST "\0\1\0\0\0\0\0\0" -#define FCGI_END_REQUEST "\0\3\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +#define FCGI_END_REQUEST "\1\x06\0\1\0\0\0\0\1\3\0\1\0\x08\0\0\0\0\0\0\0\0\0\0" ssize_t fcgi_send_record(int, uint8_t, uint16_t, char *); ssize_t fcgi_send_param(int, char *, uint16_t, char *, uint16_t); uint16_t fcgi_get_record(int, char *);