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