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 {"Key", HDR_KEY
, ftStr
},
116 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
117 {"Link", HDR_LINK
, ftStr
},
118 {"Location", HDR_LOCATION
, ftStr
},
119 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
120 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
121 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
122 {"Origin", HDR_ORIGIN
, ftStr
},
123 {"Pragma", HDR_PRAGMA
, ftStr
},
124 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
125 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
126 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
127 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
128 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
129 {"Public", HDR_PUBLIC
, ftStr
},
130 {"Range", HDR_RANGE
, ftPRange
},
131 {"Referer", HDR_REFERER
, ftStr
},
132 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
133 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
134 {"Server", HDR_SERVER
, ftStr
},
135 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
136 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
137 {"TE", HDR_TE
, ftStr
},
138 {"Title", HDR_TITLE
, ftStr
},
139 {"Trailer", HDR_TRAILER
, ftStr
},
140 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
141 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
142 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
143 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
144 {"User-Agent", HDR_USER_AGENT
, ftStr
},
145 {"Vary", HDR_VARY
, ftStr
}, /* for now */
146 {"Via", HDR_VIA
, ftStr
}, /* for now */
147 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
148 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
149 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
150 {"X-Cache", HDR_X_CACHE
, ftStr
},
151 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
152 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
153 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
154 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
155 #if X_ACCELERATOR_VARY
156 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
159 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
161 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
162 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
163 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
164 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
167 static HttpHeaderFieldInfo
*Headers
= NULL
;
169 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
171 int tmp
= (int)aHeader
;
172 aHeader
= (http_hdr_type
)(++tmp
);
177 * headers with field values defined as #(values) in HTTP/1.1
178 * Headers that are currently not recognized, are commented out.
180 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
181 static http_hdr_type ListHeadersArr
[] = {
183 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
184 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
186 HDR_CONTENT_ENCODING
,
187 HDR_CONTENT_LANGUAGE
,
190 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
192 HDR_LINK
, HDR_PRAGMA
,
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
,
215 /* general-headers */
216 static http_hdr_type GeneralHeadersArr
[] = {
217 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
218 HDR_TRANSFER_ENCODING
,
225 static http_hdr_type EntityHeadersArr
[] = {
226 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
227 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
228 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
232 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
233 static http_hdr_type ReplyHeadersArr
[] = {
234 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
235 HDR_ACCEPT_RANGES
, HDR_AGE
,
237 HDR_LOCATION
, HDR_MAX_FORWARDS
,
238 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
, HDR_SET_COOKIE2
,
241 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
244 #if X_ACCELERATOR_VARY
245 HDR_X_ACCELERATOR_VARY
,
251 HDR_SURROGATE_CONTROL
254 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
255 static http_hdr_type RequestHeadersArr
[] = {
256 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
257 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
258 HDR_IF_RANGE
, HDR_MAX_FORWARDS
,
260 HDR_PROXY_CONNECTION
,
261 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
262 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
265 static HttpHeaderMask HopByHopHeadersMask
;
266 static http_hdr_type HopByHopHeadersArr
[] = {
267 HDR_CONNECTION
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
268 HDR_TE
, HDR_TRAILER
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
271 /* header accounting */
272 static HttpHeaderStat HttpHeaderStats
[] = {
280 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
282 static int HeaderEntryParsedCount
= 0;
285 * forward declarations and local routines
289 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
291 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
293 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
294 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
296 /** store report about current header usage and other stats */
297 static void httpHeaderStoreReport(StoreEntry
* e
);
300 * Module initialization routines
304 httpHeaderRegisterWithCacheManager(void)
306 Mgr::RegisterAction("http_headers",
307 "HTTP Header Statistics",
308 httpHeaderStoreReport
, 0, 1);
312 httpHeaderInitModule(void)
315 /* check that we have enough space for masks */
316 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
317 /* all headers must be described */
318 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
321 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
324 httpHeaderMaskInit(&ListHeadersMask
, 0);
326 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
328 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
330 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
332 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
334 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
336 httpHeaderMaskInit(&RequestHeadersMask
, 0);
338 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
340 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
342 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
344 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
346 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
348 /* init header stats */
349 assert(HttpHeaderStatCount
== hoReply
+ 1);
351 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
352 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
354 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
356 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
360 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
363 /* init dependent modules */
364 httpHdrCcInitModule();
366 httpHdrScInitModule();
368 httpHeaderRegisterWithCacheManager();
372 httpHeaderCleanModule(void)
374 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
376 httpHdrCcCleanModule();
377 httpHdrScCleanModule();
381 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
385 memset(hs
, 0, sizeof(HttpHeaderStat
));
387 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
388 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
389 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
390 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
394 * HttpHeader Implementation
397 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
399 httpHeaderMaskInit(&mask
, 0);
402 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
404 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
405 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
406 httpHeaderMaskInit(&mask
, 0);
409 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
411 httpHeaderMaskInit(&mask
, 0);
412 update(&other
, NULL
); // will update the mask as well
415 HttpHeader::~HttpHeader()
421 HttpHeader::operator =(const HttpHeader
&other
)
423 if (this != &other
) {
424 // we do not really care, but the caller probably does
425 assert(owner
== other
.owner
);
427 update(&other
, NULL
); // will update the mask as well
436 HttpHeaderPos pos
= HttpHeaderInitPos
;
439 assert(owner
> hoNone
&& owner
< hoEnd
);
440 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
442 PROF_start(HttpHeaderClean
);
444 if (owner
<= hoReply
) {
446 * An unfortunate bug. The entries array is initialized
447 * such that count is set to zero. httpHeaderClean() seems to
448 * be called both when 'hdr' is created, and destroyed. Thus,
449 * we accumulate a large number of zero counts for 'hdr' before
450 * it is ever used. Can't think of a good way to fix it, except
451 * adding a state variable that indicates whether or not 'hdr'
452 * has been used. As a hack, just never count zero-sized header
455 if (0 != entries
.count
)
456 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.count
);
458 ++ HttpHeaderStats
[owner
].destroyedCount
;
460 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
461 } // if (owner <= hoReply)
463 while ((e
= getEntry(&pos
))) {
464 /* tmp hack to try to avoid coredumps */
466 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
467 debugs(55, DBG_CRITICAL
, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
469 if (owner
<= hoReply
)
470 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
471 /* yes, this deletion leaves us in an inconsistent state */
476 httpHeaderMaskInit(&mask
, 0);
478 PROF_stop(HttpHeaderClean
);
481 /* append entries (also see httpHeaderUpdate) */
483 HttpHeader::append(const HttpHeader
* src
)
485 const HttpHeaderEntry
*e
;
486 HttpHeaderPos pos
= HttpHeaderInitPos
;
489 debugs(55, 7, "appending hdr: " << this << " += " << src
);
491 while ((e
= src
->getEntry(&pos
))) {
492 addEntry(e
->clone());
496 /* use fresh entries to replace old ones */
498 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
501 old
->update (fresh
, denied_mask
);
505 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
507 const HttpHeaderEntry
*e
;
508 HttpHeaderPos pos
= HttpHeaderInitPos
;
510 assert(this != fresh
);
512 while ((e
= fresh
->getEntry(&pos
))) {
513 /* deny bad guys (ok to check for HDR_OTHER) here */
515 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
518 if (e
->id
!= HDR_OTHER
)
521 delByName(e
->name
.termedBuf());
524 pos
= HttpHeaderInitPos
;
525 while ((e
= fresh
->getEntry(&pos
))) {
526 /* deny bad guys (ok to check for HDR_OTHER) here */
528 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
531 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
533 addEntry(e
->clone());
537 /* just handy in parsing: resets and returns false */
546 HttpHeader::parse(const char *header_start
, const char *header_end
)
548 const char *field_ptr
= header_start
;
549 HttpHeaderEntry
*e
, *e2
;
551 PROF_start(HttpHeaderParse
);
553 assert(header_start
&& header_end
);
554 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
555 ++ HttpHeaderStats
[owner
].parsedCount
;
558 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
559 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
560 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
564 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
565 * continuation lines start with a (single) space or tab */
566 while (field_ptr
< header_end
) {
567 const char *field_start
= field_ptr
;
568 const char *field_end
;
571 const char *this_line
= field_ptr
;
572 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
575 goto reset
; /* missing <LF> */
577 field_end
= field_ptr
;
579 ++field_ptr
; /* Move to next line */
581 if (field_end
> this_line
&& field_end
[-1] == '\r') {
582 --field_end
; /* Ignore CR LF */
584 if (owner
== hoRequest
&& field_end
> this_line
) {
586 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
591 debugs(55, DBG_IMPORTANT
, "WARNING: Rejecting HTTP request with a CR+ "
592 "header field to prevent request smuggling attacks: {" <<
593 getStringPrefix(header_start
, header_end
) << "}");
599 /* Barf on stray CR characters */
600 if (memchr(this_line
, '\r', field_end
- this_line
)) {
601 debugs(55, DBG_IMPORTANT
, "WARNING: suspicious CR characters in HTTP header {" <<
602 getStringPrefix(field_start
, field_end
) << "}");
604 if (Config
.onoff
.relaxed_header_parser
) {
605 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
607 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
615 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
616 debugs(55, DBG_IMPORTANT
, "WARNING: Blank continuation line in HTTP header {" <<
617 getStringPrefix(header_start
, header_end
) << "}");
620 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
622 if (field_start
== field_end
) {
623 if (field_ptr
< header_end
) {
624 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field near {" <<
625 getStringPrefix(field_start
, header_end
) << "}");
629 break; /* terminating blank line */
632 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
633 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field {" <<
634 getStringPrefix(field_start
, field_end
) << "}");
635 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
636 " in {" << getStringPrefix(header_start
, header_end
) << "}");
638 if (Config
.onoff
.relaxed_header_parser
)
644 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
645 // if (e->value.cmp(e2->value.termedBuf()) != 0) {
646 if (e
->value
!= e2
->value
) {
648 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
649 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
651 if (!Config
.onoff
.relaxed_header_parser
) {
656 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
657 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
660 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
661 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
663 } else if (l1
> l2
) {
670 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
671 "NOTICE: found double content-length header");
673 if (Config
.onoff
.relaxed_header_parser
) {
683 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
684 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
685 "WARNING: found whitespace in HTTP header name {" <<
686 getStringPrefix(field_start
, field_end
) << "}");
688 if (!Config
.onoff
.relaxed_header_parser
) {
698 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
699 delById(HDR_CONTENT_LENGTH
);
702 PROF_stop(HttpHeaderParse
);
703 return 1; /* even if no fields where found, it is a valid header */
705 PROF_stop(HttpHeaderParse
);
709 /* packs all the entries using supplied packer */
711 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
713 HttpHeaderPos pos
= HttpHeaderInitPos
;
714 const HttpHeaderEntry
*e
;
716 debugs(55, 7, "packing hdr: (" << this << ")");
717 /* pack all entries one by one */
718 while ((e
= getEntry(&pos
))) {
719 if (!mask_sensitive_info
) {
724 case HDR_AUTHORIZATION
:
725 case HDR_PROXY_AUTHORIZATION
:
726 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
727 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
734 /* Pack in the "special" entries */
739 /* returns next valid entry */
741 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
744 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
746 for (++(*pos
); *pos
< (ssize_t
)entries
.count
; ++(*pos
)) {
747 if (entries
.items
[*pos
])
748 return (HttpHeaderEntry
*)entries
.items
[*pos
];
755 * returns a pointer to a specified entry if any
756 * note that we return one entry so it does not make much sense to ask for
760 HttpHeader::findEntry(http_hdr_type id
) const
762 HttpHeaderPos pos
= HttpHeaderInitPos
;
765 assert(!CBIT_TEST(ListHeadersMask
, id
));
767 /* check mask first */
769 if (!CBIT_TEST(mask
, id
))
772 /* looks like we must have it, do linear search */
773 while ((e
= getEntry(&pos
))) {
778 /* hm.. we thought it was there, but it was not found */
781 return NULL
; /* not reached */
785 * same as httpHeaderFindEntry
788 HttpHeader::findLastEntry(http_hdr_type id
) const
790 HttpHeaderPos pos
= HttpHeaderInitPos
;
792 HttpHeaderEntry
*result
= NULL
;
794 assert(!CBIT_TEST(ListHeadersMask
, id
));
796 /* check mask first */
798 if (!CBIT_TEST(mask
, id
))
801 /* looks like we must have it, do linear search */
802 while ((e
= getEntry(&pos
))) {
807 assert(result
); /* must be there! */
812 * deletes all fields with a given name if any, returns #fields deleted;
815 HttpHeader::delByName(const char *name
)
818 HttpHeaderPos pos
= HttpHeaderInitPos
;
820 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
821 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
823 while ((e
= getEntry(&pos
))) {
824 if (!e
->name
.caseCmp(name
))
827 CBIT_SET(mask
, e
->id
);
833 /* deletes all entries with a given id, returns the #entries deleted */
835 HttpHeader::delById(http_hdr_type id
)
838 HttpHeaderPos pos
= HttpHeaderInitPos
;
840 debugs(55, 8, this << " del-by-id " << id
);
842 assert(id
!= HDR_OTHER
); /* does not make sense */
844 if (!CBIT_TEST(mask
, id
))
847 while ((e
= getEntry(&pos
))) {
858 * deletes an entry at pos and leaves a gap; leaving a gap makes it
859 * possible to iterate(search) and delete fields at the same time
860 * NOTE: Does not update the header mask. Caller must follow up with
861 * a call to refreshMask() if headers_deleted was incremented.
864 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
867 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
868 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
869 entries
.items
[pos
] = NULL
;
870 /* decrement header length, allow for ": " and crlf */
871 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
878 * Compacts the header storage
881 HttpHeader::compact()
887 * Refreshes the header mask. Required after delAt() calls.
890 HttpHeader::refreshMask()
892 httpHeaderMaskInit(&mask
, 0);
893 debugs(55, 7, "refreshing the mask in hdr " << this);
894 HttpHeaderPos pos
= HttpHeaderInitPos
;
895 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
896 CBIT_SET(mask
, e
->id
);
901 * does not call e->clone() so one should not reuse "*e"
904 HttpHeader::addEntry(HttpHeaderEntry
* e
)
908 assert(e
->name
.size());
910 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
912 if (CBIT_TEST(mask
, e
->id
))
913 ++ Headers
[e
->id
].stat
.repCount
;
915 CBIT_SET(mask
, e
->id
);
917 entries
.push_back(e
);
919 /* increment header length, allow for ": " and crlf */
920 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
924 * does not call e->clone() so one should not reuse "*e"
927 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
932 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
934 if (CBIT_TEST(mask
, e
->id
))
935 ++ Headers
[e
->id
].stat
.repCount
;
937 CBIT_SET(mask
, e
->id
);
941 /* increment header length, allow for ": " and crlf */
942 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
946 HttpHeader::getList(http_hdr_type id
, String
*s
) const
949 HttpHeaderPos pos
= HttpHeaderInitPos
;
950 debugs(55, 9, this << " joining for id " << id
);
951 /* only fields from ListHeaders array can be "listed" */
952 assert(CBIT_TEST(ListHeadersMask
, id
));
954 if (!CBIT_TEST(mask
, id
))
957 while ((e
= getEntry(&pos
))) {
959 strListAdd(s
, e
->value
.termedBuf(), ',');
963 * note: we might get an empty (size==0) string if there was an "empty"
964 * header. This results in an empty length String, which may have a NULL
967 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
969 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
971 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
976 /* return a list of entries with the same id separated by ',' and ws */
978 HttpHeader::getList(http_hdr_type id
) const
981 HttpHeaderPos pos
= HttpHeaderInitPos
;
982 debugs(55, 9, this << "joining for id " << id
);
983 /* only fields from ListHeaders array can be "listed" */
984 assert(CBIT_TEST(ListHeadersMask
, id
));
986 if (!CBIT_TEST(mask
, id
))
991 while ((e
= getEntry(&pos
))) {
993 strListAdd(&s
, e
->value
.termedBuf(), ',');
997 * note: we might get an empty (size==0) string if there was an "empty"
998 * header. This results in an empty length String, which may have a NULL
1001 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1003 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1005 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1010 /* return a string or list of entries with the same id separated by ',' and ws */
1012 HttpHeader::getStrOrList(http_hdr_type id
) const
1016 if (CBIT_TEST(ListHeadersMask
, id
))
1019 if ((e
= findEntry(id
)))
1026 * Returns the value of the specified header and/or an undefined String.
1029 HttpHeader::getByName(const char *name
) const
1032 // ignore presence: return undefined string if an empty header is present
1033 (void)getByNameIfPresent(name
, result
);
1038 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
1041 HttpHeaderPos pos
= HttpHeaderInitPos
;
1046 /* First try the quick path */
1047 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1052 result
= getStrOrList(id
);
1056 /* Sorry, an unknown header name. Do linear search */
1058 while ((e
= getEntry(&pos
))) {
1059 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1061 strListAdd(&result
, e
->value
.termedBuf(), ',');
1069 * Returns a the value of the specified list member, if any.
1072 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1075 const char *pos
= NULL
;
1078 int mlen
= strlen(member
);
1082 header
= getByName(name
);
1086 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1087 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1088 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1097 * returns a the value of the specified list member, if any.
1100 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1103 const char *pos
= NULL
;
1106 int mlen
= strlen(member
);
1110 header
= getStrOrList(id
);
1113 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1114 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1115 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1124 /* test if a field is present */
1126 HttpHeader::has(http_hdr_type id
) const
1129 assert(id
!= HDR_OTHER
);
1130 debugs(55, 9, this << " lookup for " << id
);
1131 return CBIT_TEST(mask
, id
);
1135 HttpHeader::putInt(http_hdr_type id
, int number
)
1138 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1139 assert(number
>= 0);
1140 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1144 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1147 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1148 assert(number
>= 0);
1149 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1153 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1156 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1158 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1162 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1165 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1167 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1171 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1174 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1176 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1180 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1182 assert(auth_scheme
&& realm
);
1183 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1187 HttpHeader::putCc(const HttpHdrCc
* cc
)
1192 /* remove old directives if any */
1193 delById(HDR_CACHE_CONTROL
);
1196 packerToMemInit(&p
, &mb
);
1199 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1206 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1211 /* remove old directives if any */
1212 delById(HDR_CONTENT_RANGE
);
1215 packerToMemInit(&p
, &mb
);
1216 httpHdrContRangePackInto(cr
, &p
);
1218 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1225 HttpHeader::putRange(const HttpHdrRange
* range
)
1230 /* remove old directives if any */
1234 packerToMemInit(&p
, &mb
);
1235 range
->packInto(&p
);
1237 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1244 HttpHeader::putSc(HttpHdrSc
*sc
)
1249 /* remove old directives if any */
1250 delById(HDR_SURROGATE_CONTROL
);
1253 packerToMemInit(&p
, &mb
);
1256 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1263 HttpHeader::putWarning(const int code
, const char *const text
)
1266 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1267 putStr(HDR_WARNING
, buf
);
1270 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1272 HttpHeader::putExt(const char *name
, const char *value
)
1274 assert(name
&& value
);
1275 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1276 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1280 HttpHeader::getInt(http_hdr_type id
) const
1283 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1286 if ((e
= findEntry(id
)))
1293 HttpHeader::getInt64(http_hdr_type id
) const
1296 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1299 if ((e
= findEntry(id
)))
1300 return e
->getInt64();
1306 HttpHeader::getTime(http_hdr_type id
) const
1311 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1313 if ((e
= findEntry(id
))) {
1314 value
= parse_rfc1123(e
->value
.termedBuf());
1315 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1321 /* sync with httpHeaderGetLastStr */
1323 HttpHeader::getStr(http_hdr_type id
) const
1327 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1329 if ((e
= findEntry(id
))) {
1330 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1331 return e
->value
.termedBuf();
1339 HttpHeader::getLastStr(http_hdr_type id
) const
1343 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1345 if ((e
= findLastEntry(id
))) {
1346 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1347 return e
->value
.termedBuf();
1354 HttpHeader::getCc() const
1356 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1358 PROF_start(HttpHeader_getCc
);
1361 getList(HDR_CACHE_CONTROL
, &s
);
1363 HttpHdrCc
*cc
=new HttpHdrCc();
1365 if (!cc
->parse(s
)) {
1370 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1373 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1375 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1377 PROF_stop(HttpHeader_getCc
);
1383 HttpHeader::getRange() const
1385 HttpHdrRange
*r
= NULL
;
1387 /* some clients will send "Request-Range" _and_ *matching* "Range"
1388 * who knows, some clients might send Request-Range only;
1389 * this "if" should work correctly in both cases;
1390 * hopefully no clients send mismatched headers! */
1392 if ((e
= findEntry(HDR_RANGE
)) ||
1393 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1394 r
= HttpHdrRange::ParseCreate(&e
->value
);
1395 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1402 HttpHeader::getSc() const
1404 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1409 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1411 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1413 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1416 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1418 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1424 HttpHeader::getContRange() const
1426 HttpHdrContRange
*cr
= NULL
;
1429 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1430 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1431 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1438 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1442 assert(auth_scheme
);
1445 if (!field
) /* no authorization field */
1448 l
= strlen(auth_scheme
);
1450 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1455 if (!xisspace(*field
)) /* wrong scheme */
1458 /* skip white space */
1459 for (; field
&& xisspace(*field
); ++field
);
1461 if (!*field
) /* no authorization cookie */
1464 static char decodedAuthToken
[8192];
1465 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1466 decodedAuthToken
[decodedLen
] = '\0';
1467 return decodedAuthToken
;
1471 HttpHeader::getETag(http_hdr_type id
) const
1473 ETag etag
= {NULL
, -1};
1475 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1477 if ((e
= findEntry(id
)))
1478 etagParseInit(&etag
, e
->value
.termedBuf());
1484 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1488 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1489 memset(&tot
, 0, sizeof(tot
));
1491 if ((e
= findEntry(id
))) {
1492 const char *str
= e
->value
.termedBuf();
1493 /* try as an ETag */
1495 if (etagParseInit(&tot
.tag
, str
)) {
1496 tot
.valid
= tot
.tag
.str
!= NULL
;
1499 /* or maybe it is time? */
1500 tot
.time
= parse_rfc1123(str
);
1501 tot
.valid
= tot
.time
>= 0;
1506 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1514 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1519 if (id
!= HDR_OTHER
)
1520 name
= Headers
[id
].name
;
1526 ++ Headers
[id
].stat
.aliveCount
;
1528 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1531 HttpHeaderEntry::~HttpHeaderEntry()
1534 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1535 /* clean name if needed */
1537 if (id
== HDR_OTHER
)
1542 assert(Headers
[id
].stat
.aliveCount
);
1544 -- Headers
[id
].stat
.aliveCount
;
1549 /* parses and inits header entry, returns true/false */
1551 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1553 /* note: name_start == field_start */
1554 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1555 int name_len
= name_end
? name_end
- field_start
:0;
1556 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1557 /* note: value_end == field_end */
1559 ++ HeaderEntryParsedCount
;
1561 /* do we have a valid field name within this field? */
1563 if (!name_len
|| name_end
> field_end
)
1566 if (name_len
> 65534) {
1567 /* String must be LESS THAN 64K and it adds a terminating NULL */
1568 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1572 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1573 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1574 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1576 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1583 /* now we know we can parse it */
1585 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1587 /* is it a "known" field? */
1588 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1599 /* set field name */
1600 if (id
== HDR_OTHER
)
1601 name
.limitInit(field_start
, name_len
);
1603 name
= Headers
[id
].name
;
1605 /* trim field value */
1606 while (value_start
< field_end
&& xisspace(*value_start
))
1609 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1612 if (field_end
- value_start
> 65534) {
1613 /* String must be LESS THAN 64K and it adds a terminating NULL */
1614 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1616 if (id
== HDR_OTHER
)
1622 /* set field value */
1623 value
.limitInit(value_start
, field_end
- value_start
);
1625 ++ Headers
[id
].stat
.seenCount
;
1627 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1629 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1633 HttpHeaderEntry::clone() const
1635 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1639 HttpHeaderEntry::packInto(Packer
* p
) const
1642 packerAppend(p
, name
.rawBuf(), name
.size());
1643 packerAppend(p
, ": ", 2);
1644 packerAppend(p
, value
.rawBuf(), value
.size());
1645 packerAppend(p
, "\r\n", 2);
1649 HttpHeaderEntry::getInt() const
1652 assert (Headers
[id
].type
== ftInt
);
1654 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1655 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1656 /* XXX: Should we check ok - ie
1657 * return ok ? -1 : value;
1663 HttpHeaderEntry::getInt64() const
1666 assert (Headers
[id
].type
== ftInt64
);
1668 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1669 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1670 /* XXX: Should we check ok - ie
1671 * return ok ? -1 : value;
1677 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1679 ++ Headers
[id
].stat
.parsCount
;
1682 ++ Headers
[id
].stat
.errCount
;
1683 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1691 /* tmp variable used to pass stat info to dumpers */
1692 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1693 const HttpHeaderStat
*dump_stat
= NULL
;
1696 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1698 const int id
= (int) val
;
1699 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1700 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1701 int visible
= count
> 0;
1702 /* for entries with zero count, list only those that belong to current type of message */
1704 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1705 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1708 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1709 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1713 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1716 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1717 idx
, (int) val
, count
,
1718 xpercent(count
, dump_stat
->destroyedCount
));
1722 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1727 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1728 storeAppendPrintf(e
, "\nField type distribution\n");
1729 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1730 "id", "name", "count", "#/header");
1731 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1732 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1733 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1734 "id", "name", "count", "#/cc_field");
1735 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1736 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1737 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1738 "id", "name", "count", "#/sc_field");
1739 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1740 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1741 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1742 "id", "#flds", "count", "%total");
1743 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1744 storeAppendPrintf(e
, "\n");
1749 httpHeaderStoreReport(StoreEntry
* e
)
1755 HttpHeaderStats
[0].parsedCount
=
1756 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1757 HttpHeaderStats
[0].ccParsedCount
=
1758 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1759 HttpHeaderStats
[0].destroyedCount
=
1760 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1761 HttpHeaderStats
[0].busyDestroyedCount
=
1762 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1764 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1765 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1768 /* field stats for all messages */
1769 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1771 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1772 "id", "name", "#alive", "%err", "%repeat");
1774 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1775 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1776 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1777 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1778 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1779 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1782 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1783 HttpHeaderStats
[hoRequest
].parsedCount
,
1784 HttpHeaderStats
[hoReply
].parsedCount
,
1785 HttpHeaderStats
[0].parsedCount
);
1786 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1790 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1793 for (int i
= 0; i
< end
; ++i
) {
1794 if (name_len
!= info
[i
].name
.size())
1797 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1806 httpHeaderIdByNameDef(const char *name
, int name_len
)
1809 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1811 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1815 httpHeaderNameById(int id
)
1818 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1820 assert(id
>= 0 && id
< HDR_ENUM_END
);
1822 return Headers
[id
].name
.termedBuf();
1826 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1829 const char *pos
= NULL
;
1832 int mlen
= strlen(member
);
1836 String
header (getStrOrList(id
));
1838 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1839 if (strncasecmp(item
, member
, mlen
) == 0
1840 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1850 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1853 const char *pos
= NULL
;
1856 int mlen
= strlen(member
);
1860 String
header (getByName(name
));
1862 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1863 if (strncasecmp(item
, member
, mlen
) == 0
1864 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1874 HttpHeader::removeHopByHopEntries()
1876 removeConnectionHeaderEntries();
1878 const HttpHeaderEntry
*e
;
1879 HttpHeaderPos pos
= HttpHeaderInitPos
;
1880 int headers_deleted
= 0;
1881 while ((e
= getEntry(&pos
))) {
1883 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1884 delAt(pos
, headers_deleted
);
1891 HttpHeader::removeConnectionHeaderEntries()
1893 if (has(HDR_CONNECTION
)) {
1894 /* anything that matches Connection list member will be deleted */
1895 String strConnection
;
1897 (void) getList(HDR_CONNECTION
, &strConnection
);
1898 const HttpHeaderEntry
*e
;
1899 HttpHeaderPos pos
= HttpHeaderInitPos
;
1901 * think: on-average-best nesting of the two loops (hdrEntry
1902 * and strListItem) @?@
1905 * maybe we should delete standard stuff ("keep-alive","close")
1906 * from strConnection first?
1909 int headers_deleted
= 0;
1910 while ((e
= getEntry(&pos
))) {
1911 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1912 delAt(pos
, headers_deleted
);
1914 if (headers_deleted
)