diff --git a/protocol.c b/protocol.c index b4cd5d28..044d3f9a 100644 --- a/protocol.c +++ b/protocol.c @@ -554,6 +554,7 @@ int uwsgi_parse_vars(struct wsgi_request *wsgi_req) { char *ptrbuf, *bufferend; uint16_t strsize = 0; + struct uwsgi_dyn_dict *udd; ptrbuf = buffer; bufferend = ptrbuf + wsgi_req->uh.pktsize; @@ -861,25 +862,46 @@ next: } // check if a file named uwsgi.check_static+env['PATH_INFO'] exists - if (uwsgi.check_static && wsgi_req->path_info_len > 1) { - if (!uwsgi_file_serve(wsgi_req, uwsgi.check_static, uwsgi.check_static_len, wsgi_req->path_info, wsgi_req->path_info_len, uwsgi.check_static, uwsgi.check_static_len)) { - return -1; + + if (wsgi_req->path_info_len > 1) { + udd = uwsgi.check_static; + while(udd) { + // need to build the path ? + if (udd->value == NULL) { + udd->value = realpath(udd->key, NULL); + if (!udd->value) goto nextcs; + udd->vallen = strlen(udd->value); + } + + if (!uwsgi_file_serve(wsgi_req, udd->value, udd->vallen, wsgi_req->path_info, wsgi_req->path_info_len)) { + return -1; + } +nextcs: + udd = udd->next; } } // check static-map - struct uwsgi_static_map *usm = uwsgi.static_maps; - while(usm) { + udd = uwsgi.static_maps; + while(udd) { #ifdef UWSGI_DEBUG - uwsgi_log("checking for %.*s <-> %.*s\n", wsgi_req->path_info_len, wsgi_req->path_info, usm->mountpoint_len, usm->mountpoint); + uwsgi_log("checking for %.*s <-> %.*s\n", wsgi_req->path_info_len, wsgi_req->path_info, udd->keylen, udd->key); #endif + if (udd->status == 0) { + char *real_docroot = realpath(udd->value, NULL); + if (!real_docroot) goto nextsm; + udd->value = real_docroot; + udd->vallen = strlen(udd->value); + udd->status = 1; + } - if (!uwsgi_starts_with(wsgi_req->path_info, wsgi_req->path_info_len, usm->mountpoint, usm->mountpoint_len)) { - if (!uwsgi_file_serve(wsgi_req, usm->document_root, usm->document_root_len, wsgi_req->path_info+usm->mountpoint_len, wsgi_req->path_info_len-usm->mountpoint_len, usm->orig_document_root, usm->orig_document_root_len)) { + if (!uwsgi_starts_with(wsgi_req->path_info, wsgi_req->path_info_len, udd->key, udd->keylen)) { + if (!uwsgi_file_serve(wsgi_req, udd->value, udd->vallen, wsgi_req->path_info+udd->keylen, wsgi_req->path_info_len-udd->keylen)) { return -1; } } - usm = usm->next; +nextsm: + udd = udd->next; } return 0; @@ -1437,7 +1459,7 @@ char *uwsgi_get_mime_type(char *name, int namelen, int *size) { return NULL; } -int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_t document_root_len, char *path_info, uint16_t path_info_len, char *orig_document_root, uint16_t orig_document_root_len) { +int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_t document_root_len, char *path_info, uint16_t path_info_len) { struct stat st; char real_filename[PATH_MAX]; @@ -1508,8 +1530,8 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_ if (uwsgi.file_serve_mode == 1) { wsgi_req->header_cnt += 2; wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, "X-Accel-Redirect: ", 18); - wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, orig_document_root, orig_document_root_len); - if (orig_document_root[orig_document_root_len-1] != '/') { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, document_root, document_root_len); + if (document_root[document_root_len-1] != '/') { wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, "/", 1); } wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, path_info, path_info_len); @@ -1520,8 +1542,8 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_ else if (uwsgi.file_serve_mode == 2) { wsgi_req->header_cnt += 2; wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, "X-Sendfile: ", 12); - wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, orig_document_root, orig_document_root_len); - if (orig_document_root[orig_document_root_len-1] != '/') { + wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, document_root, document_root_len); + if (document_root[document_root_len-1] != '/') { wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, "/", 1); } wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, path_info, path_info_len); diff --git a/socket.c b/socket.c index 9f9c75c1..e98ae898 100644 --- a/socket.c +++ b/socket.c @@ -412,7 +412,7 @@ char *generate_socket_name(char *socket_name) { if (!strncmp(socket_name, new_addr, strlen(socket_name))) { asterisk[0] = '*'; new_socket = uwsgi_concat3(new_addr, ":", tcp_port + 1); - uwsgi_log("found %s for %s on interface %s\n", new_socket, socket_name, ifa->ifa_name); + uwsgi_log("[uwsgi-autoip] found %s for %s on interface %s\n", new_socket, socket_name, ifa->ifa_name); freeifaddrs(ifap); return new_socket; } diff --git a/utils.c b/utils.c index 5aac6516..ca5b260e 100644 --- a/utils.c +++ b/utils.c @@ -1701,7 +1701,7 @@ int uwsgi_read_whole_body(struct wsgi_request *wsgi_req, char *buf, size_t len) return 0; } -void _add_exported_option(char *key, char *value, int configured) { +void add_exported_option(char *key, char *value, int configured) { if (!uwsgi.exported_opts) { uwsgi.exported_opts = uwsgi_malloc(sizeof(struct uwsgi_opt *)); @@ -1721,32 +1721,6 @@ void _add_exported_option(char *key, char *value, int configured) { uwsgi.exported_opts_cnt++; } -void add_exported_option(char *key, char *value, int configured) { - - char *v = value; - if (value && strchr(value, ';')) { - v = uwsgi_str(value); - if (v[0] == '\\') { - _add_exported_option(key, v+1, configured); - return; - } - } - - if (v == NULL) { - _add_exported_option(key, v, configured); - return; - } - - char *p = strtok(v, ";"); - while(p != NULL) { - - _add_exported_option(key, p, configured); - - p = strtok(NULL, ";"); - } - -} - int uwsgi_waitfd(int fd, int timeout) { int ret; @@ -2704,6 +2678,7 @@ struct uwsgi_dyn_dict *uwsgi_dyn_dict_new(struct uwsgi_dyn_dict **dd, char *key, uwsgi_dd->value = val; uwsgi_dd->vallen = vallen; uwsgi_dd->hits = 0; + uwsgi_dd->status = 0; uwsgi_dd->next = NULL; return uwsgi_dd; diff --git a/uwsgi.c b/uwsgi.c index 0fe407b9..84342a32 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1428,15 +1428,6 @@ int main(int argc, char *argv[], char *envp[]) { /* uWSGI IS CONFIGURED !!! */ - if (uwsgi.build_mime_dict) { - if (!access(uwsgi.mime_file, R_OK)) { - uwsgi_build_mime_dict(uwsgi.mime_file); - } - else { - uwsgi_log("!!! no mime.types file found !!!\n"); - } - } - if (uwsgi.dump_options) { struct option *lopt = uwsgi.long_options; while (lopt->name) { @@ -1697,6 +1688,15 @@ int uwsgi_start(void *v_argv) { } sanitize_args(); + if (uwsgi.build_mime_dict) { + if (!access(uwsgi.mime_file, R_OK)) { + uwsgi_build_mime_dict(uwsgi.mime_file); + } + else { + uwsgi_log("!!! no mime.types file found !!!\n"); + } + } + // end of generic initialization @@ -2964,7 +2964,7 @@ void uwsgi_ignition() { static int manage_base_opt(int i, char *optarg) { char *p; - struct uwsgi_static_map *usm, *old_usm; + char *docroot, *mountpoint; struct uwsgi_config_template *uct, *old_uct; struct uwsgi_cron *uc, *old_uc; struct uwsgi_socket *uwsgi_sock = NULL; @@ -3238,12 +3238,8 @@ static int manage_base_opt(int i, char *optarg) { uwsgi.mime_file = optarg; return 1; case LONG_ARGS_CHECK_STATIC: - uwsgi.check_static = realpath(optarg, NULL); - if (!uwsgi.check_static) { - uwsgi_error("check-static realpath()"); - exit(1); - } - uwsgi.check_static_len = strlen(uwsgi.check_static); + uwsgi_dyn_dict_new(&uwsgi.check_static, optarg, strlen(optarg), NULL, 0); + uwsgi_log("[uwsgi-static] added check for %s\n", optarg); uwsgi.build_mime_dict = 1; return 1; case LONG_ARGS_FILE_SERVE_MODE: @@ -3344,45 +3340,17 @@ static int manage_base_opt(int i, char *optarg) { uc->command = optarg+i; return 1; case LONG_ARGS_STATIC_MAP: - usm = uwsgi.static_maps; - if (!usm) { - usm = uwsgi_malloc(sizeof(struct uwsgi_static_map)); - uwsgi.static_maps = usm; - } - else { - old_usm = usm; - while (usm->next) { - usm = usm->next; - old_usm = usm; - } - - old_usm->next = uwsgi_malloc(sizeof(struct uwsgi_static_map)); - usm = old_usm->next; - } - - char *docroot = strchr(optarg, '='); + mountpoint = uwsgi_str(optarg); + docroot = strchr(mountpoint, '='); if (!docroot) { - uwsgi_log("invalid document root in static map\n"); + uwsgi_log("invalid document root in static map, syntax mountpoint=docroot\n"); exit(1); } - - usm->mountpoint = optarg; - usm->mountpoint_len = docroot - usm->mountpoint; - - usm->document_root = realpath(docroot + 1, NULL); - if (!usm->document_root) { - uwsgi_error("static-map realpath()"); - exit(1); - } - usm->document_root_len = strlen(usm->document_root); - - usm->orig_document_root = usm->document_root; - usm->orig_document_root_len = usm->document_root_len; - - uwsgi_log("static-mapped %.*s to %.*s\n", usm->mountpoint_len, usm->mountpoint, usm->document_root_len, usm->document_root); + docroot[0] = 0; + docroot++; + uwsgi_dyn_dict_new(&uwsgi.static_maps, mountpoint, strlen(mountpoint), docroot, strlen(docroot)); + uwsgi_log("[uwsgi-static] added mapping for %s => %s\n", mountpoint, docroot); uwsgi.build_mime_dict = 1; - - usm->next = NULL; return 1; case LONG_ARGS_STOP: signal_pidfile(SIGINT, optarg); @@ -3857,7 +3825,7 @@ static int manage_base_opt(int i, char *optarg) { return 0; } -int _manage_opt(int i, char *p) { +int manage_opt(int i, char *p) { int j; if (manage_base_opt(i, p)) { @@ -3883,41 +3851,6 @@ int _manage_opt(int i, char *p) { return 0; } -void manage_opt(int i, char *optarg) { - - char *value = optarg; - - if (value && strchr(optarg, ';')) { - value = uwsgi_str(optarg); - if (value[0] == '\\') { - if (!_manage_opt(i, value+1)) { - exit(1); - } - return; - } - } - - if (!value) { - if (!_manage_opt(i, NULL)) { - exit(1); - } - return; - } - - char *p = strtok(value, ";"); - while(p != NULL) { - - if (_manage_opt(i, p)) goto next; - exit(1); -next: - p = strtok(NULL, ";"); - - } - - return; - -} - void build_options() { int i; struct option *lopt, *aopt; diff --git a/uwsgi.h b/uwsgi.h index 907e61c9..c4cb3812 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -273,20 +273,6 @@ struct uwsgi_config_template { struct uwsgi_config_template *next; }; -struct uwsgi_static_map { - - char *mountpoint; - int mountpoint_len; - - char *document_root; - int document_root_len; - - char *orig_document_root; - int orig_document_root_len; - - struct uwsgi_static_map *next; -}; - struct uwsgi_string_list { char *value; @@ -303,6 +289,7 @@ struct uwsgi_dyn_dict { int vallen; uint64_t hits; + int status; struct uwsgi_dyn_dict *prev; struct uwsgi_dyn_dict *next; @@ -1167,13 +1154,12 @@ struct uwsgi_server { // static file serving - char *check_static; - size_t check_static_len; int file_serve_mode; - int build_mime_dict; char *mime_file; - struct uwsgi_static_map *static_maps; + + struct uwsgi_dyn_dict *static_maps; + struct uwsgi_dyn_dict *check_static; struct uwsgi_dyn_dict *mimetypes; char *logfile; @@ -1869,7 +1855,7 @@ int uwsgi_parse_vars(struct wsgi_request *); int uwsgi_enqueue_message(char *, int, uint8_t, uint8_t, char *, int, int); -void manage_opt(int, char *); +int manage_opt(int, char *); void uwsgi_cluster_add_node(struct uwsgi_cluster_node *, int); int uwsgi_ping_node(int, struct wsgi_request *); @@ -2258,7 +2244,7 @@ int uwsgi_netlink_del(char *); int uwsgi_amqp_consume_queue(int, char *, char *, char *, char *, char *, char *); char *uwsgi_amqp_consume(int, uint64_t *, char **); -int uwsgi_file_serve(struct wsgi_request *, char *, uint16_t, char *, uint16_t, char *, uint16_t); +int uwsgi_file_serve(struct wsgi_request *, char *, uint16_t, char *, uint16_t); inline int uwsgi_starts_with(char *, int, char *, int); #ifdef __sun__