xref: /lighttpd1.4/src/http_etag.c (revision 94edf7e8)
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_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
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_copy_string_len(etag, CONST_STR_LEN("\""));
58   #else
59     buffer_copy_string_len(etag, CONST_STR_LEN("\""));
60   #endif
61     buffer_append_int(etag, h);
62     buffer_append_string_len(etag, CONST_STR_LEN("\""));
63 }
64 
65 void
66 http_etag_create (buffer * const etag, const struct stat * const st, const int flags)
67 {
68     if (0 == flags) return;
69 
70     uint64_t x[4];
71     uint32_t len = 0;
72 
73     if (flags & ETAG_USE_INODE)
74         x[len++] = (uint64_t)st->st_ino;
75 
76     if (flags & ETAG_USE_SIZE)
77         x[len++] = (uint64_t)st->st_size;
78 
79     if (flags & ETAG_USE_MTIME) {
80         x[len++] = (uint64_t)st->st_mtime;
81       #ifdef st_mtime /* use high-precision timestamp if available */
82       #if defined(__APPLE__) && defined(__MACH__)
83         x[len++] = (uint64_t)st->st_mtimespec.tv_nsec;
84       #else
85         x[len++] = (uint64_t)st->st_mtim.tv_nsec;
86       #endif
87       #endif
88     }
89 
90     buffer_clear(etag);
91     http_etag_remix(etag, (char *)x, len << 3);
92 }
93