]>
Commit | Line | Data |
---|---|---|
cb69b4c7 | 1 | /* |
f6e9a3ee | 2 | * Copyright (C) 1996-2019 The Squid Software Foundation and contributors |
cb69b4c7 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
cb69b4c7 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 55 HTTP Header */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
d5f18517 | 12 | #include "base/CharacterSet.h" |
81ab22b6 | 13 | #include "base/EnumIterator.h" |
25f98340 | 14 | #include "base64.h" |
67679543 | 15 | #include "globals.h" |
a1b9ec20 | 16 | #include "http/ContentLengthInterpreter.h" |
7ebe76de | 17 | #include "HttpHdrCc.h" |
582c2af2 | 18 | #include "HttpHdrContRange.h" |
1da82544 | 19 | #include "HttpHdrScTarget.h" // also includes HttpHdrSc.h |
8822ebee | 20 | #include "HttpHeader.h" |
79cb238d | 21 | #include "HttpHeaderFieldInfo.h" |
e1656dc4 | 22 | #include "HttpHeaderStat.h" |
a5bac1d2 | 23 | #include "HttpHeaderTools.h" |
0eb49b6d | 24 | #include "MemBuf.h" |
8822ebee | 25 | #include "mgr/Registration.h" |
69c698a3 | 26 | #include "mime_header.h" |
582c2af2 | 27 | #include "profiler/Profiler.h" |
25f98340 | 28 | #include "rfc1123.h" |
90be6ff5 | 29 | #include "sbuf/StringConvert.h" |
602d9612 | 30 | #include "SquidConfig.h" |
00a7574e | 31 | #include "StatHist.h" |
8822ebee | 32 | #include "Store.h" |
28204b3b | 33 | #include "StrList.h" |
81a94152 | 34 | #include "TimeOrTag.h" |
ed6e9fb9 | 35 | #include "util.h" |
cb69b4c7 | 36 | |
42865d25 FC |
37 | #include <algorithm> |
38 | ||
6fda1515 FC |
39 | /* XXX: the whole set of API managing the entries vector should be rethought |
40 | * after the parse4r-ng effort is complete. | |
41 | */ | |
42 | ||
cb69b4c7 | 43 | /* |
2ac76861 | 44 | * On naming conventions: |
26ac0430 AJ |
45 | * |
46 | * HTTP/1.1 defines message-header as | |
47 | * | |
2ac76861 | 48 | * message-header = field-name ":" [ field-value ] CRLF |
49 | * field-name = token | |
50 | * field-value = *( field-content | LWS ) | |
26ac0430 | 51 | * |
2ac76861 | 52 | * HTTP/1.1 does not give a name name a group of all message-headers in a message. |
53 | * Squid 1.1 seems to refer to that group _plus_ start-line as "headers". | |
26ac0430 | 54 | * |
2ac76861 | 55 | * HttpHeader is an object that represents all message-headers in a message. |
56 | * HttpHeader does not manage start-line. | |
26ac0430 | 57 | * |
d8b249ef | 58 | * HttpHeader is implemented as a collection of header "entries". |
59 | * An entry is a (field_id, field_name, field_value) triplet. | |
2ac76861 | 60 | */ |
cb69b4c7 | 61 | |
cb69b4c7 | 62 | /* |
63 | * local constants and vars | |
64 | */ | |
65 | ||
789217a2 | 66 | // statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted |
81ab22b6 | 67 | std::vector<HttpHeaderFieldStat> headerStatsTable(Http::HdrType::enumEnd_); |
cb69b4c7 | 68 | |
81ab22b6 | 69 | /* request-only headers. Used for cachemgr */ |
f53969cc | 70 | static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */ |
16d3204a | 71 | |
81ab22b6 | 72 | /* reply-only headers. Used for cachemgr */ |
f53969cc | 73 | static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */ |
2cdeea82 | 74 | |
12cf1be2 | 75 | /* header accounting */ |
26735116 | 76 | // NP: keep in sync with enum http_hdr_owner_type |
26ac0430 | 77 | static HttpHeaderStat HttpHeaderStats[] = { |
95cc1e3e | 78 | HttpHeaderStat(/*hoNone*/ "all", NULL), |
9f4d4f65 | 79 | #if USE_HTCP |
95cc1e3e | 80 | HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask), |
9f4d4f65 | 81 | #endif |
95cc1e3e FC |
82 | HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask), |
83 | HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask) | |
26735116 AJ |
84 | #if USE_OPENSSL |
85 | /* hoErrorDetail */ | |
86 | #endif | |
87 | /* hoEnd */ | |
26ac0430 | 88 | }; |
9bea1d5b | 89 | static int HttpHeaderStatCount = countof(HttpHeaderStats); |
12cf1be2 | 90 | |
7faf2bdb | 91 | static int HeaderEntryParsedCount = 0; |
cb69b4c7 | 92 | |
12cf1be2 | 93 | /* |
f734cda2 | 94 | * forward declarations and local routines |
12cf1be2 | 95 | */ |
cb69b4c7 | 96 | |
f734cda2 | 97 | class StoreEntry; |
cb69b4c7 | 98 | |
81ab22b6 FC |
99 | // update parse statistics for header id; if error is true also account |
100 | // for errors and write to debug log what happened | |
101 | static void httpHeaderNoteParsedEntry(Http::HdrType id, String const &value, bool error); | |
73b2aa87 | 102 | static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e); |
f734cda2 FC |
103 | /** store report about current header usage and other stats */ |
104 | static void httpHeaderStoreReport(StoreEntry * e); | |
105 | ||
cb69b4c7 | 106 | /* |
107 | * Module initialization routines | |
108 | */ | |
109 | ||
6b7d87bb FC |
110 | static void |
111 | httpHeaderRegisterWithCacheManager(void) | |
112 | { | |
8822ebee | 113 | Mgr::RegisterAction("http_headers", |
d9fc6862 A |
114 | "HTTP Header Statistics", |
115 | httpHeaderStoreReport, 0, 1); | |
6b7d87bb FC |
116 | } |
117 | ||
cb69b4c7 | 118 | void |
9bea1d5b | 119 | httpHeaderInitModule(void) |
cb69b4c7 | 120 | { |
9bea1d5b | 121 | /* check that we have enough space for masks */ |
81ab22b6 FC |
122 | assert(8 * sizeof(HttpHeaderMask) >= Http::HdrType::enumEnd_); |
123 | ||
124 | // masks are needed for stats page still | |
125 | for (auto h : WholeEnum<Http::HdrType>()) { | |
126 | if (Http::HeaderLookupTable.lookup(h).request) | |
127 | CBIT_SET(RequestHeadersMask,h); | |
128 | if (Http::HeaderLookupTable.lookup(h).reply) | |
129 | CBIT_SET(ReplyHeadersMask,h); | |
130 | } | |
dcf3665b | 131 | |
26735116 | 132 | /* header stats initialized by class constructor */ |
9bea1d5b | 133 | assert(HttpHeaderStatCount == hoReply + 1); |
16d3204a | 134 | |
9bea1d5b | 135 | /* init dependent modules */ |
136 | httpHdrCcInitModule(); | |
43ae1d95 | 137 | httpHdrScInitModule(); |
ea391f18 FC |
138 | |
139 | httpHeaderRegisterWithCacheManager(); | |
62ee09ca | 140 | } |
43ae1d95 | 141 | |
cb69b4c7 | 142 | /* |
143 | * HttpHeader Implementation | |
144 | */ | |
145 | ||
3e42b356 | 146 | HttpHeader::HttpHeader() : owner (hoNone), len (0), conflictingContentLength_(false) |
cb69b4c7 | 147 | { |
c3b51d64 | 148 | entries.reserve(32); |
75faaa7a | 149 | httpHeaderMaskInit(&mask, 0); |
150 | } | |
151 | ||
3e42b356 | 152 | HttpHeader::HttpHeader(const http_hdr_owner_type anOwner): owner(anOwner), len(0), conflictingContentLength_(false) |
75faaa7a | 153 | { |
02259ff8 | 154 | assert(anOwner > hoNone && anOwner < hoEnd); |
efc26e8e | 155 | debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner); |
c3b51d64 | 156 | entries.reserve(32); |
75faaa7a | 157 | httpHeaderMaskInit(&mask, 0); |
158 | } | |
159 | ||
3e42b356 | 160 | HttpHeader::HttpHeader(const HttpHeader &other): owner(other.owner), len(other.len), conflictingContentLength_(false) |
5e5fa5b1 | 161 | { |
c3b51d64 | 162 | entries.reserve(other.entries.capacity()); |
5e5fa5b1 | 163 | httpHeaderMaskInit(&mask, 0); |
2d4f252d | 164 | update(&other); // will update the mask as well |
5e5fa5b1 AR |
165 | } |
166 | ||
75faaa7a | 167 | HttpHeader::~HttpHeader() |
168 | { | |
519e0948 | 169 | clean(); |
cb69b4c7 | 170 | } |
171 | ||
5e5fa5b1 AR |
172 | HttpHeader & |
173 | HttpHeader::operator =(const HttpHeader &other) | |
174 | { | |
175 | if (this != &other) { | |
176 | // we do not really care, but the caller probably does | |
177 | assert(owner == other.owner); | |
178 | clean(); | |
2d4f252d | 179 | update(&other); // will update the mask as well |
5e5fa5b1 | 180 | len = other.len; |
3e42b356 | 181 | conflictingContentLength_ = other.conflictingContentLength_; |
5e5fa5b1 AR |
182 | } |
183 | return *this; | |
184 | } | |
185 | ||
cb69b4c7 | 186 | void |
519e0948 | 187 | HttpHeader::clean() |
cb69b4c7 | 188 | { |
9bea1d5b | 189 | |
02259ff8 | 190 | assert(owner > hoNone && owner < hoEnd); |
efc26e8e | 191 | debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner); |
9bea1d5b | 192 | |
3a9cf2d5 | 193 | PROF_start(HttpHeaderClean); |
194 | ||
0de73a38 | 195 | if (owner <= hoReply) { |
c594d76c A |
196 | /* |
197 | * An unfortunate bug. The entries array is initialized | |
198 | * such that count is set to zero. httpHeaderClean() seems to | |
199 | * be called both when 'hdr' is created, and destroyed. Thus, | |
200 | * we accumulate a large number of zero counts for 'hdr' before | |
201 | * it is ever used. Can't think of a good way to fix it, except | |
202 | * adding a state variable that indicates whether or not 'hdr' | |
203 | * has been used. As a hack, just never count zero-sized header | |
204 | * arrays. | |
205 | */ | |
4c9eadc2 FC |
206 | if (!entries.empty()) |
207 | HttpHeaderStats[owner].hdrUCountDistr.count(entries.size()); | |
51328da8 | 208 | |
95dc7ff4 | 209 | ++ HttpHeaderStats[owner].destroyedCount; |
51328da8 | 210 | |
4c9eadc2 | 211 | HttpHeaderStats[owner].busyDestroyedCount += entries.size() > 0; |
0de73a38 | 212 | } // if (owner <= hoReply) |
62e76326 | 213 | |
81ab22b6 | 214 | for (HttpHeaderEntry *e : entries) { |
1da82544 | 215 | if (e == nullptr) |
6fda1515 | 216 | continue; |
1da82544 | 217 | if (!Http::any_valid_header(e->id)) { |
6fda1515 | 218 | debugs(55, DBG_CRITICAL, "BUG: invalid entry (" << e->id << "). Ignored."); |
c594d76c A |
219 | } else { |
220 | if (owner <= hoReply) | |
f30f7998 | 221 | HttpHeaderStats[owner].fieldTypeDistr.count(e->id); |
c594d76c | 222 | delete e; |
62e76326 | 223 | } |
c594d76c | 224 | } |
6fda1515 | 225 | |
c33a88ca | 226 | entries.clear(); |
519e0948 | 227 | httpHeaderMaskInit(&mask, 0); |
5e5fa5b1 | 228 | len = 0; |
3e42b356 | 229 | conflictingContentLength_ = false; |
3a9cf2d5 | 230 | PROF_stop(HttpHeaderClean); |
cb69b4c7 | 231 | } |
232 | ||
2246b732 | 233 | /* append entries (also see httpHeaderUpdate) */ |
234 | void | |
a9925b40 | 235 | HttpHeader::append(const HttpHeader * src) |
2246b732 | 236 | { |
a9925b40 | 237 | assert(src); |
238 | assert(src != this); | |
efc26e8e | 239 | debugs(55, 7, "appending hdr: " << this << " += " << src); |
9bea1d5b | 240 | |
81ab22b6 FC |
241 | for (auto e : src->entries) { |
242 | if (e) | |
243 | addEntry(e->clone()); | |
2246b732 | 244 | } |
245 | } | |
246 | ||
1a210de4 EB |
247 | /// check whether the fresh header has any new/changed updatable fields |
248 | bool | |
249 | HttpHeader::needUpdate(HttpHeader const *fresh) const | |
250 | { | |
251 | for (const auto e: fresh->entries) { | |
d747ab54 | 252 | if (!e || skipUpdateHeader(e->id)) |
1a210de4 EB |
253 | continue; |
254 | String value; | |
d5f18517 AJ |
255 | if (!hasNamed(e->name, &value) || |
256 | (value != fresh->getByName(e->name))) | |
1a210de4 EB |
257 | return true; |
258 | } | |
259 | return false; | |
260 | } | |
261 | ||
924f73bc | 262 | void |
2d4f252d EB |
263 | HttpHeader::updateWarnings() |
264 | { | |
265 | int count = 0; | |
266 | HttpHeaderPos pos = HttpHeaderInitPos; | |
267 | ||
268 | // RFC 7234, section 4.3.4: delete 1xx warnings and retain 2xx warnings | |
269 | while (HttpHeaderEntry *e = getEntry(&pos)) { | |
270 | if (e->id == Http::HdrType::WARNING && (e->getInt()/100 == 1) ) | |
271 | delAt(pos, count); | |
272 | } | |
273 | } | |
274 | ||
275 | bool | |
276 | HttpHeader::skipUpdateHeader(const Http::HdrType id) const | |
277 | { | |
278 | // RFC 7234, section 4.3.4: use fields other from Warning for update | |
279 | return id == Http::HdrType::WARNING; | |
280 | } | |
281 | ||
1a210de4 | 282 | bool |
2d4f252d | 283 | HttpHeader::update(HttpHeader const *fresh) |
cb69b4c7 | 284 | { |
a9925b40 | 285 | assert(fresh); |
924f73bc | 286 | assert(this != fresh); |
9bea1d5b | 287 | |
1a210de4 EB |
288 | // Optimization: Finding whether a header field changed is expensive |
289 | // and probably not worth it except for collapsed revalidation needs. | |
290 | if (Config.onoff.collapsed_forwarding && !needUpdate(fresh)) | |
291 | return false; | |
292 | ||
2d4f252d EB |
293 | updateWarnings(); |
294 | ||
1a210de4 EB |
295 | const HttpHeaderEntry *e; |
296 | HttpHeaderPos pos = HttpHeaderInitPos; | |
297 | ||
a9925b40 | 298 | while ((e = fresh->getEntry(&pos))) { |
789217a2 | 299 | /* deny bad guys (ok to check for Http::HdrType::OTHER) here */ |
62e76326 | 300 | |
2d4f252d | 301 | if (skipUpdateHeader(e->id)) |
62e76326 | 302 | continue; |
303 | ||
789217a2 | 304 | if (e->id != Http::HdrType::OTHER) |
ba9fb01d | 305 | delById(e->id); |
306 | else | |
d5f18517 | 307 | delByName(e->name); |
9917847f | 308 | } |
309 | ||
310 | pos = HttpHeaderInitPos; | |
311 | while ((e = fresh->getEntry(&pos))) { | |
789217a2 | 312 | /* deny bad guys (ok to check for Http::HdrType::OTHER) here */ |
9917847f | 313 | |
2d4f252d | 314 | if (skipUpdateHeader(e->id)) |
9917847f | 315 | continue; |
316 | ||
81ab22b6 | 317 | debugs(55, 7, "Updating header '" << Http::HeaderLookupTable.lookup(e->id).name << "' in cached entry"); |
62e76326 | 318 | |
eede25e7 | 319 | addEntry(e->clone()); |
cb69b4c7 | 320 | } |
1a210de4 | 321 | return true; |
cb69b4c7 | 322 | } |
323 | ||
69c698a3 EB |
324 | bool |
325 | HttpHeader::Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end) | |
326 | { | |
327 | /* | |
328 | * parse_start points to the first line of HTTP message *headers*, | |
329 | * not including the request or status lines | |
330 | */ | |
331 | const size_t end = headersEnd(*parse_start, l); | |
332 | ||
333 | if (end) { | |
334 | *blk_start = *parse_start; | |
335 | *blk_end = *parse_start + end - 1; | |
336 | assert(**blk_end == '\n'); | |
337 | // Point blk_end to the first character after the last header field. | |
338 | // In other words, blk_end should point to the CR?LF header terminator. | |
339 | if (end > 1 && *(*blk_end - 1) == '\r') | |
340 | --(*blk_end); | |
341 | *parse_start += end; | |
342 | } | |
343 | return end; | |
344 | } | |
345 | ||
346 | int | |
4f1c93a7 | 347 | HttpHeader::parse(const char *buf, size_t buf_len, bool atEnd, size_t &hdr_sz, Http::ContentLengthInterpreter &clen) |
69c698a3 EB |
348 | { |
349 | const char *parse_start = buf; | |
350 | const char *blk_start, *blk_end; | |
351 | hdr_sz = 0; | |
352 | ||
353 | if (!Isolate(&parse_start, buf_len, &blk_start, &blk_end)) { | |
354 | // XXX: do not parse non-isolated headers even if the connection is closed. | |
355 | // Treat unterminated headers as "partial headers" framing errors. | |
356 | if (!atEnd) | |
357 | return 0; | |
358 | blk_start = parse_start; | |
359 | blk_end = blk_start + strlen(blk_start); | |
360 | } | |
361 | ||
4f1c93a7 | 362 | if (parse(blk_start, blk_end - blk_start, clen)) { |
69c698a3 EB |
363 | hdr_sz = parse_start - buf; |
364 | return 1; | |
365 | } | |
366 | return -1; | |
367 | } | |
368 | ||
cb69b4c7 | 369 | int |
4f1c93a7 | 370 | HttpHeader::parse(const char *header_start, size_t hdrLen, Http::ContentLengthInterpreter &clen) |
cb69b4c7 | 371 | { |
52d3f198 | 372 | const char *field_ptr = header_start; |
784619e6 | 373 | const char *header_end = header_start + hdrLen; // XXX: remove |
56cd29e6 | 374 | int warnOnError = (Config.onoff.relaxed_header_parser <= 0 ? DBG_IMPORTANT : 2); |
9bea1d5b | 375 | |
3a9cf2d5 | 376 | PROF_start(HttpHeaderParse); |
377 | ||
9bea1d5b | 378 | assert(header_start && header_end); |
81858ebc | 379 | debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl << getStringPrefix(header_start, hdrLen)); |
95dc7ff4 | 380 | ++ HttpHeaderStats[owner].parsedCount; |
62e76326 | 381 | |
1d8a896b | 382 | char *nulpos; |
784619e6 | 383 | if ((nulpos = (char*)memchr(header_start, '\0', hdrLen))) { |
e0236918 | 384 | debugs(55, DBG_IMPORTANT, "WARNING: HTTP header contains NULL characters {" << |
81858ebc | 385 | getStringPrefix(header_start, nulpos-header_start) << "}\nNULL\n{" << getStringPrefix(nulpos+1, hdrLen-(nulpos-header_start)-1)); |
6d7bc97b | 386 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
387 | clean(); |
388 | return 0; | |
52d3f198 | 389 | } |
390 | ||
391 | /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>. | |
392 | * continuation lines start with a (single) space or tab */ | |
393 | while (field_ptr < header_end) { | |
394 | const char *field_start = field_ptr; | |
62e76326 | 395 | const char *field_end; |
62e76326 | 396 | |
397 | do { | |
52d3f198 | 398 | const char *this_line = field_ptr; |
399 | field_ptr = (const char *)memchr(field_ptr, '\n', header_end - field_ptr); | |
400 | ||
6d7bc97b AJ |
401 | if (!field_ptr) { |
402 | // missing <LF> | |
403 | PROF_stop(HttpHeaderParse); | |
81ab22b6 FC |
404 | clean(); |
405 | return 0; | |
6d7bc97b | 406 | } |
52d3f198 | 407 | |
408 | field_end = field_ptr; | |
409 | ||
f53969cc | 410 | ++field_ptr; /* Move to next line */ |
52d3f198 | 411 | |
412 | if (field_end > this_line && field_end[-1] == '\r') { | |
f53969cc | 413 | --field_end; /* Ignore CR LF */ |
52d3f198 | 414 | |
e10272fc AR |
415 | if (owner == hoRequest && field_end > this_line) { |
416 | bool cr_only = true; | |
417 | for (const char *p = this_line; p < field_end && cr_only; ++p) { | |
418 | if (*p != '\r') | |
419 | cr_only = false; | |
420 | } | |
421 | if (cr_only) { | |
6e09ed7b | 422 | debugs(55, DBG_IMPORTANT, "SECURITY WARNING: Rejecting HTTP request with a CR+ " |
e10272fc | 423 | "header field to prevent request smuggling attacks: {" << |
81858ebc | 424 | getStringPrefix(header_start, hdrLen) << "}"); |
6d7bc97b | 425 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
426 | clean(); |
427 | return 0; | |
e10272fc | 428 | } |
52d3f198 | 429 | } |
430 | } | |
431 | ||
432 | /* Barf on stray CR characters */ | |
433 | if (memchr(this_line, '\r', field_end - this_line)) { | |
6e09ed7b | 434 | debugs(55, warnOnError, "WARNING: suspicious CR characters in HTTP header {" << |
81858ebc | 435 | getStringPrefix(field_start, field_end-field_start) << "}"); |
52d3f198 | 436 | |
437 | if (Config.onoff.relaxed_header_parser) { | |
f53969cc | 438 | char *p = (char *) this_line; /* XXX Warning! This destroys original header content and violates specifications somewhat */ |
52d3f198 | 439 | |
a38ec4b1 FC |
440 | while ((p = (char *)memchr(p, '\r', field_end - p)) != NULL) { |
441 | *p = ' '; | |
442 | ++p; | |
443 | } | |
6d7bc97b AJ |
444 | } else { |
445 | PROF_stop(HttpHeaderParse); | |
81ab22b6 FC |
446 | clean(); |
447 | return 0; | |
6d7bc97b | 448 | } |
52d3f198 | 449 | } |
450 | ||
451 | if (this_line + 1 == field_end && this_line > field_start) { | |
6e09ed7b | 452 | debugs(55, warnOnError, "WARNING: Blank continuation line in HTTP header {" << |
81858ebc | 453 | getStringPrefix(header_start, hdrLen) << "}"); |
6d7bc97b | 454 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
455 | clean(); |
456 | return 0; | |
52d3f198 | 457 | } |
458 | } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t')); | |
459 | ||
460 | if (field_start == field_end) { | |
461 | if (field_ptr < header_end) { | |
6e09ed7b | 462 | debugs(55, warnOnError, "WARNING: unparseable HTTP header field near {" << |
81858ebc | 463 | getStringPrefix(field_start, hdrLen-(field_start-header_start)) << "}"); |
6d7bc97b | 464 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
465 | clean(); |
466 | return 0; | |
52d3f198 | 467 | } |
468 | ||
f53969cc | 469 | break; /* terminating blank line */ |
52d3f198 | 470 | } |
62e76326 | 471 | |
a1b9ec20 | 472 | HttpHeaderEntry *e; |
cdce6c61 | 473 | if ((e = HttpHeaderEntry::parse(field_start, field_end)) == NULL) { |
6e09ed7b | 474 | debugs(55, warnOnError, "WARNING: unparseable HTTP header field {" << |
81858ebc AJ |
475 | getStringPrefix(field_start, field_end-field_start) << "}"); |
476 | debugs(55, warnOnError, " in {" << getStringPrefix(header_start, hdrLen) << "}"); | |
52d3f198 | 477 | |
478 | if (Config.onoff.relaxed_header_parser) | |
479 | continue; | |
6e09ed7b | 480 | |
6d7bc97b | 481 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
482 | clean(); |
483 | return 0; | |
47ac2ebe | 484 | } |
62e76326 | 485 | |
a1b9ec20 AR |
486 | if (e->id == Http::HdrType::CONTENT_LENGTH && !clen.checkField(e->value)) { |
487 | delete e; | |
b3123159 | 488 | |
a1b9ec20 AR |
489 | if (Config.onoff.relaxed_header_parser) |
490 | continue; // clen has printed any necessary warnings | |
6e09ed7b | 491 | |
a1b9ec20 AR |
492 | PROF_stop(HttpHeaderParse); |
493 | clean(); | |
494 | return 0; | |
52d3f198 | 495 | } |
62e76326 | 496 | |
d5f18517 AJ |
497 | /* AYJ 2017-05-23: I suspect we need to change this whitespace check to conform to the |
498 | updated WSP character set in RFC 7230/7231. For now I left it as the | |
499 | characters in w_space which the previous code was using. */ | |
500 | static CharacterSet wsp = (CharacterSet::WSP + CharacterSet::CR + CharacterSet::LF); | |
501 | if (e->id == Http::HdrType::OTHER && e->name.findFirstOf(wsp) != SBuf::npos) { | |
6e09ed7b | 502 | debugs(55, warnOnError, "WARNING: found whitespace in HTTP header name {" << |
81858ebc | 503 | getStringPrefix(field_start, field_end-field_start) << "}"); |
47ac2ebe | 504 | |
52d3f198 | 505 | if (!Config.onoff.relaxed_header_parser) { |
eede25e7 | 506 | delete e; |
6d7bc97b | 507 | PROF_stop(HttpHeaderParse); |
81ab22b6 FC |
508 | clean(); |
509 | return 0; | |
52d3f198 | 510 | } |
511 | } | |
62e76326 | 512 | |
a9925b40 | 513 | addEntry(e); |
cb69b4c7 | 514 | } |
62e76326 | 515 | |
a1b9ec20 AR |
516 | if (clen.headerWideProblem) { |
517 | debugs(55, warnOnError, "WARNING: " << clen.headerWideProblem << | |
518 | " Content-Length field values in" << | |
519 | Raw("header", header_start, hdrLen)); | |
520 | } | |
521 | ||
4f1c93a7 EB |
522 | if (clen.prohibitedAndIgnored()) { |
523 | // RFC 7230 section 3.3.2: A server MUST NOT send a Content-Length | |
524 | // header field in any response with a status code of 1xx (Informational) | |
525 | // or 204 (No Content). And RFC 7230 3.3.3#1 tells recipients to ignore | |
526 | // such Content-Lengths. | |
527 | if (delById(Http::HdrType::CONTENT_LENGTH)) | |
528 | debugs(55, 3, "Content-Length is " << clen.prohibitedAndIgnored()); | |
529 | } else if (chunked()) { | |
c3d0ba0c | 530 | // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding |
a1b9ec20 | 531 | // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length |
789217a2 | 532 | delById(Http::HdrType::CONTENT_LENGTH); |
a1b9ec20 AR |
533 | // and clen state becomes irrelevant |
534 | } else if (clen.sawBad) { | |
535 | // ensure our callers do not accidentally see bad Content-Length values | |
3e42b356 | 536 | delById(Http::HdrType::CONTENT_LENGTH); |
a1b9ec20 AR |
537 | conflictingContentLength_ = true; // TODO: Rename to badContentLength_. |
538 | } else if (clen.needsSanitizing) { | |
539 | // RFC 7230 section 3.3.2: MUST either reject or ... [sanitize]; | |
540 | // ensure our callers see a clean Content-Length value or none at all | |
541 | delById(Http::HdrType::CONTENT_LENGTH); | |
542 | if (clen.sawGood) { | |
543 | putInt64(Http::HdrType::CONTENT_LENGTH, clen.value); | |
544 | debugs(55, 5, "sanitized Content-Length to be " << clen.value); | |
545 | } | |
c3d0ba0c AR |
546 | } |
547 | ||
3a9cf2d5 | 548 | PROF_stop(HttpHeaderParse); |
f53969cc | 549 | return 1; /* even if no fields where found, it is a valid header */ |
cb69b4c7 | 550 | } |
551 | ||
99edd1c3 | 552 | /* packs all the entries using supplied packer */ |
cb69b4c7 | 553 | void |
17802cf1 | 554 | HttpHeader::packInto(Packable * p, bool mask_sensitive_info) const |
cb69b4c7 | 555 | { |
9bea1d5b | 556 | HttpHeaderPos pos = HttpHeaderInitPos; |
557 | const HttpHeaderEntry *e; | |
a9925b40 | 558 | assert(p); |
3cc0f4e7 AR |
559 | debugs(55, 7, this << " into " << p << |
560 | (mask_sensitive_info ? " while masking" : "")); | |
9bea1d5b | 561 | /* pack all entries one by one */ |
d8f6c79c FC |
562 | while ((e = getEntry(&pos))) { |
563 | if (!mask_sensitive_info) { | |
564 | e->packInto(p); | |
565 | continue; | |
566 | } | |
3cc0f4e7 AR |
567 | |
568 | bool maskThisEntry = false; | |
d8f6c79c | 569 | switch (e->id) { |
789217a2 FC |
570 | case Http::HdrType::AUTHORIZATION: |
571 | case Http::HdrType::PROXY_AUTHORIZATION: | |
3cc0f4e7 | 572 | maskThisEntry = true; |
d3823ea7 | 573 | break; |
3cc0f4e7 | 574 | |
789217a2 FC |
575 | case Http::HdrType::FTP_ARGUMENTS: |
576 | if (const HttpHeaderEntry *cmd = findEntry(Http::HdrType::FTP_COMMAND)) | |
3cc0f4e7 AR |
577 | maskThisEntry = (cmd->value == "PASS"); |
578 | break; | |
579 | ||
d3823ea7 | 580 | default: |
d3823ea7 | 581 | break; |
d8f6c79c | 582 | } |
3cc0f4e7 | 583 | if (maskThisEntry) { |
d5f18517 | 584 | p->append(e->name.rawContent(), e->name.length()); |
785b508d | 585 | p->append(": ** NOT DISPLAYED **\r\n", 23); |
3cc0f4e7 AR |
586 | } else { |
587 | e->packInto(p); | |
588 | } | |
589 | ||
d8f6c79c | 590 | } |
35b88ed2 | 591 | /* Pack in the "special" entries */ |
592 | ||
593 | /* Cache-Control */ | |
cb69b4c7 | 594 | } |
595 | ||
596 | /* returns next valid entry */ | |
99edd1c3 | 597 | HttpHeaderEntry * |
a9925b40 | 598 | HttpHeader::getEntry(HttpHeaderPos * pos) const |
cb69b4c7 | 599 | { |
a9925b40 | 600 | assert(pos); |
c0abe2ec | 601 | assert(*pos >= HttpHeaderInitPos && *pos < static_cast<ssize_t>(entries.size())); |
62e76326 | 602 | |
c0abe2ec | 603 | for (++(*pos); *pos < static_cast<ssize_t>(entries.size()); ++(*pos)) { |
4c9eadc2 | 604 | if (entries[*pos]) |
c0abe2ec | 605 | return static_cast<HttpHeaderEntry*>(entries[*pos]); |
cb69b4c7 | 606 | } |
62e76326 | 607 | |
9bea1d5b | 608 | return NULL; |
cb69b4c7 | 609 | } |
610 | ||
611 | /* | |
26ac0430 | 612 | * returns a pointer to a specified entry if any |
d8b249ef | 613 | * note that we return one entry so it does not make much sense to ask for |
614 | * "list" headers | |
cb69b4c7 | 615 | */ |
de336bbe | 616 | HttpHeaderEntry * |
789217a2 | 617 | HttpHeader::findEntry(Http::HdrType id) const |
cb69b4c7 | 618 | { |
8cccf1f8 | 619 | assert(any_registered_header(id)); |
81ab22b6 | 620 | assert(!Http::HeaderLookupTable.lookup(id).list); |
9bea1d5b | 621 | |
622 | /* check mask first */ | |
62e76326 | 623 | |
a9925b40 | 624 | if (!CBIT_TEST(mask, id)) |
62e76326 | 625 | return NULL; |
626 | ||
9bea1d5b | 627 | /* looks like we must have it, do linear search */ |
81ab22b6 FC |
628 | for (auto e : entries) { |
629 | if (e && e->id == id) | |
62e76326 | 630 | return e; |
cb69b4c7 | 631 | } |
62e76326 | 632 | |
9bea1d5b | 633 | /* hm.. we thought it was there, but it was not found */ |
81ab22b6 FC |
634 | assert(false); |
635 | return nullptr; /* not reached */ | |
cb69b4c7 | 636 | } |
637 | ||
a622fff0 | 638 | /* |
639 | * same as httpHeaderFindEntry | |
640 | */ | |
a9925b40 | 641 | HttpHeaderEntry * |
789217a2 | 642 | HttpHeader::findLastEntry(Http::HdrType id) const |
a622fff0 | 643 | { |
8cccf1f8 | 644 | assert(any_registered_header(id)); |
81ab22b6 | 645 | assert(!Http::HeaderLookupTable.lookup(id).list); |
9bea1d5b | 646 | |
647 | /* check mask first */ | |
a9925b40 | 648 | if (!CBIT_TEST(mask, id)) |
62e76326 | 649 | return NULL; |
650 | ||
81ab22b6 FC |
651 | for (auto e = entries.rbegin(); e != entries.rend(); ++e) { |
652 | if (*e && (*e)->id == id) | |
653 | return *e; | |
a622fff0 | 654 | } |
62e76326 | 655 | |
81ab22b6 FC |
656 | /* hm.. we thought it was there, but it was not found */ |
657 | assert(false); | |
658 | return nullptr; /* not reached */ | |
a622fff0 | 659 | } |
660 | ||
2ac76861 | 661 | int |
d5f18517 | 662 | HttpHeader::delByName(const SBuf &name) |
cb69b4c7 | 663 | { |
9bea1d5b | 664 | int count = 0; |
665 | HttpHeaderPos pos = HttpHeaderInitPos; | |
f53969cc | 666 | httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */ |
efc26e8e | 667 | debugs(55, 9, "deleting '" << name << "' fields in hdr " << this); |
62e76326 | 668 | |
d5f18517 | 669 | while (const HttpHeaderEntry *e = getEntry(&pos)) { |
30abd221 | 670 | if (!e->name.caseCmp(name)) |
ba9fb01d | 671 | delAt(pos, count); |
672 | else | |
a9925b40 | 673 | CBIT_SET(mask, e->id); |
cb69b4c7 | 674 | } |
62e76326 | 675 | |
9bea1d5b | 676 | return count; |
cb69b4c7 | 677 | } |
678 | ||
2246b732 | 679 | /* deletes all entries with a given id, returns the #entries deleted */ |
680 | int | |
789217a2 | 681 | HttpHeader::delById(Http::HdrType id) |
d8b249ef | 682 | { |
efc26e8e | 683 | debugs(55, 8, this << " del-by-id " << id); |
8cccf1f8 | 684 | assert(any_registered_header(id)); |
62e76326 | 685 | |
a9925b40 | 686 | if (!CBIT_TEST(mask, id)) |
62e76326 | 687 | return 0; |
688 | ||
5134d3a4 AR |
689 | int count = 0; |
690 | ||
691 | HttpHeaderPos pos = HttpHeaderInitPos; | |
692 | while (HttpHeaderEntry *e = getEntry(&pos)) { | |
693 | if (e->id == id) | |
694 | delAt(pos, count); // deletes e | |
695 | } | |
62e76326 | 696 | |
a9925b40 | 697 | CBIT_CLR(mask, id); |
9bea1d5b | 698 | assert(count); |
699 | return count; | |
d8b249ef | 700 | } |
d8b249ef | 701 | |
cb69b4c7 | 702 | /* |
703 | * deletes an entry at pos and leaves a gap; leaving a gap makes it | |
704 | * possible to iterate(search) and delete fields at the same time | |
ba9fb01d | 705 | * NOTE: Does not update the header mask. Caller must follow up with |
706 | * a call to refreshMask() if headers_deleted was incremented. | |
cb69b4c7 | 707 | */ |
2246b732 | 708 | void |
ba9fb01d | 709 | HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted) |
cb69b4c7 | 710 | { |
9bea1d5b | 711 | HttpHeaderEntry *e; |
c0abe2ec FC |
712 | assert(pos >= HttpHeaderInitPos && pos < static_cast<ssize_t>(entries.size())); |
713 | e = static_cast<HttpHeaderEntry*>(entries[pos]); | |
4c9eadc2 | 714 | entries[pos] = NULL; |
9bea1d5b | 715 | /* decrement header length, allow for ": " and crlf */ |
d5f18517 | 716 | len -= e->name.length() + 2 + e->value.size() + 2; |
a9925b40 | 717 | assert(len >= 0); |
eede25e7 | 718 | delete e; |
ba9fb01d | 719 | ++headers_deleted; |
cb69b4c7 | 720 | } |
721 | ||
394499bd | 722 | /* |
723 | * Compacts the header storage | |
724 | */ | |
725 | void | |
726 | HttpHeader::compact() | |
727 | { | |
6e1410f9 | 728 | // TODO: optimize removal, or possibly make it so that's not needed. |
81ab22b6 FC |
729 | entries.erase( std::remove(entries.begin(), entries.end(), nullptr), |
730 | entries.end()); | |
394499bd | 731 | } |
732 | ||
ba9fb01d | 733 | /* |
734 | * Refreshes the header mask. Required after delAt() calls. | |
735 | */ | |
736 | void | |
737 | HttpHeader::refreshMask() | |
738 | { | |
739 | httpHeaderMaskInit(&mask, 0); | |
740 | debugs(55, 7, "refreshing the mask in hdr " << this); | |
81ab22b6 FC |
741 | for (auto e : entries) { |
742 | if (e) | |
743 | CBIT_SET(mask, e->id); | |
ba9fb01d | 744 | } |
745 | } | |
99edd1c3 | 746 | |
62e76326 | 747 | /* appends an entry; |
eede25e7 | 748 | * does not call e->clone() so one should not reuse "*e" |
cb69b4c7 | 749 | */ |
99edd1c3 | 750 | void |
a9925b40 | 751 | HttpHeader::addEntry(HttpHeaderEntry * e) |
cb69b4c7 | 752 | { |
a9925b40 | 753 | assert(e); |
1da82544 | 754 | assert(any_HdrType_enum_value(e->id)); |
d5f18517 | 755 | assert(e->name.length()); |
9bea1d5b | 756 | |
4c9eadc2 | 757 | debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size()); |
62e76326 | 758 | |
789217a2 | 759 | if (e->id != Http::HdrType::BAD_HDR) { |
73b2aa87 FC |
760 | if (CBIT_TEST(mask, e->id)) { |
761 | ++ headerStatsTable[e->id].repCount; | |
762 | } else { | |
763 | CBIT_SET(mask, e->id); | |
764 | } | |
b88a45ed | 765 | } |
62e76326 | 766 | |
a9925b40 | 767 | entries.push_back(e); |
62e76326 | 768 | |
9bea1d5b | 769 | /* increment header length, allow for ": " and crlf */ |
d5f18517 | 770 | len += e->name.length() + 2 + e->value.size() + 2; |
cb69b4c7 | 771 | } |
772 | ||
bbe58ab5 | 773 | /* inserts an entry; |
eede25e7 | 774 | * does not call e->clone() so one should not reuse "*e" |
bbe58ab5 | 775 | */ |
776 | void | |
a9925b40 | 777 | HttpHeader::insertEntry(HttpHeaderEntry * e) |
bbe58ab5 | 778 | { |
a9925b40 | 779 | assert(e); |
229eb96e | 780 | assert(any_valid_header(e->id)); |
bbe58ab5 | 781 | |
f9514ae8 | 782 | debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size()); |
bbe58ab5 | 783 | |
789217a2 | 784 | // Http::HdrType::BAD_HDR is filtered out by assert_any_valid_header |
b88a45ed | 785 | if (CBIT_TEST(mask, e->id)) { |
73b2aa87 | 786 | ++ headerStatsTable[e->id].repCount; |
b88a45ed | 787 | } else { |
a9925b40 | 788 | CBIT_SET(mask, e->id); |
b88a45ed | 789 | } |
bbe58ab5 | 790 | |
42865d25 | 791 | entries.insert(entries.begin(),e); |
bbe58ab5 | 792 | |
793 | /* increment header length, allow for ": " and crlf */ | |
d5f18517 | 794 | len += e->name.length() + 2 + e->value.size() + 2; |
bbe58ab5 | 795 | } |
796 | ||
35b88ed2 | 797 | bool |
789217a2 | 798 | HttpHeader::getList(Http::HdrType id, String *s) const |
35b88ed2 | 799 | { |
efc26e8e | 800 | debugs(55, 9, this << " joining for id " << id); |
35b88ed2 | 801 | /* only fields from ListHeaders array can be "listed" */ |
81ab22b6 | 802 | assert(Http::HeaderLookupTable.lookup(id).list); |
35b88ed2 | 803 | |
804 | if (!CBIT_TEST(mask, id)) | |
805 | return false; | |
806 | ||
81ab22b6 FC |
807 | for (auto e: entries) { |
808 | if (e && e->id == id) | |
9d7a89a5 | 809 | strListAdd(s, e->value.termedBuf(), ','); |
35b88ed2 | 810 | } |
811 | ||
812 | /* | |
813 | * note: we might get an empty (size==0) string if there was an "empty" | |
814 | * header. This results in an empty length String, which may have a NULL | |
815 | * buffer. | |
816 | */ | |
817 | /* temporary warning: remove it? (Is it useful for diagnostics ?) */ | |
818 | if (!s->size()) | |
81ab22b6 | 819 | debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")"); |
35b88ed2 | 820 | else |
821 | debugs(55, 6, this << ": joined for id " << id << ": " << s); | |
822 | ||
823 | return true; | |
824 | } | |
825 | ||
99edd1c3 | 826 | /* return a list of entries with the same id separated by ',' and ws */ |
30abd221 | 827 | String |
789217a2 | 828 | HttpHeader::getList(Http::HdrType id) const |
cb69b4c7 | 829 | { |
9bea1d5b | 830 | HttpHeaderEntry *e; |
831 | HttpHeaderPos pos = HttpHeaderInitPos; | |
efc26e8e | 832 | debugs(55, 9, this << "joining for id " << id); |
9bea1d5b | 833 | /* only fields from ListHeaders array can be "listed" */ |
81ab22b6 | 834 | assert(Http::HeaderLookupTable.lookup(id).list); |
62e76326 | 835 | |
a9925b40 | 836 | if (!CBIT_TEST(mask, id)) |
30abd221 | 837 | return String(); |
650c4b88 | 838 | |
30abd221 | 839 | String s; |
62e76326 | 840 | |
a9925b40 | 841 | while ((e = getEntry(&pos))) { |
62e76326 | 842 | if (e->id == id) |
9d7a89a5 | 843 | strListAdd(&s, e->value.termedBuf(), ','); |
d35b9a94 | 844 | } |
62e76326 | 845 | |
9bea1d5b | 846 | /* |
948078e3 | 847 | * note: we might get an empty (size==0) string if there was an "empty" |
848 | * header. This results in an empty length String, which may have a NULL | |
849 | * buffer. | |
9bea1d5b | 850 | */ |
948078e3 | 851 | /* temporary warning: remove it? (Is it useful for diagnostics ?) */ |
528b2c61 | 852 | if (!s.size()) |
81ab22b6 | 853 | debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")"); |
948078e3 | 854 | else |
855 | debugs(55, 6, this << ": joined for id " << id << ": " << s); | |
62e76326 | 856 | |
9bea1d5b | 857 | return s; |
cb69b4c7 | 858 | } |
859 | ||
f66a9ef4 | 860 | /* return a string or list of entries with the same id separated by ',' and ws */ |
30abd221 | 861 | String |
789217a2 | 862 | HttpHeader::getStrOrList(Http::HdrType id) const |
f66a9ef4 | 863 | { |
c7327fa0 | 864 | HttpHeaderEntry *e; |
9bea1d5b | 865 | |
81ab22b6 | 866 | if (Http::HeaderLookupTable.lookup(id).list) |
a9925b40 | 867 | return getList(id); |
62e76326 | 868 | |
a9925b40 | 869 | if ((e = findEntry(id))) |
62e76326 | 870 | return e->value; |
871 | ||
30abd221 | 872 | return String(); |
f66a9ef4 | 873 | } |
874 | ||
875 | /* | |
b2c44718 | 876 | * Returns the value of the specified header and/or an undefined String. |
f66a9ef4 | 877 | */ |
30abd221 | 878 | String |
a9925b40 | 879 | HttpHeader::getByName(const char *name) const |
81ab22b6 FC |
880 | { |
881 | String result; | |
882 | // ignore presence: return undefined string if an empty header is present | |
f29d429e | 883 | (void)hasNamed(name, strlen(name), &result); |
81ab22b6 FC |
884 | return result; |
885 | } | |
886 | ||
887 | String | |
888 | HttpHeader::getByName(const SBuf &name) const | |
b2c44718 AR |
889 | { |
890 | String result; | |
891 | // ignore presence: return undefined string if an empty header is present | |
f29d429e | 892 | (void)hasNamed(name, &result); |
b2c44718 AR |
893 | return result; |
894 | } | |
895 | ||
81ab22b6 FC |
896 | String |
897 | HttpHeader::getById(Http::HdrType id) const | |
898 | { | |
899 | String result; | |
f29d429e | 900 | (void)getByIdIfPresent(id, &result); |
81ab22b6 FC |
901 | return result; |
902 | } | |
903 | ||
904 | bool | |
f29d429e | 905 | HttpHeader::hasNamed(const SBuf &s, String *result) const |
81ab22b6 | 906 | { |
f29d429e | 907 | return hasNamed(s.rawContent(), s.length(), result); |
81ab22b6 FC |
908 | } |
909 | ||
910 | bool | |
f29d429e | 911 | HttpHeader::getByIdIfPresent(Http::HdrType id, String *result) const |
81ab22b6 FC |
912 | { |
913 | if (id == Http::HdrType::BAD_HDR) | |
914 | return false; | |
915 | if (!has(id)) | |
916 | return false; | |
f29d429e EB |
917 | if (result) |
918 | *result = getStrOrList(id); | |
81ab22b6 FC |
919 | return true; |
920 | } | |
921 | ||
b2c44718 | 922 | bool |
d5f18517 | 923 | HttpHeader::hasNamed(const char *name, unsigned int namelen, String *result) const |
f66a9ef4 | 924 | { |
789217a2 | 925 | Http::HdrType id; |
9bea1d5b | 926 | HttpHeaderPos pos = HttpHeaderInitPos; |
927 | HttpHeaderEntry *e; | |
9bea1d5b | 928 | |
9bea1d5b | 929 | assert(name); |
930 | ||
931 | /* First try the quick path */ | |
81ab22b6 | 932 | id = Http::HeaderLookupTable.lookup(name,namelen).id; |
62e76326 | 933 | |
789217a2 | 934 | if (id != Http::HdrType::BAD_HDR) { |
81ab22b6 FC |
935 | if (getByIdIfPresent(id, result)) |
936 | return true; | |
b2c44718 | 937 | } |
650c4b88 | 938 | |
9bea1d5b | 939 | /* Sorry, an unknown header name. Do linear search */ |
b2c44718 | 940 | bool found = false; |
a9925b40 | 941 | while ((e = getEntry(&pos))) { |
d5f18517 | 942 | if (e->id == Http::HdrType::OTHER && e->name.length() == namelen && e->name.caseCmp(name, namelen) == 0) { |
b2c44718 | 943 | found = true; |
f29d429e EB |
944 | if (!result) |
945 | break; | |
946 | strListAdd(result, e->value.termedBuf(), ','); | |
62e76326 | 947 | } |
f66a9ef4 | 948 | } |
62e76326 | 949 | |
b2c44718 | 950 | return found; |
f66a9ef4 | 951 | } |
cb69b4c7 | 952 | |
14b463aa | 953 | /* |
372fdfbf | 954 | * Returns a the value of the specified list member, if any. |
14b463aa | 955 | */ |
36c774f7 | 956 | SBuf |
a9925b40 | 957 | HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const |
14b463aa | 958 | { |
14b463aa | 959 | assert(name); |
36c774f7 EB |
960 | const auto header = getByName(name); |
961 | return ::getListMember(header, member, separator); | |
14b463aa | 962 | } |
963 | ||
964 | /* | |
965 | * returns a the value of the specified list member, if any. | |
966 | */ | |
36c774f7 | 967 | SBuf |
789217a2 | 968 | HttpHeader::getListMember(Http::HdrType id, const char *member, const char separator) const |
14b463aa | 969 | { |
8cccf1f8 | 970 | assert(any_registered_header(id)); |
36c774f7 EB |
971 | const auto header = getStrOrList(id); |
972 | return ::getListMember(header, member, separator); | |
14b463aa | 973 | } |
974 | ||
cb69b4c7 | 975 | /* test if a field is present */ |
2ac76861 | 976 | int |
789217a2 | 977 | HttpHeader::has(Http::HdrType id) const |
cb69b4c7 | 978 | { |
8cccf1f8 | 979 | assert(any_registered_header(id)); |
efc26e8e | 980 | debugs(55, 9, this << " lookup for " << id); |
a9925b40 | 981 | return CBIT_TEST(mask, id); |
cb69b4c7 | 982 | } |
983 | ||
90be6ff5 EB |
984 | void |
985 | HttpHeader::addVia(const AnyP::ProtocolVersion &ver, const HttpHeader *from) | |
986 | { | |
987 | // TODO: do not add Via header for messages where Squid itself | |
988 | // generated the message (i.e., Downloader or ESI) there should be no Via header added at all. | |
989 | ||
990 | if (Config.onoff.via) { | |
991 | SBuf buf; | |
992 | // RFC 7230 section 5.7.1.: protocol-name is omitted when | |
993 | // the received protocol is HTTP. | |
994 | if (ver.protocol > AnyP::PROTO_NONE && ver.protocol < AnyP::PROTO_UNKNOWN && | |
995 | ver.protocol != AnyP::PROTO_HTTP && ver.protocol != AnyP::PROTO_HTTPS) | |
996 | buf.appendf("%s/", AnyP::ProtocolType_str[ver.protocol]); | |
997 | buf.appendf("%d.%d %s", ver.major, ver.minor, ThisCache); | |
998 | const HttpHeader *hdr = from ? from : this; | |
999 | SBuf strVia = StringToSBuf(hdr->getList(Http::HdrType::VIA)); | |
1000 | if (!strVia.isEmpty()) | |
1001 | strVia.append(", ", 2); | |
1002 | strVia.append(buf); | |
1003 | // XXX: putStr() still suffers from String size limits | |
1004 | Must(strVia.length() < String::SizeMaxXXX()); | |
1005 | delById(Http::HdrType::VIA); | |
1006 | putStr(Http::HdrType::VIA, strVia.c_str()); | |
1007 | } | |
1008 | } | |
1009 | ||
cb69b4c7 | 1010 | void |
789217a2 | 1011 | HttpHeader::putInt(Http::HdrType id, int number) |
cb69b4c7 | 1012 | { |
8cccf1f8 | 1013 | assert(any_registered_header(id)); |
81ab22b6 | 1014 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */ |
9bea1d5b | 1015 | assert(number >= 0); |
d5f18517 | 1016 | addEntry(new HttpHeaderEntry(id, SBuf(), xitoa(number))); |
cb69b4c7 | 1017 | } |
1018 | ||
47f6e231 | 1019 | void |
789217a2 | 1020 | HttpHeader::putInt64(Http::HdrType id, int64_t number) |
47f6e231 | 1021 | { |
8cccf1f8 | 1022 | assert(any_registered_header(id)); |
81ab22b6 | 1023 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */ |
47f6e231 | 1024 | assert(number >= 0); |
d5f18517 | 1025 | addEntry(new HttpHeaderEntry(id, SBuf(), xint64toa(number))); |
47f6e231 | 1026 | } |
1027 | ||
cb69b4c7 | 1028 | void |
789217a2 | 1029 | HttpHeader::putTime(Http::HdrType id, time_t htime) |
cb69b4c7 | 1030 | { |
8cccf1f8 | 1031 | assert(any_registered_header(id)); |
81ab22b6 | 1032 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */ |
a1d6870f | 1033 | assert(htime >= 0); |
d5f18517 | 1034 | addEntry(new HttpHeaderEntry(id, SBuf(), mkrfc1123(htime))); |
cb69b4c7 | 1035 | } |
2ac76861 | 1036 | |
cb69b4c7 | 1037 | void |
789217a2 | 1038 | HttpHeader::putStr(Http::HdrType id, const char *str) |
cb69b4c7 | 1039 | { |
8cccf1f8 | 1040 | assert(any_registered_header(id)); |
81ab22b6 | 1041 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */ |
9bea1d5b | 1042 | assert(str); |
d5f18517 | 1043 | addEntry(new HttpHeaderEntry(id, SBuf(), str)); |
cb69b4c7 | 1044 | } |
1045 | ||
63259c34 | 1046 | void |
a9925b40 | 1047 | HttpHeader::putAuth(const char *auth_scheme, const char *realm) |
63259c34 | 1048 | { |
a9925b40 | 1049 | assert(auth_scheme && realm); |
789217a2 | 1050 | httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm); |
63259c34 | 1051 | } |
1052 | ||
99edd1c3 | 1053 | void |
a9925b40 | 1054 | HttpHeader::putCc(const HttpHdrCc * cc) |
99edd1c3 | 1055 | { |
a9925b40 | 1056 | assert(cc); |
9bea1d5b | 1057 | /* remove old directives if any */ |
789217a2 | 1058 | delById(Http::HdrType::CACHE_CONTROL); |
9bea1d5b | 1059 | /* pack into mb */ |
10201568 | 1060 | MemBuf mb; |
2fe7eff9 | 1061 | mb.init(); |
10201568 | 1062 | cc->packInto(&mb); |
9bea1d5b | 1063 | /* put */ |
d5f18517 | 1064 | addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL, SBuf(), mb.buf)); |
9bea1d5b | 1065 | /* cleanup */ |
2fe7eff9 | 1066 | mb.clean(); |
99edd1c3 | 1067 | } |
1068 | ||
d192d11f | 1069 | void |
a9925b40 | 1070 | HttpHeader::putContRange(const HttpHdrContRange * cr) |
d192d11f | 1071 | { |
a9925b40 | 1072 | assert(cr); |
9bea1d5b | 1073 | /* remove old directives if any */ |
789217a2 | 1074 | delById(Http::HdrType::CONTENT_RANGE); |
9bea1d5b | 1075 | /* pack into mb */ |
10201568 | 1076 | MemBuf mb; |
2fe7eff9 | 1077 | mb.init(); |
10201568 | 1078 | httpHdrContRangePackInto(cr, &mb); |
9bea1d5b | 1079 | /* put */ |
d5f18517 | 1080 | addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE, SBuf(), mb.buf)); |
9bea1d5b | 1081 | /* cleanup */ |
2fe7eff9 | 1082 | mb.clean(); |
d192d11f | 1083 | } |
1084 | ||
1085 | void | |
a9925b40 | 1086 | HttpHeader::putRange(const HttpHdrRange * range) |
d192d11f | 1087 | { |
a9925b40 | 1088 | assert(range); |
9bea1d5b | 1089 | /* remove old directives if any */ |
789217a2 | 1090 | delById(Http::HdrType::RANGE); |
9bea1d5b | 1091 | /* pack into mb */ |
10201568 | 1092 | MemBuf mb; |
2fe7eff9 | 1093 | mb.init(); |
10201568 | 1094 | range->packInto(&mb); |
9bea1d5b | 1095 | /* put */ |
d5f18517 | 1096 | addEntry(new HttpHeaderEntry(Http::HdrType::RANGE, SBuf(), mb.buf)); |
9bea1d5b | 1097 | /* cleanup */ |
2fe7eff9 | 1098 | mb.clean(); |
d192d11f | 1099 | } |
1100 | ||
43ae1d95 | 1101 | void |
a9925b40 | 1102 | HttpHeader::putSc(HttpHdrSc *sc) |
43ae1d95 | 1103 | { |
a9925b40 | 1104 | assert(sc); |
43ae1d95 | 1105 | /* remove old directives if any */ |
789217a2 | 1106 | delById(Http::HdrType::SURROGATE_CONTROL); |
43ae1d95 | 1107 | /* pack into mb */ |
10201568 | 1108 | MemBuf mb; |
2fe7eff9 | 1109 | mb.init(); |
10201568 | 1110 | sc->packInto(&mb); |
43ae1d95 | 1111 | /* put */ |
d5f18517 | 1112 | addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL, SBuf(), mb.buf)); |
43ae1d95 | 1113 | /* cleanup */ |
2fe7eff9 | 1114 | mb.clean(); |
43ae1d95 | 1115 | } |
1116 | ||
bcfba8bd AR |
1117 | void |
1118 | HttpHeader::putWarning(const int code, const char *const text) | |
1119 | { | |
1120 | char buf[512]; | |
1121 | snprintf(buf, sizeof(buf), "%i %s \"%s\"", code, visible_appname_string, text); | |
789217a2 | 1122 | putStr(Http::HdrType::WARNING, buf); |
bcfba8bd AR |
1123 | } |
1124 | ||
cb69b4c7 | 1125 | /* add extension header (these fields are not parsed/analyzed/joined, etc.) */ |
1126 | void | |
a9925b40 | 1127 | HttpHeader::putExt(const char *name, const char *value) |
cb69b4c7 | 1128 | { |
9bea1d5b | 1129 | assert(name && value); |
efc26e8e | 1130 | debugs(55, 8, this << " adds ext entry " << name << " : " << value); |
d5f18517 | 1131 | addEntry(new HttpHeaderEntry(Http::HdrType::OTHER, SBuf(name), value)); |
528b2c61 | 1132 | } |
1133 | ||
1134 | int | |
789217a2 | 1135 | HttpHeader::getInt(Http::HdrType id) const |
528b2c61 | 1136 | { |
8cccf1f8 | 1137 | assert(any_registered_header(id)); |
81ab22b6 | 1138 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */ |
528b2c61 | 1139 | HttpHeaderEntry *e; |
62e76326 | 1140 | |
a9925b40 | 1141 | if ((e = findEntry(id))) |
eede25e7 | 1142 | return e->getInt(); |
62e76326 | 1143 | |
528b2c61 | 1144 | return -1; |
7c525cc2 | 1145 | } |
1146 | ||
47f6e231 | 1147 | int64_t |
789217a2 | 1148 | HttpHeader::getInt64(Http::HdrType id) const |
47f6e231 | 1149 | { |
8cccf1f8 | 1150 | assert(any_registered_header(id)); |
81ab22b6 | 1151 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */ |
47f6e231 | 1152 | HttpHeaderEntry *e; |
1153 | ||
1154 | if ((e = findEntry(id))) | |
1155 | return e->getInt64(); | |
1156 | ||
1157 | return -1; | |
1158 | } | |
1159 | ||
de336bbe | 1160 | time_t |
789217a2 | 1161 | HttpHeader::getTime(Http::HdrType id) const |
cb69b4c7 | 1162 | { |
9bea1d5b | 1163 | HttpHeaderEntry *e; |
1164 | time_t value = -1; | |
8cccf1f8 | 1165 | assert(any_registered_header(id)); |
81ab22b6 | 1166 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */ |
62e76326 | 1167 | |
a9925b40 | 1168 | if ((e = findEntry(id))) { |
9d7a89a5 | 1169 | value = parse_rfc1123(e->value.termedBuf()); |
62e76326 | 1170 | httpHeaderNoteParsedEntry(e->id, e->value, value < 0); |
d8b249ef | 1171 | } |
62e76326 | 1172 | |
9bea1d5b | 1173 | return value; |
cb69b4c7 | 1174 | } |
1175 | ||
99edd1c3 | 1176 | /* sync with httpHeaderGetLastStr */ |
de336bbe | 1177 | const char * |
789217a2 | 1178 | HttpHeader::getStr(Http::HdrType id) const |
cb69b4c7 | 1179 | { |
9bea1d5b | 1180 | HttpHeaderEntry *e; |
8cccf1f8 | 1181 | assert(any_registered_header(id)); |
81ab22b6 | 1182 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */ |
62e76326 | 1183 | |
a9925b40 | 1184 | if ((e = findEntry(id))) { |
81ab22b6 | 1185 | httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */ |
9d7a89a5 | 1186 | return e->value.termedBuf(); |
d8b249ef | 1187 | } |
62e76326 | 1188 | |
9bea1d5b | 1189 | return NULL; |
cb69b4c7 | 1190 | } |
1191 | ||
a622fff0 | 1192 | /* unusual */ |
1193 | const char * | |
789217a2 | 1194 | HttpHeader::getLastStr(Http::HdrType id) const |
a622fff0 | 1195 | { |
9bea1d5b | 1196 | HttpHeaderEntry *e; |
8cccf1f8 | 1197 | assert(any_registered_header(id)); |
81ab22b6 | 1198 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */ |
62e76326 | 1199 | |
a9925b40 | 1200 | if ((e = findLastEntry(id))) { |
81ab22b6 | 1201 | httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */ |
9d7a89a5 | 1202 | return e->value.termedBuf(); |
a622fff0 | 1203 | } |
62e76326 | 1204 | |
9bea1d5b | 1205 | return NULL; |
a622fff0 | 1206 | } |
1207 | ||
7faf2bdb | 1208 | HttpHdrCc * |
a9925b40 | 1209 | HttpHeader::getCc() const |
cb69b4c7 | 1210 | { |
789217a2 | 1211 | if (!CBIT_TEST(mask, Http::HdrType::CACHE_CONTROL)) |
62e76326 | 1212 | return NULL; |
35b88ed2 | 1213 | PROF_start(HttpHeader_getCc); |
62e76326 | 1214 | |
c57b8f05 | 1215 | String s; |
789217a2 | 1216 | getList(Http::HdrType::CACHE_CONTROL, &s); |
62e76326 | 1217 | |
c57b8f05 | 1218 | HttpHdrCc *cc=new HttpHdrCc(); |
7ebe76de FC |
1219 | |
1220 | if (!cc->parse(s)) { | |
cf66f10a FC |
1221 | delete cc; |
1222 | cc = NULL; | |
1223 | } | |
62e76326 | 1224 | |
95dc7ff4 | 1225 | ++ HttpHeaderStats[owner].ccParsedCount; |
62e76326 | 1226 | |
9bea1d5b | 1227 | if (cc) |
a9925b40 | 1228 | httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr); |
62e76326 | 1229 | |
789217a2 | 1230 | httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL, s, !cc); |
62e76326 | 1231 | |
35b88ed2 | 1232 | PROF_stop(HttpHeader_getCc); |
62e76326 | 1233 | |
9bea1d5b | 1234 | return cc; |
cb69b4c7 | 1235 | } |
1236 | ||
02922e76 | 1237 | HttpHdrRange * |
a9925b40 | 1238 | HttpHeader::getRange() const |
02922e76 | 1239 | { |
9bea1d5b | 1240 | HttpHdrRange *r = NULL; |
1241 | HttpHeaderEntry *e; | |
1242 | /* some clients will send "Request-Range" _and_ *matching* "Range" | |
1243 | * who knows, some clients might send Request-Range only; | |
1244 | * this "if" should work correctly in both cases; | |
1245 | * hopefully no clients send mismatched headers! */ | |
62e76326 | 1246 | |
789217a2 FC |
1247 | if ((e = findEntry(Http::HdrType::RANGE)) || |
1248 | (e = findEntry(Http::HdrType::REQUEST_RANGE))) { | |
62e76326 | 1249 | r = HttpHdrRange::ParseCreate(&e->value); |
1250 | httpHeaderNoteParsedEntry(e->id, e->value, !r); | |
d192d11f | 1251 | } |
62e76326 | 1252 | |
9bea1d5b | 1253 | return r; |
02922e76 | 1254 | } |
1255 | ||
43ae1d95 | 1256 | HttpHdrSc * |
a9925b40 | 1257 | HttpHeader::getSc() const |
43ae1d95 | 1258 | { |
789217a2 | 1259 | if (!CBIT_TEST(mask, Http::HdrType::SURROGATE_CONTROL)) |
43ae1d95 | 1260 | return NULL; |
1261 | ||
30abd221 | 1262 | String s; |
26ac0430 | 1263 | |
789217a2 | 1264 | (void) getList(Http::HdrType::SURROGATE_CONTROL, &s); |
43ae1d95 | 1265 | |
45a58345 | 1266 | HttpHdrSc *sc = httpHdrScParseCreate(s); |
43ae1d95 | 1267 | |
95dc7ff4 | 1268 | ++ HttpHeaderStats[owner].ccParsedCount; |
43ae1d95 | 1269 | |
1270 | if (sc) | |
45a58345 | 1271 | sc->updateStats(&HttpHeaderStats[owner].scTypeDistr); |
43ae1d95 | 1272 | |
789217a2 | 1273 | httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL, s, !sc); |
43ae1d95 | 1274 | |
1275 | return sc; | |
1276 | } | |
1277 | ||
d76fcfa7 | 1278 | HttpHdrContRange * |
a9925b40 | 1279 | HttpHeader::getContRange() const |
d76fcfa7 | 1280 | { |
9bea1d5b | 1281 | HttpHdrContRange *cr = NULL; |
1282 | HttpHeaderEntry *e; | |
62e76326 | 1283 | |
789217a2 | 1284 | if ((e = findEntry(Http::HdrType::CONTENT_RANGE))) { |
9d7a89a5 | 1285 | cr = httpHdrContRangeParseCreate(e->value.termedBuf()); |
62e76326 | 1286 | httpHeaderNoteParsedEntry(e->id, e->value, !cr); |
d8b249ef | 1287 | } |
62e76326 | 1288 | |
9bea1d5b | 1289 | return cr; |
cb69b4c7 | 1290 | } |
1291 | ||
99edd1c3 | 1292 | const char * |
789217a2 | 1293 | HttpHeader::getAuth(Http::HdrType id, const char *auth_scheme) const |
cb69b4c7 | 1294 | { |
9bea1d5b | 1295 | const char *field; |
1296 | int l; | |
a9925b40 | 1297 | assert(auth_scheme); |
1298 | field = getStr(id); | |
62e76326 | 1299 | |
f53969cc | 1300 | if (!field) /* no authorization field */ |
62e76326 | 1301 | return NULL; |
1302 | ||
9bea1d5b | 1303 | l = strlen(auth_scheme); |
62e76326 | 1304 | |
f53969cc | 1305 | if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */ |
62e76326 | 1306 | return NULL; |
1307 | ||
9bea1d5b | 1308 | field += l; |
62e76326 | 1309 | |
f53969cc | 1310 | if (!xisspace(*field)) /* wrong scheme */ |
62e76326 | 1311 | return NULL; |
1312 | ||
9bea1d5b | 1313 | /* skip white space */ |
95dc7ff4 | 1314 | for (; field && xisspace(*field); ++field); |
62e76326 | 1315 | |
f53969cc | 1316 | if (!*field) /* no authorization cookie */ |
62e76326 | 1317 | return NULL; |
1318 | ||
8bdd0cec | 1319 | static char decodedAuthToken[8192]; |
aadbbd7d AJ |
1320 | struct base64_decode_ctx ctx; |
1321 | base64_decode_init(&ctx); | |
1322 | size_t decodedLen = 0; | |
1d11e9b3 | 1323 | if (!base64_decode_update(&ctx, &decodedLen, reinterpret_cast<uint8_t*>(decodedAuthToken), strlen(field), field) || |
aadbbd7d AJ |
1324 | !base64_decode_final(&ctx)) { |
1325 | return NULL; | |
1326 | } | |
8bdd0cec AJ |
1327 | decodedAuthToken[decodedLen] = '\0'; |
1328 | return decodedAuthToken; | |
cb69b4c7 | 1329 | } |
1330 | ||
a9771e51 | 1331 | ETag |
789217a2 | 1332 | HttpHeader::getETag(Http::HdrType id) const |
a9771e51 | 1333 | { |
26ac0430 | 1334 | ETag etag = {NULL, -1}; |
9bea1d5b | 1335 | HttpHeaderEntry *e; |
81ab22b6 | 1336 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftETag); /* must be of an appropriate type */ |
62e76326 | 1337 | |
a9925b40 | 1338 | if ((e = findEntry(id))) |
9d7a89a5 | 1339 | etagParseInit(&etag, e->value.termedBuf()); |
62e76326 | 1340 | |
9bea1d5b | 1341 | return etag; |
a9771e51 | 1342 | } |
1343 | ||
1344 | TimeOrTag | |
789217a2 | 1345 | HttpHeader::getTimeOrTag(Http::HdrType id) const |
a9771e51 | 1346 | { |
9bea1d5b | 1347 | TimeOrTag tot; |
1348 | HttpHeaderEntry *e; | |
81ab22b6 | 1349 | assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123_or_ETag); /* must be of an appropriate type */ |
9bea1d5b | 1350 | memset(&tot, 0, sizeof(tot)); |
62e76326 | 1351 | |
a9925b40 | 1352 | if ((e = findEntry(id))) { |
9d7a89a5 | 1353 | const char *str = e->value.termedBuf(); |
62e76326 | 1354 | /* try as an ETag */ |
1355 | ||
1356 | if (etagParseInit(&tot.tag, str)) { | |
1357 | tot.valid = tot.tag.str != NULL; | |
1358 | tot.time = -1; | |
1359 | } else { | |
1360 | /* or maybe it is time? */ | |
1361 | tot.time = parse_rfc1123(str); | |
1362 | tot.valid = tot.time >= 0; | |
1363 | tot.tag.str = NULL; | |
1364 | } | |
a9771e51 | 1365 | } |
62e76326 | 1366 | |
f53969cc | 1367 | assert(tot.time < 0 || !tot.tag.str); /* paranoid */ |
9bea1d5b | 1368 | return tot; |
a9771e51 | 1369 | } |
1370 | ||
cb69b4c7 | 1371 | /* |
1372 | * HttpHeaderEntry | |
1373 | */ | |
1374 | ||
d5f18517 | 1375 | HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId, const SBuf &aName, const char *aValue) |
cb69b4c7 | 1376 | { |
1da82544 | 1377 | assert(any_HdrType_enum_value(anId)); |
eede25e7 | 1378 | id = anId; |
62e76326 | 1379 | |
789217a2 | 1380 | if (id != Http::HdrType::OTHER) |
81ab22b6 | 1381 | name = Http::HeaderLookupTable.lookup(id).name; |
9bea1d5b | 1382 | else |
eede25e7 | 1383 | name = aName; |
62e76326 | 1384 | |
eede25e7 | 1385 | value = aValue; |
62e76326 | 1386 | |
1da82544 | 1387 | if (id != Http::HdrType::BAD_HDR) |
73b2aa87 | 1388 | ++ headerStatsTable[id].aliveCount; |
62e76326 | 1389 | |
9d7a89a5 | 1390 | debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name << " : " << value ); |
eede25e7 | 1391 | } |
62e76326 | 1392 | |
eede25e7 | 1393 | HttpHeaderEntry::~HttpHeaderEntry() |
2ac76861 | 1394 | { |
9d7a89a5 | 1395 | debugs(55, 9, "destroying entry " << this << ": '" << name << ": " << value << "'"); |
62e76326 | 1396 | |
1da82544 FC |
1397 | if (id != Http::HdrType::BAD_HDR) { |
1398 | assert(headerStatsTable[id].aliveCount); | |
1399 | -- headerStatsTable[id].aliveCount; | |
1400 | id = Http::HdrType::BAD_HDR; // it already is BAD_HDR, no sense in resetting it | |
1401 | } | |
62e76326 | 1402 | |
cb69b4c7 | 1403 | } |
1404 | ||
eede25e7 | 1405 | /* parses and inits header entry, returns true/false */ |
cdce6c61 | 1406 | HttpHeaderEntry * |
eede25e7 | 1407 | HttpHeaderEntry::parse(const char *field_start, const char *field_end) |
cb69b4c7 | 1408 | { |
9bea1d5b | 1409 | /* note: name_start == field_start */ |
52d3f198 | 1410 | const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start); |
26ac0430 | 1411 | int name_len = name_end ? name_end - field_start :0; |
f53969cc | 1412 | const char *value_start = field_start + name_len + 1; /* skip ':' */ |
9bea1d5b | 1413 | /* note: value_end == field_end */ |
1414 | ||
95dc7ff4 | 1415 | ++ HeaderEntryParsedCount; |
9bea1d5b | 1416 | |
1417 | /* do we have a valid field name within this field? */ | |
62e76326 | 1418 | |
9bea1d5b | 1419 | if (!name_len || name_end > field_end) |
cdce6c61 | 1420 | return NULL; |
62e76326 | 1421 | |
67746eea | 1422 | if (name_len > 65534) { |
1423 | /* String must be LESS THAN 64K and it adds a terminating NULL */ | |
e0236918 | 1424 | debugs(55, DBG_IMPORTANT, "WARNING: ignoring header name of " << name_len << " bytes"); |
cdce6c61 | 1425 | return NULL; |
25acfb53 | 1426 | } |
62e76326 | 1427 | |
52d3f198 | 1428 | if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) { |
bf8fe701 | 1429 | debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2, |
81858ebc | 1430 | "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end-field_start) << "'"); |
52d3f198 | 1431 | |
1432 | while (name_len > 0 && xisspace(field_start[name_len - 1])) | |
5e263176 | 1433 | --name_len; |
52d3f198 | 1434 | |
1435 | if (!name_len) | |
cdce6c61 | 1436 | return NULL; |
52d3f198 | 1437 | } |
1438 | ||
9bea1d5b | 1439 | /* now we know we can parse it */ |
62e76326 | 1440 | |
81858ebc | 1441 | debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end-field_start) << "'"); |
62e76326 | 1442 | |
9bea1d5b | 1443 | /* is it a "known" field? */ |
81ab22b6 | 1444 | Http::HdrType id = Http::HeaderLookupTable.lookup(field_start,name_len).id; |
92991271 | 1445 | debugs(55, 9, "got hdr-id=" << id); |
cdce6c61 | 1446 | |
d5f18517 | 1447 | SBuf theName; |
cdce6c61 | 1448 | |
30abd221 | 1449 | String value; |
62e76326 | 1450 | |
789217a2 FC |
1451 | if (id == Http::HdrType::BAD_HDR) |
1452 | id = Http::HdrType::OTHER; | |
62e76326 | 1453 | |
9bea1d5b | 1454 | /* set field name */ |
789217a2 | 1455 | if (id == Http::HdrType::OTHER) |
d5f18517 | 1456 | theName.append(field_start, name_len); |
9bea1d5b | 1457 | else |
d5f18517 | 1458 | theName = Http::HeaderLookupTable.lookup(id).name; |
62e76326 | 1459 | |
9bea1d5b | 1460 | /* trim field value */ |
1461 | while (value_start < field_end && xisspace(*value_start)) | |
95dc7ff4 | 1462 | ++value_start; |
62e76326 | 1463 | |
52d3f198 | 1464 | while (value_start < field_end && xisspace(field_end[-1])) |
5e263176 | 1465 | --field_end; |
52d3f198 | 1466 | |
67746eea | 1467 | if (field_end - value_start > 65534) { |
1468 | /* String must be LESS THAN 64K and it adds a terminating NULL */ | |
d5f18517 | 1469 | debugs(55, DBG_IMPORTANT, "WARNING: ignoring '" << theName << "' header of " << (field_end - value_start) << " bytes"); |
cdce6c61 | 1470 | return NULL; |
25acfb53 | 1471 | } |
62e76326 | 1472 | |
9bea1d5b | 1473 | /* set field value */ |
eede25e7 | 1474 | value.limitInit(value_start, field_end - value_start); |
62e76326 | 1475 | |
789217a2 | 1476 | if (id != Http::HdrType::BAD_HDR) |
73b2aa87 | 1477 | ++ headerStatsTable[id].seenCount; |
62e76326 | 1478 | |
d5f18517 | 1479 | debugs(55, 9, "parsed HttpHeaderEntry: '" << theName << ": " << value << "'"); |
62e76326 | 1480 | |
d5f18517 | 1481 | return new HttpHeaderEntry(id, theName, value.termedBuf()); |
cb69b4c7 | 1482 | } |
1483 | ||
99edd1c3 | 1484 | HttpHeaderEntry * |
eede25e7 | 1485 | HttpHeaderEntry::clone() const |
cb69b4c7 | 1486 | { |
d5f18517 | 1487 | return new HttpHeaderEntry(id, name, value.termedBuf()); |
cb69b4c7 | 1488 | } |
1489 | ||
de336bbe | 1490 | void |
17802cf1 | 1491 | HttpHeaderEntry::packInto(Packable * p) const |
cb69b4c7 | 1492 | { |
eede25e7 | 1493 | assert(p); |
d5f18517 | 1494 | p->append(name.rawContent(), name.length()); |
785b508d AJ |
1495 | p->append(": ", 2); |
1496 | p->append(value.rawBuf(), value.size()); | |
1497 | p->append("\r\n", 2); | |
cb69b4c7 | 1498 | } |
1499 | ||
eede25e7 | 1500 | int |
1501 | HttpHeaderEntry::getInt() const | |
1502 | { | |
eede25e7 | 1503 | int val = -1; |
9d7a89a5 | 1504 | int ok = httpHeaderParseInt(value.termedBuf(), &val); |
81ab22b6 | 1505 | httpHeaderNoteParsedEntry(id, value, ok == 0); |
eede25e7 | 1506 | /* XXX: Should we check ok - ie |
1507 | * return ok ? -1 : value; | |
1508 | */ | |
1509 | return val; | |
1510 | } | |
1511 | ||
47f6e231 | 1512 | int64_t |
1513 | HttpHeaderEntry::getInt64() const | |
1514 | { | |
47f6e231 | 1515 | int64_t val = -1; |
a1b9ec20 | 1516 | const bool ok = httpHeaderParseOffset(value.termedBuf(), &val); |
a7fea967 | 1517 | httpHeaderNoteParsedEntry(id, value, !ok); |
a1b9ec20 | 1518 | return val; // remains -1 if !ok (XXX: bad method API) |
47f6e231 | 1519 | } |
1520 | ||
cb69b4c7 | 1521 | static void |
81ab22b6 | 1522 | httpHeaderNoteParsedEntry(Http::HdrType id, String const &context, bool error) |
cb69b4c7 | 1523 | { |
789217a2 | 1524 | if (id != Http::HdrType::BAD_HDR) |
73b2aa87 | 1525 | ++ headerStatsTable[id].parsCount; |
62e76326 | 1526 | |
9bea1d5b | 1527 | if (error) { |
789217a2 | 1528 | if (id != Http::HdrType::BAD_HDR) |
73b2aa87 | 1529 | ++ headerStatsTable[id].errCount; |
81ab22b6 | 1530 | debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderLookupTable.lookup(id).name << ": " << context << "'"); |
cb69b4c7 | 1531 | } |
cb69b4c7 | 1532 | } |
1533 | ||
cb69b4c7 | 1534 | /* |
12cf1be2 | 1535 | * Reports |
cb69b4c7 | 1536 | */ |
cb69b4c7 | 1537 | |
fcd2d3ef | 1538 | /* tmp variable used to pass stat info to dumpers */ |
f53969cc | 1539 | extern const HttpHeaderStat *dump_stat; /* argh! */ |
fcd2d3ef | 1540 | const HttpHeaderStat *dump_stat = NULL; |
1541 | ||
59a09b98 | 1542 | void |
ced8def3 | 1543 | httpHeaderFieldStatDumper(StoreEntry * sentry, int, double val, double, int count) |
cb69b4c7 | 1544 | { |
92991271 | 1545 | const int id = static_cast<int>(val); |
1da82544 | 1546 | const bool valid_id = Http::any_valid_header(static_cast<Http::HdrType>(id)); |
81ab22b6 | 1547 | const char *name = valid_id ? Http::HeaderLookupTable.lookup(static_cast<Http::HdrType>(id)).name : "INVALID"; |
9bea1d5b | 1548 | int visible = count > 0; |
1549 | /* for entries with zero count, list only those that belong to current type of message */ | |
62e76326 | 1550 | |
9bea1d5b | 1551 | if (!visible && valid_id && dump_stat->owner_mask) |
62e76326 | 1552 | visible = CBIT_TEST(*dump_stat->owner_mask, id); |
1553 | ||
9bea1d5b | 1554 | if (visible) |
62e76326 | 1555 | storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n", |
1556 | id, name, count, xdiv(count, dump_stat->busyDestroyedCount)); | |
cb69b4c7 | 1557 | } |
1558 | ||
12cf1be2 | 1559 | static void |
ced8def3 | 1560 | httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double, int count) |
cb69b4c7 | 1561 | { |
9bea1d5b | 1562 | if (count) |
62e76326 | 1563 | storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n", |
1564 | idx, (int) val, count, | |
1565 | xpercent(count, dump_stat->destroyedCount)); | |
cb69b4c7 | 1566 | } |
1567 | ||
cb69b4c7 | 1568 | static void |
9bea1d5b | 1569 | httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e) |
12cf1be2 | 1570 | { |
73b2aa87 FC |
1571 | assert(hs); |
1572 | assert(e); | |
9bea1d5b | 1573 | |
1574 | dump_stat = hs; | |
1575 | storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label); | |
1576 | storeAppendPrintf(e, "\nField type distribution\n"); | |
1577 | storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n", | |
62e76326 | 1578 | "id", "name", "count", "#/header"); |
96886986 | 1579 | hs->fieldTypeDistr.dump(e, httpHeaderFieldStatDumper); |
9bea1d5b | 1580 | storeAppendPrintf(e, "\nCache-control directives distribution\n"); |
1581 | storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n", | |
62e76326 | 1582 | "id", "name", "count", "#/cc_field"); |
96886986 | 1583 | hs->ccTypeDistr.dump(e, httpHdrCcStatDumper); |
43ae1d95 | 1584 | storeAppendPrintf(e, "\nSurrogate-control directives distribution\n"); |
1585 | storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n", | |
1586 | "id", "name", "count", "#/sc_field"); | |
96886986 | 1587 | hs->scTypeDistr.dump(e, httpHdrScStatDumper); |
9bea1d5b | 1588 | storeAppendPrintf(e, "\nNumber of fields per header distribution\n"); |
1589 | storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n", | |
62e76326 | 1590 | "id", "#flds", "count", "%total"); |
96886986 | 1591 | hs->hdrUCountDistr.dump(e, httpHeaderFldsPerHdrDumper); |
41b43e70 | 1592 | storeAppendPrintf(e, "\n"); |
9bea1d5b | 1593 | dump_stat = NULL; |
cb69b4c7 | 1594 | } |
1595 | ||
12cf1be2 | 1596 | void |
9bea1d5b | 1597 | httpHeaderStoreReport(StoreEntry * e) |
cb69b4c7 | 1598 | { |
9bea1d5b | 1599 | int i; |
9bea1d5b | 1600 | assert(e); |
1601 | ||
1602 | HttpHeaderStats[0].parsedCount = | |
62e76326 | 1603 | HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount; |
9bea1d5b | 1604 | HttpHeaderStats[0].ccParsedCount = |
62e76326 | 1605 | HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount; |
9bea1d5b | 1606 | HttpHeaderStats[0].destroyedCount = |
62e76326 | 1607 | HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount; |
9bea1d5b | 1608 | HttpHeaderStats[0].busyDestroyedCount = |
62e76326 | 1609 | HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount; |
9bea1d5b | 1610 | |
95dc7ff4 | 1611 | for (i = 1; i < HttpHeaderStatCount; ++i) { |
62e76326 | 1612 | httpHeaderStatDump(HttpHeaderStats + i, e); |
12cf1be2 | 1613 | } |
62e76326 | 1614 | |
9bea1d5b | 1615 | /* field stats for all messages */ |
1616 | storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n"); | |
62e76326 | 1617 | |
077fe581 | 1618 | storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n", |
62e76326 | 1619 | "id", "name", "#alive", "%err", "%repeat"); |
1620 | ||
fda6d769 | 1621 | // scan heaaderTable and output |
81ab22b6 FC |
1622 | for (auto h : WholeEnum<Http::HdrType>()) { |
1623 | auto stats = headerStatsTable[h]; | |
fda6d769 | 1624 | storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n", |
81ab22b6 FC |
1625 | Http::HeaderLookupTable.lookup(h).id, |
1626 | Http::HeaderLookupTable.lookup(h).name, | |
92991271 | 1627 | stats.aliveCount, |
f7ad4af5 FC |
1628 | xpercent(stats.errCount, stats.parsCount), |
1629 | xpercent(stats.repCount, stats.seenCount)); | |
fda6d769 | 1630 | } |
62e76326 | 1631 | |
9bea1d5b | 1632 | storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n", |
62e76326 | 1633 | HttpHeaderStats[hoRequest].parsedCount, |
1634 | HttpHeaderStats[hoReply].parsedCount, | |
1635 | HttpHeaderStats[0].parsedCount); | |
9bea1d5b | 1636 | storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount); |
cb69b4c7 | 1637 | } |
97474590 | 1638 | |
924f73bc | 1639 | int |
789217a2 | 1640 | HttpHeader::hasListMember(Http::HdrType id, const char *member, const char separator) const |
924f73bc | 1641 | { |
1642 | int result = 0; | |
1643 | const char *pos = NULL; | |
1644 | const char *item; | |
1645 | int ilen; | |
1646 | int mlen = strlen(member); | |
1647 | ||
8cccf1f8 | 1648 | assert(any_registered_header(id)); |
924f73bc | 1649 | |
30abd221 | 1650 | String header (getStrOrList(id)); |
924f73bc | 1651 | |
9d0cdbb9 | 1652 | while (strListGetItem(&header, separator, &item, &ilen, &pos)) { |
380e7faf | 1653 | if (strncasecmp(item, member, mlen) == 0 |
9d0cdbb9 | 1654 | && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) { |
1655 | result = 1; | |
1656 | break; | |
1657 | } | |
1658 | } | |
1659 | ||
1660 | return result; | |
1661 | } | |
1662 | ||
1663 | int | |
a9925b40 | 1664 | HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const |
9d0cdbb9 | 1665 | { |
1666 | int result = 0; | |
1667 | const char *pos = NULL; | |
1668 | const char *item; | |
1669 | int ilen; | |
1670 | int mlen = strlen(member); | |
1671 | ||
9d0cdbb9 | 1672 | assert(name); |
1673 | ||
30abd221 | 1674 | String header (getByName(name)); |
9d0cdbb9 | 1675 | |
924f73bc | 1676 | while (strListGetItem(&header, separator, &item, &ilen, &pos)) { |
380e7faf | 1677 | if (strncasecmp(item, member, mlen) == 0 |
924f73bc | 1678 | && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) { |
1679 | result = 1; | |
1680 | break; | |
1681 | } | |
1682 | } | |
1683 | ||
1684 | return result; | |
1685 | } | |
1686 | ||
2cdeea82 | 1687 | void |
1688 | HttpHeader::removeHopByHopEntries() | |
1689 | { | |
1690 | removeConnectionHeaderEntries(); | |
26ac0430 | 1691 | |
dcf3665b AJ |
1692 | const HttpHeaderEntry *e; |
1693 | HttpHeaderPos pos = HttpHeaderInitPos; | |
1694 | int headers_deleted = 0; | |
1695 | while ((e = getEntry(&pos))) { | |
789217a2 | 1696 | Http::HdrType id = e->id; |
81ab22b6 | 1697 | if (Http::HeaderLookupTable.lookup(id).hopbyhop) { |
26ac0430 AJ |
1698 | delAt(pos, headers_deleted); |
1699 | CBIT_CLR(mask, id); | |
1700 | } | |
dcf3665b | 1701 | } |
2cdeea82 | 1702 | } |
1703 | ||
924f73bc | 1704 | void |
1705 | HttpHeader::removeConnectionHeaderEntries() | |
1706 | { | |
789217a2 | 1707 | if (has(Http::HdrType::CONNECTION)) { |
924f73bc | 1708 | /* anything that matches Connection list member will be deleted */ |
30abd221 | 1709 | String strConnection; |
26ac0430 | 1710 | |
789217a2 | 1711 | (void) getList(Http::HdrType::CONNECTION, &strConnection); |
924f73bc | 1712 | const HttpHeaderEntry *e; |
1713 | HttpHeaderPos pos = HttpHeaderInitPos; | |
1714 | /* | |
1715 | * think: on-average-best nesting of the two loops (hdrEntry | |
1716 | * and strListItem) @?@ | |
1717 | */ | |
1718 | /* | |
1719 | * maybe we should delete standard stuff ("keep-alive","close") | |
1720 | * from strConnection first? | |
1721 | */ | |
1722 | ||
ba9fb01d | 1723 | int headers_deleted = 0; |
a9925b40 | 1724 | while ((e = getEntry(&pos))) { |
d5f18517 | 1725 | if (strListIsMember(&strConnection, e->name, ',')) |
ba9fb01d | 1726 | delAt(pos, headers_deleted); |
924f73bc | 1727 | } |
ba9fb01d | 1728 | if (headers_deleted) |
1729 | refreshMask(); | |
924f73bc | 1730 | } |
1731 | } | |
f53969cc | 1732 |