2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 55 HTTP Header */
12 #include "base/Assure.h"
13 #include "base/CharacterSet.h"
14 #include "base/EnumIterator.h"
18 #include "http/ContentLengthInterpreter.h"
19 #include "HttpHdrCc.h"
20 #include "HttpHdrContRange.h"
21 #include "HttpHdrScTarget.h" // also includes HttpHdrSc.h
22 #include "HttpHeader.h"
23 #include "HttpHeaderFieldStat.h"
24 #include "HttpHeaderStat.h"
25 #include "HttpHeaderTools.h"
27 #include "mgr/Registration.h"
28 #include "mime_header.h"
29 #include "sbuf/StringConvert.h"
30 #include "SquidConfig.h"
34 #include "time/gadgets.h"
35 #include "TimeOrTag.h"
41 /* XXX: the whole set of API managing the entries vector should be rethought
42 * after the parse4r-ng effort is complete.
46 * On naming conventions:
48 * HTTP/1.1 defines message-header as
50 * message-header = field-name ":" [ field-value ] CRLF
52 * field-value = *( field-content | LWS )
54 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
55 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
57 * HttpHeader is an object that represents all message-headers in a message.
58 * HttpHeader does not manage start-line.
60 * HttpHeader is implemented as a collection of header "entries".
61 * An entry is a (field_id, field_name, field_value) triplet.
65 * local constants and vars
68 // statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted
69 std::vector
<HttpHeaderFieldStat
> headerStatsTable(Http::HdrType::enumEnd_
);
71 /* request-only headers. Used for cachemgr */
72 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
74 /* reply-only headers. Used for cachemgr */
75 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
77 /* header accounting */
78 // NP: keep in sync with enum http_hdr_owner_type
79 static std::array
<HttpHeaderStat
, hoEnd
> HttpHeaderStats
= {{
80 HttpHeaderStat(/*hoNone*/ "all", nullptr),
82 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask
),
84 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask
),
85 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask
)
87 , HttpHeaderStat(/*hoErrorDetail*/ "error detail templates", nullptr)
93 static int HeaderEntryParsedCount
= 0;
96 * forward declarations and local routines
101 // update parse statistics for header id; if error is true also account
102 // for errors and write to debug log what happened
103 static void httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &value
, bool error
);
104 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
105 /** store report about current header usage and other stats */
106 static void httpHeaderStoreReport(StoreEntry
* e
);
109 * Module initialization routines
113 httpHeaderRegisterWithCacheManager(void)
115 Mgr::RegisterAction("http_headers",
116 "HTTP Header Statistics",
117 httpHeaderStoreReport
, 0, 1);
121 httpHeaderInitModule(void)
123 /* check that we have enough space for masks */
124 assert(8 * sizeof(HttpHeaderMask
) >= Http::HdrType::enumEnd_
);
126 // masks are needed for stats page still
127 for (auto h
: WholeEnum
<Http::HdrType
>()) {
128 if (Http::HeaderLookupTable
.lookup(h
).request
)
129 CBIT_SET(RequestHeadersMask
,h
);
130 if (Http::HeaderLookupTable
.lookup(h
).reply
)
131 CBIT_SET(ReplyHeadersMask
,h
);
134 assert(HttpHeaderStats
[0].label
&& "httpHeaderInitModule() called via main()");
135 assert(HttpHeaderStats
[hoEnd
-1].label
&& "HttpHeaderStats created with all elements");
137 /* init dependent modules */
138 httpHdrScInitModule();
140 httpHeaderRegisterWithCacheManager();
144 * HttpHeader Implementation
147 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0), conflictingContentLength_(false)
149 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
150 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
152 httpHeaderMaskInit(&mask
, 0);
155 // XXX: Delete as unused, expensive, and violating copy semantics by skipping Warnings
156 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
), conflictingContentLength_(false)
158 entries
.reserve(other
.entries
.capacity());
159 httpHeaderMaskInit(&mask
, 0);
160 update(&other
); // will update the mask as well
163 HttpHeader::~HttpHeader()
168 // XXX: Delete as unused, expensive, and violating assignment semantics by skipping Warnings
170 HttpHeader::operator =(const HttpHeader
&other
)
172 if (this != &other
) {
173 // we do not really care, but the caller probably does
174 assert(owner
== other
.owner
);
176 update(&other
); // will update the mask as well
178 conflictingContentLength_
= other
.conflictingContentLength_
;
179 teUnsupported_
= other
.teUnsupported_
;
188 assert(owner
> hoNone
&& owner
< hoEnd
);
189 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
191 if (owner
<= hoReply
) {
193 * An unfortunate bug. The entries array is initialized
194 * such that count is set to zero. httpHeaderClean() seems to
195 * be called both when 'hdr' is created, and destroyed. Thus,
196 * we accumulate a large number of zero counts for 'hdr' before
197 * it is ever used. Can't think of a good way to fix it, except
198 * adding a state variable that indicates whether or not 'hdr'
199 * has been used. As a hack, just never count zero-sized header
202 if (!entries
.empty())
203 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
205 ++ HttpHeaderStats
[owner
].destroyedCount
;
207 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
208 } // if (owner <= hoReply)
210 for (HttpHeaderEntry
*e
: entries
) {
213 if (!Http::any_valid_header(e
->id
)) {
214 debugs(55, DBG_CRITICAL
, "ERROR: Squid BUG: invalid entry (" << e
->id
<< "). Ignored.");
216 if (owner
<= hoReply
)
217 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
223 httpHeaderMaskInit(&mask
, 0);
225 conflictingContentLength_
= false;
226 teUnsupported_
= false;
229 /* append entries (also see httpHeaderUpdate) */
231 HttpHeader::append(const HttpHeader
* src
)
235 debugs(55, 7, "appending hdr: " << this << " += " << src
);
237 for (auto e
: src
->entries
) {
239 addEntry(e
->clone());
244 HttpHeader::needUpdate(HttpHeader
const *fresh
) const
246 for (const auto e
: fresh
->entries
) {
247 if (!e
|| skipUpdateHeader(e
->id
))
250 if (!hasNamed(e
->name
, &value
) ||
251 (value
!= fresh
->getByName(e
->name
)))
258 HttpHeader::skipUpdateHeader(const Http::HdrType id
) const
261 // TODO: Consider updating Vary headers after comparing the magnitude of
262 // the required changes (and/or cache losses) with compliance gains.
263 (id
== Http::HdrType::VARY
);
267 HttpHeader::update(HttpHeader
const *fresh
)
270 assert(this != fresh
);
272 const HttpHeaderEntry
*e
;
273 HttpHeaderPos pos
= HttpHeaderInitPos
;
275 while ((e
= fresh
->getEntry(&pos
))) {
276 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
278 if (skipUpdateHeader(e
->id
))
281 if (e
->id
!= Http::HdrType::OTHER
)
287 pos
= HttpHeaderInitPos
;
288 while ((e
= fresh
->getEntry(&pos
))) {
289 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
291 if (skipUpdateHeader(e
->id
))
294 debugs(55, 7, "Updating header '" << Http::HeaderLookupTable
.lookup(e
->id
).name
<< "' in cached entry");
296 addEntry(e
->clone());
301 HttpHeader::Isolate(const char **parse_start
, size_t l
, const char **blk_start
, const char **blk_end
)
304 * parse_start points to the first line of HTTP message *headers*,
305 * not including the request or status lines
307 const size_t end
= headersEnd(*parse_start
, l
);
310 *blk_start
= *parse_start
;
311 *blk_end
= *parse_start
+ end
- 1;
312 assert(**blk_end
== '\n');
313 // Point blk_end to the first character after the last header field.
314 // In other words, blk_end should point to the CR?LF header terminator.
315 if (end
> 1 && *(*blk_end
- 1) == '\r')
323 HttpHeader::parse(const char *buf
, size_t buf_len
, bool atEnd
, size_t &hdr_sz
, Http::ContentLengthInterpreter
&clen
)
325 const char *parse_start
= buf
;
326 const char *blk_start
, *blk_end
;
329 if (!Isolate(&parse_start
, buf_len
, &blk_start
, &blk_end
)) {
330 // XXX: do not parse non-isolated headers even if the connection is closed.
331 // Treat unterminated headers as "partial headers" framing errors.
334 blk_start
= parse_start
;
335 blk_end
= blk_start
+ strlen(blk_start
);
338 if (parse(blk_start
, blk_end
- blk_start
, clen
)) {
339 hdr_sz
= parse_start
- buf
;
345 // XXX: callers treat this return as boolean.
346 // XXX: A better mechanism is needed to signal different types of error.
347 // lexicon, syntax, semantics, validation, access policy - are all (ab)using 'return 0'
349 HttpHeader::parse(const char *header_start
, size_t hdrLen
, Http::ContentLengthInterpreter
&clen
)
351 const char *field_ptr
= header_start
;
352 const char *header_end
= header_start
+ hdrLen
; // XXX: remove
353 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
355 assert(header_start
&& header_end
);
356 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, hdrLen
));
357 ++ HttpHeaderStats
[owner
].parsedCount
;
360 if ((nulpos
= (char*)memchr(header_start
, '\0', hdrLen
))) {
361 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
362 getStringPrefix(header_start
, nulpos
-header_start
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, hdrLen
-(nulpos
-header_start
)-1));
367 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
368 * continuation lines start with a (single) space or tab */
369 while (field_ptr
< header_end
) {
370 const char *field_start
= field_ptr
;
371 const char *field_end
;
373 const char *hasBareCr
= nullptr;
376 const char *this_line
= field_ptr
;
377 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
386 field_end
= field_ptr
;
388 ++field_ptr
; /* Move to next line */
390 if (field_end
> this_line
&& field_end
[-1] == '\r') {
391 --field_end
; /* Ignore CR LF */
393 if (owner
== hoRequest
&& field_end
> this_line
) {
395 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
400 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
401 "header field to prevent request smuggling attacks: {" <<
402 getStringPrefix(header_start
, hdrLen
) << "}");
409 /* Barf on stray CR characters */
410 if (memchr(this_line
, '\r', field_end
- this_line
)) {
411 hasBareCr
= "bare CR";
412 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
413 getStringPrefix(field_start
, field_end
-field_start
) << "}");
415 if (Config
.onoff
.relaxed_header_parser
) {
416 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
418 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != nullptr) {
428 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
429 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
430 getStringPrefix(header_start
, hdrLen
) << "}");
434 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
436 if (field_start
== field_end
) {
437 if (field_ptr
< header_end
) {
438 debugs(55, warnOnError
, "WARNING: unparsable HTTP header field near {" <<
439 getStringPrefix(field_start
, hdrLen
-(field_start
-header_start
)) << "}");
444 break; /* terminating blank line */
447 const auto e
= HttpHeaderEntry::parse(field_start
, field_end
, owner
);
449 debugs(55, warnOnError
, "WARNING: unparsable HTTP header field {" <<
450 getStringPrefix(field_start
, field_end
-field_start
) << "}");
451 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, hdrLen
) << "}");
457 if (lines
> 1 || hasBareCr
) {
458 const auto framingHeader
= (e
->id
== Http::HdrType::CONTENT_LENGTH
|| e
->id
== Http::HdrType::TRANSFER_ENCODING
);
460 if (!hasBareCr
) // already warned about bare CRs
461 debugs(55, warnOnError
, "WARNING: obs-fold in framing-sensitive " << e
->name
<< ": " << e
->value
);
468 if (e
->id
== Http::HdrType::CONTENT_LENGTH
&& !clen
.checkField(e
->value
)) {
471 if (Config
.onoff
.relaxed_header_parser
)
472 continue; // clen has printed any necessary warnings
481 if (clen
.headerWideProblem
) {
482 debugs(55, warnOnError
, "WARNING: " << clen
.headerWideProblem
<<
483 " Content-Length field values in" <<
484 Raw("header", header_start
, hdrLen
));
488 if (clen
.prohibitedAndIgnored()) {
489 // prohibitedAndIgnored() includes trailer header blocks
490 // being parsed as a case to forbid/ignore these headers.
492 // RFC 7230 section 3.3.2: A server MUST NOT send a Content-Length
493 // header field in any response with a status code of 1xx (Informational)
494 // or 204 (No Content). And RFC 7230 3.3.3#1 tells recipients to ignore
495 // such Content-Lengths.
496 if (delById(Http::HdrType::CONTENT_LENGTH
))
497 debugs(55, 3, "Content-Length is " << clen
.prohibitedAndIgnored());
499 // The same RFC 7230 3.3.3#1-based logic applies to Transfer-Encoding
500 // banned by RFC 7230 section 3.3.1.
501 if (delById(Http::HdrType::TRANSFER_ENCODING
))
502 debugs(55, 3, "Transfer-Encoding is " << clen
.prohibitedAndIgnored());
504 } else if (getByIdIfPresent(Http::HdrType::TRANSFER_ENCODING
, &rawTe
)) {
505 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
506 // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length
507 delById(Http::HdrType::CONTENT_LENGTH
);
508 // and clen state becomes irrelevant
510 if (rawTe
.caseCmp("chunked") == 0) {
511 ; // leave header present for chunked() method
512 } else if (rawTe
.caseCmp("identity") == 0) { // deprecated. no coding
513 delById(Http::HdrType::TRANSFER_ENCODING
);
515 // This also rejects multiple encodings until we support them properly.
516 debugs(55, warnOnError
, "WARNING: unsupported Transfer-Encoding used by client: " << rawTe
);
517 teUnsupported_
= true;
520 } else if (clen
.sawBad
) {
521 // ensure our callers do not accidentally see bad Content-Length values
522 delById(Http::HdrType::CONTENT_LENGTH
);
523 conflictingContentLength_
= true; // TODO: Rename to badContentLength_.
524 } else if (clen
.needsSanitizing
) {
525 // RFC 7230 section 3.3.2: MUST either reject or ... [sanitize];
526 // ensure our callers see a clean Content-Length value or none at all
527 delById(Http::HdrType::CONTENT_LENGTH
);
529 putInt64(Http::HdrType::CONTENT_LENGTH
, clen
.value
);
530 debugs(55, 5, "sanitized Content-Length to be " << clen
.value
);
534 return 1; /* even if no fields where found, it is a valid header */
537 /* packs all the entries using supplied packer */
539 HttpHeader::packInto(Packable
* p
, bool mask_sensitive_info
) const
541 HttpHeaderPos pos
= HttpHeaderInitPos
;
542 const HttpHeaderEntry
*e
;
544 debugs(55, 7, this << " into " << p
<<
545 (mask_sensitive_info
? " while masking" : ""));
546 /* pack all entries one by one */
547 while ((e
= getEntry(&pos
))) {
548 if (!mask_sensitive_info
) {
553 bool maskThisEntry
= false;
555 case Http::HdrType::AUTHORIZATION
:
556 case Http::HdrType::PROXY_AUTHORIZATION
:
557 maskThisEntry
= true;
560 case Http::HdrType::FTP_ARGUMENTS
:
561 if (const HttpHeaderEntry
*cmd
= findEntry(Http::HdrType::FTP_COMMAND
))
562 maskThisEntry
= (cmd
->value
== "PASS");
569 p
->append(e
->name
.rawContent(), e
->name
.length());
570 p
->append(": ** NOT DISPLAYED **\r\n", 23);
576 /* Pack in the "special" entries */
581 /* returns next valid entry */
583 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
586 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
588 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
590 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
597 * returns a pointer to a specified entry if any
598 * note that we return one entry so it does not make much sense to ask for
602 HttpHeader::findEntry(Http::HdrType id
) const
604 assert(any_registered_header(id
));
605 assert(!Http::HeaderLookupTable
.lookup(id
).list
);
607 /* check mask first */
609 if (!CBIT_TEST(mask
, id
))
612 /* looks like we must have it, do linear search */
613 for (auto e
: entries
) {
614 if (e
&& e
->id
== id
)
618 /* hm.. we thought it was there, but it was not found */
620 return nullptr; /* not reached */
624 * same as httpHeaderFindEntry
627 HttpHeader::findLastEntry(Http::HdrType id
) const
629 assert(any_registered_header(id
));
630 assert(!Http::HeaderLookupTable
.lookup(id
).list
);
632 /* check mask first */
633 if (!CBIT_TEST(mask
, id
))
636 for (auto e
= entries
.rbegin(); e
!= entries
.rend(); ++e
) {
637 if (*e
&& (*e
)->id
== id
)
641 /* hm.. we thought it was there, but it was not found */
643 return nullptr; /* not reached */
647 HttpHeader::delByName(const SBuf
&name
)
650 HttpHeaderPos pos
= HttpHeaderInitPos
;
651 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
652 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
654 while (const HttpHeaderEntry
*e
= getEntry(&pos
)) {
655 if (!e
->name
.caseCmp(name
))
658 CBIT_SET(mask
, e
->id
);
664 /* deletes all entries with a given id, returns the #entries deleted */
666 HttpHeader::delById(Http::HdrType id
)
668 debugs(55, 8, this << " del-by-id " << id
);
669 assert(any_registered_header(id
));
671 if (!CBIT_TEST(mask
, id
))
676 HttpHeaderPos pos
= HttpHeaderInitPos
;
677 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
679 delAt(pos
, count
); // deletes e
688 * deletes an entry at pos and leaves a gap; leaving a gap makes it
689 * possible to iterate(search) and delete fields at the same time
690 * NOTE: Does not update the header mask. Caller must follow up with
691 * a call to refreshMask() if headers_deleted was incremented.
694 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
697 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
698 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
699 entries
[pos
] = nullptr;
700 /* decrement header length, allow for ": " and crlf */
701 len
-= e
->name
.length() + 2 + e
->value
.size() + 2;
708 * Compacts the header storage
711 HttpHeader::compact()
713 // TODO: optimize removal, or possibly make it so that's not needed.
714 entries
.erase( std::remove(entries
.begin(), entries
.end(), nullptr),
719 * Refreshes the header mask. Required after delAt() calls.
722 HttpHeader::refreshMask()
724 httpHeaderMaskInit(&mask
, 0);
725 debugs(55, 7, "refreshing the mask in hdr " << this);
726 for (auto e
: entries
) {
728 CBIT_SET(mask
, e
->id
);
733 * does not call e->clone() so one should not reuse "*e"
736 HttpHeader::addEntry(HttpHeaderEntry
* e
)
739 assert(any_HdrType_enum_value(e
->id
));
740 assert(e
->name
.length());
742 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
744 if (e
->id
!= Http::HdrType::BAD_HDR
) {
745 if (CBIT_TEST(mask
, e
->id
)) {
746 ++ headerStatsTable
[e
->id
].repCount
;
748 CBIT_SET(mask
, e
->id
);
752 entries
.push_back(e
);
758 HttpHeader::getList(Http::HdrType id
, String
*s
) const
760 debugs(55, 9, this << " joining for id " << id
);
761 /* only fields from ListHeaders array can be "listed" */
762 assert(Http::HeaderLookupTable
.lookup(id
).list
);
764 if (!CBIT_TEST(mask
, id
))
767 for (auto e
: entries
) {
768 if (e
&& e
->id
== id
)
769 strListAdd(s
, e
->value
.termedBuf(), ',');
773 * note: we might get an empty (size==0) string if there was an "empty"
774 * header. This results in an empty length String, which may have a NULL
777 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
779 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable
.lookup(id
).name
<< "(" << id
<< ")");
781 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
786 /* return a list of entries with the same id separated by ',' and ws */
788 HttpHeader::getList(Http::HdrType id
) const
791 HttpHeaderPos pos
= HttpHeaderInitPos
;
792 debugs(55, 9, this << "joining for id " << id
);
793 /* only fields from ListHeaders array can be "listed" */
794 assert(Http::HeaderLookupTable
.lookup(id
).list
);
796 if (!CBIT_TEST(mask
, id
))
801 while ((e
= getEntry(&pos
))) {
803 strListAdd(&s
, e
->value
.termedBuf(), ',');
807 * note: we might get an empty (size==0) string if there was an "empty"
808 * header. This results in an empty length String, which may have a NULL
811 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
813 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable
.lookup(id
).name
<< "(" << id
<< ")");
815 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
820 /* return a string or list of entries with the same id separated by ',' and ws */
822 HttpHeader::getStrOrList(Http::HdrType id
) const
826 if (Http::HeaderLookupTable
.lookup(id
).list
)
829 if ((e
= findEntry(id
)))
836 * Returns the value of the specified header and/or an undefined String.
839 HttpHeader::getByName(const char *name
) const
842 // ignore presence: return undefined string if an empty header is present
843 (void)hasNamed(name
, strlen(name
), &result
);
848 HttpHeader::getByName(const SBuf
&name
) const
851 // ignore presence: return undefined string if an empty header is present
852 (void)hasNamed(name
, &result
);
857 HttpHeader::getById(Http::HdrType id
) const
860 (void)getByIdIfPresent(id
, &result
);
865 HttpHeader::hasNamed(const SBuf
&s
, String
*result
) const
867 return hasNamed(s
.rawContent(), s
.length(), result
);
871 HttpHeader::getByIdIfPresent(Http::HdrType id
, String
*result
) const
873 if (id
== Http::HdrType::BAD_HDR
)
878 *result
= getStrOrList(id
);
883 HttpHeader::hasNamed(const char *name
, unsigned int namelen
, String
*result
) const
886 HttpHeaderPos pos
= HttpHeaderInitPos
;
891 /* First try the quick path */
892 id
= Http::HeaderLookupTable
.lookup(name
,namelen
).id
;
894 if (id
!= Http::HdrType::BAD_HDR
) {
895 if (getByIdIfPresent(id
, result
))
899 /* Sorry, an unknown header name. Do linear search */
901 while ((e
= getEntry(&pos
))) {
902 if (e
->id
== Http::HdrType::OTHER
&& e
->name
.length() == namelen
&& e
->name
.caseCmp(name
, namelen
) == 0) {
906 strListAdd(result
, e
->value
.termedBuf(), ',');
914 * Returns a the value of the specified list member, if any.
917 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
920 const auto header
= getByName(name
);
921 return ::getListMember(header
, member
, separator
);
925 * returns a the value of the specified list member, if any.
928 HttpHeader::getListMember(Http::HdrType id
, const char *member
, const char separator
) const
930 assert(any_registered_header(id
));
931 const auto header
= getStrOrList(id
);
932 return ::getListMember(header
, member
, separator
);
935 /* test if a field is present */
937 HttpHeader::has(Http::HdrType id
) const
939 assert(any_registered_header(id
));
940 debugs(55, 9, this << " lookup for " << id
);
941 return CBIT_TEST(mask
, id
);
945 HttpHeader::addVia(const AnyP::ProtocolVersion
&ver
, const HttpHeader
*from
)
947 // TODO: do not add Via header for messages where Squid itself
948 // generated the message (i.e., Downloader) there should be no Via header added at all.
950 if (Config
.onoff
.via
) {
952 // RFC 7230 section 5.7.1.: protocol-name is omitted when
953 // the received protocol is HTTP.
954 if (ver
.protocol
> AnyP::PROTO_NONE
&& ver
.protocol
< AnyP::PROTO_UNKNOWN
&&
955 ver
.protocol
!= AnyP::PROTO_HTTP
&& ver
.protocol
!= AnyP::PROTO_HTTPS
)
956 buf
.appendf("%s/", AnyP::ProtocolType_str
[ver
.protocol
]);
957 buf
.appendf("%d.%d %s", ver
.major
, ver
.minor
, ThisCache
);
958 const HttpHeader
*hdr
= from
? from
: this;
959 SBuf strVia
= StringToSBuf(hdr
->getList(Http::HdrType::VIA
));
960 if (!strVia
.isEmpty())
961 strVia
.append(", ", 2);
963 updateOrAddStr(Http::HdrType::VIA
, strVia
);
968 HttpHeader::putInt(Http::HdrType id
, int number
)
970 assert(any_registered_header(id
));
971 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
973 addEntry(new HttpHeaderEntry(id
, SBuf(), xitoa(number
)));
977 HttpHeader::putInt64(Http::HdrType id
, int64_t number
)
979 assert(any_registered_header(id
));
980 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
982 addEntry(new HttpHeaderEntry(id
, SBuf(), xint64toa(number
)));
986 HttpHeader::putTime(Http::HdrType id
, time_t htime
)
988 assert(any_registered_header(id
));
989 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
991 addEntry(new HttpHeaderEntry(id
, SBuf(), Time::FormatRfc1123(htime
)));
995 HttpHeader::putStr(Http::HdrType id
, const char *str
)
997 assert(any_registered_header(id
));
998 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1000 addEntry(new HttpHeaderEntry(id
, SBuf(), str
));
1004 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1006 assert(auth_scheme
&& realm
);
1007 httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1011 HttpHeader::putCc(const HttpHdrCc
&cc
)
1013 /* remove old directives if any */
1014 delById(Http::HdrType::CACHE_CONTROL
);
1020 addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL
, SBuf(), mb
.buf
));
1026 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1029 /* remove old directives if any */
1030 delById(Http::HdrType::CONTENT_RANGE
);
1034 httpHdrContRangePackInto(cr
, &mb
);
1036 addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE
, SBuf(), mb
.buf
));
1042 HttpHeader::putRange(const HttpHdrRange
* range
)
1045 /* remove old directives if any */
1046 delById(Http::HdrType::RANGE
);
1050 range
->packInto(&mb
);
1052 addEntry(new HttpHeaderEntry(Http::HdrType::RANGE
, SBuf(), mb
.buf
));
1058 HttpHeader::putSc(HttpHdrSc
*sc
)
1061 /* remove old directives if any */
1062 delById(Http::HdrType::SURROGATE_CONTROL
);
1068 addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL
, SBuf(), mb
.buf
));
1073 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1075 HttpHeader::putExt(const char *name
, const char *value
)
1077 assert(name
&& value
);
1078 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1079 addEntry(new HttpHeaderEntry(Http::HdrType::OTHER
, SBuf(name
), value
));
1083 HttpHeader::updateOrAddStr(const Http::HdrType id
, const SBuf
&newValue
)
1085 assert(any_registered_header(id
));
1086 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
);
1088 // XXX: HttpHeaderEntry::value suffers from String size limits
1089 Assure(newValue
.length() < String::SizeMaxXXX());
1091 if (!CBIT_TEST(mask
, id
)) {
1092 auto newValueCopy
= newValue
; // until HttpHeaderEntry::value becomes SBuf
1093 addEntry(new HttpHeaderEntry(id
, SBuf(), newValueCopy
.c_str()));
1097 auto foundSameName
= false;
1098 for (auto &e
: entries
) {
1099 if (!e
|| e
->id
!= id
)
1102 if (foundSameName
) {
1103 // get rid of this repeated same-name entry
1109 if (newValue
.cmp(e
->value
.termedBuf()) != 0)
1110 e
->value
.assign(newValue
.rawContent(), newValue
.plength());
1112 foundSameName
= true;
1113 // continue to delete any repeated same-name entries
1115 assert(foundSameName
);
1116 debugs(55, 5, "synced: " << Http::HeaderLookupTable
.lookup(id
).name
<< ": " << newValue
);
1120 HttpHeader::getInt(Http::HdrType id
) const
1122 assert(any_registered_header(id
));
1123 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
1126 if ((e
= findEntry(id
)))
1133 HttpHeader::getInt64(Http::HdrType id
) const
1135 assert(any_registered_header(id
));
1136 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
1139 if ((e
= findEntry(id
)))
1140 return e
->getInt64();
1146 HttpHeader::getTime(Http::HdrType id
) const
1150 assert(any_registered_header(id
));
1151 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1153 if ((e
= findEntry(id
))) {
1154 value
= Time::ParseRfc1123(e
->value
.termedBuf());
1155 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1161 /* sync with httpHeaderGetLastStr */
1163 HttpHeader::getStr(Http::HdrType id
) const
1166 assert(any_registered_header(id
));
1167 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1169 if ((e
= findEntry(id
))) {
1170 httpHeaderNoteParsedEntry(e
->id
, e
->value
, false); /* no errors are possible */
1171 return e
->value
.termedBuf();
1179 HttpHeader::getLastStr(Http::HdrType id
) const
1182 assert(any_registered_header(id
));
1183 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1185 if ((e
= findLastEntry(id
))) {
1186 httpHeaderNoteParsedEntry(e
->id
, e
->value
, false); /* no errors are possible */
1187 return e
->value
.termedBuf();
1194 HttpHeader::getCc() const
1196 if (!CBIT_TEST(mask
, Http::HdrType::CACHE_CONTROL
))
1200 getList(Http::HdrType::CACHE_CONTROL
, &s
);
1202 HttpHdrCc
*cc
=new HttpHdrCc();
1204 if (!cc
->parse(s
)) {
1209 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1212 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1214 httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL
, s
, !cc
);
1220 HttpHeader::getRange() const
1222 HttpHdrRange
*r
= nullptr;
1224 /* some clients will send "Request-Range" _and_ *matching* "Range"
1225 * who knows, some clients might send Request-Range only;
1226 * this "if" should work correctly in both cases;
1227 * hopefully no clients send mismatched headers! */
1229 if ((e
= findEntry(Http::HdrType::RANGE
)) ||
1230 (e
= findEntry(Http::HdrType::REQUEST_RANGE
))) {
1231 r
= HttpHdrRange::ParseCreate(&e
->value
);
1232 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1239 HttpHeader::getSc() const
1241 if (!CBIT_TEST(mask
, Http::HdrType::SURROGATE_CONTROL
))
1246 (void) getList(Http::HdrType::SURROGATE_CONTROL
, &s
);
1248 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1250 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1253 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1255 httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL
, s
, !sc
);
1261 HttpHeader::getContRange() const
1263 HttpHdrContRange
*cr
= nullptr;
1266 if ((e
= findEntry(Http::HdrType::CONTENT_RANGE
))) {
1267 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1268 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1275 HttpHeader::getAuthToken(Http::HdrType id
, const char *auth_scheme
) const
1279 assert(auth_scheme
);
1282 static const SBuf nil
;
1283 if (!field
) /* no authorization field */
1286 l
= strlen(auth_scheme
);
1288 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1293 if (!xisspace(*field
)) /* wrong scheme */
1296 /* skip white space */
1297 for (; field
&& xisspace(*field
); ++field
);
1299 if (!*field
) /* no authorization cookie */
1302 const auto fieldLen
= strlen(field
);
1304 char *decodedAuthToken
= result
.rawAppendStart(BASE64_DECODE_LENGTH(fieldLen
));
1305 struct base64_decode_ctx ctx
;
1306 base64_decode_init(&ctx
);
1307 size_t decodedLen
= 0;
1308 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(decodedAuthToken
), fieldLen
, field
) ||
1309 !base64_decode_final(&ctx
)) {
1312 result
.rawAppendFinish(decodedAuthToken
, decodedLen
);
1317 HttpHeader::getETag(Http::HdrType id
) const
1319 ETag etag
= {nullptr, -1};
1321 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftETag
); /* must be of an appropriate type */
1323 if ((e
= findEntry(id
)))
1324 etagParseInit(&etag
, e
->value
.termedBuf());
1330 HttpHeader::getTimeOrTag(Http::HdrType id
) const
1334 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123_or_ETag
); /* must be of an appropriate type */
1335 memset(&tot
, 0, sizeof(tot
));
1337 if ((e
= findEntry(id
))) {
1338 const char *str
= e
->value
.termedBuf();
1339 /* try as an ETag */
1341 if (etagParseInit(&tot
.tag
, str
)) {
1342 tot
.valid
= tot
.tag
.str
!= nullptr;
1345 /* or maybe it is time? */
1346 tot
.time
= Time::ParseRfc1123(str
);
1347 tot
.valid
= tot
.time
>= 0;
1348 tot
.tag
.str
= nullptr;
1352 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1360 HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId
, const SBuf
&aName
, const char *aValue
)
1362 assert(any_HdrType_enum_value(anId
));
1365 if (id
!= Http::HdrType::OTHER
)
1366 name
= Http::HeaderLookupTable
.lookup(id
).name
;
1372 if (id
!= Http::HdrType::BAD_HDR
)
1373 ++ headerStatsTable
[id
].aliveCount
;
1375 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1378 HttpHeaderEntry::~HttpHeaderEntry()
1380 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1382 if (id
!= Http::HdrType::BAD_HDR
) {
1383 assert(headerStatsTable
[id
].aliveCount
);
1384 -- headerStatsTable
[id
].aliveCount
;
1385 id
= Http::HdrType::BAD_HDR
; // it already is BAD_HDR, no sense in resetting it
1390 /* parses and inits header entry, returns true/false */
1392 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
, const http_hdr_owner_type msgType
)
1394 /* note: name_start == field_start */
1395 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1396 int name_len
= name_end
? name_end
- field_start
:0;
1397 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1398 /* note: value_end == field_end */
1400 ++ HeaderEntryParsedCount
;
1402 /* do we have a valid field name within this field? */
1404 if (!name_len
|| name_end
> field_end
)
1407 if (name_len
> 65534) {
1408 /* String must be LESS THAN 64K and it adds a terminating NULL */
1409 // TODO: update this to show proper name_len in Raw markup, but not print all that
1410 debugs(55, 2, "ignoring huge header field (" << Raw("field_start", field_start
, 100) << "...)");
1415 * RFC 7230 section 3.2.4:
1416 * "No whitespace is allowed between the header field-name and colon.
1418 * A server MUST reject any received request message that contains
1419 * whitespace between a header field-name and colon with a response code
1420 * of 400 (Bad Request). A proxy MUST remove any such whitespace from a
1421 * response message before forwarding the message downstream."
1423 if (xisspace(field_start
[name_len
- 1])) {
1425 if (msgType
== hoRequest
)
1428 // for now, also let relaxed parser remove this BWS from any non-HTTP messages
1429 const bool stripWhitespace
= (msgType
== hoReply
) ||
1430 Config
.onoff
.relaxed_header_parser
;
1431 if (!stripWhitespace
)
1432 return nullptr; // reject if we cannot strip
1434 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1435 "WARNING: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1437 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1441 debugs(55, 2, "found header with only whitespace for name");
1446 /* RFC 7230 section 3.2:
1448 * header-field = field-name ":" OWS field-value OWS
1449 * field-name = token
1452 for (const char *pos
= field_start
; pos
< (field_start
+name_len
); ++pos
) {
1453 if (!CharacterSet::TCHAR
[*pos
]) {
1454 debugs(55, 2, "found header with invalid characters in " <<
1455 Raw("field-name", field_start
, min(name_len
,100)) << "...");
1460 /* now we know we can parse it */
1462 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1464 /* is it a "known" field? */
1465 Http::HdrType id
= Http::HeaderLookupTable
.lookup(field_start
,name_len
).id
;
1466 debugs(55, 9, "got hdr-id=" << id
);
1472 if (id
== Http::HdrType::BAD_HDR
)
1473 id
= Http::HdrType::OTHER
;
1475 /* set field name */
1476 if (id
== Http::HdrType::OTHER
)
1477 theName
.append(field_start
, name_len
);
1479 theName
= Http::HeaderLookupTable
.lookup(id
).name
;
1481 /* trim field value */
1482 while (value_start
< field_end
&& xisspace(*value_start
))
1485 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1488 if (field_end
- value_start
> 65534) {
1489 /* String must be LESS THAN 64K and it adds a terminating NULL */
1490 debugs(55, 2, "WARNING: found '" << theName
<< "' header of " << (field_end
- value_start
) << " bytes");
1494 /* set field value */
1495 value
.assign(value_start
, field_end
- value_start
);
1497 if (id
!= Http::HdrType::BAD_HDR
)
1498 ++ headerStatsTable
[id
].seenCount
;
1500 debugs(55, 9, "parsed HttpHeaderEntry: '" << theName
<< ": " << value
<< "'");
1502 return new HttpHeaderEntry(id
, theName
, value
.termedBuf());
1506 HttpHeaderEntry::clone() const
1508 return new HttpHeaderEntry(id
, name
, value
.termedBuf());
1512 HttpHeaderEntry::packInto(Packable
* p
) const
1515 p
->append(name
.rawContent(), name
.length());
1517 p
->append(value
.rawBuf(), value
.size());
1518 p
->append("\r\n", 2);
1522 HttpHeaderEntry::getInt() const
1525 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1526 httpHeaderNoteParsedEntry(id
, value
, ok
== 0);
1527 /* XXX: Should we check ok - ie
1528 * return ok ? -1 : value;
1534 HttpHeaderEntry::getInt64() const
1537 const bool ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1538 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1539 return val
; // remains -1 if !ok (XXX: bad method API)
1543 httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &context
, bool error
)
1545 if (id
!= Http::HdrType::BAD_HDR
)
1546 ++ headerStatsTable
[id
].parsCount
;
1549 if (id
!= Http::HdrType::BAD_HDR
)
1550 ++ headerStatsTable
[id
].errCount
;
1551 debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderLookupTable
.lookup(id
).name
<< ": " << context
<< "'");
1559 /* tmp variable used to pass stat info to dumpers */
1560 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1561 const HttpHeaderStat
*dump_stat
= nullptr;
1564 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
1566 const int id
= static_cast<int>(val
);
1567 const bool valid_id
= Http::any_valid_header(static_cast<Http::HdrType
>(id
));
1568 const char *name
= valid_id
? Http::HeaderLookupTable
.lookup(static_cast<Http::HdrType
>(id
)).name
: "INVALID";
1569 int visible
= count
> 0;
1570 /* for entries with zero count, list only those that belong to current type of message */
1572 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1573 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1576 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1577 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1581 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double, int count
)
1584 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1585 idx
, (int) val
, count
,
1586 xpercent(count
, dump_stat
->destroyedCount
));
1590 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1595 if (!hs
->owner_mask
)
1596 return; // these HttpHeaderStat objects were not meant to be dumped here
1599 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1600 storeAppendPrintf(e
, "\nField type distribution\n");
1601 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1602 "id", "name", "count", "#/header");
1603 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1604 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1605 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1606 "id", "name", "count", "#/cc_field");
1607 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1608 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1609 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1610 "id", "name", "count", "#/sc_field");
1611 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1612 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1613 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1614 "id", "#flds", "count", "%total");
1615 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1616 storeAppendPrintf(e
, "\n");
1617 dump_stat
= nullptr;
1621 httpHeaderStoreReport(StoreEntry
* e
)
1625 HttpHeaderStats
[0].parsedCount
=
1626 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1627 HttpHeaderStats
[0].ccParsedCount
=
1628 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1629 HttpHeaderStats
[0].destroyedCount
=
1630 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1631 HttpHeaderStats
[0].busyDestroyedCount
=
1632 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1634 for (const auto &stats
: HttpHeaderStats
)
1635 httpHeaderStatDump(&stats
, e
);
1637 /* field stats for all messages */
1638 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1640 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1641 "id", "name", "#alive", "%err", "%repeat");
1643 // scan heaaderTable and output
1644 for (auto h
: WholeEnum
<Http::HdrType
>()) {
1645 auto stats
= headerStatsTable
[h
];
1646 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1647 Http::HeaderLookupTable
.lookup(h
).id
,
1648 Http::HeaderLookupTable
.lookup(h
).name
,
1650 xpercent(stats
.errCount
, stats
.parsCount
),
1651 xpercent(stats
.repCount
, stats
.seenCount
));
1654 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1655 HttpHeaderStats
[hoRequest
].parsedCount
,
1656 HttpHeaderStats
[hoReply
].parsedCount
,
1657 HttpHeaderStats
[0].parsedCount
);
1658 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1662 HttpHeader::hasListMember(Http::HdrType id
, const char *member
, const char separator
) const
1665 const char *pos
= nullptr;
1668 int mlen
= strlen(member
);
1670 assert(any_registered_header(id
));
1672 String
header (getStrOrList(id
));
1674 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1675 if (strncasecmp(item
, member
, mlen
) == 0
1676 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1686 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1689 const char *pos
= nullptr;
1692 int mlen
= strlen(member
);
1696 String
header (getByName(name
));
1698 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1699 if (strncasecmp(item
, member
, mlen
) == 0
1700 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1710 HttpHeader::removeHopByHopEntries()
1712 removeConnectionHeaderEntries();
1714 const HttpHeaderEntry
*e
;
1715 HttpHeaderPos pos
= HttpHeaderInitPos
;
1716 int headers_deleted
= 0;
1717 while ((e
= getEntry(&pos
))) {
1718 Http::HdrType id
= e
->id
;
1719 if (Http::HeaderLookupTable
.lookup(id
).hopbyhop
) {
1720 delAt(pos
, headers_deleted
);
1727 HttpHeader::removeConnectionHeaderEntries()
1729 if (has(Http::HdrType::CONNECTION
)) {
1730 /* anything that matches Connection list member will be deleted */
1731 String strConnection
;
1733 (void) getList(Http::HdrType::CONNECTION
, &strConnection
);
1734 const HttpHeaderEntry
*e
;
1735 HttpHeaderPos pos
= HttpHeaderInitPos
;
1737 * think: on-average-best nesting of the two loops (hdrEntry
1738 * and strListItem) @?@
1741 * maybe we should delete standard stuff ("keep-alive","close")
1742 * from strConnection first?
1745 int headers_deleted
= 0;
1746 while ((e
= getEntry(&pos
))) {
1747 if (strListIsMember(&strConnection
, e
->name
, ','))
1748 delAt(pos
, headers_deleted
);
1750 if (headers_deleted
)