From c84a22dde3abc22b94d4ddfcae192bb1b0d89baf Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 3 May 2012 17:19:22 +0000 Subject: [package] uhttpd: display errors in init script, code formatting changes, bump package version git-svn-id: svn://svn.openwrt.org/openwrt/trunk@31572 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- package/uhttpd/src/uhttpd-file.c | 178 ++++++++++++++++++++++----------------- 1 file changed, 99 insertions(+), 79 deletions(-) (limited to 'package/uhttpd/src/uhttpd-file.c') diff --git a/package/uhttpd/src/uhttpd-file.c b/package/uhttpd/src/uhttpd-file.c index 816a69124e..0d9a207b11 100644 --- a/package/uhttpd/src/uhttpd-file.c +++ b/package/uhttpd/src/uhttpd-file.c @@ -31,13 +31,13 @@ static const char * uh_file_mime_lookup(const char *path) struct mimetype *m = &uh_mime_types[0]; const char *e; - while( m->extn ) + while (m->extn) { e = &path[strlen(path)-1]; - while( e >= path ) + while (e >= path) { - if( (*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn) ) + if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn)) return m->mime; e--; @@ -54,10 +54,9 @@ static const char * uh_file_mktag(struct stat *s) static char tag[128]; snprintf(tag, sizeof(tag), "\"%x-%x-%x\"", - (unsigned int) s->st_ino, - (unsigned int) s->st_size, - (unsigned int) s->st_mtime - ); + (unsigned int) s->st_ino, + (unsigned int) s->st_size, + (unsigned int) s->st_mtime); return tag; } @@ -68,7 +67,7 @@ static time_t uh_file_date2unix(const char *date) memset(&t, 0, sizeof(t)); - if( strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL ) + if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL) return timegm(&t); return 0; @@ -90,7 +89,7 @@ static char * uh_file_header_lookup(struct http_request *req, const char *name) foreach_header(i, req->headers) { - if( ! strcasecmp(req->headers[i], name) ) + if (!strcasecmp(req->headers[i], name)) return req->headers[i+1]; } @@ -98,26 +97,30 @@ static char * uh_file_header_lookup(struct http_request *req, const char *name) } -static int 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) { ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n")); - if( s ) + if (s) { 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))); + ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n", + uh_file_unix2date(s->st_mtime))); } return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL))); } -static int 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) { 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 int 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) { 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); @@ -130,25 +133,26 @@ static int uh_file_response_412(struct client *cl, struct http_request *req) "Connection: close\r\n", req->version); } -static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) +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"); char *p; int i; - if( hdr ) + if (hdr) { p = &hdr[0]; - for( i = 0; i < strlen(hdr); i++ ) + for (i = 0; i < strlen(hdr); i++) { - if( (hdr[i] == ' ') || (hdr[i] == ',') ) + if ((hdr[i] == ' ') || (hdr[i] == ',')) { hdr[i++] = 0; p = &hdr[i]; } - else if( !strcmp(p, "*") || !strcmp(p, tag) ) + else if (!strcmp(p, "*") || !strcmp(p, tag)) { *ok = 1; return *ok; @@ -164,14 +168,16 @@ static int uh_file_if_match(struct client *cl, struct http_request *req, struct return *ok; } -static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) +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 (hdr) { - if( uh_file_date2unix(hdr) >= s->st_mtime ) + if (uh_file_date2unix(hdr) >= s->st_mtime) { *ok = 0; ensure_ret(uh_file_response_304(cl, req, s)); @@ -181,7 +187,8 @@ static int uh_file_if_modified_since(struct client *cl, struct http_request *req return *ok; } -static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) +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"); @@ -189,26 +196,30 @@ static int uh_file_if_none_match(struct client *cl, struct http_request *req, st int i; *ok = 1; - if( hdr ) + if (hdr) { p = &hdr[0]; - for( i = 0; i < strlen(hdr); i++ ) + for (i = 0; i < strlen(hdr); i++) { - if( (hdr[i] == ' ') || (hdr[i] == ',') ) + if ((hdr[i] == ' ') || (hdr[i] == ',')) { hdr[i++] = 0; p = &hdr[i]; } - else if( !strcmp(p, "*") || !strcmp(p, tag) ) + else if (!strcmp(p, "*") || !strcmp(p, tag)) { *ok = 0; - if( (req->method == UH_HTTP_MSG_GET) || - (req->method == UH_HTTP_MSG_HEAD) ) + if ((req->method == UH_HTTP_MSG_GET) || + (req->method == UH_HTTP_MSG_HEAD)) + { ensure_ret(uh_file_response_304(cl, req, s)); + } else + { ensure_ret(uh_file_response_412(cl, req)); + } break; } @@ -218,12 +229,13 @@ static int uh_file_if_none_match(struct client *cl, struct http_request *req, st return *ok; } -static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s, int *ok) +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 ) + if (hdr) { *ok = 0; ensure_ret(uh_file_response_412(cl, req)); @@ -232,14 +244,16 @@ static int uh_file_if_range(struct client *cl, struct http_request *req, struct return *ok; } -static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) +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 (hdr) { - if( uh_file_date2unix(hdr) <= s->st_mtime ) + if (uh_file_date2unix(hdr) <= s->st_mtime) { *ok = 0; ensure_ret(uh_file_response_412(cl, req)); @@ -255,7 +269,8 @@ static int uh_file_scandir_filter_dir(const struct dirent *e) return strcmp(e->d_name, ".") ? 1 : 0; } -static void uh_file_dirlist(struct client *cl, struct http_request *req, struct path_info *pi) +static void uh_file_dirlist(struct client *cl, struct http_request *req, + struct path_info *pi) { int i; int count = 0; @@ -265,54 +280,60 @@ static void uh_file_dirlist(struct client *cl, struct http_request *req, struct struct stat s; ensure_out(uh_http_sendf(cl, req, - "Index of %s" - "

