kore

a fork of the worlds most advanced web framework
Log | Files | Refs | README | LICENSE

commit cf1f624367f8b64bc0262bf9c5b7092cf29b357e
parent a6e662a8055ce69728274ae6e531e7e2529206fa
Author: Joris Vink <joris@coders.se>
Date:   Tue, 24 Jul 2018 19:56:36 +0200

let filerefs to operate on ms precision for mtime.

Diffstat:
Minclude/kore/kore.h | 7+++++--
Msrc/filemap.c | 2+-
Msrc/fileref.c | 15+++++++++++----
Msrc/http.c | 4++--
4 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/include/kore/kore.h b/include/kore/kore.h @@ -51,6 +51,7 @@ extern "C" { #if defined(__APPLE__) #undef daemon extern int daemon(int, int); +#define st_mtim st_mtimespec #endif #if !defined(KORE_NO_SENDFILE) && defined(KORE_NO_TLS) @@ -116,7 +117,8 @@ struct kore_fileref { int flags; off_t size; char *path; - time_t mtime; + u_int64_t mtime; + time_t mtime_sec; u_int64_t expiration; #if !defined(KORE_USE_PLATFORM_SENDFILE) void *base; @@ -675,7 +677,8 @@ extern char *kore_filemap_index; void kore_fileref_init(void); struct kore_fileref *kore_fileref_get(const char *); -struct kore_fileref *kore_fileref_create(const char *, int, off_t, time_t); +struct kore_fileref *kore_fileref_create(const char *, int, off_t, + struct timespec *); void kore_fileref_release(struct kore_fileref *); void kore_domain_init(void); diff --git a/src/filemap.c b/src/filemap.c @@ -272,7 +272,7 @@ lookup: /* kore_fileref_create() takes ownership of the fd. */ ref = kore_fileref_create(fpath, fd, - st.st_size, st.st_mtime); + st.st_size, &st.st_mtim); if (ref == NULL) { http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0); diff --git a/src/fileref.c b/src/fileref.c @@ -44,7 +44,7 @@ kore_fileref_init(void) } struct kore_fileref * -kore_fileref_create(const char *path, int fd, off_t size, time_t mtime) +kore_fileref_create(const char *path, int fd, off_t size, struct timespec *ts) { struct kore_fileref *ref; @@ -56,12 +56,15 @@ kore_fileref_create(const char *path, int fd, off_t size, time_t mtime) ref->cnt = 1; ref->flags = 0; ref->size = size; - ref->mtime = mtime; ref->path = kore_strdup(path); + ref->mtime_sec = ts->tv_sec; + ref->mtime = ((u_int64_t)(ts->tv_sec * 1000 + (ts->tv_nsec / 1000000))); #if !defined(KORE_USE_PLATFORM_SENDFILE) - if ((uintmax_t)size> SIZE_MAX) + if ((uintmax_t)size> SIZE_MAX) { + kore_pool_put(&ref_pool, ref); return (NULL); + } ref->base = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE, fd, 0); if (ref->base == MAP_FAILED) @@ -91,6 +94,7 @@ kore_fileref_get(const char *path) { struct stat st; struct kore_fileref *ref; + u_int64_t mtime; TAILQ_FOREACH(ref, &refs, list) { if (!strcmp(ref->path, path)) { @@ -101,7 +105,10 @@ kore_fileref_get(const char *path) return (NULL); } - if (st.st_mtime != ref->mtime) { + mtime = ((u_int64_t)(st.st_mtim.tv_sec * 1000 + + (st.st_mtim.tv_nsec / 1000000))); + + if (ref->mtime != mtime) { fileref_soft_remove(ref); return (NULL); } diff --git a/src/http.c b/src/http.c @@ -568,14 +568,14 @@ http_response_fileref(struct http_request *req, int status, if (http_request_header(req, "if-modified-since", &modified)) { mtime = kore_date_to_time(modified); - if (mtime == ref->mtime) { + if (mtime == ref->mtime_sec) { kore_fileref_release(ref); http_response(req, HTTP_STATUS_NOT_MODIFIED, NULL, 0); return; } } - if ((tm = gmtime(&ref->mtime)) != NULL) { + if ((tm = gmtime(&ref->mtime_sec)) != NULL) { if (strftime(tbuf, sizeof(tbuf), "%a, %d %b %Y %H:%M:%S GMT", tm) > 0) { http_response_header(req, "last-modified", tbuf);