diff --git a/uwsgi.c b/uwsgi.c index e961db1e..ef4ac982 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -109,6 +109,7 @@ char *xml_config = NULL; #endif int single_interpreter = 0 ; +int py_optimize = 0 ; PyObject *py_sendfile ; @@ -120,7 +121,10 @@ int harakiri_timeout = 60 ; PyObject *wsgi_writeout ; -#define VEC_SIZE 64 +#define MAX_VARS 64 + +int max_vars = MAX_VARS ; +int vec_size = 4+1+(4*MAX_VARS) ; void log_request(void) ; void get_memusage(void) ; @@ -168,10 +172,11 @@ struct __attribute__((packed)) wsgi_request { // memory debug unsigned long vsz_size; long rss_size; - // iovec - struct iovec hvec[4+1+4*VEC_SIZE] ; } wsgi_req; +// iovec +struct iovec *hvec ; + void harakiri() { PyThreadState *_myself ; @@ -253,58 +258,58 @@ PyObject *py_uwsgi_spit(PyObject *self, PyObject *args) { if (cgi_mode == 0) { base = 4 ; #endif - wsgi_req.hvec[0].iov_base = wsgi_req.protocol ; - wsgi_req.hvec[0].iov_len = wsgi_req.protocol_len ; - wsgi_req.hvec[1].iov_base = " " ; - wsgi_req.hvec[1].iov_len = 1 ; - wsgi_req.hvec[2].iov_base = PyString_AsString(head) ; - wsgi_req.hvec[2].iov_len = PyString_Size(head) ; - wsgi_req.status = atoi(wsgi_req.hvec[2].iov_base) ; - wsgi_req.hvec[3].iov_base = nl ; - wsgi_req.hvec[3].iov_len = NL_SIZE ; + hvec[0].iov_base = wsgi_req.protocol ; + hvec[0].iov_len = wsgi_req.protocol_len ; + hvec[1].iov_base = " " ; + hvec[1].iov_len = 1 ; + hvec[2].iov_base = PyString_AsString(head) ; + hvec[2].iov_len = PyString_Size(head) ; + wsgi_req.status = atoi(hvec[2].iov_base) ; + hvec[3].iov_base = nl ; + hvec[3].iov_len = NL_SIZE ; #ifndef UNBIT } else { // drop http status on cgi mode base = 3 ; - wsgi_req.hvec[0].iov_base = "Status: " ; - wsgi_req.hvec[0].iov_len = 8 ; - wsgi_req.hvec[1].iov_base = PyString_AsString(head) ; - wsgi_req.hvec[1].iov_len = PyString_Size(head) ; - wsgi_req.status = atoi(wsgi_req.hvec[1].iov_base) ; - wsgi_req.hvec[2].iov_base = nl ; - wsgi_req.hvec[2].iov_len = NL_SIZE ; + hvec[0].iov_base = "Status: " ; + hvec[0].iov_len = 8 ; + hvec[1].iov_base = PyString_AsString(head) ; + hvec[1].iov_len = PyString_Size(head) ; + wsgi_req.status = atoi(hvec[1].iov_base) ; + hvec[2].iov_base = nl ; + hvec[2].iov_len = NL_SIZE ; } #endif headers = PyTuple_GetItem(args,1) ; wsgi_req.header_cnt = PyList_Size(headers) ; - if (wsgi_req.header_cnt > VEC_SIZE) { - wsgi_req.header_cnt = VEC_SIZE ; + if (wsgi_req.header_cnt > max_vars) { + wsgi_req.header_cnt = max_vars ; } for(i=0;i\tpath (or name) of UNIX socket to bind to\n\ +\t-l \tset socket listen queue to \n\ +\t-x \tpath of xml config file\n\ +\t-t \tset harakiri timeout to seconds\n\ +\t-p \t\tspawn uwsgi worker processes\n\ +\t-O \t\tset python optimization level to \n\ +\t-v \t\tset maximum number of vars/headers to \n\ +\t-c\t\tset cgi mode\n\ +\t-C\t\tchmod socket to 666\n\ +\t-P\t\tenable profiler\n\ +\t-m\t\tenable memory usage report (Linux only)\n\ +\t-i\t\tsingle interpreter mode\n\ +\t-a\t\tset socket in the abstract namespace (Linux only)\n\ +\t-T\t\tenable threads support\n\ +\t-M\t\tenable master process manager\n\ +\t-h\t\tthis help\n\ +\t-d daemonize and log into \n", argv[0]); + exit(1); +#endif default: exit(1); } @@ -440,6 +474,8 @@ int main(int argc, char *argv[]) { Py_SetProgramName("uWSGI"); Py_Initialize() ; + Py_OptimizeFlag = py_optimize; + wsgi_thread = PyThreadState_Get(); @@ -598,10 +634,18 @@ int main(int argc, char *argv[]) { #endif + memset(&wsgi_req, 0, sizeof(struct wsgi_request)); wsgi_req.app_id = default_app ; wsgi_req.sendfile_fd = -1 ; + hvec = malloc(sizeof(struct iovec)*vec_size) ; + if (hvec == NULL) { + fprintf(stderr,"unable to allocate memory for iovec.\n"); + exit(1); + } + + while( (wsgi_poll.fd = accept(serverfd,(struct sockaddr *)&c_addr, (socklen_t *) &c_len)) ) { if (wsgi_poll.fd < 0){ @@ -665,14 +709,14 @@ int main(int argc, char *argv[]) { ptrbuf+=2; if (ptrbuf+strsize < bufferend) { // var key - wsgi_req.hvec[wsgi_req.var_cnt].iov_base = ptrbuf ; - wsgi_req.hvec[wsgi_req.var_cnt].iov_len = strsize ; + hvec[wsgi_req.var_cnt].iov_base = ptrbuf ; + hvec[wsgi_req.var_cnt].iov_len = strsize ; ptrbuf+=strsize; if (ptrbuf+2 < bufferend) { memcpy(&strsize,ptrbuf,2); ptrbuf+=2 ; if ( ptrbuf+strsize <= bufferend) { - if (!strncmp("SCRIPT_NAME", wsgi_req.hvec[wsgi_req.var_cnt].iov_base , wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + if (!strncmp("SCRIPT_NAME", hvec[wsgi_req.var_cnt].iov_base , hvec[wsgi_req.var_cnt].iov_len)) { // set the request app_id // LOCKED SECTION if (strsize > 0) { @@ -694,35 +738,47 @@ int main(int argc, char *argv[]) { } // UNLOCK } - else if (!strncmp("SERVER_PROTOCOL", wsgi_req.hvec[wsgi_req.var_cnt].iov_base , wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("SERVER_PROTOCOL", hvec[wsgi_req.var_cnt].iov_base , hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.protocol = ptrbuf ; wsgi_req.protocol_len = strsize ; } - else if (!strncmp("REQUEST_URI", wsgi_req.hvec[wsgi_req.var_cnt].iov_base, wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("REQUEST_URI", hvec[wsgi_req.var_cnt].iov_base, hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.uri = ptrbuf ; wsgi_req.uri_len = strsize ; } - else if (!strncmp("QUERY_STRING", wsgi_req.hvec[wsgi_req.var_cnt].iov_base, wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("QUERY_STRING", hvec[wsgi_req.var_cnt].iov_base, hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.query_string = ptrbuf ; wsgi_req.query_string_len = strsize ; } - else if (!strncmp("REQUEST_METHOD", wsgi_req.hvec[wsgi_req.var_cnt].iov_base, wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("REQUEST_METHOD", hvec[wsgi_req.var_cnt].iov_base, hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.method = ptrbuf ; wsgi_req.method_len = strsize ; } - else if (!strncmp("REMOTE_ADDR", wsgi_req.hvec[wsgi_req.var_cnt].iov_base, wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("REMOTE_ADDR", hvec[wsgi_req.var_cnt].iov_base, hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.remote_addr = ptrbuf ; wsgi_req.remote_addr_len = strsize ; } - else if (!strncmp("REMOTE_USER", wsgi_req.hvec[wsgi_req.var_cnt].iov_base, wsgi_req.hvec[wsgi_req.var_cnt].iov_len)) { + else if (!strncmp("REMOTE_USER", hvec[wsgi_req.var_cnt].iov_base, hvec[wsgi_req.var_cnt].iov_len)) { wsgi_req.remote_user = ptrbuf ; wsgi_req.remote_user_len = strsize ; } - wsgi_req.var_cnt++ ; + if (wsgi_req.var_cnt < vec_size-(4+1)) { + wsgi_req.var_cnt++ ; + } + else { + fprintf(stderr, "max vec size reached. skip this header.\n"); + break; + } // var value - wsgi_req.hvec[wsgi_req.var_cnt].iov_base = ptrbuf ; - wsgi_req.hvec[wsgi_req.var_cnt].iov_len = strsize ; - wsgi_req.var_cnt++ ; + hvec[wsgi_req.var_cnt].iov_base = ptrbuf ; + hvec[wsgi_req.var_cnt].iov_len = strsize ; + if (wsgi_req.var_cnt < vec_size-(4+1)) { + wsgi_req.var_cnt++ ; + } + else { + fprintf(stderr, "max vec size reached. skip this header.\n"); + break; + } ptrbuf+=strsize; } else { @@ -751,21 +807,21 @@ int main(int argc, char *argv[]) { if (wsgi_req.app_id == -1) { #endif for(i=0;iwsgi_environ, pydictkey, pydictvalue); Py_DECREF(pydictkey); Py_DECREF(pydictvalue); diff --git a/uwsgi_client.c b/uwsgi_client.c index f5ae5625..e7d6f315 100644 --- a/uwsgi_client.c +++ b/uwsgi_client.c @@ -28,6 +28,7 @@ int main() { unsigned short len ; char message[4096]; char *mptr; + char *content_length; memset(&s_addr, 0, sizeof(struct sockaddr_un)) ; @@ -75,9 +76,13 @@ int main() { res = send(uwsgi_socket, message, mptr-message, 0); - while( (cnt = read(0, message, 4096)) ) { - send(uwsgi_socket, message, cnt, 0) ; - } + if ( (content_length = getenv("CONTENT_LENGTH")) != NULL) { + if (atoi(content_length) > 0) { + while( (cnt = read(0, message, 4096)) ) { + send(uwsgi_socket, message, cnt, 0) ; + } + } + } if (res == mptr-message) { while( (res = recv(uwsgi_socket, message, 4096,0)) ) {