1 /*
2 * http_etag - HTTP ETag manipulation
3 *
4 * Copyright(c) 2015,2020 Glenn Strauss gstrauss()gluelogic.com All rights reserved
5 * License: BSD 3-clause (same as lighttpd)
6 */
7 #include "first.h"
8
9 #include "http_etag.h"
10
11 #include <sys/stat.h>
12 #include <string.h>
13
14 #include "algo_md.h"
15 #include "buffer.h"
16
17 int
http_etag_matches(const buffer * const etag,const char * s,const int weak_ok)18 http_etag_matches (const buffer * const etag, const char *s, const int weak_ok)
19 {
20 if ('*' == s[0] && '\0' == s[1]) return 1;
21 if (buffer_is_blank(etag)) return 0;
22
23 uint32_t etag_sz = buffer_clen(etag);
24 const char *etag_ptr = etag->ptr;
25
26 if (etag_ptr[0] == 'W' && etag_ptr[1] == '/') {
27 if (!weak_ok) return 0;
28 etag_ptr += 2;
29 etag_sz -= 2;
30 }
31
32 while (*s) {
33 while (*s == ' ' || *s == '\t' || *s == ',') ++s;
34 if (s[0] == 'W' && s[1] == '/' ? (s+=2, weak_ok) : 1) {
35 if (0 == strncmp(s, etag_ptr, etag_sz) || *s == '*') {
36 s += (*s != '*' ? etag_sz : 1);
37 if (*s == '\0' || *s == ' ' || *s == '\t' || *s == ',')
38 return 1;
39 }
40 }
41 while (*s != '\0' && *s != ',') ++s;
42 }
43 return 0;
44 }
45
46 static void
http_etag_remix(buffer * const etag,const char * const str,const uint32_t len)47 http_etag_remix (buffer * const etag, const char * const str, const uint32_t len)
48 {
49 uint32_t h = dekhash(str, len, len); /*(pass len as initial hash value)*/
50 #if 0 /*(currently never elen > 2; always cleared in http_etag_create())*/
51 uint32_t elen = buffer_clen(etag);
52 if (elen > 2) {/*(expect "..." if set)*/
53 h = dekhash(etag->ptr+1, elen-2, h);
54 buffer_truncate(etag, 1);
55 }
56 else {
57 buffer_clear(etag);
58 buffer_append_char(etag, '"');
59 }
60 #else
61 /*buffer_clear(etag);*//*(currently always cleared in http_etag_create())*/
62 buffer_append_char(etag, '"');
63 #endif
64 buffer_append_int(etag, h);
65 buffer_append_char(etag, '"');
66 }
67
68 void
http_etag_create(buffer * const etag,const struct stat * const st,const int flags)69 http_etag_create (buffer * const etag, const struct stat * const st, const int flags)
70 {
71 if (0 == flags) return;
72
73 uint64_t x[4];
74 uint32_t len = 0;
75
76 if (flags & ETAG_USE_INODE)
77 x[len++] = (uint64_t)st->st_ino;
78
79 if (flags & ETAG_USE_SIZE)
80 x[len++] = (uint64_t)st->st_size;
81
82 if (flags & ETAG_USE_MTIME) {
83 x[len++] = (uint64_t)st->st_mtime;
84 #ifdef st_mtime /* use high-precision timestamp if available */
85 #if defined(__APPLE__) && defined(__MACH__)
86 x[len++] = (uint64_t)st->st_mtimespec.tv_nsec;
87 #else
88 x[len++] = (uint64_t)st->st_mtim.tv_nsec;
89 #endif
90 #endif
91 }
92
93 buffer_clear(etag);
94 http_etag_remix(etag, (char *)x, len << 3);
95 }
96