2 * Copyright (C) 1996-2015 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/LookupTable.h" // pulled by HttpHdrCc.h
15 #include "HttpHdrCc.h"
16 #include "HttpHdrContRange.h"
17 #include "HttpHdrScTarget.h" // also includes HttpHdrSc.h
18 #include "HttpHeader.h"
19 #include "HttpHeaderFieldInfo.h"
20 #include "HttpHeaderStat.h"
21 #include "HttpHeaderTools.h"
23 #include "mgr/Registration.h"
24 #include "profiler/Profiler.h"
26 #include "SquidConfig.h"
27 //#include "SquidString.h" // pulled by HttpHdrCc.h
31 #include "TimeOrTag.h"
36 /* XXX: the whole set of API managing the entries vector should be rethought
37 * after the parse4r-ng effort is complete.
41 * On naming conventions:
43 * HTTP/1.1 defines message-header as
45 * message-header = field-name ":" [ field-value ] CRLF
47 * field-value = *( field-content | LWS )
49 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
50 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
52 * HttpHeader is an object that represents all message-headers in a message.
53 * HttpHeader does not manage start-line.
55 * HttpHeader is implemented as a collection of header "entries".
56 * An entry is a (field_id, field_name, field_value) triplet.
60 * local constants and vars
63 // statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted
64 std::vector
<HttpHeaderFieldStat
> headerStatsTable(Http::HdrType::ENUM_END
);
67 * headers with field values defined as #(values) in HTTP/1.1
68 * Headers that are currently not recognized, are commented out.
70 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
71 static Http::HdrType ListHeadersArr
[] = {
72 Http::HdrType::ACCEPT
,
73 Http::HdrType::ACCEPT_CHARSET
,
74 Http::HdrType::ACCEPT_ENCODING
,
75 Http::HdrType::ACCEPT_LANGUAGE
,
76 Http::HdrType::ACCEPT_RANGES
,
78 Http::HdrType::CACHE_CONTROL
,
79 Http::HdrType::CONTENT_ENCODING
,
80 Http::HdrType::CONTENT_LANGUAGE
,
81 Http::HdrType::CONNECTION
,
82 Http::HdrType::EXPECT
,
83 Http::HdrType::IF_MATCH
,
84 Http::HdrType::IF_NONE_MATCH
,
87 Http::HdrType::PRAGMA
,
88 Http::HdrType::PROXY_CONNECTION
,
89 Http::HdrType::PROXY_SUPPORT
,
90 Http::HdrType::TRANSFER_ENCODING
,
91 Http::HdrType::UPGRADE
,
94 Http::HdrType::WARNING
,
95 Http::HdrType::WWW_AUTHENTICATE
,
96 Http::HdrType::AUTHENTICATION_INFO
,
97 Http::HdrType::PROXY_AUTHENTICATION_INFO
,
98 /* Http::HdrType::TE, Http::HdrType::TRAILER */
99 #if X_ACCELERATOR_VARY
100 Http::HdrType::HDR_X_ACCELERATOR_VARY
,
103 Http::HdrType::X_NEXT_SERVICES
,
105 Http::HdrType::SURROGATE_CAPABILITY
,
106 Http::HdrType::SURROGATE_CONTROL
,
107 Http::HdrType::FORWARDED
,
108 Http::HdrType::X_FORWARDED_FOR
111 /* general-headers */
112 static Http::HdrType GeneralHeadersArr
[] = {
113 Http::HdrType::CACHE_CONTROL
,
114 Http::HdrType::CONNECTION
,
116 Http::HdrType::FORWARDED
,
117 Http::HdrType::X_FORWARDED_FOR
,
118 Http::HdrType::MIME_VERSION
,
119 Http::HdrType::PRAGMA
,
120 Http::HdrType::PROXY_CONNECTION
,
121 Http::HdrType::TRANSFER_ENCODING
,
122 Http::HdrType::UPGRADE
,
123 /* Http::HdrType::TRAILER, */
128 static Http::HdrType EntityHeadersArr
[] = {
129 Http::HdrType::ALLOW
,
130 Http::HdrType::CONTENT_BASE
,
131 Http::HdrType::CONTENT_ENCODING
,
132 Http::HdrType::CONTENT_LANGUAGE
,
133 Http::HdrType::CONTENT_LENGTH
,
134 Http::HdrType::CONTENT_LOCATION
,
135 Http::HdrType::CONTENT_MD5
,
136 Http::HdrType::CONTENT_RANGE
,
137 Http::HdrType::CONTENT_TYPE
,
139 Http::HdrType::EXPIRES
,
140 Http::HdrType::LAST_MODIFIED
,
145 /* request-only headers */
146 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
147 static Http::HdrType RequestHeadersArr
[] = {
148 Http::HdrType::ACCEPT
,
149 Http::HdrType::ACCEPT_CHARSET
,
150 Http::HdrType::ACCEPT_ENCODING
,
151 Http::HdrType::ACCEPT_LANGUAGE
,
152 Http::HdrType::AUTHORIZATION
,
153 Http::HdrType::EXPECT
,
156 Http::HdrType::HTTP2_SETTINGS
,
157 Http::HdrType::IF_MATCH
,
158 Http::HdrType::IF_MODIFIED_SINCE
,
159 Http::HdrType::IF_NONE_MATCH
,
160 Http::HdrType::IF_RANGE
,
161 Http::HdrType::IF_UNMODIFIED_SINCE
,
162 Http::HdrType::MAX_FORWARDS
,
163 Http::HdrType::ORIGIN
,
164 Http::HdrType::PROXY_AUTHORIZATION
,
165 Http::HdrType::RANGE
,
166 Http::HdrType::REFERER
,
167 Http::HdrType::REQUEST_RANGE
,
169 Http::HdrType::USER_AGENT
,
170 Http::HdrType::SURROGATE_CAPABILITY
173 /* reply-only headers */
174 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
175 static Http::HdrType ReplyHeadersArr
[] = {
176 Http::HdrType::ACCEPT_ENCODING
,
177 Http::HdrType::ACCEPT_RANGES
,
180 Http::HdrType::LOCATION
,
181 Http::HdrType::PROXY_AUTHENTICATE
,
182 Http::HdrType::PUBLIC
,
183 Http::HdrType::RETRY_AFTER
,
184 Http::HdrType::SERVER
,
185 Http::HdrType::SET_COOKIE
,
186 Http::HdrType::SET_COOKIE2
,
188 Http::HdrType::WARNING
,
189 Http::HdrType::WWW_AUTHENTICATE
,
190 Http::HdrType::X_CACHE
,
191 Http::HdrType::X_CACHE_LOOKUP
,
192 Http::HdrType::X_REQUEST_URI
,
193 #if X_ACCELERATOR_VARY
194 Http::HdrType::HDR_X_ACCELERATOR_VARY
,
197 Http::HdrType::X_NEXT_SERVICES
,
199 Http::HdrType::X_SQUID_ERROR
,
200 Http::HdrType::SURROGATE_CONTROL
203 /* hop-by-hop headers */
204 static HttpHeaderMask HopByHopHeadersMask
;
205 static Http::HdrType HopByHopHeadersArr
[] = {
206 Http::HdrType::ALTERNATE_PROTOCOL
,
207 Http::HdrType::CONNECTION
,
208 Http::HdrType::HTTP2_SETTINGS
,
209 Http::HdrType::KEEP_ALIVE
,
210 /*Http::HdrType::PROXY_AUTHENTICATE, // removal handled specially for peer login */
211 Http::HdrType::PROXY_AUTHORIZATION
,
213 Http::HdrType::TRAILER
,
214 Http::HdrType::TRANSFER_ENCODING
,
215 Http::HdrType::UPGRADE
,
216 Http::HdrType::PROXY_CONNECTION
219 /* header accounting */
220 // NP: keep in sync with enum http_hdr_owner_type
221 static HttpHeaderStat HttpHeaderStats
[] = {
222 HttpHeaderStat(/*hoNone*/ "all", NULL
),
224 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask
),
226 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask
),
227 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask
)
233 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
235 static int HeaderEntryParsedCount
= 0;
238 * forward declarations and local routines
243 static void httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &value
, int error
);
244 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
245 /** store report about current header usage and other stats */
246 static void httpHeaderStoreReport(StoreEntry
* e
);
249 * Module initialization routines
253 httpHeaderRegisterWithCacheManager(void)
255 Mgr::RegisterAction("http_headers",
256 "HTTP Header Statistics",
257 httpHeaderStoreReport
, 0, 1);
261 httpHeaderInitModule(void)
263 /* check that we have enough space for masks */
264 assert(8 * sizeof(HttpHeaderMask
) >= Http::HdrType::ENUM_END
);
266 // check invariant: for each index in headerTable, (int)headerTable[index] = index
267 for (int i
= 0; Http::HeaderTable
[i
].name
; ++i
)
268 assert(Http::HeaderTable
[i
].id
== i
);
270 /* create masks. XXX: migrate to std::vector<bool>? */
271 httpHeaderMaskInit(&ListHeadersMask
, 0);
272 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
274 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
275 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
276 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
277 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
279 httpHeaderMaskInit(&RequestHeadersMask
, 0);
280 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
281 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
282 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
284 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
285 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
287 /* header stats initialized by class constructor */
288 assert(HttpHeaderStatCount
== hoReply
+ 1);
290 /* init dependent modules */
291 httpHdrCcInitModule();
292 httpHdrScInitModule();
294 httpHeaderRegisterWithCacheManager();
298 * HttpHeader Implementation
301 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
303 httpHeaderMaskInit(&mask
, 0);
306 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
308 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
309 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
310 httpHeaderMaskInit(&mask
, 0);
313 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
315 httpHeaderMaskInit(&mask
, 0);
316 update(&other
, NULL
); // will update the mask as well
319 HttpHeader::~HttpHeader()
325 HttpHeader::operator =(const HttpHeader
&other
)
327 if (this != &other
) {
328 // we do not really care, but the caller probably does
329 assert(owner
== other
.owner
);
331 update(&other
, NULL
); // will update the mask as well
341 assert(owner
> hoNone
&& owner
< hoEnd
);
342 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
344 PROF_start(HttpHeaderClean
);
346 if (owner
<= hoReply
) {
348 * An unfortunate bug. The entries array is initialized
349 * such that count is set to zero. httpHeaderClean() seems to
350 * be called both when 'hdr' is created, and destroyed. Thus,
351 * we accumulate a large number of zero counts for 'hdr' before
352 * it is ever used. Can't think of a good way to fix it, except
353 * adding a state variable that indicates whether or not 'hdr'
354 * has been used. As a hack, just never count zero-sized header
357 if (!entries
.empty())
358 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
360 ++ HttpHeaderStats
[owner
].destroyedCount
;
362 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
363 } // if (owner <= hoReply)
365 for(HttpHeaderEntry
*e
: entries
) {
368 if (!Http::any_valid_header(e
->id
)) {
369 debugs(55, DBG_CRITICAL
, "BUG: invalid entry (" << e
->id
<< "). Ignored.");
371 if (owner
<= hoReply
)
372 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
378 httpHeaderMaskInit(&mask
, 0);
380 PROF_stop(HttpHeaderClean
);
383 /* append entries (also see httpHeaderUpdate) */
385 HttpHeader::append(const HttpHeader
* src
)
387 const HttpHeaderEntry
*e
;
388 HttpHeaderPos pos
= HttpHeaderInitPos
;
391 debugs(55, 7, "appending hdr: " << this << " += " << src
);
393 while ((e
= src
->getEntry(&pos
))) {
394 addEntry(e
->clone());
398 /* use fresh entries to replace old ones */
400 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
403 old
->update (fresh
, denied_mask
);
407 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
409 const HttpHeaderEntry
*e
;
410 HttpHeaderPos pos
= HttpHeaderInitPos
;
412 assert(this != fresh
);
414 while ((e
= fresh
->getEntry(&pos
))) {
415 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
417 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
420 if (e
->id
!= Http::HdrType::OTHER
)
423 delByName(e
->name
.termedBuf());
426 pos
= HttpHeaderInitPos
;
427 while ((e
= fresh
->getEntry(&pos
))) {
428 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
430 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
433 debugs(55, 7, "Updating header '" << Http::HeaderTable
[e
->id
].name
<< "' in cached entry");
435 addEntry(e
->clone());
439 /* just handy in parsing: resets and returns false */
448 HttpHeader::parse(const char *header_start
, size_t hdrLen
)
450 const char *field_ptr
= header_start
;
451 const char *header_end
= header_start
+ hdrLen
; // XXX: remove
452 HttpHeaderEntry
*e
, *e2
;
453 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
455 PROF_start(HttpHeaderParse
);
457 assert(header_start
&& header_end
);
458 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, hdrLen
));
459 ++ HttpHeaderStats
[owner
].parsedCount
;
462 if ((nulpos
= (char*)memchr(header_start
, '\0', hdrLen
))) {
463 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
464 getStringPrefix(header_start
, nulpos
-header_start
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, hdrLen
-(nulpos
-header_start
)-1));
465 PROF_stop(HttpHeaderParse
);
469 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
470 * continuation lines start with a (single) space or tab */
471 while (field_ptr
< header_end
) {
472 const char *field_start
= field_ptr
;
473 const char *field_end
;
476 const char *this_line
= field_ptr
;
477 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
481 PROF_stop(HttpHeaderParse
);
485 field_end
= field_ptr
;
487 ++field_ptr
; /* Move to next line */
489 if (field_end
> this_line
&& field_end
[-1] == '\r') {
490 --field_end
; /* Ignore CR LF */
492 if (owner
== hoRequest
&& field_end
> this_line
) {
494 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
499 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
500 "header field to prevent request smuggling attacks: {" <<
501 getStringPrefix(header_start
, hdrLen
) << "}");
502 PROF_stop(HttpHeaderParse
);
508 /* Barf on stray CR characters */
509 if (memchr(this_line
, '\r', field_end
- this_line
)) {
510 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
511 getStringPrefix(field_start
, field_end
-field_start
) << "}");
513 if (Config
.onoff
.relaxed_header_parser
) {
514 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
516 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
521 PROF_stop(HttpHeaderParse
);
526 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
527 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
528 getStringPrefix(header_start
, hdrLen
) << "}");
529 PROF_stop(HttpHeaderParse
);
532 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
534 if (field_start
== field_end
) {
535 if (field_ptr
< header_end
) {
536 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field near {" <<
537 getStringPrefix(field_start
, hdrLen
-(field_start
-header_start
)) << "}");
538 PROF_stop(HttpHeaderParse
);
542 break; /* terminating blank line */
545 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
546 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field {" <<
547 getStringPrefix(field_start
, field_end
-field_start
) << "}");
548 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, hdrLen
) << "}");
550 if (Config
.onoff
.relaxed_header_parser
)
553 PROF_stop(HttpHeaderParse
);
557 if (e
->id
== Http::HdrType::CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != nullptr) {
558 if (e
->value
!= e2
->value
) {
560 debugs(55, warnOnError
, "WARNING: found two conflicting content-length headers in {" <<
561 getStringPrefix(header_start
, hdrLen
) << "}");
563 if (!Config
.onoff
.relaxed_header_parser
) {
565 PROF_stop(HttpHeaderParse
);
569 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
570 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
573 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
574 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
576 } else if (l1
> l2
) {
583 debugs(55, warnOnError
, "NOTICE: found double content-length header");
586 if (Config
.onoff
.relaxed_header_parser
)
589 PROF_stop(HttpHeaderParse
);
594 if (e
->id
== Http::HdrType::OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
595 debugs(55, warnOnError
, "WARNING: found whitespace in HTTP header name {" <<
596 getStringPrefix(field_start
, field_end
-field_start
) << "}");
598 if (!Config
.onoff
.relaxed_header_parser
) {
600 PROF_stop(HttpHeaderParse
);
609 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
610 delById(Http::HdrType::CONTENT_LENGTH
);
613 PROF_stop(HttpHeaderParse
);
614 return 1; /* even if no fields where found, it is a valid header */
617 /* packs all the entries using supplied packer */
619 HttpHeader::packInto(Packable
* p
, bool mask_sensitive_info
) const
621 HttpHeaderPos pos
= HttpHeaderInitPos
;
622 const HttpHeaderEntry
*e
;
624 debugs(55, 7, this << " into " << p
<<
625 (mask_sensitive_info
? " while masking" : ""));
626 /* pack all entries one by one */
627 while ((e
= getEntry(&pos
))) {
628 if (!mask_sensitive_info
) {
633 bool maskThisEntry
= false;
635 case Http::HdrType::AUTHORIZATION
:
636 case Http::HdrType::PROXY_AUTHORIZATION
:
637 maskThisEntry
= true;
640 case Http::HdrType::FTP_ARGUMENTS
:
641 if (const HttpHeaderEntry
*cmd
= findEntry(Http::HdrType::FTP_COMMAND
))
642 maskThisEntry
= (cmd
->value
== "PASS");
649 p
->append(e
->name
.rawBuf(), e
->name
.size());
650 p
->append(": ** NOT DISPLAYED **\r\n", 23);
656 /* Pack in the "special" entries */
661 /* returns next valid entry */
663 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
666 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
668 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
670 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
677 * returns a pointer to a specified entry if any
678 * note that we return one entry so it does not make much sense to ask for
682 HttpHeader::findEntry(Http::HdrType id
) const
684 HttpHeaderPos pos
= HttpHeaderInitPos
;
686 assert(any_valid_header(id
));
687 assert(!CBIT_TEST(ListHeadersMask
, id
));
689 /* check mask first */
691 if (!CBIT_TEST(mask
, id
))
694 /* looks like we must have it, do linear search */
695 while ((e
= getEntry(&pos
))) {
700 /* hm.. we thought it was there, but it was not found */
703 return NULL
; /* not reached */
707 * same as httpHeaderFindEntry
710 HttpHeader::findLastEntry(Http::HdrType id
) const
712 HttpHeaderPos pos
= HttpHeaderInitPos
;
714 HttpHeaderEntry
*result
= NULL
;
715 assert(any_valid_header(id
));
716 assert(!CBIT_TEST(ListHeadersMask
, id
));
718 /* check mask first */
720 if (!CBIT_TEST(mask
, id
))
723 /* looks like we must have it, do linear search */
724 while ((e
= getEntry(&pos
))) {
729 assert(result
); /* must be there! */
734 * deletes all fields with a given name if any, returns #fields deleted;
737 HttpHeader::delByName(const char *name
)
740 HttpHeaderPos pos
= HttpHeaderInitPos
;
742 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
743 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
745 while ((e
= getEntry(&pos
))) {
746 if (!e
->name
.caseCmp(name
))
749 CBIT_SET(mask
, e
->id
);
755 /* deletes all entries with a given id, returns the #entries deleted */
757 HttpHeader::delById(Http::HdrType id
)
760 HttpHeaderPos pos
= HttpHeaderInitPos
;
762 debugs(55, 8, this << " del-by-id " << id
);
763 assert(any_valid_header(id
));
764 assert(id
!= Http::HdrType::OTHER
); /* does not make sense */
766 if (!CBIT_TEST(mask
, id
))
769 while ((e
= getEntry(&pos
))) {
780 * deletes an entry at pos and leaves a gap; leaving a gap makes it
781 * possible to iterate(search) and delete fields at the same time
782 * NOTE: Does not update the header mask. Caller must follow up with
783 * a call to refreshMask() if headers_deleted was incremented.
786 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
789 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
790 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
792 /* decrement header length, allow for ": " and crlf */
793 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
800 * Compacts the header storage
803 HttpHeader::compact()
805 // TODO: optimize removal, or possibly make it so that's not needed.
806 std::vector
<HttpHeaderEntry
*>::iterator newend
;
807 newend
= std::remove(entries
.begin(), entries
.end(), static_cast<HttpHeaderEntry
*>(NULL
));
808 entries
.resize(newend
-entries
.begin());
812 * Refreshes the header mask. Required after delAt() calls.
815 HttpHeader::refreshMask()
817 httpHeaderMaskInit(&mask
, 0);
818 debugs(55, 7, "refreshing the mask in hdr " << this);
819 HttpHeaderPos pos
= HttpHeaderInitPos
;
820 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
821 CBIT_SET(mask
, e
->id
);
826 * does not call e->clone() so one should not reuse "*e"
829 HttpHeader::addEntry(HttpHeaderEntry
* e
)
832 assert(any_HdrType_enum_value(e
->id
));
833 assert(e
->name
.size());
835 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
837 if (e
->id
!= Http::HdrType::BAD_HDR
) {
838 if (CBIT_TEST(mask
, e
->id
)) {
839 ++ headerStatsTable
[e
->id
].repCount
;
841 CBIT_SET(mask
, e
->id
);
845 entries
.push_back(e
);
847 /* increment header length, allow for ": " and crlf */
848 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
852 * does not call e->clone() so one should not reuse "*e"
855 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
858 assert(any_valid_header(e
->id
));
860 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
862 // Http::HdrType::BAD_HDR is filtered out by assert_any_valid_header
863 if (CBIT_TEST(mask
, e
->id
)) {
864 ++ headerStatsTable
[e
->id
].repCount
;
866 CBIT_SET(mask
, e
->id
);
869 entries
.insert(entries
.begin(),e
);
871 /* increment header length, allow for ": " and crlf */
872 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
876 HttpHeader::getList(Http::HdrType id
, String
*s
) const
879 HttpHeaderPos pos
= HttpHeaderInitPos
;
880 debugs(55, 9, this << " joining for id " << id
);
881 /* only fields from ListHeaders array can be "listed" */
882 assert(CBIT_TEST(ListHeadersMask
, id
));
884 if (!CBIT_TEST(mask
, id
))
887 while ((e
= getEntry(&pos
))) {
889 strListAdd(s
, e
->value
.termedBuf(), ',');
893 * note: we might get an empty (size==0) string if there was an "empty"
894 * header. This results in an empty length String, which may have a NULL
897 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
899 debugs(55, 3, "empty list header: " << Http::HeaderTable
[id
].name
<< "(" << id
<< ")");
901 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
906 /* return a list of entries with the same id separated by ',' and ws */
908 HttpHeader::getList(Http::HdrType id
) const
911 HttpHeaderPos pos
= HttpHeaderInitPos
;
912 debugs(55, 9, this << "joining for id " << id
);
913 /* only fields from ListHeaders array can be "listed" */
914 assert(CBIT_TEST(ListHeadersMask
, id
));
916 if (!CBIT_TEST(mask
, id
))
921 while ((e
= getEntry(&pos
))) {
923 strListAdd(&s
, e
->value
.termedBuf(), ',');
927 * note: we might get an empty (size==0) string if there was an "empty"
928 * header. This results in an empty length String, which may have a NULL
931 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
933 debugs(55, 3, "empty list header: " << Http::HeaderTable
[id
].name
<< "(" << id
<< ")");
935 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
940 /* return a string or list of entries with the same id separated by ',' and ws */
942 HttpHeader::getStrOrList(Http::HdrType id
) const
946 if (CBIT_TEST(ListHeadersMask
, id
))
949 if ((e
= findEntry(id
)))
956 * Returns the value of the specified header and/or an undefined String.
959 HttpHeader::getByName(const char *name
) const
962 // ignore presence: return undefined string if an empty header is present
963 (void)getByNameIfPresent(name
, result
);
968 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
971 HttpHeaderPos pos
= HttpHeaderInitPos
;
976 /* First try the quick path */
977 id
= Http::HeaderLookupTable
.lookup(SBuf(name
));
979 if (id
!= Http::HdrType::BAD_HDR
) {
982 result
= getStrOrList(id
);
986 /* Sorry, an unknown header name. Do linear search */
988 while ((e
= getEntry(&pos
))) {
989 if (e
->id
== Http::HdrType::OTHER
&& e
->name
.caseCmp(name
) == 0) {
991 strListAdd(&result
, e
->value
.termedBuf(), ',');
999 * Returns a the value of the specified list member, if any.
1002 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1005 const char *pos
= NULL
;
1008 int mlen
= strlen(member
);
1012 header
= getByName(name
);
1016 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1017 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1018 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1027 * returns a the value of the specified list member, if any.
1030 HttpHeader::getListMember(Http::HdrType id
, const char *member
, const char separator
) const
1033 const char *pos
= NULL
;
1036 int mlen
= strlen(member
);
1038 assert(any_valid_header(id
));
1040 header
= getStrOrList(id
);
1043 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1044 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1045 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1054 /* test if a field is present */
1056 HttpHeader::has(Http::HdrType id
) const
1058 assert(any_valid_header(id
));
1059 assert(id
!= Http::HdrType::OTHER
);
1060 debugs(55, 9, this << " lookup for " << id
);
1061 return CBIT_TEST(mask
, id
);
1065 HttpHeader::putInt(Http::HdrType id
, int number
)
1067 assert(any_valid_header(id
));
1068 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
1069 assert(number
>= 0);
1070 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1074 HttpHeader::putInt64(Http::HdrType id
, int64_t number
)
1076 assert(any_valid_header(id
));
1077 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
1078 assert(number
>= 0);
1079 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1083 HttpHeader::putTime(Http::HdrType id
, time_t htime
)
1085 assert(any_valid_header(id
));
1086 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1088 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1092 HttpHeader::insertTime(Http::HdrType id
, time_t htime
)
1094 assert(any_valid_header(id
));
1095 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1097 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1101 HttpHeader::putStr(Http::HdrType id
, const char *str
)
1103 assert(any_valid_header(id
));
1104 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1106 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1110 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1112 assert(auth_scheme
&& realm
);
1113 httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1117 HttpHeader::putCc(const HttpHdrCc
* cc
)
1120 /* remove old directives if any */
1121 delById(Http::HdrType::CACHE_CONTROL
);
1127 addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL
, NULL
, mb
.buf
));
1133 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1136 /* remove old directives if any */
1137 delById(Http::HdrType::CONTENT_RANGE
);
1141 httpHdrContRangePackInto(cr
, &mb
);
1143 addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE
, NULL
, mb
.buf
));
1149 HttpHeader::putRange(const HttpHdrRange
* range
)
1152 /* remove old directives if any */
1153 delById(Http::HdrType::RANGE
);
1157 range
->packInto(&mb
);
1159 addEntry(new HttpHeaderEntry(Http::HdrType::RANGE
, NULL
, mb
.buf
));
1165 HttpHeader::putSc(HttpHdrSc
*sc
)
1168 /* remove old directives if any */
1169 delById(Http::HdrType::SURROGATE_CONTROL
);
1175 addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL
, NULL
, mb
.buf
));
1181 HttpHeader::putWarning(const int code
, const char *const text
)
1184 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1185 putStr(Http::HdrType::WARNING
, buf
);
1188 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1190 HttpHeader::putExt(const char *name
, const char *value
)
1192 assert(name
&& value
);
1193 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1194 addEntry(new HttpHeaderEntry(Http::HdrType::OTHER
, name
, value
));
1198 HttpHeader::getInt(Http::HdrType id
) const
1200 assert(any_valid_header(id
));
1201 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftInt
); /* must be of an appropriate type */
1204 if ((e
= findEntry(id
)))
1211 HttpHeader::getInt64(Http::HdrType id
) const
1213 assert(any_valid_header(id
));
1214 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftInt64
); /* must be of an appropriate type */
1217 if ((e
= findEntry(id
)))
1218 return e
->getInt64();
1224 HttpHeader::getTime(Http::HdrType id
) const
1228 assert(any_valid_header(id
));
1229 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftDate_1123
); /* must be of an appropriate type */
1231 if ((e
= findEntry(id
))) {
1232 value
= parse_rfc1123(e
->value
.termedBuf());
1233 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1239 /* sync with httpHeaderGetLastStr */
1241 HttpHeader::getStr(Http::HdrType id
) const
1244 assert(any_valid_header(id
));
1245 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1247 if ((e
= findEntry(id
))) {
1248 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1249 return e
->value
.termedBuf();
1257 HttpHeader::getLastStr(Http::HdrType id
) const
1260 assert(any_valid_header(id
));
1261 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftStr
); /* must be of an appropriate type */
1263 if ((e
= findLastEntry(id
))) {
1264 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1265 return e
->value
.termedBuf();
1272 HttpHeader::getCc() const
1274 if (!CBIT_TEST(mask
, Http::HdrType::CACHE_CONTROL
))
1276 PROF_start(HttpHeader_getCc
);
1279 getList(Http::HdrType::CACHE_CONTROL
, &s
);
1281 HttpHdrCc
*cc
=new HttpHdrCc();
1283 if (!cc
->parse(s
)) {
1288 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1291 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1293 httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL
, s
, !cc
);
1295 PROF_stop(HttpHeader_getCc
);
1301 HttpHeader::getRange() const
1303 HttpHdrRange
*r
= NULL
;
1305 /* some clients will send "Request-Range" _and_ *matching* "Range"
1306 * who knows, some clients might send Request-Range only;
1307 * this "if" should work correctly in both cases;
1308 * hopefully no clients send mismatched headers! */
1310 if ((e
= findEntry(Http::HdrType::RANGE
)) ||
1311 (e
= findEntry(Http::HdrType::REQUEST_RANGE
))) {
1312 r
= HttpHdrRange::ParseCreate(&e
->value
);
1313 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1320 HttpHeader::getSc() const
1322 if (!CBIT_TEST(mask
, Http::HdrType::SURROGATE_CONTROL
))
1327 (void) getList(Http::HdrType::SURROGATE_CONTROL
, &s
);
1329 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1331 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1334 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1336 httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL
, s
, !sc
);
1342 HttpHeader::getContRange() const
1344 HttpHdrContRange
*cr
= NULL
;
1347 if ((e
= findEntry(Http::HdrType::CONTENT_RANGE
))) {
1348 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1349 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1356 HttpHeader::getAuth(Http::HdrType id
, const char *auth_scheme
) const
1360 assert(auth_scheme
);
1363 if (!field
) /* no authorization field */
1366 l
= strlen(auth_scheme
);
1368 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1373 if (!xisspace(*field
)) /* wrong scheme */
1376 /* skip white space */
1377 for (; field
&& xisspace(*field
); ++field
);
1379 if (!*field
) /* no authorization cookie */
1382 static char decodedAuthToken
[8192];
1383 struct base64_decode_ctx ctx
;
1384 base64_decode_init(&ctx
);
1385 size_t decodedLen
= 0;
1386 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(decodedAuthToken
), strlen(field
), reinterpret_cast<const uint8_t*>(field
)) ||
1387 !base64_decode_final(&ctx
)) {
1390 decodedAuthToken
[decodedLen
] = '\0';
1391 return decodedAuthToken
;
1395 HttpHeader::getETag(Http::HdrType id
) const
1397 ETag etag
= {NULL
, -1};
1399 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftETag
); /* must be of an appropriate type */
1401 if ((e
= findEntry(id
)))
1402 etagParseInit(&etag
, e
->value
.termedBuf());
1408 HttpHeader::getTimeOrTag(Http::HdrType id
) const
1412 assert(Http::HeaderTable
[id
].type
== Http::HdrFieldType::ftDate_1123_or_ETag
); /* must be of an appropriate type */
1413 memset(&tot
, 0, sizeof(tot
));
1415 if ((e
= findEntry(id
))) {
1416 const char *str
= e
->value
.termedBuf();
1417 /* try as an ETag */
1419 if (etagParseInit(&tot
.tag
, str
)) {
1420 tot
.valid
= tot
.tag
.str
!= NULL
;
1423 /* or maybe it is time? */
1424 tot
.time
= parse_rfc1123(str
);
1425 tot
.valid
= tot
.time
>= 0;
1430 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1438 HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId
, const char *aName
, const char *aValue
)
1440 assert(any_HdrType_enum_value(anId
));
1443 if (id
!= Http::HdrType::OTHER
)
1444 name
= Http::HeaderTable
[id
].name
;
1450 if (id
!= Http::HdrType::BAD_HDR
)
1451 ++ headerStatsTable
[id
].aliveCount
;
1453 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1456 HttpHeaderEntry::~HttpHeaderEntry()
1458 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1460 if (id
!= Http::HdrType::BAD_HDR
) {
1461 assert(headerStatsTable
[id
].aliveCount
);
1462 -- headerStatsTable
[id
].aliveCount
;
1463 id
= Http::HdrType::BAD_HDR
; // it already is BAD_HDR, no sense in resetting it
1468 /* parses and inits header entry, returns true/false */
1470 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1472 /* note: name_start == field_start */
1473 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1474 int name_len
= name_end
? name_end
- field_start
:0;
1475 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1476 /* note: value_end == field_end */
1478 ++ HeaderEntryParsedCount
;
1480 /* do we have a valid field name within this field? */
1482 if (!name_len
|| name_end
> field_end
)
1485 if (name_len
> 65534) {
1486 /* String must be LESS THAN 64K and it adds a terminating NULL */
1487 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1491 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1492 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1493 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1495 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1502 /* now we know we can parse it */
1504 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1506 /* is it a "known" field? */
1507 Http::HdrType id
= Http::HeaderLookupTable
.lookup(SBuf(field_start
,name_len
));
1508 debugs(55, 9, "got hdr-id=" << id
);
1514 if (id
== Http::HdrType::BAD_HDR
)
1515 id
= Http::HdrType::OTHER
;
1517 /* set field name */
1518 if (id
== Http::HdrType::OTHER
)
1519 name
.limitInit(field_start
, name_len
);
1521 name
= Http::HeaderTable
[id
].name
;
1523 /* trim field value */
1524 while (value_start
< field_end
&& xisspace(*value_start
))
1527 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1530 if (field_end
- value_start
> 65534) {
1531 /* String must be LESS THAN 64K and it adds a terminating NULL */
1532 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1534 if (id
== Http::HdrType::OTHER
)
1540 /* set field value */
1541 value
.limitInit(value_start
, field_end
- value_start
);
1543 if (id
!= Http::HdrType::BAD_HDR
)
1544 ++ headerStatsTable
[id
].seenCount
;
1546 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1548 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1552 HttpHeaderEntry::clone() const
1554 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1558 HttpHeaderEntry::packInto(Packable
* p
) const
1561 p
->append(name
.rawBuf(), name
.size());
1563 p
->append(value
.rawBuf(), value
.size());
1564 p
->append("\r\n", 2);
1568 HttpHeaderEntry::getInt() const
1571 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1572 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1573 /* XXX: Should we check ok - ie
1574 * return ok ? -1 : value;
1580 HttpHeaderEntry::getInt64() const
1583 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1584 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1585 /* XXX: Should we check ok - ie
1586 * return ok ? -1 : value;
1592 httpHeaderNoteParsedEntry(Http::HdrType id
, String
const &context
, int error
)
1594 if (id
!= Http::HdrType::BAD_HDR
)
1595 ++ headerStatsTable
[id
].parsCount
;
1598 if (id
!= Http::HdrType::BAD_HDR
)
1599 ++ headerStatsTable
[id
].errCount
;
1600 debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderTable
[id
].name
<< ": " << context
<< "'");
1608 /* tmp variable used to pass stat info to dumpers */
1609 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1610 const HttpHeaderStat
*dump_stat
= NULL
;
1613 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
1615 const int id
= static_cast<int>(val
);
1616 const bool valid_id
= Http::any_valid_header(static_cast<Http::HdrType
>(id
));
1617 const char *name
= valid_id
? Http::HeaderTable
[id
].name
: "INVALID";
1618 int visible
= count
> 0;
1619 /* for entries with zero count, list only those that belong to current type of message */
1621 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1622 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1625 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1626 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1630 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double, int count
)
1633 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1634 idx
, (int) val
, count
,
1635 xpercent(count
, dump_stat
->destroyedCount
));
1639 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1645 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1646 storeAppendPrintf(e
, "\nField type distribution\n");
1647 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1648 "id", "name", "count", "#/header");
1649 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1650 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1651 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1652 "id", "name", "count", "#/cc_field");
1653 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1654 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1655 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1656 "id", "name", "count", "#/sc_field");
1657 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1658 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1659 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1660 "id", "#flds", "count", "%total");
1661 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1662 storeAppendPrintf(e
, "\n");
1667 httpHeaderStoreReport(StoreEntry
* e
)
1672 HttpHeaderStats
[0].parsedCount
=
1673 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1674 HttpHeaderStats
[0].ccParsedCount
=
1675 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1676 HttpHeaderStats
[0].destroyedCount
=
1677 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1678 HttpHeaderStats
[0].busyDestroyedCount
=
1679 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1681 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1682 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1685 /* field stats for all messages */
1686 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1688 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1689 "id", "name", "#alive", "%err", "%repeat");
1691 // scan heaaderTable and output
1692 for (int j
= 0; Http::HeaderTable
[j
].name
!= nullptr; ++j
) {
1693 auto stats
= headerStatsTable
[j
];
1694 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1695 Http::HeaderTable
[j
].id
,
1696 Http::HeaderTable
[j
].name
,
1698 xpercent(stats
.errCount
, stats
.parsCount
),
1699 xpercent(stats
.repCount
, stats
.seenCount
));
1702 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1703 HttpHeaderStats
[hoRequest
].parsedCount
,
1704 HttpHeaderStats
[hoReply
].parsedCount
,
1705 HttpHeaderStats
[0].parsedCount
);
1706 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1710 HttpHeader::hasListMember(Http::HdrType id
, const char *member
, const char separator
) const
1713 const char *pos
= NULL
;
1716 int mlen
= strlen(member
);
1718 assert(any_valid_header(id
));
1720 String
header (getStrOrList(id
));
1722 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1723 if (strncasecmp(item
, member
, mlen
) == 0
1724 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1734 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1737 const char *pos
= NULL
;
1740 int mlen
= strlen(member
);
1744 String
header (getByName(name
));
1746 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1747 if (strncasecmp(item
, member
, mlen
) == 0
1748 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1758 HttpHeader::removeHopByHopEntries()
1760 removeConnectionHeaderEntries();
1762 const HttpHeaderEntry
*e
;
1763 HttpHeaderPos pos
= HttpHeaderInitPos
;
1764 int headers_deleted
= 0;
1765 while ((e
= getEntry(&pos
))) {
1766 Http::HdrType id
= e
->id
;
1767 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1768 delAt(pos
, headers_deleted
);
1775 HttpHeader::removeConnectionHeaderEntries()
1777 if (has(Http::HdrType::CONNECTION
)) {
1778 /* anything that matches Connection list member will be deleted */
1779 String strConnection
;
1781 (void) getList(Http::HdrType::CONNECTION
, &strConnection
);
1782 const HttpHeaderEntry
*e
;
1783 HttpHeaderPos pos
= HttpHeaderInitPos
;
1785 * think: on-average-best nesting of the two loops (hdrEntry
1786 * and strListItem) @?@
1789 * maybe we should delete standard stuff ("keep-alive","close")
1790 * from strConnection first?
1793 int headers_deleted
= 0;
1794 while ((e
= getEntry(&pos
))) {
1795 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1796 delAt(pos
, headers_deleted
);
1798 if (headers_deleted
)