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"
35 /* XXX: the whole set of API managing the entries vector should be rethought
36 * after the parse4r-ng effort is complete.
40 * On naming conventions:
42 * HTTP/1.1 defines message-header as
44 * message-header = field-name ":" [ field-value ] CRLF
46 * field-value = *( field-content | LWS )
48 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
49 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
51 * HttpHeader is an object that represents all message-headers in a message.
52 * HttpHeader does not manage start-line.
54 * HttpHeader is implemented as a collection of header "entries".
55 * An entry is a (field_id, field_name, field_value) triplet.
59 * local constants and vars
63 * A table with major attributes for every known field.
64 * We calculate name lengths and reorganize this array on start up.
65 * After reorganization, field id can be used as an index to the table.
67 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
68 {"Accept", HDR_ACCEPT
, ftStr
},
70 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
71 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
72 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
73 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
74 {"Age", HDR_AGE
, ftInt
},
75 {"Allow", HDR_ALLOW
, ftStr
},
76 {"Alternate-Protocol", HDR_ALTERNATE_PROTOCOL
, ftStr
},
77 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
78 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
79 {"Connection", HDR_CONNECTION
, ftStr
},
80 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
81 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
82 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
83 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
84 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
85 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
86 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
87 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
88 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
89 {"Cookie", HDR_COOKIE
, ftStr
},
90 {"Cookie2", HDR_COOKIE2
, ftStr
},
91 {"Date", HDR_DATE
, ftDate_1123
},
92 {"ETag", HDR_ETAG
, ftETag
},
93 {"Expect", HDR_EXPECT
, ftStr
},
94 {"Expires", HDR_EXPIRES
, ftDate_1123
},
95 {"Forwarded", HDR_FORWARDED
, ftStr
},
96 {"From", HDR_FROM
, ftStr
},
97 {"Host", HDR_HOST
, ftStr
},
98 {"HTTP2-Settings", HDR_HTTP2_SETTINGS
, ftStr
}, /* for now */
99 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
100 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
101 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
102 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
103 {"If-Unmodified-Since", HDR_IF_UNMODIFIED_SINCE
, ftDate_1123
},
104 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
105 {"Key", HDR_KEY
, ftStr
},
106 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
107 {"Link", HDR_LINK
, ftStr
},
108 {"Location", HDR_LOCATION
, ftStr
},
109 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
110 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
111 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
112 {"Origin", HDR_ORIGIN
, ftStr
},
113 {"Pragma", HDR_PRAGMA
, ftStr
},
114 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
115 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
116 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
117 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
118 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
119 {"Public", HDR_PUBLIC
, ftStr
},
120 {"Range", HDR_RANGE
, ftPRange
},
121 {"Referer", HDR_REFERER
, ftStr
},
122 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
123 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
124 {"Server", HDR_SERVER
, ftStr
},
125 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
126 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
127 {"TE", HDR_TE
, ftStr
},
128 {"Title", HDR_TITLE
, ftStr
},
129 {"Trailer", HDR_TRAILER
, ftStr
},
130 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
131 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
132 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
133 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
134 {"User-Agent", HDR_USER_AGENT
, ftStr
},
135 {"Vary", HDR_VARY
, ftStr
}, /* for now */
136 {"Via", HDR_VIA
, ftStr
}, /* for now */
137 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
138 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
139 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
140 {"X-Cache", HDR_X_CACHE
, ftStr
},
141 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
142 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
143 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
144 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
145 #if X_ACCELERATOR_VARY
146 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
149 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
151 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
152 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
153 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
154 {"FTP-Command", HDR_FTP_COMMAND
, ftStr
},
155 {"FTP-Arguments", HDR_FTP_ARGUMENTS
, ftStr
},
156 {"FTP-Pre", HDR_FTP_PRE
, ftStr
},
157 {"FTP-Status", HDR_FTP_STATUS
, ftInt
},
158 {"FTP-Reason", HDR_FTP_REASON
, ftStr
},
159 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
162 static HttpHeaderFieldInfo
*Headers
= NULL
;
164 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
166 int tmp
= (int)aHeader
;
167 aHeader
= (http_hdr_type
)(++tmp
);
172 * headers with field values defined as #(values) in HTTP/1.1
173 * Headers that are currently not recognized, are commented out.
175 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
176 static http_hdr_type ListHeadersArr
[] = {
184 HDR_CONTENT_ENCODING
,
185 HDR_CONTENT_LANGUAGE
,
193 HDR_PROXY_CONNECTION
,
195 HDR_TRANSFER_ENCODING
,
200 HDR_WWW_AUTHENTICATE
,
201 HDR_AUTHENTICATION_INFO
,
202 HDR_PROXY_AUTHENTICATION_INFO
,
203 /* HDR_TE, HDR_TRAILER */
204 #if X_ACCELERATOR_VARY
205 HDR_X_ACCELERATOR_VARY
,
210 HDR_SURROGATE_CAPABILITY
,
211 HDR_SURROGATE_CONTROL
,
216 /* general-headers */
217 static http_hdr_type GeneralHeadersArr
[] = {
225 HDR_PROXY_CONNECTION
,
226 HDR_TRANSFER_ENCODING
,
233 static http_hdr_type EntityHeadersArr
[] = {
236 HDR_CONTENT_ENCODING
,
237 HDR_CONTENT_LANGUAGE
,
239 HDR_CONTENT_LOCATION
,
250 /* request-only headers */
251 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
252 static http_hdr_type RequestHeadersArr
[] = {
263 HDR_IF_MODIFIED_SINCE
,
266 HDR_IF_UNMODIFIED_SINCE
,
269 HDR_PROXY_AUTHORIZATION
,
275 HDR_SURROGATE_CAPABILITY
278 /* reply-only headers */
279 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
280 static http_hdr_type ReplyHeadersArr
[] = {
286 HDR_PROXY_AUTHENTICATE
,
294 HDR_WWW_AUTHENTICATE
,
298 #if X_ACCELERATOR_VARY
299 HDR_X_ACCELERATOR_VARY
,
305 HDR_SURROGATE_CONTROL
308 /* hop-by-hop headers */
309 static HttpHeaderMask HopByHopHeadersMask
;
310 static http_hdr_type HopByHopHeadersArr
[] = {
311 HDR_ALTERNATE_PROTOCOL
,
315 /*HDR_PROXY_AUTHENTICATE, // removal handled specially for peer login */
316 HDR_PROXY_AUTHORIZATION
,
319 HDR_TRANSFER_ENCODING
,
324 /* header accounting */
325 static HttpHeaderStat HttpHeaderStats
[] = {
333 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
335 static int HeaderEntryParsedCount
= 0;
338 * forward declarations and local routines
342 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
344 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
346 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
347 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
349 /** store report about current header usage and other stats */
350 static void httpHeaderStoreReport(StoreEntry
* e
);
353 * Module initialization routines
357 httpHeaderRegisterWithCacheManager(void)
359 Mgr::RegisterAction("http_headers",
360 "HTTP Header Statistics",
361 httpHeaderStoreReport
, 0, 1);
365 httpHeaderInitModule(void)
368 /* check that we have enough space for masks */
369 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
370 /* all headers must be described */
371 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
374 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
377 httpHeaderMaskInit(&ListHeadersMask
, 0);
378 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
380 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
381 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
382 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
383 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
385 httpHeaderMaskInit(&RequestHeadersMask
, 0);
386 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
387 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
388 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
390 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
391 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
393 /* init header stats */
394 assert(HttpHeaderStatCount
== hoReply
+ 1);
395 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
396 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
398 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
400 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
403 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
406 /* init dependent modules */
407 httpHdrCcInitModule();
408 httpHdrScInitModule();
410 httpHeaderRegisterWithCacheManager();
414 httpHeaderCleanModule(void)
416 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
418 httpHdrCcCleanModule();
419 httpHdrScCleanModule();
423 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
427 memset(hs
, 0, sizeof(HttpHeaderStat
));
429 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
430 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
431 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
432 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
436 * HttpHeader Implementation
439 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
441 httpHeaderMaskInit(&mask
, 0);
444 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
446 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
447 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
448 httpHeaderMaskInit(&mask
, 0);
451 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
453 httpHeaderMaskInit(&mask
, 0);
454 update(&other
, NULL
); // will update the mask as well
457 HttpHeader::~HttpHeader()
463 HttpHeader::operator =(const HttpHeader
&other
)
465 if (this != &other
) {
466 // we do not really care, but the caller probably does
467 assert(owner
== other
.owner
);
469 update(&other
, NULL
); // will update the mask as well
479 assert(owner
> hoNone
&& owner
< hoEnd
);
480 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
482 PROF_start(HttpHeaderClean
);
484 if (owner
<= hoReply
) {
486 * An unfortunate bug. The entries array is initialized
487 * such that count is set to zero. httpHeaderClean() seems to
488 * be called both when 'hdr' is created, and destroyed. Thus,
489 * we accumulate a large number of zero counts for 'hdr' before
490 * it is ever used. Can't think of a good way to fix it, except
491 * adding a state variable that indicates whether or not 'hdr'
492 * has been used. As a hack, just never count zero-sized header
495 if (!entries
.empty())
496 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
498 ++ HttpHeaderStats
[owner
].destroyedCount
;
500 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
501 } // if (owner <= hoReply)
503 for (std::vector
<HttpHeaderEntry
*>::iterator i
= entries
.begin(); i
!= entries
.end(); ++i
) {
504 HttpHeaderEntry
*e
= *i
;
507 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
508 debugs(55, DBG_CRITICAL
, "BUG: invalid entry (" << e
->id
<< "). Ignored.");
510 if (owner
<= hoReply
)
511 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
517 httpHeaderMaskInit(&mask
, 0);
519 PROF_stop(HttpHeaderClean
);
522 /* append entries (also see httpHeaderUpdate) */
524 HttpHeader::append(const HttpHeader
* src
)
526 const HttpHeaderEntry
*e
;
527 HttpHeaderPos pos
= HttpHeaderInitPos
;
530 debugs(55, 7, "appending hdr: " << this << " += " << src
);
532 while ((e
= src
->getEntry(&pos
))) {
533 addEntry(e
->clone());
537 /* use fresh entries to replace old ones */
539 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
542 old
->update (fresh
, denied_mask
);
546 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
548 const HttpHeaderEntry
*e
;
549 HttpHeaderPos pos
= HttpHeaderInitPos
;
551 assert(this != fresh
);
553 while ((e
= fresh
->getEntry(&pos
))) {
554 /* deny bad guys (ok to check for HDR_OTHER) here */
556 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
559 if (e
->id
!= HDR_OTHER
)
562 delByName(e
->name
.termedBuf());
565 pos
= HttpHeaderInitPos
;
566 while ((e
= fresh
->getEntry(&pos
))) {
567 /* deny bad guys (ok to check for HDR_OTHER) here */
569 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
572 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
574 addEntry(e
->clone());
578 /* just handy in parsing: resets and returns false */
587 HttpHeader::parse(const char *header_start
, size_t hdrLen
)
589 const char *field_ptr
= header_start
;
590 const char *header_end
= header_start
+ hdrLen
; // XXX: remove
591 HttpHeaderEntry
*e
, *e2
;
592 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
594 PROF_start(HttpHeaderParse
);
596 assert(header_start
&& header_end
);
597 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, hdrLen
));
598 ++ HttpHeaderStats
[owner
].parsedCount
;
601 if ((nulpos
= (char*)memchr(header_start
, '\0', hdrLen
))) {
602 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
603 getStringPrefix(header_start
, nulpos
-header_start
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, hdrLen
-(nulpos
-header_start
)-1));
604 PROF_stop(HttpHeaderParse
);
608 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
609 * continuation lines start with a (single) space or tab */
610 while (field_ptr
< header_end
) {
611 const char *field_start
= field_ptr
;
612 const char *field_end
;
615 const char *this_line
= field_ptr
;
616 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
620 PROF_stop(HttpHeaderParse
);
624 field_end
= field_ptr
;
626 ++field_ptr
; /* Move to next line */
628 if (field_end
> this_line
&& field_end
[-1] == '\r') {
629 --field_end
; /* Ignore CR LF */
631 if (owner
== hoRequest
&& field_end
> this_line
) {
633 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
638 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
639 "header field to prevent request smuggling attacks: {" <<
640 getStringPrefix(header_start
, hdrLen
) << "}");
641 PROF_stop(HttpHeaderParse
);
647 /* Barf on stray CR characters */
648 if (memchr(this_line
, '\r', field_end
- this_line
)) {
649 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
650 getStringPrefix(field_start
, field_end
-field_start
) << "}");
652 if (Config
.onoff
.relaxed_header_parser
) {
653 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
655 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
660 PROF_stop(HttpHeaderParse
);
665 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
666 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
667 getStringPrefix(header_start
, hdrLen
) << "}");
668 PROF_stop(HttpHeaderParse
);
671 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
673 if (field_start
== field_end
) {
674 if (field_ptr
< header_end
) {
675 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field near {" <<
676 getStringPrefix(field_start
, hdrLen
-(field_start
-header_start
)) << "}");
677 PROF_stop(HttpHeaderParse
);
681 break; /* terminating blank line */
684 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
685 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field {" <<
686 getStringPrefix(field_start
, field_end
-field_start
) << "}");
687 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, hdrLen
) << "}");
689 if (Config
.onoff
.relaxed_header_parser
)
692 PROF_stop(HttpHeaderParse
);
696 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
697 if (e
->value
!= e2
->value
) {
699 debugs(55, warnOnError
, "WARNING: found two conflicting content-length headers in {" <<
700 getStringPrefix(header_start
, hdrLen
) << "}");
702 if (!Config
.onoff
.relaxed_header_parser
) {
704 PROF_stop(HttpHeaderParse
);
708 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
709 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
712 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
713 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
715 } else if (l1
> l2
) {
722 debugs(55, warnOnError
, "NOTICE: found double content-length header");
725 if (Config
.onoff
.relaxed_header_parser
)
728 PROF_stop(HttpHeaderParse
);
733 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
734 debugs(55, warnOnError
, "WARNING: found whitespace in HTTP header name {" <<
735 getStringPrefix(field_start
, field_end
-field_start
) << "}");
737 if (!Config
.onoff
.relaxed_header_parser
) {
739 PROF_stop(HttpHeaderParse
);
748 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
749 delById(HDR_CONTENT_LENGTH
);
752 PROF_stop(HttpHeaderParse
);
753 return 1; /* even if no fields where found, it is a valid header */
756 /* packs all the entries using supplied packer */
758 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
760 HttpHeaderPos pos
= HttpHeaderInitPos
;
761 const HttpHeaderEntry
*e
;
763 debugs(55, 7, this << " into " << p
<<
764 (mask_sensitive_info
? " while masking" : ""));
765 /* pack all entries one by one */
766 while ((e
= getEntry(&pos
))) {
767 if (!mask_sensitive_info
) {
772 bool maskThisEntry
= false;
774 case HDR_AUTHORIZATION
:
775 case HDR_PROXY_AUTHORIZATION
:
776 maskThisEntry
= true;
779 case HDR_FTP_ARGUMENTS
:
780 if (const HttpHeaderEntry
*cmd
= findEntry(HDR_FTP_COMMAND
))
781 maskThisEntry
= (cmd
->value
== "PASS");
788 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
789 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
795 /* Pack in the "special" entries */
800 /* returns next valid entry */
802 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
805 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
807 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
809 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
816 * returns a pointer to a specified entry if any
817 * note that we return one entry so it does not make much sense to ask for
821 HttpHeader::findEntry(http_hdr_type id
) const
823 HttpHeaderPos pos
= HttpHeaderInitPos
;
826 assert(!CBIT_TEST(ListHeadersMask
, id
));
828 /* check mask first */
830 if (!CBIT_TEST(mask
, id
))
833 /* looks like we must have it, do linear search */
834 while ((e
= getEntry(&pos
))) {
839 /* hm.. we thought it was there, but it was not found */
842 return NULL
; /* not reached */
846 * same as httpHeaderFindEntry
849 HttpHeader::findLastEntry(http_hdr_type id
) const
851 HttpHeaderPos pos
= HttpHeaderInitPos
;
853 HttpHeaderEntry
*result
= NULL
;
855 assert(!CBIT_TEST(ListHeadersMask
, id
));
857 /* check mask first */
859 if (!CBIT_TEST(mask
, id
))
862 /* looks like we must have it, do linear search */
863 while ((e
= getEntry(&pos
))) {
868 assert(result
); /* must be there! */
873 * deletes all fields with a given name if any, returns #fields deleted;
876 HttpHeader::delByName(const char *name
)
879 HttpHeaderPos pos
= HttpHeaderInitPos
;
881 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
882 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
884 while ((e
= getEntry(&pos
))) {
885 if (!e
->name
.caseCmp(name
))
888 CBIT_SET(mask
, e
->id
);
894 /* deletes all entries with a given id, returns the #entries deleted */
896 HttpHeader::delById(http_hdr_type id
)
899 HttpHeaderPos pos
= HttpHeaderInitPos
;
901 debugs(55, 8, this << " del-by-id " << id
);
903 assert(id
!= HDR_OTHER
); /* does not make sense */
905 if (!CBIT_TEST(mask
, id
))
908 while ((e
= getEntry(&pos
))) {
919 * deletes an entry at pos and leaves a gap; leaving a gap makes it
920 * possible to iterate(search) and delete fields at the same time
921 * NOTE: Does not update the header mask. Caller must follow up with
922 * a call to refreshMask() if headers_deleted was incremented.
925 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
928 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
929 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
931 /* decrement header length, allow for ": " and crlf */
932 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
939 * Compacts the header storage
942 HttpHeader::compact()
944 // TODO: optimize removal, or possibly make it so that's not needed.
945 std::vector
<HttpHeaderEntry
*>::iterator newend
;
946 newend
= std::remove(entries
.begin(), entries
.end(), static_cast<HttpHeaderEntry
*>(NULL
));
947 entries
.resize(newend
-entries
.begin());
951 * Refreshes the header mask. Required after delAt() calls.
954 HttpHeader::refreshMask()
956 httpHeaderMaskInit(&mask
, 0);
957 debugs(55, 7, "refreshing the mask in hdr " << this);
958 HttpHeaderPos pos
= HttpHeaderInitPos
;
959 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
960 CBIT_SET(mask
, e
->id
);
965 * does not call e->clone() so one should not reuse "*e"
968 HttpHeader::addEntry(HttpHeaderEntry
* e
)
972 assert(e
->name
.size());
974 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
976 if (CBIT_TEST(mask
, e
->id
))
977 ++ Headers
[e
->id
].stat
.repCount
;
979 CBIT_SET(mask
, e
->id
);
981 entries
.push_back(e
);
983 /* increment header length, allow for ": " and crlf */
984 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
988 * does not call e->clone() so one should not reuse "*e"
991 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
996 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
998 if (CBIT_TEST(mask
, e
->id
))
999 ++ Headers
[e
->id
].stat
.repCount
;
1001 CBIT_SET(mask
, e
->id
);
1003 entries
.insert(entries
.begin(),e
);
1005 /* increment header length, allow for ": " and crlf */
1006 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
1010 HttpHeader::getList(http_hdr_type id
, String
*s
) const
1013 HttpHeaderPos pos
= HttpHeaderInitPos
;
1014 debugs(55, 9, this << " joining for id " << id
);
1015 /* only fields from ListHeaders array can be "listed" */
1016 assert(CBIT_TEST(ListHeadersMask
, id
));
1018 if (!CBIT_TEST(mask
, id
))
1021 while ((e
= getEntry(&pos
))) {
1023 strListAdd(s
, e
->value
.termedBuf(), ',');
1027 * note: we might get an empty (size==0) string if there was an "empty"
1028 * header. This results in an empty length String, which may have a NULL
1031 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1033 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1035 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1040 /* return a list of entries with the same id separated by ',' and ws */
1042 HttpHeader::getList(http_hdr_type id
) const
1045 HttpHeaderPos pos
= HttpHeaderInitPos
;
1046 debugs(55, 9, this << "joining for id " << id
);
1047 /* only fields from ListHeaders array can be "listed" */
1048 assert(CBIT_TEST(ListHeadersMask
, id
));
1050 if (!CBIT_TEST(mask
, id
))
1055 while ((e
= getEntry(&pos
))) {
1057 strListAdd(&s
, e
->value
.termedBuf(), ',');
1061 * note: we might get an empty (size==0) string if there was an "empty"
1062 * header. This results in an empty length String, which may have a NULL
1065 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1067 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1069 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1074 /* return a string or list of entries with the same id separated by ',' and ws */
1076 HttpHeader::getStrOrList(http_hdr_type id
) const
1080 if (CBIT_TEST(ListHeadersMask
, id
))
1083 if ((e
= findEntry(id
)))
1090 * Returns the value of the specified header and/or an undefined String.
1093 HttpHeader::getByName(const char *name
) const
1096 // ignore presence: return undefined string if an empty header is present
1097 (void)getByNameIfPresent(name
, result
);
1102 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
1105 HttpHeaderPos pos
= HttpHeaderInitPos
;
1110 /* First try the quick path */
1111 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1116 result
= getStrOrList(id
);
1120 /* Sorry, an unknown header name. Do linear search */
1122 while ((e
= getEntry(&pos
))) {
1123 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1125 strListAdd(&result
, e
->value
.termedBuf(), ',');
1133 * Returns a the value of the specified list member, if any.
1136 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1139 const char *pos
= NULL
;
1142 int mlen
= strlen(member
);
1146 header
= getByName(name
);
1150 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1151 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1152 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1161 * returns a the value of the specified list member, if any.
1164 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1167 const char *pos
= NULL
;
1170 int mlen
= strlen(member
);
1174 header
= getStrOrList(id
);
1177 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1178 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1179 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1188 /* test if a field is present */
1190 HttpHeader::has(http_hdr_type id
) const
1193 assert(id
!= HDR_OTHER
);
1194 debugs(55, 9, this << " lookup for " << id
);
1195 return CBIT_TEST(mask
, id
);
1199 HttpHeader::putInt(http_hdr_type id
, int number
)
1202 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1203 assert(number
>= 0);
1204 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1208 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1211 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1212 assert(number
>= 0);
1213 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1217 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1220 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1222 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1226 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1229 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1231 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1235 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1238 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1240 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1244 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1246 assert(auth_scheme
&& realm
);
1247 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1251 HttpHeader::putCc(const HttpHdrCc
* cc
)
1256 /* remove old directives if any */
1257 delById(HDR_CACHE_CONTROL
);
1260 packerToMemInit(&p
, &mb
);
1263 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1270 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1275 /* remove old directives if any */
1276 delById(HDR_CONTENT_RANGE
);
1279 packerToMemInit(&p
, &mb
);
1280 httpHdrContRangePackInto(cr
, &p
);
1282 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1289 HttpHeader::putRange(const HttpHdrRange
* range
)
1294 /* remove old directives if any */
1298 packerToMemInit(&p
, &mb
);
1299 range
->packInto(&p
);
1301 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1308 HttpHeader::putSc(HttpHdrSc
*sc
)
1313 /* remove old directives if any */
1314 delById(HDR_SURROGATE_CONTROL
);
1317 packerToMemInit(&p
, &mb
);
1320 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1327 HttpHeader::putWarning(const int code
, const char *const text
)
1330 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1331 putStr(HDR_WARNING
, buf
);
1334 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1336 HttpHeader::putExt(const char *name
, const char *value
)
1338 assert(name
&& value
);
1339 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1340 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1344 HttpHeader::getInt(http_hdr_type id
) const
1347 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1350 if ((e
= findEntry(id
)))
1357 HttpHeader::getInt64(http_hdr_type id
) const
1360 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1363 if ((e
= findEntry(id
)))
1364 return e
->getInt64();
1370 HttpHeader::getTime(http_hdr_type id
) const
1375 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1377 if ((e
= findEntry(id
))) {
1378 value
= parse_rfc1123(e
->value
.termedBuf());
1379 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1385 /* sync with httpHeaderGetLastStr */
1387 HttpHeader::getStr(http_hdr_type id
) const
1391 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1393 if ((e
= findEntry(id
))) {
1394 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1395 return e
->value
.termedBuf();
1403 HttpHeader::getLastStr(http_hdr_type id
) const
1407 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1409 if ((e
= findLastEntry(id
))) {
1410 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1411 return e
->value
.termedBuf();
1418 HttpHeader::getCc() const
1420 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1422 PROF_start(HttpHeader_getCc
);
1425 getList(HDR_CACHE_CONTROL
, &s
);
1427 HttpHdrCc
*cc
=new HttpHdrCc();
1429 if (!cc
->parse(s
)) {
1434 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1437 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1439 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1441 PROF_stop(HttpHeader_getCc
);
1447 HttpHeader::getRange() const
1449 HttpHdrRange
*r
= NULL
;
1451 /* some clients will send "Request-Range" _and_ *matching* "Range"
1452 * who knows, some clients might send Request-Range only;
1453 * this "if" should work correctly in both cases;
1454 * hopefully no clients send mismatched headers! */
1456 if ((e
= findEntry(HDR_RANGE
)) ||
1457 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1458 r
= HttpHdrRange::ParseCreate(&e
->value
);
1459 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1466 HttpHeader::getSc() const
1468 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1473 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1475 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1477 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1480 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1482 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1488 HttpHeader::getContRange() const
1490 HttpHdrContRange
*cr
= NULL
;
1493 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1494 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1495 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1502 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1506 assert(auth_scheme
);
1509 if (!field
) /* no authorization field */
1512 l
= strlen(auth_scheme
);
1514 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1519 if (!xisspace(*field
)) /* wrong scheme */
1522 /* skip white space */
1523 for (; field
&& xisspace(*field
); ++field
);
1525 if (!*field
) /* no authorization cookie */
1528 static char decodedAuthToken
[8192];
1529 struct base64_decode_ctx ctx
;
1530 base64_decode_init(&ctx
);
1531 size_t decodedLen
= 0;
1532 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(decodedAuthToken
), strlen(field
), reinterpret_cast<const uint8_t*>(field
)) ||
1533 !base64_decode_final(&ctx
)) {
1536 decodedAuthToken
[decodedLen
] = '\0';
1537 return decodedAuthToken
;
1541 HttpHeader::getETag(http_hdr_type id
) const
1543 ETag etag
= {NULL
, -1};
1545 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1547 if ((e
= findEntry(id
)))
1548 etagParseInit(&etag
, e
->value
.termedBuf());
1554 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1558 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1559 memset(&tot
, 0, sizeof(tot
));
1561 if ((e
= findEntry(id
))) {
1562 const char *str
= e
->value
.termedBuf();
1563 /* try as an ETag */
1565 if (etagParseInit(&tot
.tag
, str
)) {
1566 tot
.valid
= tot
.tag
.str
!= NULL
;
1569 /* or maybe it is time? */
1570 tot
.time
= parse_rfc1123(str
);
1571 tot
.valid
= tot
.time
>= 0;
1576 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1584 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1589 if (id
!= HDR_OTHER
)
1590 name
= Headers
[id
].name
;
1596 ++ Headers
[id
].stat
.aliveCount
;
1598 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1601 HttpHeaderEntry::~HttpHeaderEntry()
1604 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1605 /* clean name if needed */
1607 if (id
== HDR_OTHER
)
1612 assert(Headers
[id
].stat
.aliveCount
);
1614 -- Headers
[id
].stat
.aliveCount
;
1619 /* parses and inits header entry, returns true/false */
1621 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1623 /* note: name_start == field_start */
1624 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1625 int name_len
= name_end
? name_end
- field_start
:0;
1626 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1627 /* note: value_end == field_end */
1629 ++ HeaderEntryParsedCount
;
1631 /* do we have a valid field name within this field? */
1633 if (!name_len
|| name_end
> field_end
)
1636 if (name_len
> 65534) {
1637 /* String must be LESS THAN 64K and it adds a terminating NULL */
1638 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1642 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1643 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1644 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1646 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1653 /* now we know we can parse it */
1655 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1657 /* is it a "known" field? */
1658 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1669 /* set field name */
1670 if (id
== HDR_OTHER
)
1671 name
.limitInit(field_start
, name_len
);
1673 name
= Headers
[id
].name
;
1675 /* trim field value */
1676 while (value_start
< field_end
&& xisspace(*value_start
))
1679 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1682 if (field_end
- value_start
> 65534) {
1683 /* String must be LESS THAN 64K and it adds a terminating NULL */
1684 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1686 if (id
== HDR_OTHER
)
1692 /* set field value */
1693 value
.limitInit(value_start
, field_end
- value_start
);
1695 ++ Headers
[id
].stat
.seenCount
;
1697 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1699 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1703 HttpHeaderEntry::clone() const
1705 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1709 HttpHeaderEntry::packInto(Packer
* p
) const
1712 packerAppend(p
, name
.rawBuf(), name
.size());
1713 packerAppend(p
, ": ", 2);
1714 packerAppend(p
, value
.rawBuf(), value
.size());
1715 packerAppend(p
, "\r\n", 2);
1719 HttpHeaderEntry::getInt() const
1722 assert (Headers
[id
].type
== ftInt
);
1724 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1725 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1726 /* XXX: Should we check ok - ie
1727 * return ok ? -1 : value;
1733 HttpHeaderEntry::getInt64() const
1736 assert (Headers
[id
].type
== ftInt64
);
1738 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1739 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1740 /* XXX: Should we check ok - ie
1741 * return ok ? -1 : value;
1747 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1749 ++ Headers
[id
].stat
.parsCount
;
1752 ++ Headers
[id
].stat
.errCount
;
1753 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1761 /* tmp variable used to pass stat info to dumpers */
1762 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1763 const HttpHeaderStat
*dump_stat
= NULL
;
1766 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1768 const int id
= (int) val
;
1769 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1770 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1771 int visible
= count
> 0;
1772 /* for entries with zero count, list only those that belong to current type of message */
1774 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1775 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1778 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1779 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1783 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1786 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1787 idx
, (int) val
, count
,
1788 xpercent(count
, dump_stat
->destroyedCount
));
1792 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1797 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1798 storeAppendPrintf(e
, "\nField type distribution\n");
1799 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1800 "id", "name", "count", "#/header");
1801 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1802 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1803 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1804 "id", "name", "count", "#/cc_field");
1805 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1806 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1807 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1808 "id", "name", "count", "#/sc_field");
1809 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1810 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1811 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1812 "id", "#flds", "count", "%total");
1813 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1814 storeAppendPrintf(e
, "\n");
1819 httpHeaderStoreReport(StoreEntry
* e
)
1825 HttpHeaderStats
[0].parsedCount
=
1826 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1827 HttpHeaderStats
[0].ccParsedCount
=
1828 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1829 HttpHeaderStats
[0].destroyedCount
=
1830 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1831 HttpHeaderStats
[0].busyDestroyedCount
=
1832 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1834 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1835 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1838 /* field stats for all messages */
1839 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1841 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1842 "id", "name", "#alive", "%err", "%repeat");
1844 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1845 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1846 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1847 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1848 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1849 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1852 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1853 HttpHeaderStats
[hoRequest
].parsedCount
,
1854 HttpHeaderStats
[hoReply
].parsedCount
,
1855 HttpHeaderStats
[0].parsedCount
);
1856 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1860 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1863 for (int i
= 0; i
< end
; ++i
) {
1864 if (name_len
!= info
[i
].name
.size())
1867 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1876 httpHeaderIdByNameDef(const char *name
, int name_len
)
1879 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1881 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1885 httpHeaderNameById(int id
)
1888 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1890 assert(id
>= 0 && id
< HDR_ENUM_END
);
1892 return Headers
[id
].name
.termedBuf();
1896 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1899 const char *pos
= NULL
;
1902 int mlen
= strlen(member
);
1906 String
header (getStrOrList(id
));
1908 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1909 if (strncasecmp(item
, member
, mlen
) == 0
1910 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1920 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1923 const char *pos
= NULL
;
1926 int mlen
= strlen(member
);
1930 String
header (getByName(name
));
1932 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1933 if (strncasecmp(item
, member
, mlen
) == 0
1934 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1944 HttpHeader::removeHopByHopEntries()
1946 removeConnectionHeaderEntries();
1948 const HttpHeaderEntry
*e
;
1949 HttpHeaderPos pos
= HttpHeaderInitPos
;
1950 int headers_deleted
= 0;
1951 while ((e
= getEntry(&pos
))) {
1953 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1954 delAt(pos
, headers_deleted
);
1961 HttpHeader::removeConnectionHeaderEntries()
1963 if (has(HDR_CONNECTION
)) {
1964 /* anything that matches Connection list member will be deleted */
1965 String strConnection
;
1967 (void) getList(HDR_CONNECTION
, &strConnection
);
1968 const HttpHeaderEntry
*e
;
1969 HttpHeaderPos pos
= HttpHeaderInitPos
;
1971 * think: on-average-best nesting of the two loops (hdrEntry
1972 * and strListItem) @?@
1975 * maybe we should delete standard stuff ("keep-alive","close")
1976 * from strConnection first?
1979 int headers_deleted
= 0;
1980 while ((e
= getEntry(&pos
))) {
1981 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1982 delAt(pos
, headers_deleted
);
1984 if (headers_deleted
)