diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2012-10-15 20:09:55 +0300 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2012-10-15 20:09:55 +0300 |
commit | 76d15b63c7f1a29fa994b2f056387101e1126e0a (patch) | |
tree | 58fed08d435a14f79d95b9659e487849479ef994 | |
parent | fa43d1a62864f912e4450affb9c86f3accbe026a (diff) | |
download | uhttpd-76d15b63c7f1a29fa994b2f056387101e1126e0a.tar.gz |
support multiple index files in the configuration and the command line args
-rw-r--r-- | uhttpd-utils.c | 42 | ||||
-rw-r--r-- | uhttpd-utils.h | 3 | ||||
-rw-r--r-- | uhttpd.c | 21 | ||||
-rw-r--r-- | uhttpd.h | 6 |
4 files changed, 48 insertions, 24 deletions
diff --git a/uhttpd-utils.c b/uhttpd-utils.c index 1e1ac80..ad8813e 100644 --- a/uhttpd-utils.c +++ b/uhttpd-utils.c @@ -24,14 +24,6 @@ #endif -static char *uh_index_files[] = { - "index.html", - "index.htm", - "default.html", - "default.htm" -}; - - const char * sa_straddr(void *sa) { static char str[INET6_ADDRSTRLEN]; @@ -553,6 +545,25 @@ static char * canonpath(const char *path, char *path_resolved) return NULL; } + +struct index_file *uh_index_files = NULL; + +struct index_file * uh_index_add(const char *filename) +{ + struct index_file *new = NULL; + + if ((filename != NULL) && (new = malloc(sizeof(*new))) != NULL) + { + new->name = filename; + new->next = uh_index_files; + + uh_index_files = new; + } + + return new; +} + + /* Returns NULL on error. ** NB: improperly encoded URL should give client 400 [Bad Syntax]; returning ** NULL here causes 404 [Not Found], but that's not too unreasonable. */ @@ -570,6 +581,7 @@ struct path_info * uh_path_lookup(struct client *cl, const char *url) int no_sym = cl->server->conf->no_symlinks; int i = 0; struct stat s; + struct index_file *idx; /* back out early if url is undefined */ if (url == NULL) @@ -680,21 +692,11 @@ struct path_info * uh_path_lookup(struct client *cl, const char *url) p.redirected = 1; } - else if (cl->server->conf->index_file) - { - strncat(buffer, cl->server->conf->index_file, sizeof(buffer)); - - if (!stat(buffer, &s) && (s.st_mode & S_IFREG)) - { - memcpy(path_phys, buffer, sizeof(path_phys)); - memcpy(&p.stat, &s, sizeof(p.stat)); - } - } else { - for (i = 0; i < array_size(uh_index_files); i++) + for (idx = uh_index_files; idx; idx = idx->next) { - strncat(buffer, uh_index_files[i], sizeof(buffer)); + strncat(buffer, idx->name, sizeof(buffer)); if (!stat(buffer, &s) && (s.st_mode & S_IFREG)) { diff --git a/uhttpd-utils.h b/uhttpd-utils.h index 309e93e..2c6785d 100644 --- a/uhttpd-utils.h +++ b/uhttpd-utils.h @@ -117,6 +117,9 @@ int uh_auth_check( ); +extern struct index_file *uh_index_files; +struct index_file * uh_index_add(const char *filename); + struct path_info * uh_path_lookup(struct client *cl, const char *url); struct listener * uh_listener_add(int sock, struct config *conf); @@ -85,7 +85,13 @@ static void uh_config_parse(struct config *conf) continue; } - conf->index_file = strdup(col1); + if (!uh_index_add(strdup(col1))) + { + fprintf(stderr, + "Unable to add index filename %s: " + "Out of memory\n", col1 + ); + } } else if (!strncmp(line, "E404:", 5)) { @@ -951,7 +957,7 @@ int main (int argc, char **argv) optarg); exit(1); } - conf.index_file = optarg; + uh_index_add(optarg); break; /* don't follow symlinks */ @@ -1109,7 +1115,7 @@ int main (int argc, char **argv) #endif " -h directory Specify the document root, default is '.'\n" " -E string Use given virtual URL as 404 error handler\n" - " -I string Use given filename as index page for directories\n" + " -I string Use given filename as index for directories, multiple allowed\n" " -S Do not follow symbolic links outside of the docroot\n" " -D Do not allow directory listings, send 403 instead\n" " -R Enable RFC1918 filter\n" @@ -1177,6 +1183,15 @@ int main (int argc, char **argv) if (conf.network_timeout <= 0) conf.network_timeout = 30; + /* default index files */ + if (!uh_index_files) + { + uh_index_add("index.html"); + uh_index_add("index.htm"); + uh_index_add("default.html"); + uh_index_add("default.htm"); + } + #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS) /* default script timeout */ if (conf.script_timeout <= 0) @@ -79,7 +79,6 @@ struct config { char docroot[PATH_MAX]; char *realm; char *file; - char *index_file; char *error_handler; int no_symlinks; int no_dirlists; @@ -206,6 +205,11 @@ struct auth_realm { struct auth_realm *next; }; +struct index_file { + const char *name; + struct index_file *next; +}; + #ifdef HAVE_CGI struct interpreter { char path[PATH_MAX]; |