From 865ec4dd8ff4da0058e1f5bc96b1a82fb458babb Mon Sep 17 00:00:00 2001 From: "roberto@oneiric64" Date: Sun, 5 Feb 2012 10:43:03 +0100 Subject: [PATCH] added internal routing subsystem --- buildconf/default.ini | 2 +- plugins/logsocket/logsocket_plugin.c | 5 - plugins/router_redirect/router_redirect.c | 48 +++++++++ plugins/router_redirect/uwsgiplugin.py | 6 ++ plugins/router_uwsgi/router_uwsgi.c | 120 ++++++++++++++++++++++ plugins/router_uwsgi/uwsgiplugin.py | 6 ++ regexp.c | 56 ++++++++++ routing.c | 82 +++++++-------- uwsgi.h | 23 ++++- 9 files changed, 299 insertions(+), 49 deletions(-) create mode 100644 plugins/router_redirect/router_redirect.c create mode 100644 plugins/router_redirect/uwsgiplugin.py create mode 100644 plugins/router_uwsgi/router_uwsgi.c create mode 100644 plugins/router_uwsgi/uwsgiplugin.py diff --git a/buildconf/default.ini b/buildconf/default.ini index 93734498..97c498f0 100644 --- a/buildconf/default.ini +++ b/buildconf/default.ini @@ -29,7 +29,7 @@ plugins = bin_name = uwsgi append_version = plugin_dir = . -embedded_plugins = python, ping, cache, nagios, rrdtool, carbon, rpc, fastrouter, http, ugreen, signal, syslog, rsyslog, logsocket +embedded_plugins = python, ping, cache, nagios, rrdtool, carbon, rpc, fastrouter, http, ugreen, signal, syslog, rsyslog, logsocket, router_uwsgi, router_redirect as_shared_library = false locking = auto diff --git a/plugins/logsocket/logsocket_plugin.c b/plugins/logsocket/logsocket_plugin.c index 0a82d2a5..3404cecd 100644 --- a/plugins/logsocket/logsocket_plugin.c +++ b/plugins/logsocket/logsocket_plugin.c @@ -64,15 +64,10 @@ void uwsgi_logsocket_register() { uwsgi_register_logger("socket", uwsgi_socket_logger); } -int uwsgi_logsocket_init() { - return 0; -} - struct uwsgi_plugin logsocket_plugin = { .name = "logsocket", .on_load = uwsgi_logsocket_register, - .init = uwsgi_logsocket_init }; diff --git a/plugins/router_redirect/router_redirect.c b/plugins/router_redirect/router_redirect.c new file mode 100644 index 00000000..0ab1d149 --- /dev/null +++ b/plugins/router_redirect/router_redirect.c @@ -0,0 +1,48 @@ +#include "../../uwsgi.h" + +extern struct uwsgi_server uwsgi; + +int uwsgi_routing_func_redirect(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) { + + struct iovec iov[4]; + + if (wsgi_req->protocol_len == 0) return 0; + + iov[0].iov_base = wsgi_req->protocol; + iov[0].iov_len = wsgi_req->protocol_len; + + iov[1].iov_base = " 302 Found\r\nLocation: "; + iov[1].iov_len = 22; + + iov[2].iov_base = uwsgi_regexp_apply_ovec(wsgi_req->path_info, wsgi_req->path_info_len, ur->data, ur->data_len, ur->ovector, ur->ovn); + iov[2].iov_len = strlen(iov[2].iov_base); + + iov[3].iov_base = "\r\n\r\n"; + iov[3].iov_len = 4; + + wsgi_req->headers_size = wsgi_req->socket->proto_writev_header(wsgi_req, iov, 4); + + free(iov[2].iov_base); + return 1; +} + + +int uwsgi_router_redirect(struct uwsgi_route *ur, char *args) { + + ur->func = uwsgi_routing_func_redirect; + ur->data = args; + ur->data_len = strlen(args); + return 0; +} + + +void router_redirect_register(void) { + + uwsgi_register_router("redirect", uwsgi_router_redirect); +} + +struct uwsgi_plugin router_redirect_plugin = { + + .name = "router_redirect", + .on_load = router_redirect_register, +}; diff --git a/plugins/router_redirect/uwsgiplugin.py b/plugins/router_redirect/uwsgiplugin.py new file mode 100644 index 00000000..5abf365f --- /dev/null +++ b/plugins/router_redirect/uwsgiplugin.py @@ -0,0 +1,6 @@ +NAME='router_redirect' + +CFLAGS = [] +LDFLAGS = [] +LIBS = [] +GCC_LIST = ['router_redirect'] diff --git a/plugins/router_uwsgi/router_uwsgi.c b/plugins/router_uwsgi/router_uwsgi.c new file mode 100644 index 00000000..743007ad --- /dev/null +++ b/plugins/router_uwsgi/router_uwsgi.c @@ -0,0 +1,120 @@ +#include "../../uwsgi.h" + +extern struct uwsgi_server uwsgi; + +int uwsgi_routing_func_uwsgi_simple(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) { + + struct uwsgi_header *uh = (struct uwsgi_header *) ur->data; + + wsgi_req->uh.modifier1 = uh->modifier1; + wsgi_req->uh.modifier2 = uh->modifier2; + + return 0; +} + +int uwsgi_routing_func_uwsgi_remote(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) { + + char buf[8192]; + ssize_t len; + struct uwsgi_header *uh = (struct uwsgi_header *) ur->data; + char *addr = ur->data + sizeof(struct uwsgi_header); + + // mark a route request + wsgi_req->status = -17; + + int uwsgi_fd = uwsgi_connect(addr, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); + if (uwsgi_fd < 0) { + uwsgi_log("unable to connect to host %s\n", addr); + return 1; + } + + if (uwsgi_send_message(uwsgi_fd, uh->modifier1, uh->modifier2, wsgi_req->buffer, wsgi_req->uh.pktsize, wsgi_req->poll.fd, wsgi_req->post_cl, 0) < 0) { + uwsgi_log("unable to send uwsgi request to host %s", addr); + return 1; + } + + for(;;) { + int ret = uwsgi_waitfd(uwsgi_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); + if (ret > 0) { + len = read(uwsgi_fd, buf, 8192); + if (len == 0) { + break; + } + else if (len < 0) { + uwsgi_error("read()"); + break; + } + + if (write(wsgi_req->poll.fd, buf, len) != len) { + uwsgi_error("write()"); + break; + } + } + else { + uwsgi_log("timeout !!!\n"); + break; + } + } + + + close(uwsgi_fd); + return 1; + +} + +int uwsgi_router_uwsgi(struct uwsgi_route *ur, char *args) { + + // check for commas + char *comma1 = strchr(args, ','); + if (!comma1) { + uwsgi_log("invalid route syntax\n"); + exit(1); + } + + char *comma2 = strchr(comma1+1, ','); + if (!comma2) { + uwsgi_log("invalid route syntax\n"); + exit(1); + } + + *comma1 = 0; + *comma2 = 0; + + // simple modifier remapper + if (*args == 0) { + struct uwsgi_header *uh = uwsgi_calloc(sizeof(struct uwsgi_header)); + ur->func = uwsgi_routing_func_uwsgi_simple; + ur->data = (void *) uh; + + uh->modifier1 = atoi(comma1+1); + uh->modifier2 = atoi(comma2+1); + return 0; + } + else { + struct uwsgi_header *uh = uwsgi_calloc(sizeof(struct uwsgi_header) + strlen(args) + 1); + ur->func = uwsgi_routing_func_uwsgi_remote; + ur->data = (void *) uh; + + uh->modifier1 = atoi(comma1+1); + uh->modifier2 = atoi(comma2+1); + + void *ptr = (void *) uh; + strcpy(ptr+sizeof(struct uwsgi_header), args); + return 0; + + } + + return -1; +} + + +void router_uwsgi_register(void) { + + uwsgi_register_router("uwsgi", uwsgi_router_uwsgi); +} + +struct uwsgi_plugin router_uwsgi_plugin = { + + .name = "router_uwsgi", + .on_load = router_uwsgi_register, +}; diff --git a/plugins/router_uwsgi/uwsgiplugin.py b/plugins/router_uwsgi/uwsgiplugin.py new file mode 100644 index 00000000..8d754ceb --- /dev/null +++ b/plugins/router_uwsgi/uwsgiplugin.py @@ -0,0 +1,6 @@ +NAME='router_uwsgi' + +CFLAGS = [] +LDFLAGS = [] +LIBS = [] +GCC_LIST = ['router_uwsgi'] diff --git a/regexp.c b/regexp.c index 14d9bbe3..4bb2ba92 100644 --- a/regexp.c +++ b/regexp.c @@ -28,4 +28,60 @@ int uwsgi_regexp_match(pcre *pattern, pcre_extra *pattern_extra, char *subject, return pcre_exec((const pcre*)pattern, (const pcre_extra *)pattern_extra, subject, length, 0, 0, NULL, 0 ); } +int uwsgi_regexp_match_ovec(pcre *pattern, pcre_extra *pattern_extra, char *subject, int length, int *ovec, int n ) { + + if (n > 0) { + return pcre_exec((const pcre*)pattern, (const pcre_extra *)pattern_extra, subject, length, 0, 0, ovec, (n+1)*3 ); + } + return pcre_exec((const pcre*)pattern, (const pcre_extra *)pattern_extra, subject, length, 0, 0, NULL, 0 ); +} + +int uwsgi_regexp_ovector(pcre *pattern, pcre_extra *pattern_extra) { + + int n; + + if (pcre_fullinfo((const pcre*)pattern, (const pcre_extra *)pattern_extra, PCRE_INFO_CAPTURECOUNT, &n)) + return 0; + + return n; +} + +char *uwsgi_regexp_apply_ovec(char *src, int src_n, char *dst, int dst_n, int *ovector, int n) { + + int i; + int dollar = 0; + + char *res = uwsgi_malloc( dst_n + (src_n * n) + 1); + char *ptr = res; + + for(i=0;ipattern, routes->pattern_extra, wsgi_req->path_info, wsgi_req->path_info_len) >= 0) { - - uwsgi_log("regexp match for %.*s\n", wsgi_req->path_info_len, wsgi_req->path_info); - return routes->func(wsgi_req, routes->data); + int n = uwsgi_regexp_match_ovec(routes->pattern, routes->pattern_extra, wsgi_req->path_info, wsgi_req->path_info_len, routes->ovector, routes->ovn); + if (n>= 0) { + return routes->func(wsgi_req, routes); } routes = routes->next; @@ -27,16 +25,6 @@ int uwsgi_apply_routes(struct wsgi_request *wsgi_req) { return 0; } -int uwsgi_routing_func_uwsgi_simple(struct wsgi_request *wsgi_req, void *data) { - - struct uwsgi_header *uh = (struct uwsgi_header *) data; - - wsgi_req->uh.modifier1 = uh->modifier1; - wsgi_req->uh.modifier2 = uh->modifier2; - - return 0; -} - void uwsgi_opt_add_route(char *opt, char *value, void *foobar) { char *route = uwsgi_str(value); @@ -69,6 +57,11 @@ void uwsgi_opt_add_route(char *opt, char *value, void *foobar) { exit(1); } + ur->ovn = uwsgi_regexp_ovector(ur->pattern, ur->pattern_extra); + if (ur->ovn > 0) { + ur->ovector = uwsgi_calloc(sizeof(int) * (3 * (ur->ovn + 1)) ); + } + char *command = space+1; char *colon = strchr(command, ':'); @@ -79,32 +72,41 @@ void uwsgi_opt_add_route(char *opt, char *value, void *foobar) { *colon = 0; - if (!strcmp(command, "uwsgi")) { - char *args = colon+1; - // check for commas - char *comma1 = strchr(args, ','); - if (!comma1) { - uwsgi_log("invalid route syntax\n"); - exit(1); - } - - char *comma2 = strchr(comma1+1, ','); - if (!comma2) { - uwsgi_log("invalid route syntax\n"); - exit(1); - } - - *comma1 = 0; - *comma2 = 0; - // simple modifier remapper - if (*args == 0) { - struct uwsgi_header *uh = uwsgi_calloc(sizeof(struct uwsgi_header)); - ur->func = uwsgi_routing_func_uwsgi_simple; - ur->data = (void *) uh; - - uh->modifier1 = atoi(comma1+1); - uh->modifier2 = atoi(comma2+1); + struct uwsgi_router *r = uwsgi.routers; + while(r) { + if (!strcmp(r->name, command)) { + if (r->func(ur, colon+1) == 0) { + return; + } } + r = r->next; } + + uwsgi_log("unable to register route \"%s\"\n", value); + exit(1); +} + +struct uwsgi_router *uwsgi_register_router(char *name, int (*func)(struct uwsgi_route *, char *)) { + + struct uwsgi_router *ur = uwsgi.routers; + if (!ur) { + uwsgi.routers = uwsgi_calloc(sizeof(struct uwsgi_router)); + uwsgi.routers->name = name; + uwsgi.routers->func = func; + return uwsgi.routers; + } + + while(ur) { + if (!ur->next) { + ur->next = uwsgi_calloc(sizeof(struct uwsgi_router)); + ur->next->name = name; + ur->next->func = func; + return ur->next; + } + ur = ur->next; + } + + return NULL; + } #endif diff --git a/uwsgi.h b/uwsgi.h index fd47c760..c90326ac 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -598,8 +598,11 @@ struct uwsgi_plugin { #ifdef UWSGI_PCRE #include -int uwsgi_regexp_build(char *re, pcre **pattern, pcre_extra **pattern_extra); -int uwsgi_regexp_match(pcre *pattern, pcre_extra *pattern_extra, char *subject, int length); +int uwsgi_regexp_build(char *, pcre **, pcre_extra **); +int uwsgi_regexp_match(pcre *, pcre_extra *, char *, int); +int uwsgi_regexp_match_ovec(pcre *, pcre_extra *, char *, int, int *, int); +int uwsgi_regexp_ovector(pcre *, pcre_extra *); +char *uwsgi_regexp_apply_ovec(char *, int, char *, int, int *, int); #endif @@ -671,14 +674,26 @@ struct uwsgi_route { pcre *pattern; pcre_extra *pattern_extra; + int ovn; + int *ovector; - int (*func)(struct wsgi_request *, void *); + int (*func)(struct wsgi_request *, struct uwsgi_route *); void *data; + size_t data_len; struct uwsgi_route *next; }; + +struct uwsgi_router { + + char *name; + int (*func)(struct uwsgi_route *, char *); + struct uwsgi_router *next; + +}; + #endif struct __attribute__ ((packed)) uwsgi_header { @@ -1296,6 +1311,7 @@ struct uwsgi_server { #ifdef UWSGI_ROUTING + struct uwsgi_router *routers; struct uwsgi_route *routes; #endif @@ -2540,6 +2556,7 @@ void uwsgi_opt_set_unshare(char *, char *, void *); char *uwsgi_tmpname(char *, char *); #ifdef UWSGI_ROUTING +struct uwsgi_router *uwsgi_register_router(char *, int (*)(struct uwsgi_route *, char *)); void uwsgi_opt_add_route(char *, char *, void *); int uwsgi_apply_routes(struct wsgi_request *); #endif