diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-09-21 02:18:35 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-09-21 02:18:35 +0000 |
commit | 3d334dca671898241732dbc0ef6838b768308da7 (patch) | |
tree | 0cac6b60ea1356455fef2553e41105f3c68bce9d /src/libtipidee | |
parent | 6be5496f8a5660875c5f45f915210f69496d231b (diff) | |
download | tipidee-3d334dca671898241732dbc0ef6838b768308da7.tar.xz |
Implement If-Modified-Since
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libtipidee')
-rw-r--r-- | src/libtipidee/deps-lib/tipidee | 5 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_common_put.c | 2 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_date.c | 19 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_date_fmt.c | 11 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_lastmodified.c | 24 | ||||
-rw-r--r-- | src/libtipidee/tipidee_util_chunked_read.c (renamed from src/libtipidee/tipidee_chunked_read.c) | 4 | ||||
-rw-r--r-- | src/libtipidee/tipidee_util_httpdate.c | 22 |
7 files changed, 73 insertions, 14 deletions
diff --git a/src/libtipidee/deps-lib/tipidee b/src/libtipidee/deps-lib/tipidee index d218af1..81cc5f7 100644 --- a/src/libtipidee/deps-lib/tipidee +++ b/src/libtipidee/deps-lib/tipidee @@ -1,4 +1,3 @@ -tipidee_chunked_read.o tipidee_conf_free.o tipidee_conf_get.o tipidee_conf_get_argv.o @@ -17,8 +16,12 @@ tipidee_method_tostr.o tipidee_response_error.o tipidee_response_header_builtin.o tipidee_response_header_common_put.o +tipidee_response_header_date.o tipidee_response_header_date_fmt.o +tipidee_response_header_lastmodified.o tipidee_response_status.o tipidee_rql_read.o tipidee_uri_parse.o +tipidee_util_chunked_read.o +tipidee_util_httpdate.o -lskarnet diff --git a/src/libtipidee/tipidee_response_header_common_put.c b/src/libtipidee/tipidee_response_header_common_put.c index 8352ba9..c67cc94 100644 --- a/src/libtipidee/tipidee_response_header_common_put.c +++ b/src/libtipidee/tipidee_response_header_common_put.c @@ -10,7 +10,7 @@ size_t tipidee_response_header_common_put (buffer *b, uint32_t options, tain const *stamp) { char fmt[128] ; - size_t m = buffer_putnoflush(b, fmt, tipidee_response_header_date_fmt(fmt, 128, stamp)) ; + size_t m = buffer_putnoflush(b, fmt, tipidee_response_header_date(fmt, 128, stamp)) ; for (tipidee_response_header_builtin const *p = tipidee_response_header_builtin_table ; p->key ; p++) { m += buffer_putsnoflush(b, p->key) ; diff --git a/src/libtipidee/tipidee_response_header_date.c b/src/libtipidee/tipidee_response_header_date.c new file mode 100644 index 0000000..d350d2c --- /dev/null +++ b/src/libtipidee/tipidee_response_header_date.c @@ -0,0 +1,19 @@ +/* ISC license. */ + +#include <errno.h> +#include <string.h> + +#include <tipidee/response.h> + +size_t tipidee_response_header_date (char *s, size_t max, tain const *stamp) +{ + size_t l ; + if (max < 8) return (errno = ENOBUFS, 0) ; + memcpy(s, "Date: ", 6) ; + l = tipidee_response_header_date_fmt(s + 6, max - 6, stamp) ; + if (!l) return 0 ; + if (l + 8 > max) return (errno = ENOBUFS, 0) ; + l += 6 ; + s[l++] = '\r' ; s[l++] = '\n' ; + return l ; +} diff --git a/src/libtipidee/tipidee_response_header_date_fmt.c b/src/libtipidee/tipidee_response_header_date_fmt.c index df19673..ca4cdd9 100644 --- a/src/libtipidee/tipidee_response_header_date_fmt.c +++ b/src/libtipidee/tipidee_response_header_date_fmt.c @@ -1,6 +1,5 @@ /* ISC license. */ -#include <string.h> #include <time.h> #include <skalibs/tai.h> @@ -10,15 +9,7 @@ size_t tipidee_response_header_date_fmt (char *s, size_t max, tain const *stamp) { - size_t m = 0, l ; struct tm tm ; - if (m + 6 > max) return 0 ; if (!localtm_from_tai(&tm, tain_secp(stamp), 0)) return 0 ; - memcpy(s, "Date: ", 6) ; m += 6 ; - l = strftime(s + m, max - m, "%a, %d %b %Y %T GMT", &tm) ; - if (!l) return 0 ; - m += l ; - if (m + 2 > max) return 0 ; - s[m++] = '\r' ; s[m++] = '\n' ; - return m ; + return strftime(s, max, "%a, %d %b %Y %T GMT", &tm) ; } diff --git a/src/libtipidee/tipidee_response_header_lastmodified.c b/src/libtipidee/tipidee_response_header_lastmodified.c new file mode 100644 index 0000000..3b3caa6 --- /dev/null +++ b/src/libtipidee/tipidee_response_header_lastmodified.c @@ -0,0 +1,24 @@ +/* ISC license. */ + +#include <skalibs/bsdsnowflake.h> + +#include <sys/stat.h> +#include <errno.h> +#include <string.h> + +#include <tipidee/response.h> + +size_t tipidee_response_header_lastmodified (char *s, size_t max, struct stat const *st) +{ + tain t ; + size_t l ; + if (max < 17) return (errno = ENOBUFS, 0) ; + if (!tain_from_timespec(&t, &st->st_mtim)) return 0 ; + memcpy(s, "Last-Modified: ", 15) ; + l = tipidee_response_header_date_fmt(s + 15, max - 15, &t) ; + if (!l) return 0 ; + if (l + 17 > max) return (errno = ENOBUFS, 0) ; + l += 15 ; + s[l++] = '\r' ; s[l++] = '\n' ; + return l ; +} diff --git a/src/libtipidee/tipidee_chunked_read.c b/src/libtipidee/tipidee_util_chunked_read.c index 66d5d80..850ffae 100644 --- a/src/libtipidee/tipidee_chunked_read.c +++ b/src/libtipidee/tipidee_util_chunked_read.c @@ -7,11 +7,11 @@ #include <skalibs/stralloc.h> #include <skalibs/unix-timed.h> -#include <tipidee/body.h> +#include <tipidee/util.h> #include <skalibs/posixishard.h> -int tipidee_chunked_read (buffer *b, stralloc *sa, size_t maxlen, tain const *deadline, tain *stamp) +int tipidee_util_chunked_read (buffer *b, stralloc *sa, size_t maxlen, tain const *deadline, tain *stamp) { char line[512] ; for (;;) diff --git a/src/libtipidee/tipidee_util_httpdate.c b/src/libtipidee/tipidee_util_httpdate.c new file mode 100644 index 0000000..cce735d --- /dev/null +++ b/src/libtipidee/tipidee_util_httpdate.c @@ -0,0 +1,22 @@ +/* ISC license. */ + +#include <errno.h> +#include <time.h> + +#include <skalibs/djbtime.h> + +#include <tipidee/util.h> + +int tipidee_util_httpdate (char const *s, tain *stamp) +{ + struct tm tm ; + if (strptime(s, "%a, %d %b %Y %T GMT", &tm)) goto got ; + if (strptime(s, "%a, %d-%b-%y %T GMT", &tm)) goto got ; + if (strptime(s, "%a %b %d %T %Y", &tm)) goto got ; + return (errno = EINVAL, 0) ; + + got: + if (!tai_from_localtm(tain_secp(stamp), &tm)) return 0 ; + stamp->nano = 0 ; + return 1 ; +} |