xref: /lighttpd1.4/src/http_etag.c (revision 3392e8fb)
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
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_string_is_empty(etag)) return 0;
22 
23     uint32_t etag_sz = buffer_string_length(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
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     uint32_t elen = buffer_string_length(etag);
51     if (elen > 2) /*(expect "..." if set)*/
52         h = dekhash(etag->ptr+1, elen-2, h);
53     buffer_string_set_length(etag, 1);
54     etag->ptr[0] = '\"';
55     buffer_append_int(etag, h);
56     buffer_append_string_len(etag, CONST_STR_LEN("\""));
57 }
58 
59 void
60 http_etag_create (buffer * const etag, const struct stat * const st, const int flags)
61 {
62     if (0 == flags) return;
63 
64     uint64_t x[4];
65     uint32_t len = 0;
66 
67     if (flags & ETAG_USE_INODE)
68         x[len++] = (uint64_t)st->st_ino;
69 
70     if (flags & ETAG_USE_SIZE)
71         x[len++] = (uint64_t)st->st_size;
72 
73     if (flags & ETAG_USE_MTIME) {
74         x[len++] = (uint64_t)st->st_mtime;
75       #ifdef st_mtime /* use high-precision timestamp if available */
76       #if defined(__APPLE__) && defined(__MACH__)
77         x[len++] = (uint64_t)st->st_mtimespec.tv_nsec;
78       #else
79         x[len++] = (uint64_t)st->st_mtim.tv_nsec;
80       #endif
81       #endif
82     }
83 
84     buffer_clear(etag);
85     http_etag_remix(etag, (char *)x, len << 3);
86 }
87