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