xref: /lighttpd1.4/src/http_header.c (revision 367f30a6)
1 /*
2  * http_header - HTTP header manipulation interfaces
3  *
4  * Copyright(c) 2018 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
5  * License: BSD 3-clause (same as lighttpd)
6  */
7 #include "first.h"
8 
9 #include <string.h>
10 #include "sys-strings.h"
11 
12 #include "http_header.h"
13 #include "base.h"
14 #include "array.h"
15 #include "buffer.h"
16 
17 
18 typedef struct keyvlenvalue {
19     const  int16_t key;
20     const uint16_t vlen;
21     const char value[28];
22 } keyvlenvalue;
23 
24 /* Note: must be sorted by length */
25 /* Note: must be kept in sync with http_header.h enum http_header_e */
26 /* Note: must be kept in sync http_headers[] and http_headers_off[] */
27 /* http_headers_off lists first offset at which string of specific len occur */
28 static const int8_t http_headers_off[] = {
29   -1, -1,  0,  1,  4,  9, 11, 17, 21, 25, 27, -1, 30, 31,
30   37, 40, 45, 49, -1, 52, -1, -1, 53, 54, -1, 55, -1, 57
31 };
32 static const keyvlenvalue http_headers[] = {
33   { HTTP_HEADER_TE,                          CONST_LEN_STR("te") }
34  ,{ HTTP_HEADER_AGE,                         CONST_LEN_STR("age") }
35  ,{ HTTP_HEADER_DNT,                         CONST_LEN_STR("dnt") }
36  ,{ HTTP_HEADER_P3P,                         CONST_LEN_STR("p3p") }
37  ,{ HTTP_HEADER_HOST,                        CONST_LEN_STR("host") }
38  ,{ HTTP_HEADER_DATE,                        CONST_LEN_STR("date") }
39  ,{ HTTP_HEADER_ETAG,                        CONST_LEN_STR("etag") }
40  ,{ HTTP_HEADER_VARY,                        CONST_LEN_STR("vary") }
41  ,{ HTTP_HEADER_LINK,                        CONST_LEN_STR("link") }
42  ,{ HTTP_HEADER_ALLOW,                       CONST_LEN_STR("allow") }
43  ,{ HTTP_HEADER_RANGE,                       CONST_LEN_STR("range") }
44  ,{ HTTP_HEADER_COOKIE,                      CONST_LEN_STR("cookie") }
45  ,{ HTTP_HEADER_ACCEPT,                      CONST_LEN_STR("accept") }
46  ,{ HTTP_HEADER_STATUS,                      CONST_LEN_STR("status") }
47  ,{ HTTP_HEADER_SERVER,                      CONST_LEN_STR("server") }
48  ,{ HTTP_HEADER_EXPECT,                      CONST_LEN_STR("expect") }
49  ,{ HTTP_HEADER_PRAGMA,                      CONST_LEN_STR("pragma") }
50  ,{ HTTP_HEADER_UPGRADE,                     CONST_LEN_STR("upgrade") }
51  ,{ HTTP_HEADER_REFERER,                     CONST_LEN_STR("referer") }
52  ,{ HTTP_HEADER_EXPIRES,                     CONST_LEN_STR("expires") }
53  ,{ HTTP_HEADER_ALT_SVC,                     CONST_LEN_STR("alt-svc") }
54  ,{ HTTP_HEADER_LOCATION,                    CONST_LEN_STR("location") }
55  ,{ HTTP_HEADER_IF_MATCH,                    CONST_LEN_STR("if-match") }
56  ,{ HTTP_HEADER_IF_RANGE,                    CONST_LEN_STR("if-range") }
57  ,{ HTTP_HEADER_ALT_USED,                    CONST_LEN_STR("alt-used") }
58  ,{ HTTP_HEADER_FORWARDED,                   CONST_LEN_STR("forwarded") }
59  ,{ HTTP_HEADER_EXPECT_CT,                   CONST_LEN_STR("expect-ct") }
60  ,{ HTTP_HEADER_CONNECTION,                  CONST_LEN_STR("connection") }
61  ,{ HTTP_HEADER_SET_COOKIE,                  CONST_LEN_STR("set-cookie") }
62  ,{ HTTP_HEADER_USER_AGENT,                  CONST_LEN_STR("user-agent") }
63  ,{ HTTP_HEADER_CONTENT_TYPE,                CONST_LEN_STR("content-type") }
64  ,{ HTTP_HEADER_LAST_MODIFIED,               CONST_LEN_STR("last-modified") }
65  ,{ HTTP_HEADER_AUTHORIZATION,               CONST_LEN_STR("authorization") }
66  ,{ HTTP_HEADER_IF_NONE_MATCH,               CONST_LEN_STR("if-none-match") }
67  ,{ HTTP_HEADER_CACHE_CONTROL,               CONST_LEN_STR("cache-control") }
68  ,{ HTTP_HEADER_ACCEPT_RANGES,               CONST_LEN_STR("accept-ranges") }
69  ,{ HTTP_HEADER_CONTENT_RANGE,               CONST_LEN_STR("content-range") }
70  ,{ HTTP_HEADER_CONTENT_LENGTH,              CONST_LEN_STR("content-length") }
71  ,{ HTTP_HEADER_HTTP2_SETTINGS,              CONST_LEN_STR("http2-settings") }
72  ,{ HTTP_HEADER_ONION_LOCATION,              CONST_LEN_STR("onion-location") }
73  ,{ HTTP_HEADER_ACCEPT_ENCODING,             CONST_LEN_STR("accept-encoding") }
74  ,{ HTTP_HEADER_ACCEPT_LANGUAGE,             CONST_LEN_STR("accept-language") }
75  ,{ HTTP_HEADER_REFERRER_POLICY,             CONST_LEN_STR("referrer-policy") }
76  ,{ HTTP_HEADER_X_FORWARDED_FOR,             CONST_LEN_STR("x-forwarded-for") }
77  ,{ HTTP_HEADER_X_FRAME_OPTIONS,             CONST_LEN_STR("x-frame-options") }
78  ,{ HTTP_HEADER_WWW_AUTHENTICATE,            CONST_LEN_STR("www-authenticate") }
79  ,{ HTTP_HEADER_CONTENT_ENCODING,            CONST_LEN_STR("content-encoding") }
80  ,{ HTTP_HEADER_CONTENT_LOCATION,            CONST_LEN_STR("content-location") }
81  ,{ HTTP_HEADER_X_XSS_PROTECTION,            CONST_LEN_STR("x-xss-protection") }
82  ,{ HTTP_HEADER_IF_MODIFIED_SINCE,           CONST_LEN_STR("if-modified-since") }
83  ,{ HTTP_HEADER_TRANSFER_ENCODING,           CONST_LEN_STR("transfer-encoding") }
84  ,{ HTTP_HEADER_X_FORWARDED_PROTO,           CONST_LEN_STR("x-forwarded-proto") }
85  ,{ HTTP_HEADER_IF_UNMODIFIED_SINCE,         CONST_LEN_STR("if-unmodified-since") }
86  ,{ HTTP_HEADER_X_CONTENT_TYPE_OPTIONS,      CONST_LEN_STR("x-content-type-options") }
87  ,{ HTTP_HEADER_CONTENT_SECURITY_POLICY,     CONST_LEN_STR("content-security-policy") }
88  ,{ HTTP_HEADER_STRICT_TRANSPORT_SECURITY,   CONST_LEN_STR("strict-transport-security") }
89  ,{ HTTP_HEADER_UPGRADE_INSECURE_REQUESTS,   CONST_LEN_STR("upgrade-insecure-requests") }
90  ,{ HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, CONST_LEN_STR("access-control-allow-origin") }
91  ,{ HTTP_HEADER_OTHER, 0, "" }
92 };
93 
94 enum http_header_e http_header_hkey_get(const char * const s, const uint32_t slen) {
95     const struct keyvlenvalue * const kv = http_headers;
96     int i = slen < sizeof(http_headers_off) ? http_headers_off[slen] : -1;
97     if (i < 0) return HTTP_HEADER_OTHER;
98     do {
99         if (buffer_eq_icase_ssn(s, kv[i].value, slen))
100             return (enum http_header_e)kv[i].key;
101     } while (slen == kv[++i].vlen);
102     return HTTP_HEADER_OTHER;
103 }
104 
105 enum http_header_e http_header_hkey_get_lc(const char * const s, const uint32_t slen) {
106     const struct keyvlenvalue * const kv = http_headers;
107     int i = slen < sizeof(http_headers_off) ? http_headers_off[slen] : -1;
108     if (i < 0) return HTTP_HEADER_OTHER;
109     do {
110         if (0 == memcmp(s, kv[i].value, slen))
111             return (enum http_header_e)kv[i].key;
112     } while (slen == kv[++i].vlen);
113     return HTTP_HEADER_OTHER;
114 }
115 
116 
117 int http_header_str_to_code (const char * const s)
118 {
119     /*(more strict than strtol(); exactly 3 digits followed by SP/TAB/NIL)*/
120     return (light_isdigit(s[0]) && light_isdigit(s[1]) && light_isdigit(s[2])
121             && (s[3] == '\0' || s[3] == ' ' || s[3] == '\t'))
122       ? (s[0]-'0')*100 + (s[1]-'0')*10 + (s[2]-'0')
123       : -1;
124 }
125 
126 int http_header_str_contains_token (const char * const s, const uint32_t slen, const char * const m, const uint32_t mlen)
127 {
128     /*if (slen < mlen) return 0;*//*(possible optimizations for caller)*/
129     /*if (slen == mlen && buffer_eq_icase_ssn(s, m, mlen)) return 1;*/
130     /*(note: does not handle quoted-string)*/
131     uint32_t i = 0;
132     do {
133         while (i < slen &&  (s[i]==' ' || s[i]=='\t' || s[i]==',')) ++i;
134         if (slen - i < mlen) return 0;
135         if (buffer_eq_icase_ssn(s+i, m, mlen)) {
136             i += mlen;
137             if (i == slen || s[i]==' ' || s[i]=='\t' || s[i]==',' || s[i]==';')
138                 return 1;
139         }
140         while (i < slen &&   s[i]!=',') ++i;
141     } while (i < slen);
142     return 0;
143 }
144 
145 
146 int http_header_remove_token (buffer * const b, const char * const m, const uint32_t mlen)
147 {
148     /*(remove all instance of token from string)*/
149     /*(note: does not handle quoted-string)*/
150     int rc = 0;
151     for (char *s = b->ptr; s; ) {
152         while (*s == ' ' || *s == '\t' || *s == ',') ++s;
153         if (0 == strncasecmp(s, m, mlen)) {
154             s += mlen;
155             if (*s=='\0' || *s==' ' || *s=='\t' || *s==',' || *s==';') {
156                 memset(s-mlen, ' ', mlen);
157                 while (*s != '\0' && *s != ',') ++s;
158                 rc = 1;
159                 if (*s == ',') {
160                     *s++ = ' ';
161                     continue;
162                 }
163                 else {
164                     for (s -= mlen; *s != ',' && s != b->ptr; --s) ;
165                     buffer_string_set_length(b, (size_t)(s - b->ptr));
166                     break;
167                 }
168             }
169         }
170         s = strchr(s, ',');
171     }
172     return rc;
173 }
174 
175 
176 static inline void http_header_token_append(buffer * const vb, const char * const v, const uint32_t vlen) {
177     if (!buffer_string_is_empty(vb))
178         buffer_append_string_len(vb, CONST_STR_LEN(", "));
179     buffer_append_string_len(vb, v, vlen);
180 }
181 
182 __attribute_cold__
183 static inline void http_header_token_append_cookie(buffer * const vb, const char * const v, const uint32_t vlen) {
184     /* Cookie request header must be special-cased to use ';' separator
185      * instead of ',' to combine multiple headers (if present) */
186     if (!buffer_string_is_empty(vb))
187         buffer_append_string_len(vb, CONST_STR_LEN("; "));
188     buffer_append_string_len(vb, v, vlen);
189 }
190 
191 __attribute_pure__
192 static inline buffer * http_header_generic_get_ifnotempty(const array * const a, const enum http_header_e id, const char * const k, const uint32_t klen) {
193     data_string * const ds =
194       (data_string *)array_get_element_klen_ext(a, id, k, klen);
195     return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL;
196 }
197 
198 static inline void http_header_set_key_value(array * const a, enum http_header_e id, const char * const k, const size_t klen, const char * const v, const size_t vlen) {
199     buffer_copy_string_len(array_get_buf_ptr_ext(a, id, k, klen), v, vlen);
200 }
201 
202 
203 buffer * http_header_response_get(const request_st * const r, enum http_header_e id, const char *k, uint32_t klen) {
204     return light_btst(r->resp_htags, id)
205       ? http_header_generic_get_ifnotempty(&r->resp_headers, id, k, klen)
206       : NULL;
207 }
208 
209 void http_header_response_unset(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) {
210     if (light_btst(r->resp_htags, id)) {
211         /* (do not clear bit for HTTP_HEADER_OTHER,
212          *  as there might be addtl "other" headers) */
213         if (id > HTTP_HEADER_OTHER) light_bclr(r->resp_htags, id);
214         http_header_set_key_value(&r->resp_headers,id,k,klen,CONST_STR_LEN(""));
215     }
216 }
217 
218 void http_header_response_set(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
219     /* set value, including setting blank value if 0 == vlen
220      * (note: if 0 == vlen, header is still inserted with blank value,
221      *  which is used to indicate a "removed" header)
222      * (do not clear bit for HTTP_HEADER_OTHER if 0 == vlen,
223      *  as there might be addtl "other" headers) */
224     (vlen)
225       ? light_bset(r->resp_htags, id)
226       : (id > HTTP_HEADER_OTHER ? light_bclr(r->resp_htags, id) : 0);
227     http_header_set_key_value(&r->resp_headers, id, k, klen, v, vlen);
228 }
229 
230 void http_header_response_append(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
231     if (0 == vlen) return;
232     light_bset(r->resp_htags, id);
233     buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen);
234     http_header_token_append(vb, v, vlen);
235 }
236 
237 __attribute_cold__
238 static void http_header_response_insert_addtl(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, buffer * const vb, uint32_t vlen) {
239     UNUSED(id);
240     buffer_append_string_len(vb, CONST_STR_LEN("\r\n"));
241     if (r->http_version >= HTTP_VERSION_2) {
242         r->resp_header_repeated = 1;
243         char * const h = buffer_string_prepare_append(vb, klen + vlen + 2);
244         for (uint32_t i = 0; i < klen; ++i)
245             h[i] = !light_isupper(k[i]) ? k[i] : (k[i] | 0x20);
246         buffer_commit(vb, klen);
247     }
248     else
249         buffer_append_string_len(vb, k, klen);
250     buffer_append_string_len(vb, CONST_STR_LEN(": "));
251 }
252 
253 void http_header_response_insert(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
254     if (0 == vlen) return;
255     light_bset(r->resp_htags, id);
256     buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen);
257     if (!buffer_string_is_empty(vb)) /*append repeated field-name on new line*/
258         http_header_response_insert_addtl(r, id, k, klen, vb, vlen);
259     buffer_append_string_len(vb, v, vlen);
260 }
261 
262 
263 buffer * http_header_request_get(const request_st * const r, enum http_header_e id, const char *k, uint32_t klen) {
264     return light_btst(r->rqst_htags, id)
265       ? http_header_generic_get_ifnotempty(&r->rqst_headers, id, k, klen)
266       : NULL;
267 }
268 
269 void http_header_request_unset(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) {
270     if (light_btst(r->rqst_htags, id)) {
271         /* (do not clear bit for HTTP_HEADER_OTHER,
272          *  as there might be addtl "other" headers) */
273         if (id > HTTP_HEADER_OTHER) light_bclr(r->rqst_htags, id);
274         http_header_set_key_value(&r->rqst_headers,id,k,klen,CONST_STR_LEN(""));
275     }
276 }
277 
278 void http_header_request_set(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
279     /* set value, including setting blank value if 0 == vlen
280      * (note: if 0 == vlen, header is still inserted with blank value,
281      *  which is used to indicate a "removed" header)
282      * (do not clear bit for HTTP_HEADER_OTHER if 0 == vlen,
283      *  as there might be addtl "other" headers) */
284     (vlen)
285       ? light_bset(r->rqst_htags, id)
286       : (id > HTTP_HEADER_OTHER ? light_bclr(r->rqst_htags, id) : 0);
287     http_header_set_key_value(&r->rqst_headers, id, k, klen, v, vlen);
288 }
289 
290 void http_header_request_append(request_st * const r, enum http_header_e id, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
291     if (0 == vlen) return;
292     light_bset(r->rqst_htags, id);
293     buffer * const vb = array_get_buf_ptr_ext(&r->rqst_headers, id, k, klen);
294     if (id != HTTP_HEADER_COOKIE)
295         http_header_token_append(vb, v, vlen);
296     else
297         http_header_token_append_cookie(vb, v, vlen);
298 }
299 
300 
301 buffer * http_header_env_get(const request_st * const r, const char *k, uint32_t klen) {
302     /* similar to http_header_generic_get_ifnotempty() but without id */
303     data_string * const ds =
304       (data_string *)array_get_element_klen(&r->env, k, klen);
305     return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL;
306 }
307 
308 void http_header_env_set(request_st * const r, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
309     array_set_key_value(&r->env, k, klen, v, vlen);
310 }
311 
312 void http_header_env_append(request_st * const r, const char *k, uint32_t klen, const char *v, uint32_t vlen) {
313     /*if (0 == vlen) return;*//* skip check; permit env var w/ blank value */
314     buffer * const vb = array_get_buf_ptr(&r->env, k, klen);
315     if (0 == vlen) return;
316     http_header_token_append(vb, v, vlen);
317 }
318 
319 
320 uint32_t
321 http_header_parse_hoff (const char *n, const uint32_t clen, unsigned short hoff[8192])
322 {
323     uint32_t hlen = 0;
324     for (const char *b; (n = memchr((b = n),'\n',clen-hlen)); ++n) {
325         uint32_t x = (uint32_t)(n - b + 1);
326         hlen += x;
327         if (x <= 2 && (x == 1 || n[-1] == '\r')) {
328             hoff[hoff[0]+1] = hlen;
329             return hlen;
330         }
331         if (++hoff[0] >= /*sizeof(hoff)/sizeof(hoff[0])-1*/ 8192-1) break;
332         hoff[hoff[0]] = hlen;
333     }
334     return 0;
335 }
336