From 9f55579b8706f5b8fdd1390f2a6f6050a58c8a92 Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Sun, 20 Jan 2013 17:29:03 +0100 Subject: [PATCH] added support for automatic status codes --- core/init.c | 71 ++++++++++++++++++++++++++++++++ core/routing.c | 10 ----- core/utils.c | 48 --------------------- core/writer.c | 21 +++++++++- plugins/php/php_plugin.c | 10 ----- plugins/psgi/psgi_response.c | 2 - plugins/python/pump_subhandler.c | 3 -- plugins/python/python_plugin.c | 8 ---- plugins/rack/rack_plugin.c | 12 ------ uwsgi.h | 8 +--- 10 files changed, 93 insertions(+), 100 deletions(-) diff --git a/core/init.c b/core/init.c index 9d2ac637..2978bda5 100644 --- a/core/init.c +++ b/core/init.c @@ -2,6 +2,61 @@ extern struct uwsgi_server uwsgi; +struct http_status_codes { + const char key[3]; + const char *message; + int message_size; +}; + +/* statistically ordered */ +struct http_status_codes hsc[] = { + {"200", "OK"}, + {"302", "Found"}, + {"404", "Not Found"}, + {"500", "Internal Server Error"}, + {"301", "Moved Permanently"}, + {"304", "Not Modified"}, + {"303", "See Other"}, + {"403", "Forbidden"}, + {"307", "Temporary Redirect"}, + {"401", "Unauthorized"}, + {"400", "Bad Request"}, + {"405", "Method Not Allowed"}, + {"408", "Request Timeout"}, + + {"100", "Continue"}, + {"101", "Switching Protocols"}, + {"201", "Created"}, + {"202", "Accepted"}, + {"203", "Non-Authoritative Information"}, + {"204", "No Content"}, + {"205", "Reset Content"}, + {"206", "Partial Content"}, + {"300", "Multiple Choices"}, + {"305", "Use Proxy"}, + {"402", "Payment Required"}, + {"406", "Not Acceptable"}, + {"407", "Proxy Authentication Required"}, + {"409", "Conflict"}, + {"410", "Gone"}, + {"411", "Length Required"}, + {"412", "Precondition Failed"}, + {"413", "Request Entity Too Large"}, + {"414", "Request-URI Too Long"}, + {"415", "Unsupported Media Type"}, + {"416", "Requested Range Not Satisfiable"}, + {"417", "Expectation Failed"}, + {"501", "Not Implemented"}, + {"502", "Bad Gateway"}, + {"503", "Service Unavailable"}, + {"504", "Gateway Timeout"}, + {"505", "HTTP Version Not Supported"}, + {"", NULL}, +}; + + + + void uwsgi_init_default() { uwsgi.cpus = 1; @@ -90,6 +145,12 @@ void uwsgi_init_default() { uwsgi.multicast_loop = 1; #endif + // filling http status codes + struct http_status_codes *http_sc; + for (http_sc = hsc; http_sc->message != NULL; http_sc++) { + http_sc->message_size = strlen(http_sc->message); + } + uwsgi.wait_write_hook = uwsgi_simple_wait_write_hook; uwsgi.buffer_write_hook = uwsgi_buffer_write_simple; uwsgi_websockets_init(); @@ -359,3 +420,13 @@ void sanitize_args() { } } +const char *uwsgi_http_status_msg(char *status, size_t *len) { + struct http_status_codes *http_sc; + for (http_sc = hsc; http_sc->message != NULL; http_sc++) { + if (!strncmp(http_sc->key, status, 3)) { + *len = http_sc->message_size; + return http_sc->message; + } + } + return NULL; +} diff --git a/core/routing.c b/core/routing.c index dc7fea78..76eade05 100644 --- a/core/routing.c +++ b/core/routing.c @@ -2,8 +2,6 @@ #include "uwsgi.h" extern struct uwsgi_server uwsgi; -// http status codes list -extern struct http_status_codes hsc[]; static int uwsgi_apply_routes_do(struct wsgi_request *wsgi_req) { @@ -210,14 +208,6 @@ static int uwsgi_router_break(struct uwsgi_route *ur, char *arg) { ur->func = uwsgi_router_break_func; ur->data = arg; ur->data_len = strlen(arg); - if (ur->data_len >=3 ) { - // filling http status codes - struct http_status_codes *http_sc; - for (http_sc = hsc; http_sc->message != NULL; http_sc++) { - http_sc->message_size = strlen(http_sc->message); - } - ur->custom = uwsgi_str3_num(ur->data); - } return 0; } diff --git a/core/utils.c b/core/utils.c index 7832a82d..579d521c 100644 --- a/core/utils.c +++ b/core/utils.c @@ -4,54 +4,6 @@ extern struct uwsgi_server uwsgi; extern char **environ; -/* statistically ordered */ -struct http_status_codes hsc[] = { - {"200", "OK"}, - {"302", "Found"}, - {"404", "Not Found"}, - {"500", "Internal Server Error"}, - {"301", "Moved Permanently"}, - {"304", "Not Modified"}, - {"303", "See Other"}, - {"403", "Forbidden"}, - {"307", "Temporary Redirect"}, - {"401", "Unauthorized"}, - {"400", "Bad Request"}, - {"405", "Method Not Allowed"}, - {"408", "Request Timeout"}, - - {"100", "Continue"}, - {"101", "Switching Protocols"}, - {"201", "Created"}, - {"202", "Accepted"}, - {"203", "Non-Authoritative Information"}, - {"204", "No Content"}, - {"205", "Reset Content"}, - {"206", "Partial Content"}, - {"300", "Multiple Choices"}, - {"305", "Use Proxy"}, - {"402", "Payment Required"}, - {"406", "Not Acceptable"}, - {"407", "Proxy Authentication Required"}, - {"409", "Conflict"}, - {"410", "Gone"}, - {"411", "Length Required"}, - {"412", "Precondition Failed"}, - {"413", "Request Entity Too Large"}, - {"414", "Request-URI Too Long"}, - {"415", "Unsupported Media Type"}, - {"416", "Requested Range Not Satisfiable"}, - {"417", "Expectation Failed"}, - {"501", "Not Implemented"}, - {"502", "Bad Gateway"}, - {"503", "Service Unavailable"}, - {"504", "Gateway Timeout"}, - {"505", "HTTP Version Not Supported"}, - {"", NULL}, -}; - - - #ifdef __BIG_ENDIAN__ uint16_t uwsgi_swap16(uint16_t x) { return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8); diff --git a/core/writer.c b/core/writer.c index 749a2165..daff4b95 100644 --- a/core/writer.c +++ b/core/writer.c @@ -24,7 +24,26 @@ int uwsgi_response_prepare_headers(struct wsgi_request *wsgi_req, char *status, // reset the buffer (could be useful for rollbacks...) wsgi_req->headers->pos = 0; - struct uwsgi_buffer *hh = wsgi_req->socket->proto_prepare_headers(wsgi_req, status, status_len); + struct uwsgi_buffer *hh = NULL; + if (status_len <= 4) { + char *new_sc = NULL; + size_t new_sc_len = 0; + size_t sc_len = 0; + const char *sc = uwsgi_http_status_msg(status, &sc_len); + if (sc) { + new_sc = uwsgi_concat3n(status, 3, " ", 1, (char *)sc, sc_len); + new_sc_len = 4+sc_len; + } + else { + new_sc = uwsgi_concat2n(status, 3, " Unknown", 8); + new_sc_len = 11; + } + hh = wsgi_req->socket->proto_prepare_headers(wsgi_req, new_sc, new_sc_len); + free(new_sc); + } + else { + hh = wsgi_req->socket->proto_prepare_headers(wsgi_req, status, status_len); + } if (!hh) {wsgi_req->write_errors++; return -1;} if (uwsgi_buffer_append(wsgi_req->headers, hh->buf, hh->pos)) goto error; uwsgi_buffer_destroy(hh); diff --git a/plugins/php/php_plugin.c b/plugins/php/php_plugin.c index e25e1d9d..a7ca1258 100644 --- a/plugins/php/php_plugin.c +++ b/plugins/php/php_plugin.c @@ -10,9 +10,6 @@ extern struct uwsgi_server uwsgi; -// http status codes list -extern struct http_status_codes hsc[]; - static sapi_module_struct uwsgi_sapi_module; struct uwsgi_php { @@ -548,8 +545,6 @@ static sapi_module_struct uwsgi_sapi_module = { int uwsgi_php_init(void) { - struct http_status_codes *http_sc; - struct uwsgi_string_list *pset = uphp.set; struct uwsgi_string_list *append_config = uphp.append_config; @@ -583,11 +578,6 @@ int uwsgi_php_init(void) { uwsgi_sapi_module.startup(&uwsgi_sapi_module); - // filling http status codes - for (http_sc = hsc; http_sc->message != NULL; http_sc++) { - http_sc->message_size = strlen(http_sc->message); - } - uwsgi_log("PHP %s initialized\n", PHP_VERSION); return 0; diff --git a/plugins/psgi/psgi_response.c b/plugins/psgi/psgi_response.c index edfdb163..4c91bf66 100644 --- a/plugins/psgi/psgi_response.c +++ b/plugins/psgi/psgi_response.c @@ -2,8 +2,6 @@ extern struct uwsgi_server uwsgi; -extern struct http_status_codes hsc[]; - int psgi_response(struct wsgi_request *wsgi_req, AV *response) { SV **status_code, **hitem ; diff --git a/plugins/python/pump_subhandler.c b/plugins/python/pump_subhandler.c index 03d4d291..f53d5376 100644 --- a/plugins/python/pump_subhandler.c +++ b/plugins/python/pump_subhandler.c @@ -5,9 +5,6 @@ extern struct uwsgi_python up; extern PyTypeObject uwsgi_InputType; -extern struct http_status_codes hsc[]; - - void *uwsgi_request_subhandler_pump(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { PyObject *zero; diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 623fb6a7..9ad87a28 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -3,8 +3,6 @@ extern struct uwsgi_server uwsgi; struct uwsgi_python up; -extern struct http_status_codes hsc[]; - #include extern PyTypeObject uwsgi_InputType; @@ -1093,8 +1091,6 @@ void uwsgi_python_preinit_apps() { void uwsgi_python_init_apps() { - struct http_status_codes *http_sc; - // lazy ? if (uwsgi.mywid > 0) { UWSGI_GET_GIL; @@ -1190,10 +1186,6 @@ next: } if (up.pump != NULL) { init_uwsgi_app(LOADER_UWSGI, up.pump, uwsgi.wsgi_req, up.main_thread, PYTHON_APP_TYPE_PUMP); - // filling http status codes - for (http_sc = hsc; http_sc->message != NULL; http_sc++) { - http_sc->message_size = (int) strlen(http_sc->message); - } } if (up.wsgi_lite != NULL) { init_uwsgi_app(LOADER_UWSGI, up.wsgi_lite, uwsgi.wsgi_req, up.main_thread, PYTHON_APP_TYPE_WSGI_LITE); diff --git a/plugins/rack/rack_plugin.c b/plugins/rack/rack_plugin.c index a551f41f..0a664738 100644 --- a/plugins/rack/rack_plugin.c +++ b/plugins/rack/rack_plugin.c @@ -57,11 +57,6 @@ void uwsgi_ruby_exception(void) { } - - -extern struct http_status_codes hsc[]; - - VALUE rb_uwsgi_io_new(VALUE class, VALUE wr) { struct wsgi_request *wsgi_req; @@ -430,19 +425,12 @@ static void rack_hack_dollar_zero(VALUE name, ID id) { int uwsgi_rack_init(){ - struct http_status_codes *http_sc; #ifdef RUBY19 int argc = 2; char *sargv[] = { (char *) "uwsgi", (char *) "-e0" }; char **argv = sargv; #endif - - // filling http status codes - for (http_sc = hsc; http_sc->message != NULL; http_sc++) { - http_sc->message_size = (int) strlen(http_sc->message); - } - if (ur.gemset) { uwsgi_ruby_gemset(ur.gemset); } diff --git a/uwsgi.h b/uwsgi.h index 44ad05cf..40b3088a 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2442,12 +2442,6 @@ void manage_opt(int, char *); void uwsgi_cluster_add_node(struct uwsgi_cluster_node *, int); int uwsgi_ping_node(int, struct wsgi_request *); -struct http_status_codes { - const char key[3]; - const char *message; - int message_size; -}; - #ifdef UWSGI_ASYNC void uwsgi_async_init(void); void async_loop(); @@ -3771,6 +3765,8 @@ ssize_t uwsgi_sendfile_do(int, int, size_t, size_t); int uwsgi_proto_base_fix_headers(struct wsgi_request *); int uwsgi_response_add_content_length(struct wsgi_request *, uint64_t); +const char *uwsgi_http_status_msg(char *, size_t *); + #define uwsgi_response_add_connection_close(x) uwsgi_response_add_header(x, "Connection", 10, "close", 5) #define uwsgi_response_add_content_type(x, y, z) uwsgi_response_add_header(x, "Content-Type", 12, y, z)