5 * DEBUG: section 55 HTTP Header
6 * AUTHOR: Alex Rousskov
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "HttpHdrContRange.h"
39 #include "HttpHdrSc.h"
40 #include "HttpHeader.h"
42 #include "mgr/Registration.h"
47 * On naming conventions:
49 * HTTP/1.1 defines message-header as
51 * message-header = field-name ":" [ field-value ] CRLF
53 * field-value = *( field-content | LWS )
55 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
56 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
58 * HttpHeader is an object that represents all message-headers in a message.
59 * HttpHeader does not manage start-line.
61 * HttpHeader is implemented as a collection of header "entries".
62 * An entry is a (field_id, field_name, field_value) triplet.
67 * local constants and vars
71 * A table with major attributes for every known field.
72 * We calculate name lengths and reorganize this array on start up.
73 * After reorganization, field id can be used as an index to the table.
75 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
76 {"Accept", HDR_ACCEPT
, ftStr
},
78 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
79 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
80 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
81 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
82 {"Age", HDR_AGE
, ftInt
},
83 {"Allow", HDR_ALLOW
, ftStr
},
84 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
85 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
86 {"Connection", HDR_CONNECTION
, ftStr
},
87 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
88 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
89 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
90 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
91 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
92 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
93 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
94 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
95 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
96 {"Cookie", HDR_COOKIE
, ftStr
},
97 {"Cookie2", HDR_COOKIE2
, ftStr
},
98 {"Date", HDR_DATE
, ftDate_1123
},
99 {"ETag", HDR_ETAG
, ftETag
},
100 {"Expires", HDR_EXPIRES
, ftDate_1123
},
101 {"Expect", HDR_EXPECT
, ftStr
},
102 {"From", HDR_FROM
, ftStr
},
103 {"Host", HDR_HOST
, ftStr
},
104 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
105 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
106 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
107 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
108 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
109 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
110 {"Link", HDR_LINK
, ftStr
},
111 {"Location", HDR_LOCATION
, ftStr
},
112 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
113 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
114 {"Pragma", HDR_PRAGMA
, ftStr
},
115 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
116 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
117 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
118 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
119 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
120 {"Public", HDR_PUBLIC
, ftStr
},
121 {"Range", HDR_RANGE
, ftPRange
},
122 {"Referer", HDR_REFERER
, ftStr
},
123 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
124 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
125 {"Server", HDR_SERVER
, ftStr
},
126 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
127 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
128 {"TE", HDR_TE
, ftStr
},
129 {"Title", HDR_TITLE
, ftStr
},
130 {"Trailer", HDR_TRAILER
, ftStr
},
131 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
132 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
133 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
134 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
135 {"User-Agent", HDR_USER_AGENT
, ftStr
},
136 {"Vary", HDR_VARY
, ftStr
}, /* for now */
137 {"Via", HDR_VIA
, ftStr
}, /* for now */
138 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
139 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
140 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
141 {"X-Cache", HDR_X_CACHE
, ftStr
},
142 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
143 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
144 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
145 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
146 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
147 #if X_ACCELERATOR_VARY
148 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
151 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
153 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
154 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
155 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
156 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
159 static HttpHeaderFieldInfo
*Headers
= NULL
;
161 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
163 int tmp
= (int)aHeader
;
164 aHeader
= (http_hdr_type
)(++tmp
);
170 * headers with field values defined as #(values) in HTTP/1.1
171 * Headers that are currently not recognized, are commented out.
173 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
174 static http_hdr_type ListHeadersArr
[] = {
176 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
177 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
179 HDR_CONTENT_ENCODING
,
180 HDR_CONTENT_LANGUAGE
,
183 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
184 HDR_LINK
, HDR_PRAGMA
,
185 HDR_PROXY_CONNECTION
,
187 HDR_TRANSFER_ENCODING
,
192 HDR_WWW_AUTHENTICATE
,
193 HDR_AUTHENTICATION_INFO
,
194 HDR_PROXY_AUTHENTICATION_INFO
,
195 /* HDR_TE, HDR_TRAILER */
196 #if X_ACCELERATOR_VARY
197 HDR_X_ACCELERATOR_VARY
,
202 HDR_SURROGATE_CAPABILITY
,
203 HDR_SURROGATE_CONTROL
,
207 /* general-headers */
208 static http_hdr_type GeneralHeadersArr
[] = {
209 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
210 HDR_TRANSFER_ENCODING
,
217 static http_hdr_type EntityHeadersArr
[] = {
218 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
219 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
220 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
224 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
225 static http_hdr_type ReplyHeadersArr
[] = {
226 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
227 HDR_ACCEPT_RANGES
, HDR_AGE
,
228 HDR_LOCATION
, HDR_MAX_FORWARDS
,
229 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
, HDR_SET_COOKIE2
,
231 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
234 #if X_ACCELERATOR_VARY
235 HDR_X_ACCELERATOR_VARY
,
241 HDR_SURROGATE_CONTROL
244 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
245 static http_hdr_type RequestHeadersArr
[] = {
246 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
247 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
248 HDR_IF_RANGE
, HDR_MAX_FORWARDS
, HDR_PROXY_CONNECTION
,
249 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
250 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
253 static HttpHeaderMask HopByHopHeadersMask
;
254 static http_hdr_type HopByHopHeadersArr
[] = {
255 HDR_CONNECTION
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
256 HDR_TE
, HDR_TRAILER
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
259 /* header accounting */
260 static HttpHeaderStat HttpHeaderStats
[] = {
268 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
270 static int HeaderEntryParsedCount
= 0;
276 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
278 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
280 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
281 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
284 * Module initialization routines
288 httpHeaderRegisterWithCacheManager(void)
290 Mgr::RegisterAction("http_headers",
291 "HTTP Header Statistics",
292 httpHeaderStoreReport
, 0, 1);
296 httpHeaderInitModule(void)
299 /* check that we have enough space for masks */
300 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
301 /* all headers must be described */
302 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
305 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
308 httpHeaderMaskInit(&ListHeadersMask
, 0);
310 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
312 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
314 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
316 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
318 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
320 httpHeaderMaskInit(&RequestHeadersMask
, 0);
322 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
324 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
326 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
328 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
330 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
332 /* init header stats */
333 assert(HttpHeaderStatCount
== hoReply
+ 1);
335 for (i
= 0; i
< HttpHeaderStatCount
; i
++)
336 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
338 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
340 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
344 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
347 /* init dependent modules */
348 httpHdrCcInitModule();
350 httpHdrScInitModule();
352 httpHeaderRegisterWithCacheManager();
356 httpHeaderCleanModule(void)
358 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
360 httpHdrCcCleanModule();
361 httpHdrScCleanModule();
365 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
369 memset(hs
, 0, sizeof(HttpHeaderStat
));
371 statHistEnumInit(&hs
->hdrUCountDistr
, 32); /* not a real enum */
372 statHistEnumInit(&hs
->fieldTypeDistr
, HDR_ENUM_END
);
373 statHistEnumInit(&hs
->ccTypeDistr
, CC_ENUM_END
);
374 statHistEnumInit(&hs
->scTypeDistr
, SC_ENUM_END
);
378 * HttpHeader Implementation
381 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
383 httpHeaderMaskInit(&mask
, 0);
386 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
388 assert(anOwner
> hoNone
&& anOwner
<= hoReply
);
389 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
390 httpHeaderMaskInit(&mask
, 0);
393 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
395 httpHeaderMaskInit(&mask
, 0);
396 update(&other
, NULL
); // will update the mask as well
399 HttpHeader::~HttpHeader()
405 HttpHeader::operator =(const HttpHeader
&other
)
407 if (this != &other
) {
408 // we do not really care, but the caller probably does
409 assert(owner
== other
.owner
);
411 update(&other
, NULL
); // will update the mask as well
420 HttpHeaderPos pos
= HttpHeaderInitPos
;
423 assert(owner
> hoNone
&& owner
<= hoReply
);
424 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
426 PROF_start(HttpHeaderClean
);
429 * An unfortunate bug. The entries array is initialized
430 * such that count is set to zero. httpHeaderClean() seems to
431 * be called both when 'hdr' is created, and destroyed. Thus,
432 * we accumulate a large number of zero counts for 'hdr' before
433 * it is ever used. Can't think of a good way to fix it, except
434 * adding a state variable that indicates whether or not 'hdr'
435 * has been used. As a hack, just never count zero-sized header
439 if (0 != entries
.count
)
440 statHistCount(&HttpHeaderStats
[owner
].hdrUCountDistr
, entries
.count
);
442 HttpHeaderStats
[owner
].destroyedCount
++;
444 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
446 while ((e
= getEntry(&pos
))) {
447 /* tmp hack to try to avoid coredumps */
449 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
450 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
452 statHistCount(&HttpHeaderStats
[owner
].fieldTypeDistr
, e
->id
);
453 /* yes, this deletion leaves us in an inconsistent state */
458 httpHeaderMaskInit(&mask
, 0);
460 PROF_stop(HttpHeaderClean
);
463 /* append entries (also see httpHeaderUpdate) */
465 HttpHeader::append(const HttpHeader
* src
)
467 const HttpHeaderEntry
*e
;
468 HttpHeaderPos pos
= HttpHeaderInitPos
;
471 debugs(55, 7, "appending hdr: " << this << " += " << src
);
473 while ((e
= src
->getEntry(&pos
))) {
474 addEntry(e
->clone());
478 /* use fresh entries to replace old ones */
480 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
483 old
->update (fresh
, denied_mask
);
487 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
489 const HttpHeaderEntry
*e
;
490 HttpHeaderPos pos
= HttpHeaderInitPos
;
492 assert(this != fresh
);
494 while ((e
= fresh
->getEntry(&pos
))) {
495 /* deny bad guys (ok to check for HDR_OTHER) here */
497 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
500 if (e
->id
!= HDR_OTHER
)
503 delByName(e
->name
.termedBuf());
506 pos
= HttpHeaderInitPos
;
507 while ((e
= fresh
->getEntry(&pos
))) {
508 /* deny bad guys (ok to check for HDR_OTHER) here */
510 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
513 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
515 addEntry(e
->clone());
519 /* just handy in parsing: resets and returns false */
528 HttpHeader::parse(const char *header_start
, const char *header_end
)
530 const char *field_ptr
= header_start
;
531 HttpHeaderEntry
*e
, *e2
;
533 PROF_start(HttpHeaderParse
);
535 assert(header_start
&& header_end
);
536 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
537 HttpHeaderStats
[owner
].parsedCount
++;
540 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
541 debugs(55, 1, "WARNING: HTTP header contains NULL characters {" <<
542 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
546 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
547 * continuation lines start with a (single) space or tab */
548 while (field_ptr
< header_end
) {
549 const char *field_start
= field_ptr
;
550 const char *field_end
;
553 const char *this_line
= field_ptr
;
554 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
557 goto reset
; /* missing <LF> */
559 field_end
= field_ptr
;
561 field_ptr
++; /* Move to next line */
563 if (field_end
> this_line
&& field_end
[-1] == '\r') {
564 field_end
--; /* Ignore CR LF */
566 if (owner
== hoRequest
&& field_end
> this_line
) {
568 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
573 debugs(55, 1, "WARNING: Rejecting HTTP request with a CR+ "
574 "header field to prevent request smuggling attacks: {" <<
575 getStringPrefix(header_start
, header_end
) << "}");
581 /* Barf on stray CR characters */
582 if (memchr(this_line
, '\r', field_end
- this_line
)) {
583 debugs(55, 1, "WARNING: suspicious CR characters in HTTP header {" <<
584 getStringPrefix(field_start
, field_end
) << "}");
586 if (Config
.onoff
.relaxed_header_parser
) {
587 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
589 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
)
595 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
596 debugs(55, 1, "WARNING: Blank continuation line in HTTP header {" <<
597 getStringPrefix(header_start
, header_end
) << "}");
600 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
602 if (field_start
== field_end
) {
603 if (field_ptr
< header_end
) {
604 debugs(55, 1, "WARNING: unparseable HTTP header field near {" <<
605 getStringPrefix(field_start
, header_end
) << "}");
609 break; /* terminating blank line */
612 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
613 debugs(55, 1, "WARNING: unparseable HTTP header field {" <<
614 getStringPrefix(field_start
, field_end
) << "}");
615 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
616 " in {" << getStringPrefix(header_start
, header_end
) << "}");
618 if (Config
.onoff
.relaxed_header_parser
)
624 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
625 // if (e->value.cmp(e2->value.termedBuf()) != 0) {
626 if (e
->value
!= e2
->value
) {
628 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
629 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
631 if (!Config
.onoff
.relaxed_header_parser
) {
636 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
637 debugs(55, 1, "WARNING: Unparseable content-length '" << e
->value
<< "'");
640 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
641 debugs(55, 1, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
643 } else if (l1
> l2
) {
650 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
651 "NOTICE: found double content-length header");
653 if (Config
.onoff
.relaxed_header_parser
) {
663 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
664 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
665 "WARNING: found whitespace in HTTP header name {" <<
666 getStringPrefix(field_start
, field_end
) << "}");
668 if (!Config
.onoff
.relaxed_header_parser
) {
678 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
679 delById(HDR_CONTENT_LENGTH
);
682 PROF_stop(HttpHeaderParse
);
683 return 1; /* even if no fields where found, it is a valid header */
685 PROF_stop(HttpHeaderParse
);
689 /* packs all the entries using supplied packer */
691 HttpHeader::packInto(Packer
* p
) const
693 HttpHeaderPos pos
= HttpHeaderInitPos
;
694 const HttpHeaderEntry
*e
;
696 debugs(55, 7, "packing hdr: (" << this << ")");
697 /* pack all entries one by one */
698 while ((e
= getEntry(&pos
)))
701 /* Pack in the "special" entries */
706 /* returns next valid entry */
708 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
711 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
713 for ((*pos
)++; *pos
< (ssize_t
)entries
.count
; (*pos
)++) {
714 if (entries
.items
[*pos
])
715 return (HttpHeaderEntry
*)entries
.items
[*pos
];
722 * returns a pointer to a specified entry if any
723 * note that we return one entry so it does not make much sense to ask for
727 HttpHeader::findEntry(http_hdr_type id
) const
729 HttpHeaderPos pos
= HttpHeaderInitPos
;
732 assert(!CBIT_TEST(ListHeadersMask
, id
));
734 /* check mask first */
736 if (!CBIT_TEST(mask
, id
))
739 /* looks like we must have it, do linear search */
740 while ((e
= getEntry(&pos
))) {
745 /* hm.. we thought it was there, but it was not found */
748 return NULL
; /* not reached */
752 * same as httpHeaderFindEntry
755 HttpHeader::findLastEntry(http_hdr_type id
) const
757 HttpHeaderPos pos
= HttpHeaderInitPos
;
759 HttpHeaderEntry
*result
= NULL
;
761 assert(!CBIT_TEST(ListHeadersMask
, id
));
763 /* check mask first */
765 if (!CBIT_TEST(mask
, id
))
768 /* looks like we must have it, do linear search */
769 while ((e
= getEntry(&pos
))) {
774 assert(result
); /* must be there! */
779 * deletes all fields with a given name if any, returns #fields deleted;
782 HttpHeader::delByName(const char *name
)
785 HttpHeaderPos pos
= HttpHeaderInitPos
;
787 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
788 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
790 while ((e
= getEntry(&pos
))) {
791 if (!e
->name
.caseCmp(name
))
794 CBIT_SET(mask
, e
->id
);
800 /* deletes all entries with a given id, returns the #entries deleted */
802 HttpHeader::delById(http_hdr_type id
)
805 HttpHeaderPos pos
= HttpHeaderInitPos
;
807 debugs(55, 8, this << " del-by-id " << id
);
809 assert(id
!= HDR_OTHER
); /* does not make sense */
811 if (!CBIT_TEST(mask
, id
))
814 while ((e
= getEntry(&pos
))) {
825 * deletes an entry at pos and leaves a gap; leaving a gap makes it
826 * possible to iterate(search) and delete fields at the same time
827 * NOTE: Does not update the header mask. Caller must follow up with
828 * a call to refreshMask() if headers_deleted was incremented.
831 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
834 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
835 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
836 entries
.items
[pos
] = NULL
;
837 /* decrement header length, allow for ": " and crlf */
838 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
845 * Compacts the header storage
848 HttpHeader::compact()
854 * Refreshes the header mask. Required after delAt() calls.
857 HttpHeader::refreshMask()
859 httpHeaderMaskInit(&mask
, 0);
860 debugs(55, 7, "refreshing the mask in hdr " << this);
861 HttpHeaderPos pos
= HttpHeaderInitPos
;
862 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
863 CBIT_SET(mask
, e
->id
);
868 * does not call e->clone() so one should not reuse "*e"
871 HttpHeader::addEntry(HttpHeaderEntry
* e
)
875 assert(e
->name
.size());
877 debugs(55, 9, this << " adding entry: " << e
->id
<< " at " <<
880 if (CBIT_TEST(mask
, e
->id
))
881 Headers
[e
->id
].stat
.repCount
++;
883 CBIT_SET(mask
, e
->id
);
885 entries
.push_back(e
);
887 /* increment header length, allow for ": " and crlf */
888 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
892 * does not call e->clone() so one should not reuse "*e"
895 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
900 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " <<
903 if (CBIT_TEST(mask
, e
->id
))
904 Headers
[e
->id
].stat
.repCount
++;
906 CBIT_SET(mask
, e
->id
);
910 /* increment header length, allow for ": " and crlf */
911 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
915 HttpHeader::getList(http_hdr_type id
, String
*s
) const
918 HttpHeaderPos pos
= HttpHeaderInitPos
;
919 debugs(55, 9, this << " joining for id " << id
);
920 /* only fields from ListHeaders array can be "listed" */
921 assert(CBIT_TEST(ListHeadersMask
, id
));
923 if (!CBIT_TEST(mask
, id
))
926 while ((e
= getEntry(&pos
))) {
928 strListAdd(s
, e
->value
.termedBuf(), ',');
932 * note: we might get an empty (size==0) string if there was an "empty"
933 * header. This results in an empty length String, which may have a NULL
936 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
938 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
940 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
945 /* return a list of entries with the same id separated by ',' and ws */
947 HttpHeader::getList(http_hdr_type id
) const
950 HttpHeaderPos pos
= HttpHeaderInitPos
;
951 debugs(55, 9, this << "joining for id " << id
);
952 /* only fields from ListHeaders array can be "listed" */
953 assert(CBIT_TEST(ListHeadersMask
, id
));
955 if (!CBIT_TEST(mask
, id
))
960 while ((e
= getEntry(&pos
))) {
962 strListAdd(&s
, e
->value
.termedBuf(), ',');
966 * note: we might get an empty (size==0) string if there was an "empty"
967 * header. This results in an empty length String, which may have a NULL
970 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
972 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
974 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
979 /* return a string or list of entries with the same id separated by ',' and ws */
981 HttpHeader::getStrOrList(http_hdr_type id
) const
985 if (CBIT_TEST(ListHeadersMask
, id
))
988 if ((e
= findEntry(id
)))
995 * Returns the value of the specified header.
998 HttpHeader::getByName(const char *name
) const
1001 HttpHeaderPos pos
= HttpHeaderInitPos
;
1006 /* First try the quick path */
1007 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1010 return getStrOrList(id
);
1014 /* Sorry, an unknown header name. Do linear search */
1015 while ((e
= getEntry(&pos
))) {
1016 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1017 strListAdd(&result
, e
->value
.termedBuf(), ',');
1025 * Returns a the value of the specified list member, if any.
1028 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1031 const char *pos
= NULL
;
1034 int mlen
= strlen(member
);
1038 header
= getByName(name
);
1042 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1043 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1044 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1053 * returns a the value of the specified list member, if any.
1056 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1059 const char *pos
= NULL
;
1062 int mlen
= strlen(member
);
1066 header
= getStrOrList(id
);
1069 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1070 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1071 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1080 /* test if a field is present */
1082 HttpHeader::has(http_hdr_type id
) const
1085 assert(id
!= HDR_OTHER
);
1086 debugs(55, 9, this << " lookup for " << id
);
1087 return CBIT_TEST(mask
, id
);
1091 HttpHeader::putInt(http_hdr_type id
, int number
)
1094 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1095 assert(number
>= 0);
1096 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1100 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1103 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1104 assert(number
>= 0);
1105 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1109 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1112 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1114 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1118 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1121 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1123 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1127 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1130 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1132 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1136 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1138 assert(auth_scheme
&& realm
);
1139 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1143 HttpHeader::putCc(const HttpHdrCc
* cc
)
1148 /* remove old directives if any */
1149 delById(HDR_CACHE_CONTROL
);
1152 packerToMemInit(&p
, &mb
);
1153 httpHdrCcPackInto(cc
, &p
);
1155 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1162 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1167 /* remove old directives if any */
1168 delById(HDR_CONTENT_RANGE
);
1171 packerToMemInit(&p
, &mb
);
1172 httpHdrContRangePackInto(cr
, &p
);
1174 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1181 HttpHeader::putRange(const HttpHdrRange
* range
)
1186 /* remove old directives if any */
1190 packerToMemInit(&p
, &mb
);
1191 range
->packInto(&p
);
1193 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1200 HttpHeader::putSc(HttpHdrSc
*sc
)
1205 /* remove old directives if any */
1206 delById(HDR_SURROGATE_CONTROL
);
1209 packerToMemInit(&p
, &mb
);
1210 httpHdrScPackInto(sc
, &p
);
1212 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1219 HttpHeader::putWarning(const int code
, const char *const text
)
1222 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1223 putStr(HDR_WARNING
, buf
);
1226 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1228 HttpHeader::putExt(const char *name
, const char *value
)
1230 assert(name
&& value
);
1231 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1232 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1236 HttpHeader::getInt(http_hdr_type id
) const
1239 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1242 if ((e
= findEntry(id
)))
1249 HttpHeader::getInt64(http_hdr_type id
) const
1252 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1255 if ((e
= findEntry(id
)))
1256 return e
->getInt64();
1262 HttpHeader::getTime(http_hdr_type id
) const
1267 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1269 if ((e
= findEntry(id
))) {
1270 value
= parse_rfc1123(e
->value
.termedBuf());
1271 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1277 /* sync with httpHeaderGetLastStr */
1279 HttpHeader::getStr(http_hdr_type id
) const
1283 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1285 if ((e
= findEntry(id
))) {
1286 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1287 return e
->value
.termedBuf();
1295 HttpHeader::getLastStr(http_hdr_type id
) const
1299 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1301 if ((e
= findLastEntry(id
))) {
1302 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1303 return e
->value
.termedBuf();
1310 HttpHeader::getCc() const
1315 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1317 PROF_start(HttpHeader_getCc
);
1319 getList(HDR_CACHE_CONTROL
, &s
);
1321 cc
= httpHdrCcParseCreate(&s
);
1323 HttpHeaderStats
[owner
].ccParsedCount
++;
1326 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1328 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1330 PROF_stop(HttpHeader_getCc
);
1336 HttpHeader::getRange() const
1338 HttpHdrRange
*r
= NULL
;
1340 /* some clients will send "Request-Range" _and_ *matching* "Range"
1341 * who knows, some clients might send Request-Range only;
1342 * this "if" should work correctly in both cases;
1343 * hopefully no clients send mismatched headers! */
1345 if ((e
= findEntry(HDR_RANGE
)) ||
1346 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1347 r
= HttpHdrRange::ParseCreate(&e
->value
);
1348 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1355 HttpHeader::getSc() const
1357 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1362 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1364 HttpHdrSc
*sc
= httpHdrScParseCreate(&s
);
1366 HttpHeaderStats
[owner
].ccParsedCount
++;
1369 httpHdrScUpdateStats(sc
, &HttpHeaderStats
[owner
].scTypeDistr
);
1371 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1377 HttpHeader::getContRange() const
1379 HttpHdrContRange
*cr
= NULL
;
1382 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1383 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1384 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1391 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1395 assert(auth_scheme
);
1398 if (!field
) /* no authorization field */
1401 l
= strlen(auth_scheme
);
1403 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1408 if (!xisspace(*field
)) /* wrong scheme */
1411 /* skip white space */
1412 field
+= xcountws(field
);
1414 if (!*field
) /* no authorization cookie */
1417 static char decodedAuthToken
[8192];
1418 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1419 decodedAuthToken
[decodedLen
] = '\0';
1420 return decodedAuthToken
;
1424 HttpHeader::getETag(http_hdr_type id
) const
1426 ETag etag
= {NULL
, -1};
1428 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1430 if ((e
= findEntry(id
)))
1431 etagParseInit(&etag
, e
->value
.termedBuf());
1437 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1441 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1442 memset(&tot
, 0, sizeof(tot
));
1444 if ((e
= findEntry(id
))) {
1445 const char *str
= e
->value
.termedBuf();
1446 /* try as an ETag */
1448 if (etagParseInit(&tot
.tag
, str
)) {
1449 tot
.valid
= tot
.tag
.str
!= NULL
;
1452 /* or maybe it is time? */
1453 tot
.time
= parse_rfc1123(str
);
1454 tot
.valid
= tot
.time
>= 0;
1459 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1467 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1472 if (id
!= HDR_OTHER
)
1473 name
= Headers
[id
].name
;
1479 Headers
[id
].stat
.aliveCount
++;
1481 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1484 HttpHeaderEntry::~HttpHeaderEntry()
1487 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1488 /* clean name if needed */
1490 if (id
== HDR_OTHER
)
1495 assert(Headers
[id
].stat
.aliveCount
);
1497 Headers
[id
].stat
.aliveCount
--;
1502 /* parses and inits header entry, returns true/false */
1504 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1506 /* note: name_start == field_start */
1507 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1508 int name_len
= name_end
? name_end
- field_start
:0;
1509 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1510 /* note: value_end == field_end */
1512 HeaderEntryParsedCount
++;
1514 /* do we have a valid field name within this field? */
1516 if (!name_len
|| name_end
> field_end
)
1519 if (name_len
> 65534) {
1520 /* String must be LESS THAN 64K and it adds a terminating NULL */
1521 debugs(55, 1, "WARNING: ignoring header name of " << name_len
<< " bytes");
1525 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1526 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1527 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1529 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1536 /* now we know we can parse it */
1538 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1540 /* is it a "known" field? */
1541 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1552 /* set field name */
1553 if (id
== HDR_OTHER
)
1554 name
.limitInit(field_start
, name_len
);
1556 name
= Headers
[id
].name
;
1558 /* trim field value */
1559 while (value_start
< field_end
&& xisspace(*value_start
))
1562 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1565 if (field_end
- value_start
> 65534) {
1566 /* String must be LESS THAN 64K and it adds a terminating NULL */
1567 debugs(55, 1, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1569 if (id
== HDR_OTHER
)
1575 /* set field value */
1576 value
.limitInit(value_start
, field_end
- value_start
);
1578 Headers
[id
].stat
.seenCount
++;
1580 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1582 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1586 HttpHeaderEntry::clone() const
1588 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1592 HttpHeaderEntry::packInto(Packer
* p
) const
1595 packerAppend(p
, name
.rawBuf(), name
.size());
1596 packerAppend(p
, ": ", 2);
1597 packerAppend(p
, value
.rawBuf(), value
.size());
1598 packerAppend(p
, "\r\n", 2);
1602 HttpHeaderEntry::getInt() const
1605 assert (Headers
[id
].type
== ftInt
);
1607 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1608 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1609 /* XXX: Should we check ok - ie
1610 * return ok ? -1 : value;
1616 HttpHeaderEntry::getInt64() const
1619 assert (Headers
[id
].type
== ftInt64
);
1621 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1622 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1623 /* XXX: Should we check ok - ie
1624 * return ok ? -1 : value;
1630 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1632 Headers
[id
].stat
.parsCount
++;
1635 Headers
[id
].stat
.errCount
++;
1636 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1644 /* tmp variable used to pass stat info to dumpers */
1645 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1646 const HttpHeaderStat
*dump_stat
= NULL
;
1649 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1651 const int id
= (int) val
;
1652 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1653 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1654 int visible
= count
> 0;
1655 /* for entries with zero count, list only those that belong to current type of message */
1657 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1658 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1661 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1662 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1666 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1669 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1670 idx
, (int) val
, count
,
1671 xpercent(count
, dump_stat
->destroyedCount
));
1676 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1681 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1682 storeAppendPrintf(e
, "\nField type distribution\n");
1683 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1684 "id", "name", "count", "#/header");
1685 statHistDump(&hs
->fieldTypeDistr
, e
, httpHeaderFieldStatDumper
);
1686 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1687 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1688 "id", "name", "count", "#/cc_field");
1689 statHistDump(&hs
->ccTypeDistr
, e
, httpHdrCcStatDumper
);
1690 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1691 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1692 "id", "name", "count", "#/sc_field");
1693 statHistDump(&hs
->scTypeDistr
, e
, httpHdrScStatDumper
);
1694 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1695 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1696 "id", "#flds", "count", "%total");
1697 statHistDump(&hs
->hdrUCountDistr
, e
, httpHeaderFldsPerHdrDumper
);
1702 httpHeaderStoreReport(StoreEntry
* e
)
1708 HttpHeaderStats
[0].parsedCount
=
1709 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1710 HttpHeaderStats
[0].ccParsedCount
=
1711 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1712 HttpHeaderStats
[0].destroyedCount
=
1713 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1714 HttpHeaderStats
[0].busyDestroyedCount
=
1715 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1717 for (i
= 1; i
< HttpHeaderStatCount
; i
++) {
1718 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1719 storeAppendPrintf(e
, "%s\n", "<br>");
1722 /* field stats for all messages */
1723 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1725 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1726 "id", "name", "#alive", "%err", "%repeat");
1728 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1729 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1730 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1731 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1732 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1733 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1736 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1737 HttpHeaderStats
[hoRequest
].parsedCount
,
1738 HttpHeaderStats
[hoReply
].parsedCount
,
1739 HttpHeaderStats
[0].parsedCount
);
1740 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1744 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1747 for (int i
= 0; i
< end
; ++i
) {
1748 if (name_len
!= info
[i
].name
.size())
1751 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1760 httpHeaderIdByNameDef(const char *name
, int name_len
)
1763 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1765 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1769 httpHeaderNameById(int id
)
1772 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1774 assert(id
>= 0 && id
< HDR_ENUM_END
);
1776 return Headers
[id
].name
.termedBuf();
1780 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1783 const char *pos
= NULL
;
1786 int mlen
= strlen(member
);
1790 String
header (getStrOrList(id
));
1792 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1793 if (strncasecmp(item
, member
, mlen
) == 0
1794 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1804 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1807 const char *pos
= NULL
;
1810 int mlen
= strlen(member
);
1814 String
header (getByName(name
));
1816 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1817 if (strncasecmp(item
, member
, mlen
) == 0
1818 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1828 HttpHeader::removeHopByHopEntries()
1830 removeConnectionHeaderEntries();
1832 const HttpHeaderEntry
*e
;
1833 HttpHeaderPos pos
= HttpHeaderInitPos
;
1834 int headers_deleted
= 0;
1835 while ((e
= getEntry(&pos
))) {
1837 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1838 delAt(pos
, headers_deleted
);
1845 HttpHeader::removeConnectionHeaderEntries()
1847 if (has(HDR_CONNECTION
)) {
1848 /* anything that matches Connection list member will be deleted */
1849 String strConnection
;
1851 (void) getList(HDR_CONNECTION
, &strConnection
);
1852 const HttpHeaderEntry
*e
;
1853 HttpHeaderPos pos
= HttpHeaderInitPos
;
1855 * think: on-average-best nesting of the two loops (hdrEntry
1856 * and strListItem) @?@
1859 * maybe we should delete standard stuff ("keep-alive","close")
1860 * from strConnection first?
1863 int headers_deleted
= 0;
1864 while ((e
= getEntry(&pos
))) {
1865 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1866 delAt(pos
, headers_deleted
);
1868 if (headers_deleted
)