diff --git a/src/http.c b/src/http.c index 4d50b54..22b3bb6 100644 --- a/src/http.c +++ b/src/http.c @@ -501,7 +501,10 @@ void http_response_fileref(struct http_request *req, int status, struct kore_fileref *ref) { - const char *media_type; + struct tm *tm; + time_t mtime; + char tbuf[128]; + const char *media_type, *modified; if (req->owner == NULL) return; @@ -510,6 +513,22 @@ http_response_fileref(struct http_request *req, int status, if (media_type != NULL) http_response_header(req, "content-type", media_type); + if (http_request_header(req, "if-modified-since", &modified)) { + mtime = kore_date_to_time(modified); + if (mtime == ref->mtime) { + kore_fileref_release(ref); + http_response(req, HTTP_STATUS_NOT_MODIFIED, NULL, 0); + return; + } + } + + if ((tm = gmtime(&ref->mtime)) != NULL) { + if (strftime(tbuf, sizeof(tbuf), + "%a, %d %b %Y %H:%M:%S GMT", tm) > 0) { + http_response_header(req, "last-modified", tbuf); + } + } + req->status = status; switch (req->owner->proto) { case CONN_PROTO_HTTP: diff --git a/src/utils.c b/src/utils.c index 6e158f7..7e5d946 100644 --- a/src/utils.c +++ b/src/utils.c @@ -300,7 +300,7 @@ kore_strip_chars(char *in, const char strip, char **out) } time_t -kore_date_to_time(char *http_date) +kore_date_to_time(const char *http_date) { time_t t; int err, i;