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(http_hdr_owner_type
const &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()
401 HttpHeaderPos pos
= HttpHeaderInitPos
;
404 assert(owner
> hoNone
&& owner
<= hoReply
);
405 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
407 PROF_start(HttpHeaderClean
);
410 * An unfortunate bug. The entries array is initialized
411 * such that count is set to zero. httpHeaderClean() seems to
412 * be called both when 'hdr' is created, and destroyed. Thus,
413 * we accumulate a large number of zero counts for 'hdr' before
414 * it is ever used. Can't think of a good way to fix it, except
415 * adding a state variable that indicates whether or not 'hdr'
416 * has been used. As a hack, just never count zero-sized header
420 if (0 != entries
.count
)
421 statHistCount(&HttpHeaderStats
[owner
].hdrUCountDistr
, entries
.count
);
423 HttpHeaderStats
[owner
].destroyedCount
++;
425 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
427 while ((e
= getEntry(&pos
))) {
428 /* tmp hack to try to avoid coredumps */
430 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
431 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
433 statHistCount(&HttpHeaderStats
[owner
].fieldTypeDistr
, e
->id
);
434 /* yes, this deletion leaves us in an inconsistent state */
439 httpHeaderMaskInit(&mask
, 0);
440 PROF_stop(HttpHeaderClean
);
443 /* append entries (also see httpHeaderUpdate) */
445 HttpHeader::append(const HttpHeader
* src
)
447 const HttpHeaderEntry
*e
;
448 HttpHeaderPos pos
= HttpHeaderInitPos
;
451 debugs(55, 7, "appending hdr: " << this << " += " << src
);
453 while ((e
= src
->getEntry(&pos
))) {
454 addEntry(e
->clone());
458 /* use fresh entries to replace old ones */
460 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
463 old
->update (fresh
, denied_mask
);
467 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
469 const HttpHeaderEntry
*e
;
470 HttpHeaderPos pos
= HttpHeaderInitPos
;
472 assert(this != fresh
);
474 while ((e
= fresh
->getEntry(&pos
))) {
475 /* deny bad guys (ok to check for HDR_OTHER) here */
477 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
480 if (e
->id
!= HDR_OTHER
)
483 delByName(e
->name
.termedBuf());
486 pos
= HttpHeaderInitPos
;
487 while ((e
= fresh
->getEntry(&pos
))) {
488 /* deny bad guys (ok to check for HDR_OTHER) here */
490 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
493 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
495 addEntry(e
->clone());
499 /* just handy in parsing: resets and returns false */
503 http_hdr_owner_type ho
;
506 *this = HttpHeader(ho
);
511 HttpHeader::parse(const char *header_start
, const char *header_end
)
513 const char *field_ptr
= header_start
;
514 HttpHeaderEntry
*e
, *e2
;
516 PROF_start(HttpHeaderParse
);
518 assert(header_start
&& header_end
);
519 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
520 HttpHeaderStats
[owner
].parsedCount
++;
523 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
524 debugs(55, 1, "WARNING: HTTP header contains NULL characters {" <<
525 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
529 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
530 * continuation lines start with a (single) space or tab */
531 while (field_ptr
< header_end
) {
532 const char *field_start
= field_ptr
;
533 const char *field_end
;
536 const char *this_line
= field_ptr
;
537 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
540 goto reset
; /* missing <LF> */
542 field_end
= field_ptr
;
544 field_ptr
++; /* Move to next line */
546 if (field_end
> this_line
&& field_end
[-1] == '\r') {
547 field_end
--; /* Ignore CR LF */
549 if (owner
== hoRequest
&& field_end
> this_line
) {
551 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
556 debugs(55, 1, "WARNING: Rejecting HTTP request with a CR+ "
557 "header field to prevent request smuggling attacks: {" <<
558 getStringPrefix(header_start
, header_end
) << "}");
564 /* Barf on stray CR characters */
565 if (memchr(this_line
, '\r', field_end
- this_line
)) {
566 debugs(55, 1, "WARNING: suspicious CR characters in HTTP header {" <<
567 getStringPrefix(field_start
, field_end
) << "}");
569 if (Config
.onoff
.relaxed_header_parser
) {
570 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
572 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
)
578 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
579 debugs(55, 1, "WARNING: Blank continuation line in HTTP header {" <<
580 getStringPrefix(header_start
, header_end
) << "}");
583 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
585 if (field_start
== field_end
) {
586 if (field_ptr
< header_end
) {
587 debugs(55, 1, "WARNING: unparseable HTTP header field near {" <<
588 getStringPrefix(field_start
, header_end
) << "}");
592 break; /* terminating blank line */
595 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
596 debugs(55, 1, "WARNING: unparseable HTTP header field {" <<
597 getStringPrefix(field_start
, field_end
) << "}");
598 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
599 " in {" << getStringPrefix(header_start
, header_end
) << "}");
601 if (Config
.onoff
.relaxed_header_parser
)
607 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
608 // if (e->value.cmp(e2->value.termedBuf()) != 0) {
609 if (e
->value
!= e2
->value
) {
611 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
612 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
614 if (!Config
.onoff
.relaxed_header_parser
) {
619 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
620 debugs(55, 1, "WARNING: Unparseable content-length '" << e
->value
<< "'");
623 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
624 debugs(55, 1, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
626 } else if (l1
> l2
) {
633 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
634 "NOTICE: found double content-length header");
636 if (Config
.onoff
.relaxed_header_parser
) {
646 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
647 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
648 "WARNING: found whitespace in HTTP header name {" <<
649 getStringPrefix(field_start
, field_end
) << "}");
651 if (!Config
.onoff
.relaxed_header_parser
) {
661 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
662 delById(HDR_CONTENT_LENGTH
);
665 PROF_stop(HttpHeaderParse
);
666 return 1; /* even if no fields where found, it is a valid header */
668 PROF_stop(HttpHeaderParse
);
672 /* packs all the entries using supplied packer */
674 HttpHeader::packInto(Packer
* p
) const
676 HttpHeaderPos pos
= HttpHeaderInitPos
;
677 const HttpHeaderEntry
*e
;
679 debugs(55, 7, "packing hdr: (" << this << ")");
680 /* pack all entries one by one */
681 while ((e
= getEntry(&pos
)))
684 /* Pack in the "special" entries */
689 /* returns next valid entry */
691 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
694 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
696 for ((*pos
)++; *pos
< (ssize_t
)entries
.count
; (*pos
)++) {
697 if (entries
.items
[*pos
])
698 return (HttpHeaderEntry
*)entries
.items
[*pos
];
705 * returns a pointer to a specified entry if any
706 * note that we return one entry so it does not make much sense to ask for
710 HttpHeader::findEntry(http_hdr_type id
) const
712 HttpHeaderPos pos
= HttpHeaderInitPos
;
715 assert(!CBIT_TEST(ListHeadersMask
, id
));
717 /* check mask first */
719 if (!CBIT_TEST(mask
, id
))
722 /* looks like we must have it, do linear search */
723 while ((e
= getEntry(&pos
))) {
728 /* hm.. we thought it was there, but it was not found */
731 return NULL
; /* not reached */
735 * same as httpHeaderFindEntry
738 HttpHeader::findLastEntry(http_hdr_type id
) const
740 HttpHeaderPos pos
= HttpHeaderInitPos
;
742 HttpHeaderEntry
*result
= NULL
;
744 assert(!CBIT_TEST(ListHeadersMask
, id
));
746 /* check mask first */
748 if (!CBIT_TEST(mask
, id
))
751 /* looks like we must have it, do linear search */
752 while ((e
= getEntry(&pos
))) {
757 assert(result
); /* must be there! */
762 * deletes all fields with a given name if any, returns #fields deleted;
765 HttpHeader::delByName(const char *name
)
768 HttpHeaderPos pos
= HttpHeaderInitPos
;
770 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
771 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
773 while ((e
= getEntry(&pos
))) {
774 if (!e
->name
.caseCmp(name
))
777 CBIT_SET(mask
, e
->id
);
783 /* deletes all entries with a given id, returns the #entries deleted */
785 HttpHeader::delById(http_hdr_type id
)
788 HttpHeaderPos pos
= HttpHeaderInitPos
;
790 debugs(55, 8, this << " del-by-id " << id
);
792 assert(id
!= HDR_OTHER
); /* does not make sense */
794 if (!CBIT_TEST(mask
, id
))
797 while ((e
= getEntry(&pos
))) {
808 * deletes an entry at pos and leaves a gap; leaving a gap makes it
809 * possible to iterate(search) and delete fields at the same time
810 * NOTE: Does not update the header mask. Caller must follow up with
811 * a call to refreshMask() if headers_deleted was incremented.
814 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
817 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
818 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
819 entries
.items
[pos
] = NULL
;
820 /* decrement header length, allow for ": " and crlf */
821 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
828 * Compacts the header storage
831 HttpHeader::compact()
837 * Refreshes the header mask. Required after delAt() calls.
840 HttpHeader::refreshMask()
842 httpHeaderMaskInit(&mask
, 0);
843 debugs(55, 7, "refreshing the mask in hdr " << this);
844 HttpHeaderPos pos
= HttpHeaderInitPos
;
845 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
846 CBIT_SET(mask
, e
->id
);
851 * does not call e->clone() so one should not reuse "*e"
854 HttpHeader::addEntry(HttpHeaderEntry
* e
)
858 assert(e
->name
.size());
860 debugs(55, 9, this << " adding entry: " << e
->id
<< " at " <<
863 if (CBIT_TEST(mask
, e
->id
))
864 Headers
[e
->id
].stat
.repCount
++;
866 CBIT_SET(mask
, e
->id
);
868 entries
.push_back(e
);
870 /* increment header length, allow for ": " and crlf */
871 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
875 * does not call e->clone() so one should not reuse "*e"
878 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
883 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " <<
886 if (CBIT_TEST(mask
, e
->id
))
887 Headers
[e
->id
].stat
.repCount
++;
889 CBIT_SET(mask
, e
->id
);
893 /* increment header length, allow for ": " and crlf */
894 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
898 HttpHeader::getList(http_hdr_type id
, String
*s
) const
901 HttpHeaderPos pos
= HttpHeaderInitPos
;
902 debugs(55, 9, this << " joining for id " << id
);
903 /* only fields from ListHeaders array can be "listed" */
904 assert(CBIT_TEST(ListHeadersMask
, id
));
906 if (!CBIT_TEST(mask
, id
))
909 while ((e
= getEntry(&pos
))) {
911 strListAdd(s
, e
->value
.termedBuf(), ',');
915 * note: we might get an empty (size==0) string if there was an "empty"
916 * header. This results in an empty length String, which may have a NULL
919 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
921 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
923 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
928 /* return a list of entries with the same id separated by ',' and ws */
930 HttpHeader::getList(http_hdr_type id
) const
933 HttpHeaderPos pos
= HttpHeaderInitPos
;
934 debugs(55, 9, this << "joining for id " << id
);
935 /* only fields from ListHeaders array can be "listed" */
936 assert(CBIT_TEST(ListHeadersMask
, id
));
938 if (!CBIT_TEST(mask
, id
))
943 while ((e
= getEntry(&pos
))) {
945 strListAdd(&s
, e
->value
.termedBuf(), ',');
949 * note: we might get an empty (size==0) string if there was an "empty"
950 * header. This results in an empty length String, which may have a NULL
953 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
955 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
957 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
962 /* return a string or list of entries with the same id separated by ',' and ws */
964 HttpHeader::getStrOrList(http_hdr_type id
) const
968 if (CBIT_TEST(ListHeadersMask
, id
))
971 if ((e
= findEntry(id
)))
978 * Returns the value of the specified header.
981 HttpHeader::getByName(const char *name
) const
984 HttpHeaderPos pos
= HttpHeaderInitPos
;
989 /* First try the quick path */
990 id
= httpHeaderIdByNameDef(name
, strlen(name
));
993 return getStrOrList(id
);
997 /* Sorry, an unknown header name. Do linear search */
998 while ((e
= getEntry(&pos
))) {
999 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1000 strListAdd(&result
, e
->value
.termedBuf(), ',');
1008 * Returns a the value of the specified list member, if any.
1011 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1014 const char *pos
= NULL
;
1017 int mlen
= strlen(member
);
1021 header
= getByName(name
);
1025 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1026 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1027 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1036 * returns a the value of the specified list member, if any.
1039 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1042 const char *pos
= NULL
;
1045 int mlen
= strlen(member
);
1049 header
= getStrOrList(id
);
1052 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1053 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1054 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1063 /* test if a field is present */
1065 HttpHeader::has(http_hdr_type id
) const
1068 assert(id
!= HDR_OTHER
);
1069 debugs(55, 9, this << " lookup for " << id
);
1070 return CBIT_TEST(mask
, id
);
1074 HttpHeader::putInt(http_hdr_type id
, int number
)
1077 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1078 assert(number
>= 0);
1079 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1083 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1086 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1087 assert(number
>= 0);
1088 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1092 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1095 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1097 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1101 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1104 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1106 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1110 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1113 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1115 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1119 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1121 assert(auth_scheme
&& realm
);
1122 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1126 HttpHeader::putCc(const HttpHdrCc
* cc
)
1131 /* remove old directives if any */
1132 delById(HDR_CACHE_CONTROL
);
1135 packerToMemInit(&p
, &mb
);
1136 httpHdrCcPackInto(cc
, &p
);
1138 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1145 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1150 /* remove old directives if any */
1151 delById(HDR_CONTENT_RANGE
);
1154 packerToMemInit(&p
, &mb
);
1155 httpHdrContRangePackInto(cr
, &p
);
1157 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1164 HttpHeader::putRange(const HttpHdrRange
* range
)
1169 /* remove old directives if any */
1173 packerToMemInit(&p
, &mb
);
1174 range
->packInto(&p
);
1176 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1183 HttpHeader::putSc(HttpHdrSc
*sc
)
1188 /* remove old directives if any */
1189 delById(HDR_SURROGATE_CONTROL
);
1192 packerToMemInit(&p
, &mb
);
1193 httpHdrScPackInto(sc
, &p
);
1195 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1202 HttpHeader::putWarning(const int code
, const char *const text
)
1205 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1206 putStr(HDR_WARNING
, buf
);
1209 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1211 HttpHeader::putExt(const char *name
, const char *value
)
1213 assert(name
&& value
);
1214 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1215 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1219 HttpHeader::getInt(http_hdr_type id
) const
1222 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1225 if ((e
= findEntry(id
)))
1232 HttpHeader::getInt64(http_hdr_type id
) const
1235 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1238 if ((e
= findEntry(id
)))
1239 return e
->getInt64();
1245 HttpHeader::getTime(http_hdr_type id
) const
1250 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1252 if ((e
= findEntry(id
))) {
1253 value
= parse_rfc1123(e
->value
.termedBuf());
1254 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1260 /* sync with httpHeaderGetLastStr */
1262 HttpHeader::getStr(http_hdr_type id
) const
1266 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1268 if ((e
= findEntry(id
))) {
1269 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1270 return e
->value
.termedBuf();
1278 HttpHeader::getLastStr(http_hdr_type id
) const
1282 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1284 if ((e
= findLastEntry(id
))) {
1285 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1286 return e
->value
.termedBuf();
1293 HttpHeader::getCc() const
1298 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1300 PROF_start(HttpHeader_getCc
);
1302 getList(HDR_CACHE_CONTROL
, &s
);
1304 cc
= httpHdrCcParseCreate(&s
);
1306 HttpHeaderStats
[owner
].ccParsedCount
++;
1309 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1311 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1313 PROF_stop(HttpHeader_getCc
);
1319 HttpHeader::getRange() const
1321 HttpHdrRange
*r
= NULL
;
1323 /* some clients will send "Request-Range" _and_ *matching* "Range"
1324 * who knows, some clients might send Request-Range only;
1325 * this "if" should work correctly in both cases;
1326 * hopefully no clients send mismatched headers! */
1328 if ((e
= findEntry(HDR_RANGE
)) ||
1329 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1330 r
= HttpHdrRange::ParseCreate(&e
->value
);
1331 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1338 HttpHeader::getSc() const
1340 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1345 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1347 HttpHdrSc
*sc
= httpHdrScParseCreate(&s
);
1349 HttpHeaderStats
[owner
].ccParsedCount
++;
1352 httpHdrScUpdateStats(sc
, &HttpHeaderStats
[owner
].scTypeDistr
);
1354 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1360 HttpHeader::getContRange() const
1362 HttpHdrContRange
*cr
= NULL
;
1365 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1366 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1367 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1374 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1378 assert(auth_scheme
);
1381 if (!field
) /* no authorization field */
1384 l
= strlen(auth_scheme
);
1386 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1391 if (!xisspace(*field
)) /* wrong scheme */
1394 /* skip white space */
1395 field
+= xcountws(field
);
1397 if (!*field
) /* no authorization cookie */
1400 return base64_decode(field
);
1404 HttpHeader::getETag(http_hdr_type id
) const
1406 ETag etag
= {NULL
, -1};
1408 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1410 if ((e
= findEntry(id
)))
1411 etagParseInit(&etag
, e
->value
.termedBuf());
1417 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1421 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1422 memset(&tot
, 0, sizeof(tot
));
1424 if ((e
= findEntry(id
))) {
1425 const char *str
= e
->value
.termedBuf();
1426 /* try as an ETag */
1428 if (etagParseInit(&tot
.tag
, str
)) {
1429 tot
.valid
= tot
.tag
.str
!= NULL
;
1432 /* or maybe it is time? */
1433 tot
.time
= parse_rfc1123(str
);
1434 tot
.valid
= tot
.time
>= 0;
1439 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1447 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1452 if (id
!= HDR_OTHER
)
1453 name
= Headers
[id
].name
;
1459 Headers
[id
].stat
.aliveCount
++;
1461 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1464 HttpHeaderEntry::~HttpHeaderEntry()
1467 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1468 /* clean name if needed */
1470 if (id
== HDR_OTHER
)
1475 assert(Headers
[id
].stat
.aliveCount
);
1477 Headers
[id
].stat
.aliveCount
--;
1482 /* parses and inits header entry, returns true/false */
1484 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1486 /* note: name_start == field_start */
1487 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1488 int name_len
= name_end
? name_end
- field_start
:0;
1489 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1490 /* note: value_end == field_end */
1492 HeaderEntryParsedCount
++;
1494 /* do we have a valid field name within this field? */
1496 if (!name_len
|| name_end
> field_end
)
1499 if (name_len
> 65534) {
1500 /* String must be LESS THAN 64K and it adds a terminating NULL */
1501 debugs(55, 1, "WARNING: ignoring header name of " << name_len
<< " bytes");
1505 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1506 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1507 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1509 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1516 /* now we know we can parse it */
1518 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1520 /* is it a "known" field? */
1521 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1532 /* set field name */
1533 if (id
== HDR_OTHER
)
1534 name
.limitInit(field_start
, name_len
);
1536 name
= Headers
[id
].name
;
1538 /* trim field value */
1539 while (value_start
< field_end
&& xisspace(*value_start
))
1542 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1545 if (field_end
- value_start
> 65534) {
1546 /* String must be LESS THAN 64K and it adds a terminating NULL */
1547 debugs(55, 1, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1549 if (id
== HDR_OTHER
)
1555 /* set field value */
1556 value
.limitInit(value_start
, field_end
- value_start
);
1558 Headers
[id
].stat
.seenCount
++;
1560 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1562 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1566 HttpHeaderEntry::clone() const
1568 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1572 HttpHeaderEntry::packInto(Packer
* p
) const
1575 packerAppend(p
, name
.rawBuf(), name
.size());
1576 packerAppend(p
, ": ", 2);
1577 packerAppend(p
, value
.rawBuf(), value
.size());
1578 packerAppend(p
, "\r\n", 2);
1582 HttpHeaderEntry::getInt() const
1585 assert (Headers
[id
].type
== ftInt
);
1587 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1588 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1589 /* XXX: Should we check ok - ie
1590 * return ok ? -1 : value;
1596 HttpHeaderEntry::getInt64() const
1599 assert (Headers
[id
].type
== ftInt64
);
1601 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1602 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1603 /* XXX: Should we check ok - ie
1604 * return ok ? -1 : value;
1610 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1612 Headers
[id
].stat
.parsCount
++;
1615 Headers
[id
].stat
.errCount
++;
1616 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1624 /* tmp variable used to pass stat info to dumpers */
1625 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1626 const HttpHeaderStat
*dump_stat
= NULL
;
1629 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1631 const int id
= (int) val
;
1632 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1633 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1634 int visible
= count
> 0;
1635 /* for entries with zero count, list only those that belong to current type of message */
1637 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1638 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1641 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1642 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1646 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1649 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1650 idx
, (int) val
, count
,
1651 xpercent(count
, dump_stat
->destroyedCount
));
1656 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1661 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1662 storeAppendPrintf(e
, "\nField type distribution\n");
1663 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1664 "id", "name", "count", "#/header");
1665 statHistDump(&hs
->fieldTypeDistr
, e
, httpHeaderFieldStatDumper
);
1666 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1667 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1668 "id", "name", "count", "#/cc_field");
1669 statHistDump(&hs
->ccTypeDistr
, e
, httpHdrCcStatDumper
);
1670 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1671 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1672 "id", "name", "count", "#/sc_field");
1673 statHistDump(&hs
->scTypeDistr
, e
, httpHdrScStatDumper
);
1674 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1675 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1676 "id", "#flds", "count", "%total");
1677 statHistDump(&hs
->hdrUCountDistr
, e
, httpHeaderFldsPerHdrDumper
);
1682 httpHeaderStoreReport(StoreEntry
* e
)
1688 HttpHeaderStats
[0].parsedCount
=
1689 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1690 HttpHeaderStats
[0].ccParsedCount
=
1691 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1692 HttpHeaderStats
[0].destroyedCount
=
1693 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1694 HttpHeaderStats
[0].busyDestroyedCount
=
1695 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1697 for (i
= 1; i
< HttpHeaderStatCount
; i
++) {
1698 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1699 storeAppendPrintf(e
, "%s\n", "<br>");
1702 /* field stats for all messages */
1703 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1705 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1706 "id", "name", "#alive", "%err", "%repeat");
1708 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1709 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1710 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1711 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1712 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1713 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1716 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1717 HttpHeaderStats
[hoRequest
].parsedCount
,
1718 HttpHeaderStats
[hoReply
].parsedCount
,
1719 HttpHeaderStats
[0].parsedCount
);
1720 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1724 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1728 for (i
= 0; i
< end
; ++i
) {
1729 if (name_len
>= 0 && name_len
!= info
[i
].name
.size())
1732 if (!strncasecmp(name
, info
[i
].name
.termedBuf(),
1733 name_len
< 0 ? info
[i
].name
.size() + 1 : name_len
))
1741 httpHeaderIdByNameDef(const char *name
, int name_len
)
1744 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1746 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1750 httpHeaderNameById(int id
)
1753 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1755 assert(id
>= 0 && id
< HDR_ENUM_END
);
1757 return Headers
[id
].name
.termedBuf();
1761 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1764 const char *pos
= NULL
;
1767 int mlen
= strlen(member
);
1771 String
header (getStrOrList(id
));
1773 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1774 if (strncasecmp(item
, member
, mlen
) == 0
1775 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1785 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1788 const char *pos
= NULL
;
1791 int mlen
= strlen(member
);
1795 String
header (getByName(name
));
1797 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1798 if (strncasecmp(item
, member
, mlen
) == 0
1799 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1809 HttpHeader::removeHopByHopEntries()
1811 removeConnectionHeaderEntries();
1813 const HttpHeaderEntry
*e
;
1814 HttpHeaderPos pos
= HttpHeaderInitPos
;
1815 int headers_deleted
= 0;
1816 while ((e
= getEntry(&pos
))) {
1818 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1819 delAt(pos
, headers_deleted
);
1826 HttpHeader::removeConnectionHeaderEntries()
1828 if (has(HDR_CONNECTION
)) {
1829 /* anything that matches Connection list member will be deleted */
1830 String strConnection
;
1832 (void) getList(HDR_CONNECTION
, &strConnection
);
1833 const HttpHeaderEntry
*e
;
1834 HttpHeaderPos pos
= HttpHeaderInitPos
;
1836 * think: on-average-best nesting of the two loops (hdrEntry
1837 * and strListItem) @?@
1840 * maybe we should delete standard stuff ("keep-alive","close")
1841 * from strConnection first?
1844 int headers_deleted
= 0;
1845 while ((e
= getEntry(&pos
))) {
1846 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1847 delAt(pos
, headers_deleted
);
1849 if (headers_deleted
)