X-Git-Url: http://git.rohieb.name/openwrt.git/blobdiff_plain/56d71638dbf48b8e4885e1e5ed5d0f1859674541..c0241ce8caf879dd9a61e6839a9dbf8a8e1c6c6b:/package/uhttpd/src/uhttpd-file.c diff --git a/package/uhttpd/src/uhttpd-file.c b/package/uhttpd/src/uhttpd-file.c index 2a06f8520..fda86d726 100644 --- a/package/uhttpd/src/uhttpd-file.c +++ b/package/uhttpd/src/uhttpd-file.c @@ -29,23 +29,21 @@ static const char * uh_file_mime_lookup(const char *path) { struct mimetype *m = &uh_mime_types[0]; - char *p, *pd, *ps; + const char *e; - ps = strrchr(path, '/'); - pd = strrchr(path, '.'); - - /* use either slash or dot as separator, whatever comes last */ - p = (ps && pd && (ps > pd)) ? ps : pd; - - if( (p != NULL) && (*(++p) != 0) ) + while( m->extn ) { - while( m->extn ) + e = &path[strlen(path)-1]; + + while( e >= path ) { - if( ! strcasecmp(p, m->extn) ) + if( (*e == '.') && !strcasecmp(&e[1], m->extn) ) return m->mime; - m++; + e--; } + + m++; } return "application/octet-stream"; @@ -99,39 +97,40 @@ static char * uh_file_header_lookup(struct http_request *req, const char *name) return NULL; } -static void uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s) + +static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s) { - uh_http_sendf(cl, NULL, "Connection: close\r\n"); + ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n")); if( s ) { - uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)); - uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n", uh_file_unix2date(s->st_mtime)); + ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s))); + ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n", uh_file_unix2date(s->st_mtime))); } - uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL))); + return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL))); } -static void uh_file_response_200(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_response_200(struct client *cl, struct http_request *req, struct stat *s) { - uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version); - uh_file_response_ok_hdrs(cl, req, s); + ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version)); + return uh_file_response_ok_hdrs(cl, req, s); } -static void uh_file_response_304(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_response_304(struct client *cl, struct http_request *req, struct stat *s) { - uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version); - uh_file_response_ok_hdrs(cl, req, s); + ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version)); + return uh_file_response_ok_hdrs(cl, req, s); } -static void uh_file_response_412(struct client *cl, struct http_request *req) +static int uh_file_response_412(struct client *cl, struct http_request *req) { - uh_http_sendf(cl, NULL, + return uh_http_sendf(cl, NULL, "HTTP/%.1f 412 Precondition Failed\r\n" "Connection: close\r\n", req->version); } -static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) { const char *tag = uh_file_mktag(s); char *hdr = uh_file_header_lookup(req, "If-Match"); @@ -151,43 +150,44 @@ static int uh_file_if_match(struct client *cl, struct http_request *req, struct } else if( !strcmp(p, "*") || !strcmp(p, tag) ) { - return 1; + *ok = 1; + return *ok; } } - uh_file_response_412(cl, req); - return 0; + *ok = 0; + ensure_ret(uh_file_response_412(cl, req)); + return *ok; } - return 1; + *ok = 1; + return *ok; } -static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) { char *hdr = uh_file_header_lookup(req, "If-Modified-Since"); + *ok = 1; if( hdr ) { - if( uh_file_date2unix(hdr) < s->st_mtime ) - { - return 1; - } - else + if( uh_file_date2unix(hdr) >= s->st_mtime ) { - uh_file_response_304(cl, req, s); - return 0; + *ok = 0; + ensure_ret(uh_file_response_304(cl, req, s)); } } - return 1; + return *ok; } -static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) { const char *tag = uh_file_mktag(s); char *hdr = uh_file_header_lookup(req, "If-None-Match"); char *p; int i; + *ok = 1; if( hdr ) { @@ -202,47 +202,51 @@ static int uh_file_if_none_match(struct client *cl, struct http_request *req, st } else if( !strcmp(p, "*") || !strcmp(p, tag) ) { + *ok = 0; + if( (req->method == UH_HTTP_MSG_GET) || (req->method == UH_HTTP_MSG_HEAD) ) - uh_file_response_304(cl, req, s); + ensure_ret(uh_file_response_304(cl, req, s)); else - uh_file_response_412(cl, req); + ensure_ret(uh_file_response_412(cl, req)); - return 0; + break; } } } - return 1; + return *ok; } -static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s, int *ok) { char *hdr = uh_file_header_lookup(req, "If-Range"); + *ok = 1; if( hdr ) { - uh_file_response_412(cl, req); - return 0; + *ok = 0; + ensure_ret(uh_file_response_412(cl, req)); } - return 1; + return *ok; } -static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s) +static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) { char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since"); + *ok = 1; if( hdr ) { if( uh_file_date2unix(hdr) <= s->st_mtime ) { - uh_file_response_412(cl, req); - return 0; + *ok = 0; + ensure_ret(uh_file_response_412(cl, req)); } } - return 1; + return *ok; } @@ -253,17 +257,18 @@ static int uh_file_scandir_filter_dir(const struct dirent *e) static void uh_file_dirlist(struct client *cl, struct http_request *req, struct path_info *pi) { - int i, count; + int i; + int count = 0; char filename[PATH_MAX]; char *pathptr; struct dirent **files = NULL; struct stat s; - uh_http_sendf(cl, req, + ensure_out(uh_http_sendf(cl, req, "