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 /* 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 uint32_t slen) { 98 const struct keyvlenvalue * const kv = http_headers; 99 int i = slen < sizeof(http_headers_off) ? http_headers_off[slen] : -1; 100 if (i < 0) return HTTP_HEADER_OTHER; 101 do { 102 if (buffer_eq_icase_ssn(s, kv[i].value, slen)) 103 return (enum http_header_e)kv[i].key; 104 } while (slen == kv[++i].vlen); 105 return HTTP_HEADER_OTHER; 106 } 107 108 enum http_header_e http_header_hkey_get_lc(const char * const s, const uint32_t slen) { 109 const struct keyvlenvalue * const kv = http_headers; 110 int i = slen < sizeof(http_headers_off) ? http_headers_off[slen] : -1; 111 if (i < 0) return HTTP_HEADER_OTHER; 112 do { 113 if (0 == memcmp(s, kv[i].value, slen)) 114 return (enum http_header_e)kv[i].key; 115 } while (slen == kv[++i].vlen); 116 return HTTP_HEADER_OTHER; 117 } 118 119 120 int http_header_str_to_code (const char * const s) 121 { 122 /*(more strict than strtol(); exactly 3 digits followed by SP/TAB/NIL)*/ 123 return (light_isdigit(s[0]) && light_isdigit(s[1]) && light_isdigit(s[2]) 124 && (s[3] == '\0' || s[3] == ' ' || s[3] == '\t')) 125 ? (s[0]-'0')*100 + (s[1]-'0')*10 + (s[2]-'0') 126 : -1; 127 } 128 129 int http_header_str_contains_token (const char * const s, const uint32_t slen, const char * const m, const uint32_t mlen) 130 { 131 /*if (slen < mlen) return 0;*//*(possible optimizations for caller)*/ 132 /*if (slen == mlen && buffer_eq_icase_ssn(s, m, mlen)) return 1;*/ 133 /*(note: does not handle quoted-string)*/ 134 uint32_t i = 0; 135 do { 136 while (i < slen && (s[i]==' ' || s[i]=='\t' || s[i]==',')) ++i; 137 if (slen - i < mlen) return 0; 138 if (buffer_eq_icase_ssn(s+i, m, mlen)) { 139 i += mlen; 140 if (i == slen || s[i]==' ' || s[i]=='\t' || s[i]==',' || s[i]==';') 141 return 1; 142 } 143 while (i < slen && s[i]!=',') ++i; 144 } while (i < slen); 145 return 0; 146 } 147 148 149 int http_header_remove_token (buffer * const b, const char * const m, const uint32_t mlen) 150 { 151 /*(remove all instance of token from string)*/ 152 /*(note: does not handle quoted-string)*/ 153 int rc = 0; 154 for (char *s = b->ptr; s; ) { 155 while (*s == ' ' || *s == '\t' || *s == ',') ++s; 156 if (0 == strncasecmp(s, m, mlen)) { 157 s += mlen; 158 if (*s=='\0' || *s==' ' || *s=='\t' || *s==',' || *s==';') { 159 memset(s-mlen, ' ', mlen); 160 while (*s != '\0' && *s != ',') ++s; 161 rc = 1; 162 if (*s == ',') { 163 *s++ = ' '; 164 continue; 165 } 166 else { 167 for (s -= mlen; *s != ',' && s != b->ptr; --s) ; 168 buffer_string_set_length(b, (size_t)(s - b->ptr)); 169 break; 170 } 171 } 172 } 173 s = strchr(s, ','); 174 } 175 return rc; 176 } 177 178 179 static inline void http_header_token_append(buffer * const vb, const char * const v, const uint32_t vlen) { 180 if (!buffer_string_is_empty(vb)) 181 buffer_append_string_len(vb, CONST_STR_LEN(", ")); 182 buffer_append_string_len(vb, v, vlen); 183 } 184 185 __attribute_cold__ 186 static inline void http_header_token_append_cookie(buffer * const vb, const char * const v, const uint32_t vlen) { 187 /* Cookie request header must be special-cased to use ';' separator 188 * instead of ',' to combine multiple headers (if present) */ 189 if (!buffer_string_is_empty(vb)) 190 buffer_append_string_len(vb, CONST_STR_LEN("; ")); 191 buffer_append_string_len(vb, v, vlen); 192 } 193 194 __attribute_pure__ 195 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) { 196 data_string * const ds = 197 (data_string *)array_get_element_klen_ext(a, id, k, klen); 198 return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL; 199 } 200 201 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) { 202 buffer_copy_string_len(array_get_buf_ptr_ext(a, id, k, klen), v, vlen); 203 } 204 205 206 buffer * http_header_response_get(const request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 207 return light_btst(r->resp_htags, id) 208 ? http_header_generic_get_ifnotempty(&r->resp_headers, id, k, klen) 209 : NULL; 210 } 211 212 buffer * http_header_response_set_ptr(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 213 /* note: caller must not leave buffer empty 214 * or must call http_header_response_unset() */ 215 light_bset(r->resp_htags, id); 216 buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen); 217 buffer_clear(vb); 218 return vb; 219 } 220 221 void http_header_response_unset(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 222 if (light_btst(r->resp_htags, id)) { 223 /* (do not clear bit for HTTP_HEADER_OTHER, 224 * as there might be addtl "other" headers) */ 225 if (id > HTTP_HEADER_OTHER) light_bclr(r->resp_htags, id); 226 http_header_set_key_value(&r->resp_headers,id,k,klen,CONST_STR_LEN("")); 227 } 228 } 229 230 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) { 231 /* set value, including setting blank value if 0 == vlen 232 * (note: if 0 == vlen, header is still inserted with blank value, 233 * which is used to indicate a "removed" header) 234 * (do not clear bit for HTTP_HEADER_OTHER if 0 == vlen, 235 * as there might be addtl "other" headers) */ 236 (vlen) 237 ? light_bset(r->resp_htags, id) 238 : (id > HTTP_HEADER_OTHER ? light_bclr(r->resp_htags, id) : 0); 239 http_header_set_key_value(&r->resp_headers, id, k, klen, v, vlen); 240 } 241 242 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) { 243 if (0 == vlen) return; 244 light_bset(r->resp_htags, id); 245 buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen); 246 http_header_token_append(vb, v, vlen); 247 } 248 249 __attribute_cold__ 250 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) { 251 UNUSED(id); 252 char *h = buffer_string_prepare_append(vb, 2 + klen + vlen + 2); 253 buffer_append_str3(vb, CONST_STR_LEN("\r\n"), k, klen, CONST_STR_LEN(": ")); 254 if (r->http_version >= HTTP_VERSION_2) { 255 r->resp_header_repeated = 1; 256 h += 2; 257 for (uint32_t i = 0; i < klen; ++i) { 258 if (light_isupper(h[i])) h[i] |= 0x20; 259 } 260 } 261 } 262 263 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) { 264 if (0 == vlen) return; 265 light_bset(r->resp_htags, id); 266 buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen); 267 if (!buffer_string_is_empty(vb)) /*append repeated field-name on new line*/ 268 http_header_response_insert_addtl(r, id, k, klen, vb, vlen); 269 buffer_append_string_len(vb, v, vlen); 270 } 271 272 273 buffer * http_header_request_get(const request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 274 return light_btst(r->rqst_htags, id) 275 ? http_header_generic_get_ifnotempty(&r->rqst_headers, id, k, klen) 276 : NULL; 277 } 278 279 buffer * http_header_request_set_ptr(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 280 /* note: caller must not leave buffer empty 281 * or must call http_header_request_unset() */ 282 light_bset(r->rqst_htags, id); 283 buffer * const vb = array_get_buf_ptr_ext(&r->rqst_headers, id, k, klen); 284 buffer_clear(vb); 285 return vb; 286 } 287 288 void http_header_request_unset(request_st * const r, enum http_header_e id, const char *k, uint32_t klen) { 289 if (light_btst(r->rqst_htags, id)) { 290 /* (do not clear bit for HTTP_HEADER_OTHER, 291 * as there might be addtl "other" headers) */ 292 if (id > HTTP_HEADER_OTHER) light_bclr(r->rqst_htags, id); 293 http_header_set_key_value(&r->rqst_headers,id,k,klen,CONST_STR_LEN("")); 294 } 295 } 296 297 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) { 298 /* set value, including setting blank value if 0 == vlen 299 * (note: if 0 == vlen, header is still inserted with blank value, 300 * which is used to indicate a "removed" header) 301 * (do not clear bit for HTTP_HEADER_OTHER if 0 == vlen, 302 * as there might be addtl "other" headers) */ 303 (vlen) 304 ? light_bset(r->rqst_htags, id) 305 : (id > HTTP_HEADER_OTHER ? light_bclr(r->rqst_htags, id) : 0); 306 http_header_set_key_value(&r->rqst_headers, id, k, klen, v, vlen); 307 } 308 309 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) { 310 if (0 == vlen) return; 311 light_bset(r->rqst_htags, id); 312 buffer * const vb = array_get_buf_ptr_ext(&r->rqst_headers, id, k, klen); 313 if (id != HTTP_HEADER_COOKIE) 314 http_header_token_append(vb, v, vlen); 315 else 316 http_header_token_append_cookie(vb, v, vlen); 317 } 318 319 320 buffer * http_header_env_get(const request_st * const r, const char *k, uint32_t klen) { 321 /* similar to http_header_generic_get_ifnotempty() but without id */ 322 data_string * const ds = 323 (data_string *)array_get_element_klen(&r->env, k, klen); 324 return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL; 325 } 326 327 buffer * http_header_env_set_ptr(request_st *r, const char *k, uint32_t klen) { 328 buffer * const vb = array_get_buf_ptr(&r->env, k, klen); 329 buffer_clear(vb); 330 return vb; 331 } 332 333 void http_header_env_set(request_st * const r, const char *k, uint32_t klen, const char *v, uint32_t vlen) { 334 array_set_key_value(&r->env, k, klen, v, vlen); 335 } 336 337 void http_header_env_append(request_st * const r, const char *k, uint32_t klen, const char *v, uint32_t vlen) { 338 /*if (0 == vlen) return;*//* skip check; permit env var w/ blank value */ 339 buffer * const vb = array_get_buf_ptr(&r->env, k, klen); 340 if (0 == vlen) return; 341 http_header_token_append(vb, v, vlen); 342 } 343 344 345 uint32_t 346 http_header_parse_hoff (const char *n, const uint32_t clen, unsigned short hoff[8192]) 347 { 348 uint32_t hlen = 0; 349 for (const char *b; (n = memchr((b = n),'\n',clen-hlen)); ++n) { 350 uint32_t x = (uint32_t)(n - b + 1); 351 hlen += x; 352 if (x <= 2 && (x == 1 || n[-1] == '\r')) { 353 hoff[hoff[0]+1] = hlen; 354 return hlen; 355 } 356 if (++hoff[0] >= /*sizeof(hoff)/sizeof(hoff[0])-1*/ 8192-1) break; 357 hoff[hoff[0]] = hlen; 358 } 359 return 0; 360 } 361