#define _XOPEN_SOURCE 500 /* strptime() ... */ #define _BSD_SOURCE /* scandir() ... */ #include "uhttpd.h" #include "uhttpd-utils.h" #include "uhttpd-file.h" #include "uhttpd-mimetypes.h" static const char * uh_file_mime_lookup(const char *path) { struct mimetype *m = &uh_mime_types[0]; char *p, *pd, *ps; 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 ) { if( ! strcasecmp(p, m->extn) ) return m->mime; m++; } } return "application/octet-stream"; } 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 ); return tag; } static time_t uh_file_date2unix(const char *date) { struct tm t; memset(&t, 0, sizeof(t)); if( strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL ) return timegm(&t); return 0; } static char * uh_file_unix2date(time_t ts) { static char str[128]; struct tm *t = gmtime(&ts); strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", t); return str; } static char * uh_file_header_lookup(struct http_request *req, const char *name) { int i; foreach_header(i, req->headers) { if( ! strcasecmp(req->headers[i], name) ) return req->headers[i+1]; } return NULL; } static void uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s) { 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)); } 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) { uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version); uh_file_response_ok_hdrs(cl, req, s); } static void 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); } static void uh_file_response_412(struct client *cl, struct http_request *req) { uh_http_sendf(cl, NULL, "HTTP/%.1f 412 Precondition Failed\r\n", req->version); } static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s) { const char *tag = uh_file_mktag(s); char *hdr = uh_file_header_lookup(req, "If-Match"); char *p; int i; if( hdr ) { p = &hdr[0]; for( i = 0; i < strlen(hdr); i++ ) { if( (hdr[i] == ' ') || (hdr[i] == ',') ) { hdr[i++] = 0; p = &hdr[i]; } else if( !strcmp(p, "*") || !strcmp(p, tag) ) { return 1; } } uh_file_response_412(cl, req); return 0; } return 1; } static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s) { char *hdr = uh_file_header_lookup(req, "If-Modified-Since"); if( hdr ) { if( uh_file_date2unix(hdr) < s->st_mtime ) { return 1; } else { uh_file_response_304(cl, req, s); return 0; } } return 1; } static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s) { const char *tag = uh_file_mktag(s); char *hdr = uh_file_header_lookup(req, "If-None-Match"); char *p; int i; if( hdr ) { p = &hdr[0]; for( i = 0; i < strlen(hdr); i++ ) { if( (hdr[i] == ' ') || (hdr[i] == ',') ) { hdr[i++] = 0; p = &hdr[i]; } else if( !strcmp(p, "*") || !strcmp(p, tag) ) { if( (req->method == UH_HTTP_MSG_GET) || (req->method == UH_HTTP_MSG_HEAD) ) uh_file_response_304(cl, req, s); else uh_file_response_412(cl, req); return 0; } } } return 1; } static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s) { char *hdr = uh_file_header_lookup(req, "If-Range"); if( hdr ) { uh_file_response_412(cl, req); return 0; } return 1; } static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s) { char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since"); if( hdr ) { if( uh_file_date2unix(hdr) <= s->st_mtime ) { uh_file_response_412(cl, req); return 0; } } return 1; } 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 uh_path_info *pi) { int i, count; char filename[PATH_MAX]; char *pathptr; struct dirent **files = NULL; struct stat s; uh_http_sendf(cl, req, "