2 * DEBUG: section 55 HTTP Header
3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "HttpHdrCc.h"
36 #include "HttpHdrContRange.h"
37 #include "HttpHdrSc.h"
38 #include "HttpHeader.h"
39 #include "HttpHeaderFieldInfo.h"
40 #include "HttpHeaderStat.h"
41 #include "HttpHeaderTools.h"
43 #include "mgr/Registration.h"
44 #include "profiler/Profiler.h"
49 #include "SquidConfig.h"
50 #include "SquidString.h"
51 #include "TimeOrTag.h"
54 * On naming conventions:
56 * HTTP/1.1 defines message-header as
58 * message-header = field-name ":" [ field-value ] CRLF
60 * field-value = *( field-content | LWS )
62 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
63 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
65 * HttpHeader is an object that represents all message-headers in a message.
66 * HttpHeader does not manage start-line.
68 * HttpHeader is implemented as a collection of header "entries".
69 * An entry is a (field_id, field_name, field_value) triplet.
73 * local constants and vars
77 * A table with major attributes for every known field.
78 * We calculate name lengths and reorganize this array on start up.
79 * After reorganization, field id can be used as an index to the table.
81 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
82 {"Accept", HDR_ACCEPT
, ftStr
},
84 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
85 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
86 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
87 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
88 {"Age", HDR_AGE
, ftInt
},
89 {"Allow", HDR_ALLOW
, ftStr
},
90 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
91 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
92 {"Connection", HDR_CONNECTION
, ftStr
},
93 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
94 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
95 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
96 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
97 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
98 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
99 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
100 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
101 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
102 {"Cookie", HDR_COOKIE
, ftStr
},
103 {"Cookie2", HDR_COOKIE2
, ftStr
},
104 {"Date", HDR_DATE
, ftDate_1123
},
105 {"ETag", HDR_ETAG
, ftETag
},
106 {"Expect", HDR_EXPECT
, ftStr
},
107 {"Expires", HDR_EXPIRES
, ftDate_1123
},
108 {"From", HDR_FROM
, ftStr
},
109 {"Host", HDR_HOST
, ftStr
},
110 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
111 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
112 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
113 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
114 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
115 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
116 {"Link", HDR_LINK
, ftStr
},
117 {"Location", HDR_LOCATION
, ftStr
},
118 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
119 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
120 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
121 {"Origin", HDR_ORIGIN
, ftStr
},
122 {"Pragma", HDR_PRAGMA
, ftStr
},
123 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
124 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
125 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
126 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
127 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
128 {"Public", HDR_PUBLIC
, ftStr
},
129 {"Range", HDR_RANGE
, ftPRange
},
130 {"Referer", HDR_REFERER
, ftStr
},
131 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
132 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
133 {"Server", HDR_SERVER
, ftStr
},
134 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
135 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
136 {"TE", HDR_TE
, ftStr
},
137 {"Title", HDR_TITLE
, ftStr
},
138 {"Trailer", HDR_TRAILER
, ftStr
},
139 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
140 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
141 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
142 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
143 {"User-Agent", HDR_USER_AGENT
, ftStr
},
144 {"Vary", HDR_VARY
, ftStr
}, /* for now */
145 {"Via", HDR_VIA
, ftStr
}, /* for now */
146 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
147 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
148 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
149 {"X-Cache", HDR_X_CACHE
, ftStr
},
150 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
151 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
152 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
153 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
154 #if X_ACCELERATOR_VARY
155 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
158 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
160 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
161 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
162 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
163 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
166 static HttpHeaderFieldInfo
*Headers
= NULL
;
168 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
170 int tmp
= (int)aHeader
;
171 aHeader
= (http_hdr_type
)(++tmp
);
176 * headers with field values defined as #(values) in HTTP/1.1
177 * Headers that are currently not recognized, are commented out.
179 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
180 static http_hdr_type ListHeadersArr
[] = {
182 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
183 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
185 HDR_CONTENT_ENCODING
,
186 HDR_CONTENT_LANGUAGE
,
189 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
190 HDR_LINK
, HDR_PRAGMA
,
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
,
213 /* general-headers */
214 static http_hdr_type GeneralHeadersArr
[] = {
215 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
216 HDR_TRANSFER_ENCODING
,
223 static http_hdr_type EntityHeadersArr
[] = {
224 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
225 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
226 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
230 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
231 static http_hdr_type ReplyHeadersArr
[] = {
232 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
233 HDR_ACCEPT_RANGES
, HDR_AGE
,
234 HDR_LOCATION
, HDR_MAX_FORWARDS
,
235 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
, HDR_SET_COOKIE2
,
238 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
241 #if X_ACCELERATOR_VARY
242 HDR_X_ACCELERATOR_VARY
,
248 HDR_SURROGATE_CONTROL
251 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
252 static http_hdr_type RequestHeadersArr
[] = {
253 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
254 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
255 HDR_IF_RANGE
, HDR_MAX_FORWARDS
,
257 HDR_PROXY_CONNECTION
,
258 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
259 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
262 static HttpHeaderMask HopByHopHeadersMask
;
263 static http_hdr_type HopByHopHeadersArr
[] = {
264 HDR_CONNECTION
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
265 HDR_TE
, HDR_TRAILER
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
268 /* header accounting */
269 static HttpHeaderStat HttpHeaderStats
[] = {
277 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
279 static int HeaderEntryParsedCount
= 0;
282 * forward declarations and local routines
286 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
288 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
290 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
291 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
293 /** store report about current header usage and other stats */
294 static void httpHeaderStoreReport(StoreEntry
* e
);
297 * Module initialization routines
301 httpHeaderRegisterWithCacheManager(void)
303 Mgr::RegisterAction("http_headers",
304 "HTTP Header Statistics",
305 httpHeaderStoreReport
, 0, 1);
309 httpHeaderInitModule(void)
312 /* check that we have enough space for masks */
313 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
314 /* all headers must be described */
315 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
318 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
321 httpHeaderMaskInit(&ListHeadersMask
, 0);
323 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
325 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
327 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
329 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
331 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
333 httpHeaderMaskInit(&RequestHeadersMask
, 0);
335 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
337 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
339 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
341 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
343 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
345 /* init header stats */
346 assert(HttpHeaderStatCount
== hoReply
+ 1);
348 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
349 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
351 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
353 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
357 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
360 /* init dependent modules */
361 httpHdrCcInitModule();
363 httpHdrScInitModule();
365 httpHeaderRegisterWithCacheManager();
369 httpHeaderCleanModule(void)
371 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
373 httpHdrCcCleanModule();
374 httpHdrScCleanModule();
378 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
382 memset(hs
, 0, sizeof(HttpHeaderStat
));
384 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
385 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
386 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
387 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
391 * HttpHeader Implementation
394 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
396 httpHeaderMaskInit(&mask
, 0);
399 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
401 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
402 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
403 httpHeaderMaskInit(&mask
, 0);
406 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
408 httpHeaderMaskInit(&mask
, 0);
409 update(&other
, NULL
); // will update the mask as well
412 HttpHeader::~HttpHeader()
418 HttpHeader::operator =(const HttpHeader
&other
)
420 if (this != &other
) {
421 // we do not really care, but the caller probably does
422 assert(owner
== other
.owner
);
424 update(&other
, NULL
); // will update the mask as well
433 HttpHeaderPos pos
= HttpHeaderInitPos
;
436 assert(owner
> hoNone
&& owner
< hoEnd
);
437 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
439 PROF_start(HttpHeaderClean
);
442 * An unfortunate bug. The entries array is initialized
443 * such that count is set to zero. httpHeaderClean() seems to
444 * be called both when 'hdr' is created, and destroyed. Thus,
445 * we accumulate a large number of zero counts for 'hdr' before
446 * it is ever used. Can't think of a good way to fix it, except
447 * adding a state variable that indicates whether or not 'hdr'
448 * has been used. As a hack, just never count zero-sized header
452 if (owner
<= hoReply
) {
453 if (0 != entries
.count
)
454 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.count
);
456 ++ HttpHeaderStats
[owner
].destroyedCount
;
458 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
460 while ((e
= getEntry(&pos
))) {
461 /* tmp hack to try to avoid coredumps */
463 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
464 debugs(55, DBG_CRITICAL
, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
466 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
467 /* yes, this deletion leaves us in an inconsistent state */
471 } // if (owner <= hoReply)
473 httpHeaderMaskInit(&mask
, 0);
475 PROF_stop(HttpHeaderClean
);
478 /* append entries (also see httpHeaderUpdate) */
480 HttpHeader::append(const HttpHeader
* src
)
482 const HttpHeaderEntry
*e
;
483 HttpHeaderPos pos
= HttpHeaderInitPos
;
486 debugs(55, 7, "appending hdr: " << this << " += " << src
);
488 while ((e
= src
->getEntry(&pos
))) {
489 addEntry(e
->clone());
493 /* use fresh entries to replace old ones */
495 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
498 old
->update (fresh
, denied_mask
);
502 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
504 const HttpHeaderEntry
*e
;
505 HttpHeaderPos pos
= HttpHeaderInitPos
;
507 assert(this != fresh
);
509 while ((e
= fresh
->getEntry(&pos
))) {
510 /* deny bad guys (ok to check for HDR_OTHER) here */
512 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
515 if (e
->id
!= HDR_OTHER
)
518 delByName(e
->name
.termedBuf());
521 pos
= HttpHeaderInitPos
;
522 while ((e
= fresh
->getEntry(&pos
))) {
523 /* deny bad guys (ok to check for HDR_OTHER) here */
525 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
528 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
530 addEntry(e
->clone());
534 /* just handy in parsing: resets and returns false */
543 HttpHeader::parse(const char *header_start
, const char *header_end
)
545 const char *field_ptr
= header_start
;
546 HttpHeaderEntry
*e
, *e2
;
548 PROF_start(HttpHeaderParse
);
550 assert(header_start
&& header_end
);
551 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
552 ++ HttpHeaderStats
[owner
].parsedCount
;
555 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
556 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
557 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
561 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
562 * continuation lines start with a (single) space or tab */
563 while (field_ptr
< header_end
) {
564 const char *field_start
= field_ptr
;
565 const char *field_end
;
568 const char *this_line
= field_ptr
;
569 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
572 goto reset
; /* missing <LF> */
574 field_end
= field_ptr
;
576 ++field_ptr
; /* Move to next line */
578 if (field_end
> this_line
&& field_end
[-1] == '\r') {
579 --field_end
; /* Ignore CR LF */
581 if (owner
== hoRequest
&& field_end
> this_line
) {
583 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
588 debugs(55, DBG_IMPORTANT
, "WARNING: Rejecting HTTP request with a CR+ "
589 "header field to prevent request smuggling attacks: {" <<
590 getStringPrefix(header_start
, header_end
) << "}");
596 /* Barf on stray CR characters */
597 if (memchr(this_line
, '\r', field_end
- this_line
)) {
598 debugs(55, DBG_IMPORTANT
, "WARNING: suspicious CR characters in HTTP header {" <<
599 getStringPrefix(field_start
, field_end
) << "}");
601 if (Config
.onoff
.relaxed_header_parser
) {
602 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
604 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
612 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
613 debugs(55, DBG_IMPORTANT
, "WARNING: Blank continuation line in HTTP header {" <<
614 getStringPrefix(header_start
, header_end
) << "}");
617 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
619 if (field_start
== field_end
) {
620 if (field_ptr
< header_end
) {
621 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field near {" <<
622 getStringPrefix(field_start
, header_end
) << "}");
626 break; /* terminating blank line */
629 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
630 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field {" <<
631 getStringPrefix(field_start
, field_end
) << "}");
632 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
633 " in {" << getStringPrefix(header_start
, header_end
) << "}");
635 if (Config
.onoff
.relaxed_header_parser
)
641 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
642 // if (e->value.cmp(e2->value.termedBuf()) != 0) {
643 if (e
->value
!= e2
->value
) {
645 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
646 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
648 if (!Config
.onoff
.relaxed_header_parser
) {
653 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
654 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
657 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
658 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
660 } else if (l1
> l2
) {
667 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
668 "NOTICE: found double content-length header");
670 if (Config
.onoff
.relaxed_header_parser
) {
680 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
681 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
682 "WARNING: found whitespace in HTTP header name {" <<
683 getStringPrefix(field_start
, field_end
) << "}");
685 if (!Config
.onoff
.relaxed_header_parser
) {
695 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
696 delById(HDR_CONTENT_LENGTH
);
699 PROF_stop(HttpHeaderParse
);
700 return 1; /* even if no fields where found, it is a valid header */
702 PROF_stop(HttpHeaderParse
);
706 /* packs all the entries using supplied packer */
708 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
710 HttpHeaderPos pos
= HttpHeaderInitPos
;
711 const HttpHeaderEntry
*e
;
713 debugs(55, 7, "packing hdr: (" << this << ")");
714 /* pack all entries one by one */
715 while ((e
= getEntry(&pos
))) {
716 if (!mask_sensitive_info
) {
721 case HDR_AUTHORIZATION
:
722 case HDR_PROXY_AUTHORIZATION
:
723 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
724 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
731 /* Pack in the "special" entries */
736 /* returns next valid entry */
738 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
741 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
743 for (++(*pos
); *pos
< (ssize_t
)entries
.count
; ++(*pos
)) {
744 if (entries
.items
[*pos
])
745 return (HttpHeaderEntry
*)entries
.items
[*pos
];
752 * returns a pointer to a specified entry if any
753 * note that we return one entry so it does not make much sense to ask for
757 HttpHeader::findEntry(http_hdr_type id
) const
759 HttpHeaderPos pos
= HttpHeaderInitPos
;
762 assert(!CBIT_TEST(ListHeadersMask
, id
));
764 /* check mask first */
766 if (!CBIT_TEST(mask
, id
))
769 /* looks like we must have it, do linear search */
770 while ((e
= getEntry(&pos
))) {
775 /* hm.. we thought it was there, but it was not found */
778 return NULL
; /* not reached */
782 * same as httpHeaderFindEntry
785 HttpHeader::findLastEntry(http_hdr_type id
) const
787 HttpHeaderPos pos
= HttpHeaderInitPos
;
789 HttpHeaderEntry
*result
= NULL
;
791 assert(!CBIT_TEST(ListHeadersMask
, id
));
793 /* check mask first */
795 if (!CBIT_TEST(mask
, id
))
798 /* looks like we must have it, do linear search */
799 while ((e
= getEntry(&pos
))) {
804 assert(result
); /* must be there! */
809 * deletes all fields with a given name if any, returns #fields deleted;
812 HttpHeader::delByName(const char *name
)
815 HttpHeaderPos pos
= HttpHeaderInitPos
;
817 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
818 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
820 while ((e
= getEntry(&pos
))) {
821 if (!e
->name
.caseCmp(name
))
824 CBIT_SET(mask
, e
->id
);
830 /* deletes all entries with a given id, returns the #entries deleted */
832 HttpHeader::delById(http_hdr_type id
)
835 HttpHeaderPos pos
= HttpHeaderInitPos
;
837 debugs(55, 8, this << " del-by-id " << id
);
839 assert(id
!= HDR_OTHER
); /* does not make sense */
841 if (!CBIT_TEST(mask
, id
))
844 while ((e
= getEntry(&pos
))) {
855 * deletes an entry at pos and leaves a gap; leaving a gap makes it
856 * possible to iterate(search) and delete fields at the same time
857 * NOTE: Does not update the header mask. Caller must follow up with
858 * a call to refreshMask() if headers_deleted was incremented.
861 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
864 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
865 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
866 entries
.items
[pos
] = NULL
;
867 /* decrement header length, allow for ": " and crlf */
868 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
875 * Compacts the header storage
878 HttpHeader::compact()
884 * Refreshes the header mask. Required after delAt() calls.
887 HttpHeader::refreshMask()
889 httpHeaderMaskInit(&mask
, 0);
890 debugs(55, 7, "refreshing the mask in hdr " << this);
891 HttpHeaderPos pos
= HttpHeaderInitPos
;
892 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
893 CBIT_SET(mask
, e
->id
);
898 * does not call e->clone() so one should not reuse "*e"
901 HttpHeader::addEntry(HttpHeaderEntry
* e
)
905 assert(e
->name
.size());
907 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
909 if (CBIT_TEST(mask
, e
->id
))
910 ++ Headers
[e
->id
].stat
.repCount
;
912 CBIT_SET(mask
, e
->id
);
914 entries
.push_back(e
);
916 /* increment header length, allow for ": " and crlf */
917 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
921 * does not call e->clone() so one should not reuse "*e"
924 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
929 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
931 if (CBIT_TEST(mask
, e
->id
))
932 ++ Headers
[e
->id
].stat
.repCount
;
934 CBIT_SET(mask
, e
->id
);
938 /* increment header length, allow for ": " and crlf */
939 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
943 HttpHeader::getList(http_hdr_type id
, String
*s
) const
946 HttpHeaderPos pos
= HttpHeaderInitPos
;
947 debugs(55, 9, this << " joining for id " << id
);
948 /* only fields from ListHeaders array can be "listed" */
949 assert(CBIT_TEST(ListHeadersMask
, id
));
951 if (!CBIT_TEST(mask
, id
))
954 while ((e
= getEntry(&pos
))) {
956 strListAdd(s
, e
->value
.termedBuf(), ',');
960 * note: we might get an empty (size==0) string if there was an "empty"
961 * header. This results in an empty length String, which may have a NULL
964 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
966 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
968 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
973 /* return a list of entries with the same id separated by ',' and ws */
975 HttpHeader::getList(http_hdr_type id
) const
978 HttpHeaderPos pos
= HttpHeaderInitPos
;
979 debugs(55, 9, this << "joining for id " << id
);
980 /* only fields from ListHeaders array can be "listed" */
981 assert(CBIT_TEST(ListHeadersMask
, id
));
983 if (!CBIT_TEST(mask
, id
))
988 while ((e
= getEntry(&pos
))) {
990 strListAdd(&s
, e
->value
.termedBuf(), ',');
994 * note: we might get an empty (size==0) string if there was an "empty"
995 * header. This results in an empty length String, which may have a NULL
998 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1000 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1002 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1007 /* return a string or list of entries with the same id separated by ',' and ws */
1009 HttpHeader::getStrOrList(http_hdr_type id
) const
1013 if (CBIT_TEST(ListHeadersMask
, id
))
1016 if ((e
= findEntry(id
)))
1023 * Returns the value of the specified header.
1026 HttpHeader::getByName(const char *name
) const
1029 HttpHeaderPos pos
= HttpHeaderInitPos
;
1034 /* First try the quick path */
1035 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1038 return getStrOrList(id
);
1042 /* Sorry, an unknown header name. Do linear search */
1043 while ((e
= getEntry(&pos
))) {
1044 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1045 strListAdd(&result
, e
->value
.termedBuf(), ',');
1053 * Returns a the value of the specified list member, if any.
1056 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1059 const char *pos
= NULL
;
1062 int mlen
= strlen(member
);
1066 header
= getByName(name
);
1070 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1071 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1072 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1081 * returns a the value of the specified list member, if any.
1084 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1087 const char *pos
= NULL
;
1090 int mlen
= strlen(member
);
1094 header
= getStrOrList(id
);
1097 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1098 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1099 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1108 /* test if a field is present */
1110 HttpHeader::has(http_hdr_type id
) const
1113 assert(id
!= HDR_OTHER
);
1114 debugs(55, 9, this << " lookup for " << id
);
1115 return CBIT_TEST(mask
, id
);
1119 HttpHeader::putInt(http_hdr_type id
, int number
)
1122 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1123 assert(number
>= 0);
1124 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1128 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1131 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1132 assert(number
>= 0);
1133 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1137 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1140 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1142 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1146 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1149 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1151 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1155 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1158 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1160 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1164 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1166 assert(auth_scheme
&& realm
);
1167 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1171 HttpHeader::putCc(const HttpHdrCc
* cc
)
1176 /* remove old directives if any */
1177 delById(HDR_CACHE_CONTROL
);
1180 packerToMemInit(&p
, &mb
);
1183 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1190 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1195 /* remove old directives if any */
1196 delById(HDR_CONTENT_RANGE
);
1199 packerToMemInit(&p
, &mb
);
1200 httpHdrContRangePackInto(cr
, &p
);
1202 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1209 HttpHeader::putRange(const HttpHdrRange
* range
)
1214 /* remove old directives if any */
1218 packerToMemInit(&p
, &mb
);
1219 range
->packInto(&p
);
1221 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1228 HttpHeader::putSc(HttpHdrSc
*sc
)
1233 /* remove old directives if any */
1234 delById(HDR_SURROGATE_CONTROL
);
1237 packerToMemInit(&p
, &mb
);
1240 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1247 HttpHeader::putWarning(const int code
, const char *const text
)
1250 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1251 putStr(HDR_WARNING
, buf
);
1254 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1256 HttpHeader::putExt(const char *name
, const char *value
)
1258 assert(name
&& value
);
1259 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1260 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1264 HttpHeader::getInt(http_hdr_type id
) const
1267 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1270 if ((e
= findEntry(id
)))
1277 HttpHeader::getInt64(http_hdr_type id
) const
1280 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1283 if ((e
= findEntry(id
)))
1284 return e
->getInt64();
1290 HttpHeader::getTime(http_hdr_type id
) const
1295 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1297 if ((e
= findEntry(id
))) {
1298 value
= parse_rfc1123(e
->value
.termedBuf());
1299 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1305 /* sync with httpHeaderGetLastStr */
1307 HttpHeader::getStr(http_hdr_type id
) const
1311 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1313 if ((e
= findEntry(id
))) {
1314 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1315 return e
->value
.termedBuf();
1323 HttpHeader::getLastStr(http_hdr_type id
) const
1327 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1329 if ((e
= findLastEntry(id
))) {
1330 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1331 return e
->value
.termedBuf();
1338 HttpHeader::getCc() const
1340 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1342 PROF_start(HttpHeader_getCc
);
1345 getList(HDR_CACHE_CONTROL
, &s
);
1347 HttpHdrCc
*cc
=new HttpHdrCc();
1349 if (!cc
->parse(s
)) {
1354 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1357 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1359 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1361 PROF_stop(HttpHeader_getCc
);
1367 HttpHeader::getRange() const
1369 HttpHdrRange
*r
= NULL
;
1371 /* some clients will send "Request-Range" _and_ *matching* "Range"
1372 * who knows, some clients might send Request-Range only;
1373 * this "if" should work correctly in both cases;
1374 * hopefully no clients send mismatched headers! */
1376 if ((e
= findEntry(HDR_RANGE
)) ||
1377 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1378 r
= HttpHdrRange::ParseCreate(&e
->value
);
1379 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1386 HttpHeader::getSc() const
1388 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1393 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1395 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1397 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1400 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1402 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1408 HttpHeader::getContRange() const
1410 HttpHdrContRange
*cr
= NULL
;
1413 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1414 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1415 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1422 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1426 assert(auth_scheme
);
1429 if (!field
) /* no authorization field */
1432 l
= strlen(auth_scheme
);
1434 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1439 if (!xisspace(*field
)) /* wrong scheme */
1442 /* skip white space */
1443 for (; field
&& xisspace(*field
); ++field
);
1445 if (!*field
) /* no authorization cookie */
1448 static char decodedAuthToken
[8192];
1449 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1450 decodedAuthToken
[decodedLen
] = '\0';
1451 return decodedAuthToken
;
1455 HttpHeader::getETag(http_hdr_type id
) const
1457 ETag etag
= {NULL
, -1};
1459 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1461 if ((e
= findEntry(id
)))
1462 etagParseInit(&etag
, e
->value
.termedBuf());
1468 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1472 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1473 memset(&tot
, 0, sizeof(tot
));
1475 if ((e
= findEntry(id
))) {
1476 const char *str
= e
->value
.termedBuf();
1477 /* try as an ETag */
1479 if (etagParseInit(&tot
.tag
, str
)) {
1480 tot
.valid
= tot
.tag
.str
!= NULL
;
1483 /* or maybe it is time? */
1484 tot
.time
= parse_rfc1123(str
);
1485 tot
.valid
= tot
.time
>= 0;
1490 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1498 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1503 if (id
!= HDR_OTHER
)
1504 name
= Headers
[id
].name
;
1510 ++ Headers
[id
].stat
.aliveCount
;
1512 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1515 HttpHeaderEntry::~HttpHeaderEntry()
1518 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1519 /* clean name if needed */
1521 if (id
== HDR_OTHER
)
1526 assert(Headers
[id
].stat
.aliveCount
);
1528 -- Headers
[id
].stat
.aliveCount
;
1533 /* parses and inits header entry, returns true/false */
1535 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1537 /* note: name_start == field_start */
1538 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1539 int name_len
= name_end
? name_end
- field_start
:0;
1540 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1541 /* note: value_end == field_end */
1543 ++ HeaderEntryParsedCount
;
1545 /* do we have a valid field name within this field? */
1547 if (!name_len
|| name_end
> field_end
)
1550 if (name_len
> 65534) {
1551 /* String must be LESS THAN 64K and it adds a terminating NULL */
1552 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1556 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1557 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1558 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1560 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1567 /* now we know we can parse it */
1569 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1571 /* is it a "known" field? */
1572 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1583 /* set field name */
1584 if (id
== HDR_OTHER
)
1585 name
.limitInit(field_start
, name_len
);
1587 name
= Headers
[id
].name
;
1589 /* trim field value */
1590 while (value_start
< field_end
&& xisspace(*value_start
))
1593 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1596 if (field_end
- value_start
> 65534) {
1597 /* String must be LESS THAN 64K and it adds a terminating NULL */
1598 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1600 if (id
== HDR_OTHER
)
1606 /* set field value */
1607 value
.limitInit(value_start
, field_end
- value_start
);
1609 ++ Headers
[id
].stat
.seenCount
;
1611 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1613 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1617 HttpHeaderEntry::clone() const
1619 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1623 HttpHeaderEntry::packInto(Packer
* p
) const
1626 packerAppend(p
, name
.rawBuf(), name
.size());
1627 packerAppend(p
, ": ", 2);
1628 packerAppend(p
, value
.rawBuf(), value
.size());
1629 packerAppend(p
, "\r\n", 2);
1633 HttpHeaderEntry::getInt() const
1636 assert (Headers
[id
].type
== ftInt
);
1638 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1639 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1640 /* XXX: Should we check ok - ie
1641 * return ok ? -1 : value;
1647 HttpHeaderEntry::getInt64() const
1650 assert (Headers
[id
].type
== ftInt64
);
1652 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1653 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1654 /* XXX: Should we check ok - ie
1655 * return ok ? -1 : value;
1661 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1663 ++ Headers
[id
].stat
.parsCount
;
1666 ++ Headers
[id
].stat
.errCount
;
1667 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1675 /* tmp variable used to pass stat info to dumpers */
1676 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1677 const HttpHeaderStat
*dump_stat
= NULL
;
1680 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1682 const int id
= (int) val
;
1683 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1684 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1685 int visible
= count
> 0;
1686 /* for entries with zero count, list only those that belong to current type of message */
1688 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1689 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1692 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1693 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1697 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1700 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1701 idx
, (int) val
, count
,
1702 xpercent(count
, dump_stat
->destroyedCount
));
1706 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1711 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1712 storeAppendPrintf(e
, "\nField type distribution\n");
1713 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1714 "id", "name", "count", "#/header");
1715 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1716 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1717 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1718 "id", "name", "count", "#/cc_field");
1719 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1720 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1721 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1722 "id", "name", "count", "#/sc_field");
1723 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1724 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1725 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1726 "id", "#flds", "count", "%total");
1727 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1732 httpHeaderStoreReport(StoreEntry
* e
)
1738 HttpHeaderStats
[0].parsedCount
=
1739 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1740 HttpHeaderStats
[0].ccParsedCount
=
1741 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1742 HttpHeaderStats
[0].destroyedCount
=
1743 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1744 HttpHeaderStats
[0].busyDestroyedCount
=
1745 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1747 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1748 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1749 storeAppendPrintf(e
, "%s\n", "<br>");
1752 /* field stats for all messages */
1753 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1755 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1756 "id", "name", "#alive", "%err", "%repeat");
1758 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1759 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1760 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1761 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1762 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1763 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1766 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1767 HttpHeaderStats
[hoRequest
].parsedCount
,
1768 HttpHeaderStats
[hoReply
].parsedCount
,
1769 HttpHeaderStats
[0].parsedCount
);
1770 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1774 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1777 for (int i
= 0; i
< end
; ++i
) {
1778 if (name_len
!= info
[i
].name
.size())
1781 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1790 httpHeaderIdByNameDef(const char *name
, int name_len
)
1793 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1795 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1799 httpHeaderNameById(int id
)
1802 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1804 assert(id
>= 0 && id
< HDR_ENUM_END
);
1806 return Headers
[id
].name
.termedBuf();
1810 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1813 const char *pos
= NULL
;
1816 int mlen
= strlen(member
);
1820 String
header (getStrOrList(id
));
1822 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1823 if (strncasecmp(item
, member
, mlen
) == 0
1824 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1834 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1837 const char *pos
= NULL
;
1840 int mlen
= strlen(member
);
1844 String
header (getByName(name
));
1846 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1847 if (strncasecmp(item
, member
, mlen
) == 0
1848 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1858 HttpHeader::removeHopByHopEntries()
1860 removeConnectionHeaderEntries();
1862 const HttpHeaderEntry
*e
;
1863 HttpHeaderPos pos
= HttpHeaderInitPos
;
1864 int headers_deleted
= 0;
1865 while ((e
= getEntry(&pos
))) {
1867 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1868 delAt(pos
, headers_deleted
);
1875 HttpHeader::removeConnectionHeaderEntries()
1877 if (has(HDR_CONNECTION
)) {
1878 /* anything that matches Connection list member will be deleted */
1879 String strConnection
;
1881 (void) getList(HDR_CONNECTION
, &strConnection
);
1882 const HttpHeaderEntry
*e
;
1883 HttpHeaderPos pos
= HttpHeaderInitPos
;
1885 * think: on-average-best nesting of the two loops (hdrEntry
1886 * and strListItem) @?@
1889 * maybe we should delete standard stuff ("keep-alive","close")
1890 * from strConnection first?
1893 int headers_deleted
= 0;
1894 while ((e
= getEntry(&pos
))) {
1895 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1896 delAt(pos
, headers_deleted
);
1898 if (headers_deleted
)