added internal routing subsystem

This commit is contained in:
roberto@oneiric64
2012-02-05 10:43:03 +01:00
parent 6ddf1afc81
commit 865ec4dd8f
9 changed files with 299 additions and 49 deletions
+1 -1
View File
@@ -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
-5
View File
@@ -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
};
+48
View File
@@ -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,
};
+6
View File
@@ -0,0 +1,6 @@
NAME='router_redirect'
CFLAGS = []
LDFLAGS = []
LIBS = []
GCC_LIST = ['router_redirect']
+120
View File
@@ -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,
};
+6
View File
@@ -0,0 +1,6 @@
NAME='router_uwsgi'
CFLAGS = []
LDFLAGS = []
LIBS = []
GCC_LIST = ['router_uwsgi']
+56
View File
@@ -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;i<dst_n;i++) {
if (dollar) {
if (isdigit(dst[i])) {
int pos = (dst[i] - 48);
if (pos <= n) {
pos = pos * 2;
memcpy(ptr, src + ovector[pos], ovector[pos+1]-ovector[pos]);
ptr+=ovector[pos+1]-ovector[pos];
}
}
else {
*ptr++= dst[i];
}
dollar = 0;
}
else {
if (dst[i] == '$') {
dollar = 1;
}
else {
*ptr++= dst[i];
}
}
}
*ptr++= 0;
return res;
}
#endif
+42 -40
View File
@@ -14,11 +14,9 @@ int uwsgi_apply_routes(struct wsgi_request *wsgi_req) {
}
while(routes) {
if (uwsgi_regexp_match(routes->pattern, 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
+20 -3
View File
@@ -598,8 +598,11 @@ struct uwsgi_plugin {
#ifdef UWSGI_PCRE
#include <pcre.h>
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