Index of %s


    ", - pi->name, pi->name - )); + "Index of %s" + "

    Index of %s


      ", + pi->name, pi->name)); - if( (count = scandir(pi->phys, &files, uh_file_scandir_filter_dir, alphasort)) > 0 ) + if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir, + alphasort)) > 0) { memset(filename, 0, sizeof(filename)); memcpy(filename, pi->phys, sizeof(filename)); pathptr = &filename[strlen(filename)]; /* list subdirs */ - for( i = 0; i < count; i++ ) + for (i = 0; i < count; i++) { strncat(filename, files[i]->d_name, - sizeof(filename) - strlen(files[i]->d_name)); + sizeof(filename) - strlen(files[i]->d_name)); - if( !stat(filename, &s) && - (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH) - ) + if (!stat(filename, &s) && + (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH)) + { ensure_out(uh_http_sendf(cl, req, - "
    1. %s/
      " - "modified: %s
      directory - %.02f kbyte" - "

    2. ", - pi->name, files[i]->d_name, files[i]->d_name, - uh_file_unix2date(s.st_mtime), s.st_size / 1024.0 - )); + "
    3. %s/" + "
      modified: %s" + "
      directory - %.02f kbyte
      " + "
    4. ", + pi->name, files[i]->d_name, + files[i]->d_name, + uh_file_unix2date(s.st_mtime), + s.st_size / 1024.0)); + } *pathptr = 0; } /* list files */ - for( i = 0; i < count; i++ ) + for (i = 0; i < count; i++) { strncat(filename, files[i]->d_name, - sizeof(filename) - strlen(files[i]->d_name)); + sizeof(filename) - strlen(files[i]->d_name)); - if( !stat(filename, &s) && - !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH) - ) + if (!stat(filename, &s) && + !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH)) + { ensure_out(uh_http_sendf(cl, req, - "
    5. %s
      " - "modified: %s
      %s - %.02f kbyte
      " - "
    6. ", - pi->name, files[i]->d_name, files[i]->d_name, - uh_file_unix2date(s.st_mtime), - uh_file_mime_lookup(filename), s.st_size / 1024.0 - )); + "
    7. %s" + "
      modified: %s" + "
      %s - %.02f kbyte
      " + "
    8. ", + pi->name, files[i]->d_name, + files[i]->d_name, + uh_file_unix2date(s.st_mtime), + uh_file_mime_lookup(filename), + s.st_size / 1024.0)); + } *pathptr = 0; } @@ -322,9 +343,9 @@ static void uh_file_dirlist(struct client *cl, struct http_request *req, struct ensure_out(uh_http_sendf(cl, req, "")); out: - if( files ) + if (files) { - for( i = 0; i < count; i++ ) + for (i = 0; i < count; i++) free(files[i]); free(files); @@ -340,16 +361,16 @@ void uh_file_request(struct client *cl, struct http_request *req, struct path_in char buf[UH_LIMIT_MSGHEAD]; /* we have a file */ - if( (pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0) ) + if ((pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0)) { /* test preconditions */ - if(ok) ensure_out(uh_file_if_modified_since(cl, req, &pi->stat, &ok)); - if(ok) ensure_out(uh_file_if_match(cl, req, &pi->stat, &ok)); - if(ok) ensure_out(uh_file_if_range(cl, req, &pi->stat, &ok)); - if(ok) ensure_out(uh_file_if_unmodified_since(cl, req, &pi->stat, &ok)); - if(ok) ensure_out(uh_file_if_none_match(cl, req, &pi->stat, &ok)); + if (ok) ensure_out(uh_file_if_modified_since(cl, req, &pi->stat, &ok)); + if (ok) ensure_out(uh_file_if_match(cl, req, &pi->stat, &ok)); + if (ok) ensure_out(uh_file_if_range(cl, req, &pi->stat, &ok)); + if (ok) ensure_out(uh_file_if_unmodified_since(cl, req, &pi->stat, &ok)); + if (ok) ensure_out(uh_file_if_none_match(cl, req, &pi->stat, &ok)); - if( ok > 0 ) + if (ok > 0) { /* write status */ ensure_out(uh_file_response_200(cl, req, &pi->stat)); @@ -358,17 +379,17 @@ void uh_file_request(struct client *cl, struct http_request *req, struct path_in ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n", pi->stat.st_size)); /* if request was HTTP 1.1 we'll respond chunked */ - if( (req->version > 1.0) && (req->method != UH_HTTP_MSG_HEAD) ) + if ((req->version > 1.0) && (req->method != UH_HTTP_MSG_HEAD)) ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1)); /* close header */ ensure_out(uh_http_send(cl, NULL, "\r\n", -1)); /* send body */ - if( req->method != UH_HTTP_MSG_HEAD ) + if (req->method != UH_HTTP_MSG_HEAD) { /* pump file data */ - while( (rlen = read(fd, buf, sizeof(buf))) > 0 ) + while ((rlen = read(fd, buf, sizeof(buf))) > 0) ensure_out(uh_http_send(cl, req, buf, rlen)); /* send trailer in chunked mode */ @@ -384,12 +405,12 @@ void uh_file_request(struct client *cl, struct http_request *req, struct path_in } /* directory */ - else if( (pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists ) + else if ((pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists) { /* write status */ ensure_out(uh_file_response_200(cl, req, NULL)); - if( req->version > 1.0 ) + if (req->version > 1.0) ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1)); ensure_out(uh_http_send(cl, NULL, "Content-Type: text/html\r\n\r\n", -1)); @@ -406,7 +427,6 @@ void uh_file_request(struct client *cl, struct http_request *req, struct path_in } out: - if( fd > -1 ) + if (fd > -1) close(fd); } - -- cgit v1.2.3