From 7f03222af734f63f534c833e7833c93e711660db Mon Sep 17 00:00:00 2001 From: florian Date: Tue, 5 May 2015 11:10:13 +0000 Subject: [PATCH] Implement If-Modified-Since. From Kyle Thompson . Tweaks by me. OK benno@ --- usr.sbin/httpd/server_file.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/usr.sbin/httpd/server_file.c b/usr.sbin/httpd/server_file.c index 2b75c574e01..52e50ffb95f 100644 --- a/usr.sbin/httpd/server_file.c +++ b/usr.sbin/httpd/server_file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server_file.c,v 1.53 2015/05/03 18:39:58 florian Exp $ */ +/* $OpenBSD: server_file.c,v 1.54 2015/05/05 11:10:13 florian Exp $ */ /* * Copyright (c) 2006 - 2015 Reyk Floeter @@ -51,6 +51,8 @@ int server_partial_file_request(struct httpd *, struct client *, char *, struct stat *, char *); int server_file_index(struct httpd *, struct client *, struct stat *); +int server_file_modified_since(struct http_descriptor *, + struct stat *); int server_file_method(struct client *); int parse_range_spec(char *, size_t, struct range *); struct range *parse_range(char *, size_t, int *); @@ -229,6 +231,9 @@ server_file_request(struct httpd *env, struct client *clt, char *path, goto abort; } + if ((ret = server_file_modified_since(clt->clt_descreq, st)) != -1) + return ret; + /* Now open the file, should be readable or we have another problem */ if ((fd = open(path, O_RDONLY)) == -1) goto abort; @@ -627,6 +632,26 @@ server_file_error(struct bufferevent *bev, short error, void *arg) return; } +int +server_file_modified_since(struct http_descriptor * desc, struct stat * st) +{ + struct kv key, *since; + struct tm tm; + + memset(&tm, 0, sizeof(struct tm)); + + key.kv_key = "If-Modified-Since"; + if ((since = kv_find(&desc->http_headers, &key)) != NULL && + since->kv_value != NULL) { + if (strptime(since->kv_value, "%a, %d %h %Y %T %Z", &tm) != + NULL && timegm(&tm) >= st->st_mtim.tv_sec) { + return (304); + } + } + + return (-1); +} + struct range * parse_range(char *str, size_t file_sz, int *nranges) { @@ -734,4 +759,3 @@ buffer_add_range(int fd, struct evbuffer *evb, struct range *range) return (1); } - -- 2.20.1