2 * Copyright (C) 1996-2014 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 */
14 #include "HttpHdrCc.h"
15 #include "HttpHdrContRange.h"
16 #include "HttpHdrSc.h"
17 #include "HttpHeader.h"
18 #include "HttpHeaderFieldInfo.h"
19 #include "HttpHeaderStat.h"
20 #include "HttpHeaderTools.h"
22 #include "mgr/Registration.h"
23 #include "profiler/Profiler.h"
25 #include "SquidConfig.h"
26 #include "SquidString.h"
30 #include "TimeOrTag.h"
34 /* XXX: the whole set of API managing the entries vector should be rethought
35 * after the parse4r-ng effort is complete.
39 * On naming conventions:
41 * HTTP/1.1 defines message-header as
43 * message-header = field-name ":" [ field-value ] CRLF
45 * field-value = *( field-content | LWS )
47 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
48 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
50 * HttpHeader is an object that represents all message-headers in a message.
51 * HttpHeader does not manage start-line.
53 * HttpHeader is implemented as a collection of header "entries".
54 * An entry is a (field_id, field_name, field_value) triplet.
58 * local constants and vars
62 * A table with major attributes for every known field.
63 * We calculate name lengths and reorganize this array on start up.
64 * After reorganization, field id can be used as an index to the table.
66 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
67 {"Accept", HDR_ACCEPT
, ftStr
},
69 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
70 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
71 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
72 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
73 {"Age", HDR_AGE
, ftInt
},
74 {"Allow", HDR_ALLOW
, ftStr
},
75 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
76 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
77 {"Connection", HDR_CONNECTION
, ftStr
},
78 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
79 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
80 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
81 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
82 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
83 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
84 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
85 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
86 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
87 {"Cookie", HDR_COOKIE
, ftStr
},
88 {"Cookie2", HDR_COOKIE2
, ftStr
},
89 {"Date", HDR_DATE
, ftDate_1123
},
90 {"ETag", HDR_ETAG
, ftETag
},
91 {"Expect", HDR_EXPECT
, ftStr
},
92 {"Expires", HDR_EXPIRES
, ftDate_1123
},
93 {"Forwarded", HDR_FORWARDED
, ftStr
},
94 {"From", HDR_FROM
, ftStr
},
95 {"Host", HDR_HOST
, ftStr
},
96 {"HTTP2-Settings", HDR_HTTP2_SETTINGS
, ftStr
}, /* for now */
97 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
98 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
99 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
100 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
101 {"If-Unmodified-Since", HDR_IF_UNMODIFIED_SINCE
, ftDate_1123
},
102 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
103 {"Key", HDR_KEY
, ftStr
},
104 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
105 {"Link", HDR_LINK
, ftStr
},
106 {"Location", HDR_LOCATION
, ftStr
},
107 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
108 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
109 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
110 {"Origin", HDR_ORIGIN
, ftStr
},
111 {"Pragma", HDR_PRAGMA
, ftStr
},
112 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
113 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
114 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
115 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
116 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
117 {"Public", HDR_PUBLIC
, ftStr
},
118 {"Range", HDR_RANGE
, ftPRange
},
119 {"Referer", HDR_REFERER
, ftStr
},
120 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
121 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
122 {"Server", HDR_SERVER
, ftStr
},
123 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
124 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
125 {"TE", HDR_TE
, ftStr
},
126 {"Title", HDR_TITLE
, ftStr
},
127 {"Trailer", HDR_TRAILER
, ftStr
},
128 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
129 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
130 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
131 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
132 {"User-Agent", HDR_USER_AGENT
, ftStr
},
133 {"Vary", HDR_VARY
, ftStr
}, /* for now */
134 {"Via", HDR_VIA
, ftStr
}, /* for now */
135 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
136 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
137 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
138 {"X-Cache", HDR_X_CACHE
, ftStr
},
139 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
140 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
141 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
142 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
143 #if X_ACCELERATOR_VARY
144 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
147 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
149 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
150 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
151 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
152 {"FTP-Command", HDR_FTP_COMMAND
, ftStr
},
153 {"FTP-Arguments", HDR_FTP_ARGUMENTS
, ftStr
},
154 {"FTP-Pre", HDR_FTP_PRE
, ftStr
},
155 {"FTP-Status", HDR_FTP_STATUS
, ftInt
},
156 {"FTP-Reason", HDR_FTP_REASON
, ftStr
},
157 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
160 static HttpHeaderFieldInfo
*Headers
= NULL
;
162 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
164 int tmp
= (int)aHeader
;
165 aHeader
= (http_hdr_type
)(++tmp
);
170 * headers with field values defined as #(values) in HTTP/1.1
171 * Headers that are currently not recognized, are commented out.
173 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
174 static http_hdr_type ListHeadersArr
[] = {
182 HDR_CONTENT_ENCODING
,
183 HDR_CONTENT_LANGUAGE
,
191 HDR_PROXY_CONNECTION
,
193 HDR_TRANSFER_ENCODING
,
198 HDR_WWW_AUTHENTICATE
,
199 HDR_AUTHENTICATION_INFO
,
200 HDR_PROXY_AUTHENTICATION_INFO
,
201 /* HDR_TE, HDR_TRAILER */
202 #if X_ACCELERATOR_VARY
203 HDR_X_ACCELERATOR_VARY
,
208 HDR_SURROGATE_CAPABILITY
,
209 HDR_SURROGATE_CONTROL
,
214 /* general-headers */
215 static http_hdr_type GeneralHeadersArr
[] = {
223 HDR_PROXY_CONNECTION
,
224 HDR_TRANSFER_ENCODING
,
231 static http_hdr_type EntityHeadersArr
[] = {
234 HDR_CONTENT_ENCODING
,
235 HDR_CONTENT_LANGUAGE
,
237 HDR_CONTENT_LOCATION
,
248 /* request-only headers */
249 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
250 static http_hdr_type RequestHeadersArr
[] = {
261 HDR_IF_MODIFIED_SINCE
,
264 HDR_IF_UNMODIFIED_SINCE
,
267 HDR_PROXY_AUTHORIZATION
,
273 HDR_SURROGATE_CAPABILITY
276 /* reply-only headers */
277 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
278 static http_hdr_type ReplyHeadersArr
[] = {
284 HDR_PROXY_AUTHENTICATE
,
292 HDR_WWW_AUTHENTICATE
,
296 #if X_ACCELERATOR_VARY
297 HDR_X_ACCELERATOR_VARY
,
303 HDR_SURROGATE_CONTROL
306 /* hop-by-hop headers */
307 static HttpHeaderMask HopByHopHeadersMask
;
308 static http_hdr_type HopByHopHeadersArr
[] = {
312 /*HDR_PROXY_AUTHENTICATE, // removal handled specially for peer login */
313 HDR_PROXY_AUTHORIZATION
,
316 HDR_TRANSFER_ENCODING
,
321 /* header accounting */
322 static HttpHeaderStat HttpHeaderStats
[] = {
330 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
332 static int HeaderEntryParsedCount
= 0;
335 * forward declarations and local routines
339 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
341 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
343 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
344 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
346 /** store report about current header usage and other stats */
347 static void httpHeaderStoreReport(StoreEntry
* e
);
350 * Module initialization routines
354 httpHeaderRegisterWithCacheManager(void)
356 Mgr::RegisterAction("http_headers",
357 "HTTP Header Statistics",
358 httpHeaderStoreReport
, 0, 1);
362 httpHeaderInitModule(void)
365 /* check that we have enough space for masks */
366 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
367 /* all headers must be described */
368 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
371 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
374 httpHeaderMaskInit(&ListHeadersMask
, 0);
375 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
377 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
378 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
379 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
380 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
382 httpHeaderMaskInit(&RequestHeadersMask
, 0);
383 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
384 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
385 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
387 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
388 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
390 /* init header stats */
391 assert(HttpHeaderStatCount
== hoReply
+ 1);
392 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
393 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
395 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
397 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
400 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
403 /* init dependent modules */
404 httpHdrCcInitModule();
405 httpHdrScInitModule();
407 httpHeaderRegisterWithCacheManager();
411 httpHeaderCleanModule(void)
413 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
415 httpHdrCcCleanModule();
416 httpHdrScCleanModule();
420 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
424 memset(hs
, 0, sizeof(HttpHeaderStat
));
426 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
427 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
428 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
429 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
433 * HttpHeader Implementation
436 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
438 httpHeaderMaskInit(&mask
, 0);
441 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
443 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
444 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
445 httpHeaderMaskInit(&mask
, 0);
448 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
450 httpHeaderMaskInit(&mask
, 0);
451 update(&other
, NULL
); // will update the mask as well
454 HttpHeader::~HttpHeader()
460 HttpHeader::operator =(const HttpHeader
&other
)
462 if (this != &other
) {
463 // we do not really care, but the caller probably does
464 assert(owner
== other
.owner
);
466 update(&other
, NULL
); // will update the mask as well
476 assert(owner
> hoNone
&& owner
< hoEnd
);
477 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
479 PROF_start(HttpHeaderClean
);
481 if (owner
<= hoReply
) {
483 * An unfortunate bug. The entries array is initialized
484 * such that count is set to zero. httpHeaderClean() seems to
485 * be called both when 'hdr' is created, and destroyed. Thus,
486 * we accumulate a large number of zero counts for 'hdr' before
487 * it is ever used. Can't think of a good way to fix it, except
488 * adding a state variable that indicates whether or not 'hdr'
489 * has been used. As a hack, just never count zero-sized header
492 if (!entries
.empty())
493 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
495 ++ HttpHeaderStats
[owner
].destroyedCount
;
497 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
498 } // if (owner <= hoReply)
500 for (std::vector
<HttpHeaderEntry
*>::iterator i
= entries
.begin(); i
!= entries
.end(); ++i
) {
501 HttpHeaderEntry
*e
= *i
;
504 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
505 debugs(55, DBG_CRITICAL
, "BUG: invalid entry (" << e
->id
<< "). Ignored.");
507 if (owner
<= hoReply
)
508 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
514 httpHeaderMaskInit(&mask
, 0);
516 PROF_stop(HttpHeaderClean
);
519 /* append entries (also see httpHeaderUpdate) */
521 HttpHeader::append(const HttpHeader
* src
)
523 const HttpHeaderEntry
*e
;
524 HttpHeaderPos pos
= HttpHeaderInitPos
;
527 debugs(55, 7, "appending hdr: " << this << " += " << src
);
529 while ((e
= src
->getEntry(&pos
))) {
530 addEntry(e
->clone());
534 /* use fresh entries to replace old ones */
536 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
539 old
->update (fresh
, denied_mask
);
543 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
545 const HttpHeaderEntry
*e
;
546 HttpHeaderPos pos
= HttpHeaderInitPos
;
548 assert(this != fresh
);
550 while ((e
= fresh
->getEntry(&pos
))) {
551 /* deny bad guys (ok to check for HDR_OTHER) here */
553 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
556 if (e
->id
!= HDR_OTHER
)
559 delByName(e
->name
.termedBuf());
562 pos
= HttpHeaderInitPos
;
563 while ((e
= fresh
->getEntry(&pos
))) {
564 /* deny bad guys (ok to check for HDR_OTHER) here */
566 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
569 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
571 addEntry(e
->clone());
575 /* just handy in parsing: resets and returns false */
584 HttpHeader::parse(const char *header_start
, const char *header_end
)
586 const char *field_ptr
= header_start
;
587 HttpHeaderEntry
*e
, *e2
;
588 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
590 PROF_start(HttpHeaderParse
);
592 assert(header_start
&& header_end
);
593 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
594 ++ HttpHeaderStats
[owner
].parsedCount
;
597 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
598 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
599 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
600 PROF_stop(HttpHeaderParse
);
604 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
605 * continuation lines start with a (single) space or tab */
606 while (field_ptr
< header_end
) {
607 const char *field_start
= field_ptr
;
608 const char *field_end
;
611 const char *this_line
= field_ptr
;
612 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
616 PROF_stop(HttpHeaderParse
);
620 field_end
= field_ptr
;
622 ++field_ptr
; /* Move to next line */
624 if (field_end
> this_line
&& field_end
[-1] == '\r') {
625 --field_end
; /* Ignore CR LF */
627 if (owner
== hoRequest
&& field_end
> this_line
) {
629 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
634 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
635 "header field to prevent request smuggling attacks: {" <<
636 getStringPrefix(header_start
, header_end
) << "}");
637 PROF_stop(HttpHeaderParse
);
643 /* Barf on stray CR characters */
644 if (memchr(this_line
, '\r', field_end
- this_line
)) {
645 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
646 getStringPrefix(field_start
, field_end
) << "}");
648 if (Config
.onoff
.relaxed_header_parser
) {
649 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
651 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
656 PROF_stop(HttpHeaderParse
);
661 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
662 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
663 getStringPrefix(header_start
, header_end
) << "}");
664 PROF_stop(HttpHeaderParse
);
667 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
669 if (field_start
== field_end
) {
670 if (field_ptr
< header_end
) {
671 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field near {" <<
672 getStringPrefix(field_start
, header_end
) << "}");
673 PROF_stop(HttpHeaderParse
);
677 break; /* terminating blank line */
680 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
681 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field {" <<
682 getStringPrefix(field_start
, field_end
) << "}");
683 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, header_end
) << "}");
685 if (Config
.onoff
.relaxed_header_parser
)
688 PROF_stop(HttpHeaderParse
);
692 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
693 if (e
->value
!= e2
->value
) {
695 debugs(55, warnOnError
, "WARNING: found two conflicting content-length headers in {" <<
696 getStringPrefix(header_start
, header_end
) << "}");
698 if (!Config
.onoff
.relaxed_header_parser
) {
700 PROF_stop(HttpHeaderParse
);
704 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
705 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
708 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
709 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
711 } else if (l1
> l2
) {
718 debugs(55, warnOnError
, "NOTICE: found double content-length header");
721 if (Config
.onoff
.relaxed_header_parser
)
724 PROF_stop(HttpHeaderParse
);
729 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
730 debugs(55, warnOnError
, "WARNING: found whitespace in HTTP header name {" <<
731 getStringPrefix(field_start
, field_end
) << "}");
733 if (!Config
.onoff
.relaxed_header_parser
) {
735 PROF_stop(HttpHeaderParse
);
744 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
745 delById(HDR_CONTENT_LENGTH
);
748 PROF_stop(HttpHeaderParse
);
749 return 1; /* even if no fields where found, it is a valid header */
752 /* packs all the entries using supplied packer */
754 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
756 HttpHeaderPos pos
= HttpHeaderInitPos
;
757 const HttpHeaderEntry
*e
;
759 debugs(55, 7, this << " into " << p
<<
760 (mask_sensitive_info
? " while masking" : ""));
761 /* pack all entries one by one */
762 while ((e
= getEntry(&pos
))) {
763 if (!mask_sensitive_info
) {
768 bool maskThisEntry
= false;
770 case HDR_AUTHORIZATION
:
771 case HDR_PROXY_AUTHORIZATION
:
772 maskThisEntry
= true;
775 case HDR_FTP_ARGUMENTS
:
776 if (const HttpHeaderEntry
*cmd
= findEntry(HDR_FTP_COMMAND
))
777 maskThisEntry
= (cmd
->value
== "PASS");
784 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
785 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
791 /* Pack in the "special" entries */
796 /* returns next valid entry */
798 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
801 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
803 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
805 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
812 * returns a pointer to a specified entry if any
813 * note that we return one entry so it does not make much sense to ask for
817 HttpHeader::findEntry(http_hdr_type id
) const
819 HttpHeaderPos pos
= HttpHeaderInitPos
;
822 assert(!CBIT_TEST(ListHeadersMask
, id
));
824 /* check mask first */
826 if (!CBIT_TEST(mask
, id
))
829 /* looks like we must have it, do linear search */
830 while ((e
= getEntry(&pos
))) {
835 /* hm.. we thought it was there, but it was not found */
838 return NULL
; /* not reached */
842 * same as httpHeaderFindEntry
845 HttpHeader::findLastEntry(http_hdr_type id
) const
847 HttpHeaderPos pos
= HttpHeaderInitPos
;
849 HttpHeaderEntry
*result
= NULL
;
851 assert(!CBIT_TEST(ListHeadersMask
, id
));
853 /* check mask first */
855 if (!CBIT_TEST(mask
, id
))
858 /* looks like we must have it, do linear search */
859 while ((e
= getEntry(&pos
))) {
864 assert(result
); /* must be there! */
869 * deletes all fields with a given name if any, returns #fields deleted;
872 HttpHeader::delByName(const char *name
)
875 HttpHeaderPos pos
= HttpHeaderInitPos
;
877 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
878 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
880 while ((e
= getEntry(&pos
))) {
881 if (!e
->name
.caseCmp(name
))
884 CBIT_SET(mask
, e
->id
);
890 /* deletes all entries with a given id, returns the #entries deleted */
892 HttpHeader::delById(http_hdr_type id
)
895 HttpHeaderPos pos
= HttpHeaderInitPos
;
897 debugs(55, 8, this << " del-by-id " << id
);
899 assert(id
!= HDR_OTHER
); /* does not make sense */
901 if (!CBIT_TEST(mask
, id
))
904 while ((e
= getEntry(&pos
))) {
915 * deletes an entry at pos and leaves a gap; leaving a gap makes it
916 * possible to iterate(search) and delete fields at the same time
917 * NOTE: Does not update the header mask. Caller must follow up with
918 * a call to refreshMask() if headers_deleted was incremented.
921 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
924 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
925 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
927 /* decrement header length, allow for ": " and crlf */
928 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
935 * Compacts the header storage
938 HttpHeader::compact()
940 // TODO: optimize removal, or possibly make it so that's not needed.
941 std::vector
<HttpHeaderEntry
*>::iterator newend
;
942 newend
= std::remove(entries
.begin(), entries
.end(), static_cast<HttpHeaderEntry
*>(NULL
));
943 entries
.resize(newend
-entries
.begin());
947 * Refreshes the header mask. Required after delAt() calls.
950 HttpHeader::refreshMask()
952 httpHeaderMaskInit(&mask
, 0);
953 debugs(55, 7, "refreshing the mask in hdr " << this);
954 HttpHeaderPos pos
= HttpHeaderInitPos
;
955 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
956 CBIT_SET(mask
, e
->id
);
961 * does not call e->clone() so one should not reuse "*e"
964 HttpHeader::addEntry(HttpHeaderEntry
* e
)
968 assert(e
->name
.size());
970 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
972 if (CBIT_TEST(mask
, e
->id
))
973 ++ Headers
[e
->id
].stat
.repCount
;
975 CBIT_SET(mask
, e
->id
);
977 entries
.push_back(e
);
979 /* increment header length, allow for ": " and crlf */
980 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
984 * does not call e->clone() so one should not reuse "*e"
987 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
992 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
994 if (CBIT_TEST(mask
, e
->id
))
995 ++ Headers
[e
->id
].stat
.repCount
;
997 CBIT_SET(mask
, e
->id
);
999 entries
.insert(entries
.begin(),e
);
1001 /* increment header length, allow for ": " and crlf */
1002 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
1006 HttpHeader::getList(http_hdr_type id
, String
*s
) const
1009 HttpHeaderPos pos
= HttpHeaderInitPos
;
1010 debugs(55, 9, this << " joining for id " << id
);
1011 /* only fields from ListHeaders array can be "listed" */
1012 assert(CBIT_TEST(ListHeadersMask
, id
));
1014 if (!CBIT_TEST(mask
, id
))
1017 while ((e
= getEntry(&pos
))) {
1019 strListAdd(s
, e
->value
.termedBuf(), ',');
1023 * note: we might get an empty (size==0) string if there was an "empty"
1024 * header. This results in an empty length String, which may have a NULL
1027 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1029 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1031 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1036 /* return a list of entries with the same id separated by ',' and ws */
1038 HttpHeader::getList(http_hdr_type id
) const
1041 HttpHeaderPos pos
= HttpHeaderInitPos
;
1042 debugs(55, 9, this << "joining for id " << id
);
1043 /* only fields from ListHeaders array can be "listed" */
1044 assert(CBIT_TEST(ListHeadersMask
, id
));
1046 if (!CBIT_TEST(mask
, id
))
1051 while ((e
= getEntry(&pos
))) {
1053 strListAdd(&s
, e
->value
.termedBuf(), ',');
1057 * note: we might get an empty (size==0) string if there was an "empty"
1058 * header. This results in an empty length String, which may have a NULL
1061 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1063 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1065 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1070 /* return a string or list of entries with the same id separated by ',' and ws */
1072 HttpHeader::getStrOrList(http_hdr_type id
) const
1076 if (CBIT_TEST(ListHeadersMask
, id
))
1079 if ((e
= findEntry(id
)))
1086 * Returns the value of the specified header and/or an undefined String.
1089 HttpHeader::getByName(const char *name
) const
1092 // ignore presence: return undefined string if an empty header is present
1093 (void)getByNameIfPresent(name
, result
);
1098 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
1101 HttpHeaderPos pos
= HttpHeaderInitPos
;
1106 /* First try the quick path */
1107 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1112 result
= getStrOrList(id
);
1116 /* Sorry, an unknown header name. Do linear search */
1118 while ((e
= getEntry(&pos
))) {
1119 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1121 strListAdd(&result
, e
->value
.termedBuf(), ',');
1129 * Returns a the value of the specified list member, if any.
1132 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1135 const char *pos
= NULL
;
1138 int mlen
= strlen(member
);
1142 header
= getByName(name
);
1146 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1147 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1148 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1157 * returns a the value of the specified list member, if any.
1160 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1163 const char *pos
= NULL
;
1166 int mlen
= strlen(member
);
1170 header
= getStrOrList(id
);
1173 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1174 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1175 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1184 /* test if a field is present */
1186 HttpHeader::has(http_hdr_type id
) const
1189 assert(id
!= HDR_OTHER
);
1190 debugs(55, 9, this << " lookup for " << id
);
1191 return CBIT_TEST(mask
, id
);
1195 HttpHeader::putInt(http_hdr_type id
, int number
)
1198 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1199 assert(number
>= 0);
1200 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1204 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1207 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1208 assert(number
>= 0);
1209 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1213 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1216 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1218 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1222 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1225 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1227 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1231 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1234 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1236 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1240 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1242 assert(auth_scheme
&& realm
);
1243 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1247 HttpHeader::putCc(const HttpHdrCc
* cc
)
1252 /* remove old directives if any */
1253 delById(HDR_CACHE_CONTROL
);
1256 packerToMemInit(&p
, &mb
);
1259 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1266 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1271 /* remove old directives if any */
1272 delById(HDR_CONTENT_RANGE
);
1275 packerToMemInit(&p
, &mb
);
1276 httpHdrContRangePackInto(cr
, &p
);
1278 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1285 HttpHeader::putRange(const HttpHdrRange
* range
)
1290 /* remove old directives if any */
1294 packerToMemInit(&p
, &mb
);
1295 range
->packInto(&p
);
1297 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1304 HttpHeader::putSc(HttpHdrSc
*sc
)
1309 /* remove old directives if any */
1310 delById(HDR_SURROGATE_CONTROL
);
1313 packerToMemInit(&p
, &mb
);
1316 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1323 HttpHeader::putWarning(const int code
, const char *const text
)
1326 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1327 putStr(HDR_WARNING
, buf
);
1330 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1332 HttpHeader::putExt(const char *name
, const char *value
)
1334 assert(name
&& value
);
1335 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1336 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1340 HttpHeader::getInt(http_hdr_type id
) const
1343 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1346 if ((e
= findEntry(id
)))
1353 HttpHeader::getInt64(http_hdr_type id
) const
1356 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1359 if ((e
= findEntry(id
)))
1360 return e
->getInt64();
1366 HttpHeader::getTime(http_hdr_type id
) const
1371 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1373 if ((e
= findEntry(id
))) {
1374 value
= parse_rfc1123(e
->value
.termedBuf());
1375 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1381 /* sync with httpHeaderGetLastStr */
1383 HttpHeader::getStr(http_hdr_type id
) const
1387 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1389 if ((e
= findEntry(id
))) {
1390 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1391 return e
->value
.termedBuf();
1399 HttpHeader::getLastStr(http_hdr_type id
) const
1403 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1405 if ((e
= findLastEntry(id
))) {
1406 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1407 return e
->value
.termedBuf();
1414 HttpHeader::getCc() const
1416 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1418 PROF_start(HttpHeader_getCc
);
1421 getList(HDR_CACHE_CONTROL
, &s
);
1423 HttpHdrCc
*cc
=new HttpHdrCc();
1425 if (!cc
->parse(s
)) {
1430 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1433 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1435 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1437 PROF_stop(HttpHeader_getCc
);
1443 HttpHeader::getRange() const
1445 HttpHdrRange
*r
= NULL
;
1447 /* some clients will send "Request-Range" _and_ *matching* "Range"
1448 * who knows, some clients might send Request-Range only;
1449 * this "if" should work correctly in both cases;
1450 * hopefully no clients send mismatched headers! */
1452 if ((e
= findEntry(HDR_RANGE
)) ||
1453 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1454 r
= HttpHdrRange::ParseCreate(&e
->value
);
1455 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1462 HttpHeader::getSc() const
1464 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1469 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1471 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1473 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1476 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1478 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1484 HttpHeader::getContRange() const
1486 HttpHdrContRange
*cr
= NULL
;
1489 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1490 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1491 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1498 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1502 assert(auth_scheme
);
1505 if (!field
) /* no authorization field */
1508 l
= strlen(auth_scheme
);
1510 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1515 if (!xisspace(*field
)) /* wrong scheme */
1518 /* skip white space */
1519 for (; field
&& xisspace(*field
); ++field
);
1521 if (!*field
) /* no authorization cookie */
1524 static char decodedAuthToken
[8192];
1525 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1526 decodedAuthToken
[decodedLen
] = '\0';
1527 return decodedAuthToken
;
1531 HttpHeader::getETag(http_hdr_type id
) const
1533 ETag etag
= {NULL
, -1};
1535 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1537 if ((e
= findEntry(id
)))
1538 etagParseInit(&etag
, e
->value
.termedBuf());
1544 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1548 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1549 memset(&tot
, 0, sizeof(tot
));
1551 if ((e
= findEntry(id
))) {
1552 const char *str
= e
->value
.termedBuf();
1553 /* try as an ETag */
1555 if (etagParseInit(&tot
.tag
, str
)) {
1556 tot
.valid
= tot
.tag
.str
!= NULL
;
1559 /* or maybe it is time? */
1560 tot
.time
= parse_rfc1123(str
);
1561 tot
.valid
= tot
.time
>= 0;
1566 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1574 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1579 if (id
!= HDR_OTHER
)
1580 name
= Headers
[id
].name
;
1586 ++ Headers
[id
].stat
.aliveCount
;
1588 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1591 HttpHeaderEntry::~HttpHeaderEntry()
1594 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1595 /* clean name if needed */
1597 if (id
== HDR_OTHER
)
1602 assert(Headers
[id
].stat
.aliveCount
);
1604 -- Headers
[id
].stat
.aliveCount
;
1609 /* parses and inits header entry, returns true/false */
1611 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1613 /* note: name_start == field_start */
1614 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1615 int name_len
= name_end
? name_end
- field_start
:0;
1616 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1617 /* note: value_end == field_end */
1619 ++ HeaderEntryParsedCount
;
1621 /* do we have a valid field name within this field? */
1623 if (!name_len
|| name_end
> field_end
)
1626 if (name_len
> 65534) {
1627 /* String must be LESS THAN 64K and it adds a terminating NULL */
1628 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1632 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1633 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1634 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1636 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1643 /* now we know we can parse it */
1645 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1647 /* is it a "known" field? */
1648 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1659 /* set field name */
1660 if (id
== HDR_OTHER
)
1661 name
.limitInit(field_start
, name_len
);
1663 name
= Headers
[id
].name
;
1665 /* trim field value */
1666 while (value_start
< field_end
&& xisspace(*value_start
))
1669 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1672 if (field_end
- value_start
> 65534) {
1673 /* String must be LESS THAN 64K and it adds a terminating NULL */
1674 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1676 if (id
== HDR_OTHER
)
1682 /* set field value */
1683 value
.limitInit(value_start
, field_end
- value_start
);
1685 ++ Headers
[id
].stat
.seenCount
;
1687 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1689 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1693 HttpHeaderEntry::clone() const
1695 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1699 HttpHeaderEntry::packInto(Packer
* p
) const
1702 packerAppend(p
, name
.rawBuf(), name
.size());
1703 packerAppend(p
, ": ", 2);
1704 packerAppend(p
, value
.rawBuf(), value
.size());
1705 packerAppend(p
, "\r\n", 2);
1709 HttpHeaderEntry::getInt() const
1712 assert (Headers
[id
].type
== ftInt
);
1714 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1715 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1716 /* XXX: Should we check ok - ie
1717 * return ok ? -1 : value;
1723 HttpHeaderEntry::getInt64() const
1726 assert (Headers
[id
].type
== ftInt64
);
1728 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1729 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1730 /* XXX: Should we check ok - ie
1731 * return ok ? -1 : value;
1737 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1739 ++ Headers
[id
].stat
.parsCount
;
1742 ++ Headers
[id
].stat
.errCount
;
1743 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1751 /* tmp variable used to pass stat info to dumpers */
1752 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1753 const HttpHeaderStat
*dump_stat
= NULL
;
1756 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1758 const int id
= (int) val
;
1759 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1760 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1761 int visible
= count
> 0;
1762 /* for entries with zero count, list only those that belong to current type of message */
1764 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1765 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1768 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1769 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1773 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1776 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1777 idx
, (int) val
, count
,
1778 xpercent(count
, dump_stat
->destroyedCount
));
1782 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1787 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1788 storeAppendPrintf(e
, "\nField type distribution\n");
1789 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1790 "id", "name", "count", "#/header");
1791 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1792 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1793 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1794 "id", "name", "count", "#/cc_field");
1795 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1796 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1797 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1798 "id", "name", "count", "#/sc_field");
1799 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1800 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1801 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1802 "id", "#flds", "count", "%total");
1803 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1804 storeAppendPrintf(e
, "\n");
1809 httpHeaderStoreReport(StoreEntry
* e
)
1815 HttpHeaderStats
[0].parsedCount
=
1816 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1817 HttpHeaderStats
[0].ccParsedCount
=
1818 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1819 HttpHeaderStats
[0].destroyedCount
=
1820 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1821 HttpHeaderStats
[0].busyDestroyedCount
=
1822 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1824 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1825 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1828 /* field stats for all messages */
1829 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1831 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1832 "id", "name", "#alive", "%err", "%repeat");
1834 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1835 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1836 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1837 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1838 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1839 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1842 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1843 HttpHeaderStats
[hoRequest
].parsedCount
,
1844 HttpHeaderStats
[hoReply
].parsedCount
,
1845 HttpHeaderStats
[0].parsedCount
);
1846 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1850 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1853 for (int i
= 0; i
< end
; ++i
) {
1854 if (name_len
!= info
[i
].name
.size())
1857 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1866 httpHeaderIdByNameDef(const char *name
, int name_len
)
1869 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1871 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1875 httpHeaderNameById(int id
)
1878 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1880 assert(id
>= 0 && id
< HDR_ENUM_END
);
1882 return Headers
[id
].name
.termedBuf();
1886 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1889 const char *pos
= NULL
;
1892 int mlen
= strlen(member
);
1896 String
header (getStrOrList(id
));
1898 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1899 if (strncasecmp(item
, member
, mlen
) == 0
1900 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1910 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1913 const char *pos
= NULL
;
1916 int mlen
= strlen(member
);
1920 String
header (getByName(name
));
1922 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1923 if (strncasecmp(item
, member
, mlen
) == 0
1924 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1934 HttpHeader::removeHopByHopEntries()
1936 removeConnectionHeaderEntries();
1938 const HttpHeaderEntry
*e
;
1939 HttpHeaderPos pos
= HttpHeaderInitPos
;
1940 int headers_deleted
= 0;
1941 while ((e
= getEntry(&pos
))) {
1943 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1944 delAt(pos
, headers_deleted
);
1951 HttpHeader::removeConnectionHeaderEntries()
1953 if (has(HDR_CONNECTION
)) {
1954 /* anything that matches Connection list member will be deleted */
1955 String strConnection
;
1957 (void) getList(HDR_CONNECTION
, &strConnection
);
1958 const HttpHeaderEntry
*e
;
1959 HttpHeaderPos pos
= HttpHeaderInitPos
;
1961 * think: on-average-best nesting of the two loops (hdrEntry
1962 * and strListItem) @?@
1965 * maybe we should delete standard stuff ("keep-alive","close")
1966 * from strConnection first?
1969 int headers_deleted
= 0;
1970 while ((e
= getEntry(&pos
))) {
1971 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1972 delAt(pos
, headers_deleted
);
1974 if (headers_deleted
)