2 * Copyright (C) 1996-2022 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/CharacterSet.h"
13 #include "base/EnumIterator.h"
17 #include "http/ContentLengthInterpreter.h"
18 #include "HttpHdrCc.h"
19 #include "HttpHdrContRange.h"
20 #include "HttpHdrScTarget.h" // also includes HttpHdrSc.h
21 #include "HttpHeader.h"
22 #include "HttpHeaderFieldInfo.h"
23 #include "HttpHeaderStat.h"
24 #include "HttpHeaderTools.h"
26 #include "mgr/Registration.h"
27 #include "mime_header.h"
28 #include "sbuf/StringConvert.h"
29 #include "SquidConfig.h"
33 #include "time/gadgets.h"
34 #include "TimeOrTag.h"
40 /* XXX: the whole set of API managing the entries vector should be rethought
41 * after the parse4r-ng effort is complete.
45 * On naming conventions:
47 * HTTP/1.1 defines message-header as
49 * message-header = field-name ":" [ field-value ] CRLF
51 * field-value = *( field-content | LWS )
53 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
54 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
56 * HttpHeader is an object that represents all message-headers in a message.
57 * HttpHeader does not manage start-line.
59 * HttpHeader is implemented as a collection of header "entries".
60 * An entry is a (field_id, field_name, field_value) triplet.
64 * local constants and vars
67 // statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted
68 std::vector
<HttpHeaderFieldStat
> headerStatsTable(Http::HdrType::enumEnd_
);
70 /* request-only headers. Used for cachemgr */
71 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
73 /* reply-only headers. Used for cachemgr */
74 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
76 /* header accounting */
77 // NP: keep in sync with enum http_hdr_owner_type
78 static std::array
<HttpHeaderStat
, hoEnd
> HttpHeaderStats
= {{
79 HttpHeaderStat(/*hoNone*/ "all", nullptr),
81 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask
),
83 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask
),
84 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask
)
86 , HttpHeaderStat(/*hoErrorDetail*/ "error detail templates", nullptr)
91 static int HeaderEntryParsedCount
= 0;
94 * forward declarations and local routines
99 // update parse statistics for header id; if error is true also account
100 // for errors and write to debug log what happened
101 static void httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &value
, bool error
);
102 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
103 /** store report about current header usage and other stats */
104 static void httpHeaderStoreReport(StoreEntry
* e
);
107 * Module initialization routines
111 httpHeaderRegisterWithCacheManager(void)
113 Mgr::RegisterAction("http_headers",
114 "HTTP Header Statistics",
115 httpHeaderStoreReport
, 0, 1);
119 httpHeaderInitModule(void)
121 /* check that we have enough space for masks */
122 assert(8 * sizeof(HttpHeaderMask
) >= Http::HdrType::enumEnd_
);
124 // masks are needed for stats page still
125 for (auto h
: WholeEnum
<Http::HdrType
>()) {
126 if (Http::HeaderLookupTable
.lookup(h
).request
)
127 CBIT_SET(RequestHeadersMask
,h
);
128 if (Http::HeaderLookupTable
.lookup(h
).reply
)
129 CBIT_SET(ReplyHeadersMask
,h
);
132 assert(HttpHeaderStats
[0].label
&& "httpHeaderInitModule() called via main()");
133 assert(HttpHeaderStats
[hoEnd
-1].label
&& "HttpHeaderStats created with all elements");
135 /* init dependent modules */
136 httpHdrCcInitModule();
137 httpHdrScInitModule();
139 httpHeaderRegisterWithCacheManager();
143 * HttpHeader Implementation
146 HttpHeader::HttpHeader() : owner (hoNone
), len (0), conflictingContentLength_(false)
149 httpHeaderMaskInit(&mask
, 0);
152 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0), conflictingContentLength_(false)
154 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
155 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
157 httpHeaderMaskInit(&mask
, 0);
160 // XXX: Delete as unused, expensive, and violating copy semantics by skipping Warnings
161 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
), conflictingContentLength_(false)
163 entries
.reserve(other
.entries
.capacity());
164 httpHeaderMaskInit(&mask
, 0);
165 update(&other
); // will update the mask as well
168 HttpHeader::~HttpHeader()
173 // XXX: Delete as unused, expensive, and violating assignment semantics by skipping Warnings
175 HttpHeader::operator =(const HttpHeader
&other
)
177 if (this != &other
) {
178 // we do not really care, but the caller probably does
179 assert(owner
== other
.owner
);
181 update(&other
); // will update the mask as well
183 conflictingContentLength_
= other
.conflictingContentLength_
;
184 teUnsupported_
= other
.teUnsupported_
;
193 assert(owner
> hoNone
&& owner
< hoEnd
);
194 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
196 if (owner
<= hoReply
) {
198 * An unfortunate bug. The entries array is initialized
199 * such that count is set to zero. httpHeaderClean() seems to
200 * be called both when 'hdr' is created, and destroyed. Thus,
201 * we accumulate a large number of zero counts for 'hdr' before
202 * it is ever used. Can't think of a good way to fix it, except
203 * adding a state variable that indicates whether or not 'hdr'
204 * has been used. As a hack, just never count zero-sized header
207 if (!entries
.empty())
208 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
210 ++ HttpHeaderStats
[owner
].destroyedCount
;
212 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
213 } // if (owner <= hoReply)
215 for (HttpHeaderEntry
*e
: entries
) {
218 if (!Http::any_valid_header(e
->id
)) {
219 debugs(55, DBG_CRITICAL
, "ERROR: Squid BUG: invalid entry (" << e
->id
<< "). Ignored.");
221 if (owner
<= hoReply
)
222 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
228 httpHeaderMaskInit(&mask
, 0);
230 conflictingContentLength_
= false;
231 teUnsupported_
= false;
234 /* append entries (also see httpHeaderUpdate) */
236 HttpHeader::append(const HttpHeader
* src
)
240 debugs(55, 7, "appending hdr: " << this << " += " << src
);
242 for (auto e
: src
->entries
) {
244 addEntry(e
->clone());
249 HttpHeader::needUpdate(HttpHeader
const *fresh
) const
251 for (const auto e
: fresh
->entries
) {
252 if (!e
|| skipUpdateHeader(e
->id
))
255 if (!hasNamed(e
->name
, &value
) ||
256 (value
!= fresh
->getByName(e
->name
)))
263 HttpHeader::skipUpdateHeader(const Http::HdrType id
) const
266 // TODO: Consider updating Vary headers after comparing the magnitude of
267 // the required changes (and/or cache losses) with compliance gains.
268 (id
== Http::HdrType::VARY
);
272 HttpHeader::update(HttpHeader
const *fresh
)
275 assert(this != fresh
);
277 const HttpHeaderEntry
*e
;
278 HttpHeaderPos pos
= HttpHeaderInitPos
;
280 while ((e
= fresh
->getEntry(&pos
))) {
281 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
283 if (skipUpdateHeader(e
->id
))
286 if (e
->id
!= Http::HdrType::OTHER
)
292 pos
= HttpHeaderInitPos
;
293 while ((e
= fresh
->getEntry(&pos
))) {
294 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
296 if (skipUpdateHeader(e
->id
))
299 debugs(55, 7, "Updating header '" << Http::HeaderLookupTable
.lookup(e
->id
).name
<< "' in cached entry");
301 addEntry(e
->clone());
306 HttpHeader::Isolate(const char **parse_start
, size_t l
, const char **blk_start
, const char **blk_end
)
309 * parse_start points to the first line of HTTP message *headers*,
310 * not including the request or status lines
312 const size_t end
= headersEnd(*parse_start
, l
);
315 *blk_start
= *parse_start
;
316 *blk_end
= *parse_start
+ end
- 1;
317 assert(**blk_end
== '\n');
318 // Point blk_end to the first character after the last header field.
319 // In other words, blk_end should point to the CR?LF header terminator.
320 if (end
> 1 && *(*blk_end
- 1) == '\r')
328 HttpHeader::parse(const char *buf
, size_t buf_len
, bool atEnd
, size_t &hdr_sz
, Http::ContentLengthInterpreter
&clen
)
330 const char *parse_start
= buf
;
331 const char *blk_start
, *blk_end
;
334 if (!Isolate(&parse_start
, buf_len
, &blk_start
, &blk_end
)) {
335 // XXX: do not parse non-isolated headers even if the connection is closed.
336 // Treat unterminated headers as "partial headers" framing errors.
339 blk_start
= parse_start
;
340 blk_end
= blk_start
+ strlen(blk_start
);
343 if (parse(blk_start
, blk_end
- blk_start
, clen
)) {
344 hdr_sz
= parse_start
- buf
;
350 // XXX: callers treat this return as boolean.
351 // XXX: A better mechanism is needed to signal different types of error.
352 // lexicon, syntax, semantics, validation, access policy - are all (ab)using 'return 0'
354 HttpHeader::parse(const char *header_start
, size_t hdrLen
, Http::ContentLengthInterpreter
&clen
)
356 const char *field_ptr
= header_start
;
357 const char *header_end
= header_start
+ hdrLen
; // XXX: remove
358 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
360 assert(header_start
&& header_end
);
361 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, hdrLen
));
362 ++ HttpHeaderStats
[owner
].parsedCount
;
365 if ((nulpos
= (char*)memchr(header_start
, '\0', hdrLen
))) {
366 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
367 getStringPrefix(header_start
, nulpos
-header_start
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, hdrLen
-(nulpos
-header_start
)-1));
372 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
373 * continuation lines start with a (single) space or tab */
374 while (field_ptr
< header_end
) {
375 const char *field_start
= field_ptr
;
376 const char *field_end
;
378 const char *hasBareCr
= nullptr;
381 const char *this_line
= field_ptr
;
382 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
391 field_end
= field_ptr
;
393 ++field_ptr
; /* Move to next line */
395 if (field_end
> this_line
&& field_end
[-1] == '\r') {
396 --field_end
; /* Ignore CR LF */
398 if (owner
== hoRequest
&& field_end
> this_line
) {
400 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
405 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
406 "header field to prevent request smuggling attacks: {" <<
407 getStringPrefix(header_start
, hdrLen
) << "}");
414 /* Barf on stray CR characters */
415 if (memchr(this_line
, '\r', field_end
- this_line
)) {
416 hasBareCr
= "bare CR";
417 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
418 getStringPrefix(field_start
, field_end
-field_start
) << "}");
420 if (Config
.onoff
.relaxed_header_parser
) {
421 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
423 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != nullptr) {
433 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
434 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
435 getStringPrefix(header_start
, hdrLen
) << "}");
439 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
441 if (field_start
== field_end
) {
442 if (field_ptr
< header_end
) {
443 debugs(55, warnOnError
, "WARNING: unparsable HTTP header field near {" <<
444 getStringPrefix(field_start
, hdrLen
-(field_start
-header_start
)) << "}");
449 break; /* terminating blank line */
452 const auto e
= HttpHeaderEntry::parse(field_start
, field_end
, owner
);
454 debugs(55, warnOnError
, "WARNING: unparsable HTTP header field {" <<
455 getStringPrefix(field_start
, field_end
-field_start
) << "}");
456 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, hdrLen
) << "}");
462 if (lines
> 1 || hasBareCr
) {
463 const auto framingHeader
= (e
->id
== Http::HdrType::CONTENT_LENGTH
|| e
->id
== Http::HdrType::TRANSFER_ENCODING
);
465 if (!hasBareCr
) // already warned about bare CRs
466 debugs(55, warnOnError
, "WARNING: obs-fold in framing-sensitive " << e
->name
<< ": " << e
->value
);
473 if (e
->id
== Http::HdrType::CONTENT_LENGTH
&& !clen
.checkField(e
->value
)) {
476 if (Config
.onoff
.relaxed_header_parser
)
477 continue; // clen has printed any necessary warnings
486 if (clen
.headerWideProblem
) {
487 debugs(55, warnOnError
, "WARNING: " << clen
.headerWideProblem
<<
488 " Content-Length field values in" <<
489 Raw("header", header_start
, hdrLen
));
493 if (clen
.prohibitedAndIgnored()) {
494 // prohibitedAndIgnored() includes trailer header blocks
495 // being parsed as a case to forbid/ignore these headers.
497 // RFC 7230 section 3.3.2: A server MUST NOT send a Content-Length
498 // header field in any response with a status code of 1xx (Informational)
499 // or 204 (No Content). And RFC 7230 3.3.3#1 tells recipients to ignore
500 // such Content-Lengths.
501 if (delById(Http::HdrType::CONTENT_LENGTH
))
502 debugs(55, 3, "Content-Length is " << clen
.prohibitedAndIgnored());
504 // The same RFC 7230 3.3.3#1-based logic applies to Transfer-Encoding
505 // banned by RFC 7230 section 3.3.1.
506 if (delById(Http::HdrType::TRANSFER_ENCODING
))
507 debugs(55, 3, "Transfer-Encoding is " << clen
.prohibitedAndIgnored());
509 } else if (getByIdIfPresent(Http::HdrType::TRANSFER_ENCODING
, &rawTe
)) {
510 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
511 // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length
512 delById(Http::HdrType::CONTENT_LENGTH
);
513 // and clen state becomes irrelevant
515 if (rawTe
.caseCmp("chunked") == 0) {
516 ; // leave header present for chunked() method
517 } else if (rawTe
.caseCmp("identity") == 0) { // deprecated. no coding
518 delById(Http::HdrType::TRANSFER_ENCODING
);
520 // This also rejects multiple encodings until we support them properly.
521 debugs(55, warnOnError
, "WARNING: unsupported Transfer-Encoding used by client: " << rawTe
);
522 teUnsupported_
= true;
525 } else if (clen
.sawBad
) {
526 // ensure our callers do not accidentally see bad Content-Length values
527 delById(Http::HdrType::CONTENT_LENGTH
);
528 conflictingContentLength_
= true; // TODO: Rename to badContentLength_.
529 } else if (clen
.needsSanitizing
) {
530 // RFC 7230 section 3.3.2: MUST either reject or ... [sanitize];
531 // ensure our callers see a clean Content-Length value or none at all
532 delById(Http::HdrType::CONTENT_LENGTH
);
534 putInt64(Http::HdrType::CONTENT_LENGTH
, clen
.value
);
535 debugs(55, 5, "sanitized Content-Length to be " << clen
.value
);
539 return 1; /* even if no fields where found, it is a valid header */
542 /* packs all the entries using supplied packer */
544 HttpHeader::packInto(Packable
* p
, bool mask_sensitive_info
) const
546 HttpHeaderPos pos
= HttpHeaderInitPos
;
547 const HttpHeaderEntry
*e
;
549 debugs(55, 7, this << " into " << p
<<
550 (mask_sensitive_info
? " while masking" : ""));
551 /* pack all entries one by one */
552 while ((e
= getEntry(&pos
))) {
553 if (!mask_sensitive_info
) {
558 bool maskThisEntry
= false;
560 case Http::HdrType::AUTHORIZATION
:
561 case Http::HdrType::PROXY_AUTHORIZATION
:
562 maskThisEntry
= true;
565 case Http::HdrType::FTP_ARGUMENTS
:
566 if (const HttpHeaderEntry
*cmd
= findEntry(Http::HdrType::FTP_COMMAND
))
567 maskThisEntry
= (cmd
->value
== "PASS");
574 p
->append(e
->name
.rawContent(), e
->name
.length());
575 p
->append(": ** NOT DISPLAYED **\r\n", 23);
581 /* Pack in the "special" entries */
586 /* returns next valid entry */
588 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
591 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
593 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
595 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
602 * returns a pointer to a specified entry if any
603 * note that we return one entry so it does not make much sense to ask for
607 HttpHeader::findEntry(Http::HdrType id
) const
609 assert(any_registered_header(id
));
610 assert(!Http::HeaderLookupTable
.lookup(id
).list
);
612 /* check mask first */
614 if (!CBIT_TEST(mask
, id
))
617 /* looks like we must have it, do linear search */
618 for (auto e
: entries
) {
619 if (e
&& e
->id
== id
)
623 /* hm.. we thought it was there, but it was not found */
625 return nullptr; /* not reached */
629 * same as httpHeaderFindEntry
632 HttpHeader::findLastEntry(Http::HdrType id
) const
634 assert(any_registered_header(id
));
635 assert(!Http::HeaderLookupTable
.lookup(id
).list
);
637 /* check mask first */
638 if (!CBIT_TEST(mask
, id
))
641 for (auto e
= entries
.rbegin(); e
!= entries
.rend(); ++e
) {
642 if (*e
&& (*e
)->id
== id
)
646 /* hm.. we thought it was there, but it was not found */
648 return nullptr; /* not reached */
652 HttpHeader::delByName(const SBuf
&name
)
655 HttpHeaderPos pos
= HttpHeaderInitPos
;
656 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
657 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
659 while (const HttpHeaderEntry
*e
= getEntry(&pos
)) {
660 if (!e
->name
.caseCmp(name
))
663 CBIT_SET(mask
, e
->id
);
669 /* deletes all entries with a given id, returns the #entries deleted */
671 HttpHeader::delById(Http::HdrType id
)
673 debugs(55, 8, this << " del-by-id " << id
);
674 assert(any_registered_header(id
));
676 if (!CBIT_TEST(mask
, id
))
681 HttpHeaderPos pos
= HttpHeaderInitPos
;
682 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
684 delAt(pos
, count
); // deletes e
693 * deletes an entry at pos and leaves a gap; leaving a gap makes it
694 * possible to iterate(search) and delete fields at the same time
695 * NOTE: Does not update the header mask. Caller must follow up with
696 * a call to refreshMask() if headers_deleted was incremented.
699 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
702 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
703 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
704 entries
[pos
] = nullptr;
705 /* decrement header length, allow for ": " and crlf */
706 len
-= e
->name
.length() + 2 + e
->value
.size() + 2;
713 * Compacts the header storage
716 HttpHeader::compact()
718 // TODO: optimize removal, or possibly make it so that's not needed.
719 entries
.erase( std::remove(entries
.begin(), entries
.end(), nullptr),
724 * Refreshes the header mask. Required after delAt() calls.
727 HttpHeader::refreshMask()
729 httpHeaderMaskInit(&mask
, 0);
730 debugs(55, 7, "refreshing the mask in hdr " << this);
731 for (auto e
: entries
) {
733 CBIT_SET(mask
, e
->id
);
738 * does not call e->clone() so one should not reuse "*e"
741 HttpHeader::addEntry(HttpHeaderEntry
* e
)
744 assert(any_HdrType_enum_value(e
->id
));
745 assert(e
->name
.length());
747 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
749 if (e
->id
!= Http::HdrType::BAD_HDR
) {
750 if (CBIT_TEST(mask
, e
->id
)) {
751 ++ headerStatsTable
[e
->id
].repCount
;
753 CBIT_SET(mask
, e
->id
);
757 entries
.push_back(e
);
759 /* increment header length, allow for ": " and crlf */
760 len
+= e
->name
.length() + 2 + e
->value
.size() + 2;
764 * does not call e->clone() so one should not reuse "*e"
767 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
770 assert(any_valid_header(e
->id
));
772 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
774 // Http::HdrType::BAD_HDR is filtered out by assert_any_valid_header
775 if (CBIT_TEST(mask
, e
->id
)) {
776 ++ headerStatsTable
[e
->id
].repCount
;
778 CBIT_SET(mask
, e
->id
);
781 entries
.insert(entries
.begin(),e
);
783 /* increment header length, allow for ": " and crlf */
784 len
+= e
->name
.length() + 2 + e
->value
.size() + 2;
788 HttpHeader::getList(Http::HdrType id
, String
*s
) const
790 debugs(55, 9, this << " joining for id " << id
);
791 /* only fields from ListHeaders array can be "listed" */
792 assert(Http::HeaderLookupTable
.lookup(id
).list
);
794 if (!CBIT_TEST(mask
, id
))
797 for (auto e
: entries
) {
798 if (e
&& e
->id
== id
)
799 strListAdd(s
, e
->value
.termedBuf(), ',');
803 * note: we might get an empty (size==0) string if there was an "empty"
804 * header. This results in an empty length String, which may have a NULL
807 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
809 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable
.lookup(id
).name
<< "(" << id
<< ")");
811 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
816 /* return a list of entries with the same id separated by ',' and ws */
818 HttpHeader::getList(Http::HdrType id
) const
821 HttpHeaderPos pos
= HttpHeaderInitPos
;
822 debugs(55, 9, this << "joining for id " << id
);
823 /* only fields from ListHeaders array can be "listed" */
824 assert(Http::HeaderLookupTable
.lookup(id
).list
);
826 if (!CBIT_TEST(mask
, id
))
831 while ((e
= getEntry(&pos
))) {
833 strListAdd(&s
, e
->value
.termedBuf(), ',');
837 * note: we might get an empty (size==0) string if there was an "empty"
838 * header. This results in an empty length String, which may have a NULL
841 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
843 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable
.lookup(id
).name
<< "(" << id
<< ")");
845 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
850 /* return a string or list of entries with the same id separated by ',' and ws */
852 HttpHeader::getStrOrList(Http::HdrType id
) const
856 if (Http::HeaderLookupTable
.lookup(id
).list
)
859 if ((e
= findEntry(id
)))
866 * Returns the value of the specified header and/or an undefined String.
869 HttpHeader::getByName(const char *name
) const
872 // ignore presence: return undefined string if an empty header is present
873 (void)hasNamed(name
, strlen(name
), &result
);
878 HttpHeader::getByName(const SBuf
&name
) const
881 // ignore presence: return undefined string if an empty header is present
882 (void)hasNamed(name
, &result
);
887 HttpHeader::getById(Http::HdrType id
) const
890 (void)getByIdIfPresent(id
, &result
);
895 HttpHeader::hasNamed(const SBuf
&s
, String
*result
) const
897 return hasNamed(s
.rawContent(), s
.length(), result
);
901 HttpHeader::getByIdIfPresent(Http::HdrType id
, String
*result
) const
903 if (id
== Http::HdrType::BAD_HDR
)
908 *result
= getStrOrList(id
);
913 HttpHeader::hasNamed(const char *name
, unsigned int namelen
, String
*result
) const
916 HttpHeaderPos pos
= HttpHeaderInitPos
;
921 /* First try the quick path */
922 id
= Http::HeaderLookupTable
.lookup(name
,namelen
).id
;
924 if (id
!= Http::HdrType::BAD_HDR
) {
925 if (getByIdIfPresent(id
, result
))
929 /* Sorry, an unknown header name. Do linear search */
931 while ((e
= getEntry(&pos
))) {
932 if (e
->id
== Http::HdrType::OTHER
&& e
->name
.length() == namelen
&& e
->name
.caseCmp(name
, namelen
) == 0) {
936 strListAdd(result
, e
->value
.termedBuf(), ',');
944 * Returns a the value of the specified list member, if any.
947 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
950 const auto header
= getByName(name
);
951 return ::getListMember(header
, member
, separator
);
955 * returns a the value of the specified list member, if any.
958 HttpHeader::getListMember(Http::HdrType id
, const char *member
, const char separator
) const
960 assert(any_registered_header(id
));
961 const auto header
= getStrOrList(id
);
962 return ::getListMember(header
, member
, separator
);
965 /* test if a field is present */
967 HttpHeader::has(Http::HdrType id
) const
969 assert(any_registered_header(id
));
970 debugs(55, 9, this << " lookup for " << id
);
971 return CBIT_TEST(mask
, id
);
975 HttpHeader::addVia(const AnyP::ProtocolVersion
&ver
, const HttpHeader
*from
)
977 // TODO: do not add Via header for messages where Squid itself
978 // generated the message (i.e., Downloader or ESI) there should be no Via header added at all.
980 if (Config
.onoff
.via
) {
982 // RFC 7230 section 5.7.1.: protocol-name is omitted when
983 // the received protocol is HTTP.
984 if (ver
.protocol
> AnyP::PROTO_NONE
&& ver
.protocol
< AnyP::PROTO_UNKNOWN
&&
985 ver
.protocol
!= AnyP::PROTO_HTTP
&& ver
.protocol
!= AnyP::PROTO_HTTPS
)
986 buf
.appendf("%s/", AnyP::ProtocolType_str
[ver
.protocol
]);
987 buf
.appendf("%d.%d %s", ver
.major
, ver
.minor
, ThisCache
);
988 const HttpHeader
*hdr
= from
? from
: this;
989 SBuf strVia
= StringToSBuf(hdr
->getList(Http::HdrType::VIA
));
990 if (!strVia
.isEmpty())
991 strVia
.append(", ", 2);
993 // XXX: putStr() still suffers from String size limits
994 Must(strVia
.length() < String::SizeMaxXXX());
995 delById(Http::HdrType::VIA
);
996 putStr(Http::HdrType::VIA
, strVia
.c_str());
1001 HttpHeader::putInt(Http::HdrType id
, int number
)
1003 assert(any_registered_header(id
));
1004 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
1005 assert(number
>= 0);
1006 addEntry(new HttpHeaderEntry(id
, SBuf(), xitoa(number
)));
1010 HttpHeader::putInt64(Http::HdrType id
, int64_t number
)
1012 assert(any_registered_header(id
));
1013 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
1014 assert(number
>= 0);
1015 addEntry(new HttpHeaderEntry(id
, SBuf(), xint64toa(number
)));
1019 HttpHeader::putTime(Http::HdrType id
, time_t htime
)
1021 assert(any_registered_header(id
));
1022 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1024 addEntry(new HttpHeaderEntry(id
, SBuf(), Time::FormatRfc1123(htime
)));
1028 HttpHeader::putStr(Http::HdrType id
, const char *str
)
1030 assert(any_registered_header(id
));
1031 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1033 addEntry(new HttpHeaderEntry(id
, SBuf(), str
));
1037 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1039 assert(auth_scheme
&& realm
);
1040 httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1044 HttpHeader::putCc(const HttpHdrCc
* cc
)
1047 /* remove old directives if any */
1048 delById(Http::HdrType::CACHE_CONTROL
);
1054 addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL
, SBuf(), mb
.buf
));
1060 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1063 /* remove old directives if any */
1064 delById(Http::HdrType::CONTENT_RANGE
);
1068 httpHdrContRangePackInto(cr
, &mb
);
1070 addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE
, SBuf(), mb
.buf
));
1076 HttpHeader::putRange(const HttpHdrRange
* range
)
1079 /* remove old directives if any */
1080 delById(Http::HdrType::RANGE
);
1084 range
->packInto(&mb
);
1086 addEntry(new HttpHeaderEntry(Http::HdrType::RANGE
, SBuf(), mb
.buf
));
1092 HttpHeader::putSc(HttpHdrSc
*sc
)
1095 /* remove old directives if any */
1096 delById(Http::HdrType::SURROGATE_CONTROL
);
1102 addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL
, SBuf(), mb
.buf
));
1107 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1109 HttpHeader::putExt(const char *name
, const char *value
)
1111 assert(name
&& value
);
1112 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1113 addEntry(new HttpHeaderEntry(Http::HdrType::OTHER
, SBuf(name
), value
));
1117 HttpHeader::getInt(Http::HdrType id
) const
1119 assert(any_registered_header(id
));
1120 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
1123 if ((e
= findEntry(id
)))
1130 HttpHeader::getInt64(Http::HdrType id
) const
1132 assert(any_registered_header(id
));
1133 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
1136 if ((e
= findEntry(id
)))
1137 return e
->getInt64();
1143 HttpHeader::getTime(Http::HdrType id
) const
1147 assert(any_registered_header(id
));
1148 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1150 if ((e
= findEntry(id
))) {
1151 value
= Time::ParseRfc1123(e
->value
.termedBuf());
1152 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1158 /* sync with httpHeaderGetLastStr */
1160 HttpHeader::getStr(Http::HdrType id
) const
1163 assert(any_registered_header(id
));
1164 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1166 if ((e
= findEntry(id
))) {
1167 httpHeaderNoteParsedEntry(e
->id
, e
->value
, false); /* no errors are possible */
1168 return e
->value
.termedBuf();
1176 HttpHeader::getLastStr(Http::HdrType id
) const
1179 assert(any_registered_header(id
));
1180 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1182 if ((e
= findLastEntry(id
))) {
1183 httpHeaderNoteParsedEntry(e
->id
, e
->value
, false); /* no errors are possible */
1184 return e
->value
.termedBuf();
1191 HttpHeader::getCc() const
1193 if (!CBIT_TEST(mask
, Http::HdrType::CACHE_CONTROL
))
1197 getList(Http::HdrType::CACHE_CONTROL
, &s
);
1199 HttpHdrCc
*cc
=new HttpHdrCc();
1201 if (!cc
->parse(s
)) {
1206 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1209 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1211 httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL
, s
, !cc
);
1217 HttpHeader::getRange() const
1219 HttpHdrRange
*r
= nullptr;
1221 /* some clients will send "Request-Range" _and_ *matching* "Range"
1222 * who knows, some clients might send Request-Range only;
1223 * this "if" should work correctly in both cases;
1224 * hopefully no clients send mismatched headers! */
1226 if ((e
= findEntry(Http::HdrType::RANGE
)) ||
1227 (e
= findEntry(Http::HdrType::REQUEST_RANGE
))) {
1228 r
= HttpHdrRange::ParseCreate(&e
->value
);
1229 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1236 HttpHeader::getSc() const
1238 if (!CBIT_TEST(mask
, Http::HdrType::SURROGATE_CONTROL
))
1243 (void) getList(Http::HdrType::SURROGATE_CONTROL
, &s
);
1245 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1247 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1250 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1252 httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL
, s
, !sc
);
1258 HttpHeader::getContRange() const
1260 HttpHdrContRange
*cr
= nullptr;
1263 if ((e
= findEntry(Http::HdrType::CONTENT_RANGE
))) {
1264 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1265 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1272 HttpHeader::getAuthToken(Http::HdrType id
, const char *auth_scheme
) const
1276 assert(auth_scheme
);
1279 static const SBuf nil
;
1280 if (!field
) /* no authorization field */
1283 l
= strlen(auth_scheme
);
1285 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1290 if (!xisspace(*field
)) /* wrong scheme */
1293 /* skip white space */
1294 for (; field
&& xisspace(*field
); ++field
);
1296 if (!*field
) /* no authorization cookie */
1299 const auto fieldLen
= strlen(field
);
1301 char *decodedAuthToken
= result
.rawAppendStart(BASE64_DECODE_LENGTH(fieldLen
));
1302 struct base64_decode_ctx ctx
;
1303 base64_decode_init(&ctx
);
1304 size_t decodedLen
= 0;
1305 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(decodedAuthToken
), fieldLen
, field
) ||
1306 !base64_decode_final(&ctx
)) {
1309 result
.rawAppendFinish(decodedAuthToken
, decodedLen
);
1314 HttpHeader::getETag(Http::HdrType id
) const
1316 ETag etag
= {nullptr, -1};
1318 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftETag
); /* must be of an appropriate type */
1320 if ((e
= findEntry(id
)))
1321 etagParseInit(&etag
, e
->value
.termedBuf());
1327 HttpHeader::getTimeOrTag(Http::HdrType id
) const
1331 assert(Http::HeaderLookupTable
.lookup(id
).type
== Http::HdrFieldType::ftDate_1123_or_ETag
); /* must be of an appropriate type */
1332 memset(&tot
, 0, sizeof(tot
));
1334 if ((e
= findEntry(id
))) {
1335 const char *str
= e
->value
.termedBuf();
1336 /* try as an ETag */
1338 if (etagParseInit(&tot
.tag
, str
)) {
1339 tot
.valid
= tot
.tag
.str
!= nullptr;
1342 /* or maybe it is time? */
1343 tot
.time
= Time::ParseRfc1123(str
);
1344 tot
.valid
= tot
.time
>= 0;
1345 tot
.tag
.str
= nullptr;
1349 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1357 HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId
, const SBuf
&aName
, const char *aValue
)
1359 assert(any_HdrType_enum_value(anId
));
1362 if (id
!= Http::HdrType::OTHER
)
1363 name
= Http::HeaderLookupTable
.lookup(id
).name
;
1369 if (id
!= Http::HdrType::BAD_HDR
)
1370 ++ headerStatsTable
[id
].aliveCount
;
1372 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1375 HttpHeaderEntry::~HttpHeaderEntry()
1377 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1379 if (id
!= Http::HdrType::BAD_HDR
) {
1380 assert(headerStatsTable
[id
].aliveCount
);
1381 -- headerStatsTable
[id
].aliveCount
;
1382 id
= Http::HdrType::BAD_HDR
; // it already is BAD_HDR, no sense in resetting it
1387 /* parses and inits header entry, returns true/false */
1389 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
, const http_hdr_owner_type msgType
)
1391 /* note: name_start == field_start */
1392 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1393 int name_len
= name_end
? name_end
- field_start
:0;
1394 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1395 /* note: value_end == field_end */
1397 ++ HeaderEntryParsedCount
;
1399 /* do we have a valid field name within this field? */
1401 if (!name_len
|| name_end
> field_end
)
1404 if (name_len
> 65534) {
1405 /* String must be LESS THAN 64K and it adds a terminating NULL */
1406 // TODO: update this to show proper name_len in Raw markup, but not print all that
1407 debugs(55, 2, "ignoring huge header field (" << Raw("field_start", field_start
, 100) << "...)");
1412 * RFC 7230 section 3.2.4:
1413 * "No whitespace is allowed between the header field-name and colon.
1415 * A server MUST reject any received request message that contains
1416 * whitespace between a header field-name and colon with a response code
1417 * of 400 (Bad Request). A proxy MUST remove any such whitespace from a
1418 * response message before forwarding the message downstream."
1420 if (xisspace(field_start
[name_len
- 1])) {
1422 if (msgType
== hoRequest
)
1425 // for now, also let relaxed parser remove this BWS from any non-HTTP messages
1426 const bool stripWhitespace
= (msgType
== hoReply
) ||
1427 Config
.onoff
.relaxed_header_parser
;
1428 if (!stripWhitespace
)
1429 return nullptr; // reject if we cannot strip
1431 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1432 "WARNING: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1434 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1438 debugs(55, 2, "found header with only whitespace for name");
1443 /* RFC 7230 section 3.2:
1445 * header-field = field-name ":" OWS field-value OWS
1446 * field-name = token
1449 for (const char *pos
= field_start
; pos
< (field_start
+name_len
); ++pos
) {
1450 if (!CharacterSet::TCHAR
[*pos
]) {
1451 debugs(55, 2, "found header with invalid characters in " <<
1452 Raw("field-name", field_start
, min(name_len
,100)) << "...");
1457 /* now we know we can parse it */
1459 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1461 /* is it a "known" field? */
1462 Http::HdrType id
= Http::HeaderLookupTable
.lookup(field_start
,name_len
).id
;
1463 debugs(55, 9, "got hdr-id=" << id
);
1469 if (id
== Http::HdrType::BAD_HDR
)
1470 id
= Http::HdrType::OTHER
;
1472 /* set field name */
1473 if (id
== Http::HdrType::OTHER
)
1474 theName
.append(field_start
, name_len
);
1476 theName
= Http::HeaderLookupTable
.lookup(id
).name
;
1478 /* trim field value */
1479 while (value_start
< field_end
&& xisspace(*value_start
))
1482 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1485 if (field_end
- value_start
> 65534) {
1486 /* String must be LESS THAN 64K and it adds a terminating NULL */
1487 debugs(55, 2, "WARNING: found '" << theName
<< "' header of " << (field_end
- value_start
) << " bytes");
1491 /* set field value */
1492 value
.assign(value_start
, field_end
- value_start
);
1494 if (id
!= Http::HdrType::BAD_HDR
)
1495 ++ headerStatsTable
[id
].seenCount
;
1497 debugs(55, 9, "parsed HttpHeaderEntry: '" << theName
<< ": " << value
<< "'");
1499 return new HttpHeaderEntry(id
, theName
, value
.termedBuf());
1503 HttpHeaderEntry::clone() const
1505 return new HttpHeaderEntry(id
, name
, value
.termedBuf());
1509 HttpHeaderEntry::packInto(Packable
* p
) const
1512 p
->append(name
.rawContent(), name
.length());
1514 p
->append(value
.rawBuf(), value
.size());
1515 p
->append("\r\n", 2);
1519 HttpHeaderEntry::getInt() const
1522 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1523 httpHeaderNoteParsedEntry(id
, value
, ok
== 0);
1524 /* XXX: Should we check ok - ie
1525 * return ok ? -1 : value;
1531 HttpHeaderEntry::getInt64() const
1534 const bool ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1535 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1536 return val
; // remains -1 if !ok (XXX: bad method API)
1540 httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &context
, bool error
)
1542 if (id
!= Http::HdrType::BAD_HDR
)
1543 ++ headerStatsTable
[id
].parsCount
;
1546 if (id
!= Http::HdrType::BAD_HDR
)
1547 ++ headerStatsTable
[id
].errCount
;
1548 debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderLookupTable
.lookup(id
).name
<< ": " << context
<< "'");
1556 /* tmp variable used to pass stat info to dumpers */
1557 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1558 const HttpHeaderStat
*dump_stat
= nullptr;
1561 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
1563 const int id
= static_cast<int>(val
);
1564 const bool valid_id
= Http::any_valid_header(static_cast<Http::HdrType
>(id
));
1565 const char *name
= valid_id
? Http::HeaderLookupTable
.lookup(static_cast<Http::HdrType
>(id
)).name
: "INVALID";
1566 int visible
= count
> 0;
1567 /* for entries with zero count, list only those that belong to current type of message */
1569 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1570 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1573 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1574 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1578 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double, int count
)
1581 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1582 idx
, (int) val
, count
,
1583 xpercent(count
, dump_stat
->destroyedCount
));
1587 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1592 if (!hs
->owner_mask
)
1593 return; // these HttpHeaderStat objects were not meant to be dumped here
1596 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1597 storeAppendPrintf(e
, "\nField type distribution\n");
1598 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1599 "id", "name", "count", "#/header");
1600 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1601 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1602 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1603 "id", "name", "count", "#/cc_field");
1604 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1605 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1606 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1607 "id", "name", "count", "#/sc_field");
1608 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1609 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1610 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1611 "id", "#flds", "count", "%total");
1612 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1613 storeAppendPrintf(e
, "\n");
1614 dump_stat
= nullptr;
1618 httpHeaderStoreReport(StoreEntry
* e
)
1622 HttpHeaderStats
[0].parsedCount
=
1623 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1624 HttpHeaderStats
[0].ccParsedCount
=
1625 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1626 HttpHeaderStats
[0].destroyedCount
=
1627 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1628 HttpHeaderStats
[0].busyDestroyedCount
=
1629 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1631 for (const auto &stats
: HttpHeaderStats
)
1632 httpHeaderStatDump(&stats
, e
);
1634 /* field stats for all messages */
1635 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1637 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1638 "id", "name", "#alive", "%err", "%repeat");
1640 // scan heaaderTable and output
1641 for (auto h
: WholeEnum
<Http::HdrType
>()) {
1642 auto stats
= headerStatsTable
[h
];
1643 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1644 Http::HeaderLookupTable
.lookup(h
).id
,
1645 Http::HeaderLookupTable
.lookup(h
).name
,
1647 xpercent(stats
.errCount
, stats
.parsCount
),
1648 xpercent(stats
.repCount
, stats
.seenCount
));
1651 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1652 HttpHeaderStats
[hoRequest
].parsedCount
,
1653 HttpHeaderStats
[hoReply
].parsedCount
,
1654 HttpHeaderStats
[0].parsedCount
);
1655 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1659 HttpHeader::hasListMember(Http::HdrType id
, const char *member
, const char separator
) const
1662 const char *pos
= nullptr;
1665 int mlen
= strlen(member
);
1667 assert(any_registered_header(id
));
1669 String
header (getStrOrList(id
));
1671 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1672 if (strncasecmp(item
, member
, mlen
) == 0
1673 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1683 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1686 const char *pos
= nullptr;
1689 int mlen
= strlen(member
);
1693 String
header (getByName(name
));
1695 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1696 if (strncasecmp(item
, member
, mlen
) == 0
1697 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1707 HttpHeader::removeHopByHopEntries()
1709 removeConnectionHeaderEntries();
1711 const HttpHeaderEntry
*e
;
1712 HttpHeaderPos pos
= HttpHeaderInitPos
;
1713 int headers_deleted
= 0;
1714 while ((e
= getEntry(&pos
))) {
1715 Http::HdrType id
= e
->id
;
1716 if (Http::HeaderLookupTable
.lookup(id
).hopbyhop
) {
1717 delAt(pos
, headers_deleted
);
1724 HttpHeader::removeConnectionHeaderEntries()
1726 if (has(Http::HdrType::CONNECTION
)) {
1727 /* anything that matches Connection list member will be deleted */
1728 String strConnection
;
1730 (void) getList(Http::HdrType::CONNECTION
, &strConnection
);
1731 const HttpHeaderEntry
*e
;
1732 HttpHeaderPos pos
= HttpHeaderInitPos
;
1734 * think: on-average-best nesting of the two loops (hdrEntry
1735 * and strListItem) @?@
1738 * maybe we should delete standard stuff ("keep-alive","close")
1739 * from strConnection first?
1742 int headers_deleted
= 0;
1743 while ((e
= getEntry(&pos
))) {
1744 if (strListIsMember(&strConnection
, e
->name
, ','))
1745 delAt(pos
, headers_deleted
);
1747 if (headers_deleted
)