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.
36 #include "squid-old.h"
38 #include "HttpHdrContRange.h"
39 #include "HttpHdrCc.h"
40 #include "HttpHdrSc.h"
41 #include "HttpHeader.h"
42 #include "HttpHeaderStat.h"
44 #include "mgr/Registration.h"
48 #include "TimeOrTag.h"
51 * On naming conventions:
53 * HTTP/1.1 defines message-header as
55 * message-header = field-name ":" [ field-value ] CRLF
57 * field-value = *( field-content | LWS )
59 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
60 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
62 * HttpHeader is an object that represents all message-headers in a message.
63 * HttpHeader does not manage start-line.
65 * HttpHeader is implemented as a collection of header "entries".
66 * An entry is a (field_id, field_name, field_value) triplet.
71 * local constants and vars
75 * A table with major attributes for every known field.
76 * We calculate name lengths and reorganize this array on start up.
77 * After reorganization, field id can be used as an index to the table.
79 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
80 {"Accept", HDR_ACCEPT
, ftStr
},
82 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
83 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
84 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
85 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
86 {"Age", HDR_AGE
, ftInt
},
87 {"Allow", HDR_ALLOW
, ftStr
},
88 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
89 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
90 {"Connection", HDR_CONNECTION
, ftStr
},
91 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
92 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
93 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
94 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
95 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
96 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
97 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
98 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
99 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
100 {"Cookie", HDR_COOKIE
, ftStr
},
101 {"Cookie2", HDR_COOKIE2
, ftStr
},
102 {"Date", HDR_DATE
, ftDate_1123
},
103 {"ETag", HDR_ETAG
, ftETag
},
104 {"Expect", HDR_EXPECT
, ftStr
},
105 {"Expires", HDR_EXPIRES
, ftDate_1123
},
106 {"From", HDR_FROM
, ftStr
},
107 {"Host", HDR_HOST
, ftStr
},
108 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
109 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
110 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
111 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
112 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
113 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
114 {"Link", HDR_LINK
, ftStr
},
115 {"Location", HDR_LOCATION
, ftStr
},
116 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
117 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
118 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
119 {"Origin", HDR_ORIGIN
, ftStr
},
120 {"Pragma", HDR_PRAGMA
, ftStr
},
121 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
122 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
123 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
124 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
125 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
126 {"Public", HDR_PUBLIC
, ftStr
},
127 {"Range", HDR_RANGE
, ftPRange
},
128 {"Referer", HDR_REFERER
, ftStr
},
129 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
130 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
131 {"Server", HDR_SERVER
, ftStr
},
132 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
133 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
134 {"TE", HDR_TE
, ftStr
},
135 {"Title", HDR_TITLE
, ftStr
},
136 {"Trailer", HDR_TRAILER
, ftStr
},
137 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
138 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
139 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
140 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
141 {"User-Agent", HDR_USER_AGENT
, ftStr
},
142 {"Vary", HDR_VARY
, ftStr
}, /* for now */
143 {"Via", HDR_VIA
, ftStr
}, /* for now */
144 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
145 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
146 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
147 {"X-Cache", HDR_X_CACHE
, ftStr
},
148 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
149 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
150 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
151 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
152 #if X_ACCELERATOR_VARY
153 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
156 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
158 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
159 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
160 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
161 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
164 static HttpHeaderFieldInfo
*Headers
= NULL
;
166 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
168 int tmp
= (int)aHeader
;
169 aHeader
= (http_hdr_type
)(++tmp
);
175 * headers with field values defined as #(values) in HTTP/1.1
176 * Headers that are currently not recognized, are commented out.
178 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
179 static http_hdr_type ListHeadersArr
[] = {
181 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
182 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
184 HDR_CONTENT_ENCODING
,
185 HDR_CONTENT_LANGUAGE
,
188 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
189 HDR_LINK
, HDR_PRAGMA
,
190 HDR_PROXY_CONNECTION
,
192 HDR_TRANSFER_ENCODING
,
197 HDR_WWW_AUTHENTICATE
,
198 HDR_AUTHENTICATION_INFO
,
199 HDR_PROXY_AUTHENTICATION_INFO
,
200 /* HDR_TE, HDR_TRAILER */
201 #if X_ACCELERATOR_VARY
202 HDR_X_ACCELERATOR_VARY
,
207 HDR_SURROGATE_CAPABILITY
,
208 HDR_SURROGATE_CONTROL
,
212 /* general-headers */
213 static http_hdr_type GeneralHeadersArr
[] = {
214 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
215 HDR_TRANSFER_ENCODING
,
222 static http_hdr_type EntityHeadersArr
[] = {
223 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
224 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
225 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
229 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
230 static http_hdr_type ReplyHeadersArr
[] = {
231 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
232 HDR_ACCEPT_RANGES
, HDR_AGE
,
233 HDR_LOCATION
, HDR_MAX_FORWARDS
,
234 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
, HDR_SET_COOKIE2
,
237 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
240 #if X_ACCELERATOR_VARY
241 HDR_X_ACCELERATOR_VARY
,
247 HDR_SURROGATE_CONTROL
250 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
251 static http_hdr_type RequestHeadersArr
[] = {
252 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
253 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
254 HDR_IF_RANGE
, HDR_MAX_FORWARDS
,
256 HDR_PROXY_CONNECTION
,
257 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
258 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
261 static HttpHeaderMask HopByHopHeadersMask
;
262 static http_hdr_type HopByHopHeadersArr
[] = {
263 HDR_CONNECTION
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
264 HDR_TE
, HDR_TRAILER
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
267 /* header accounting */
268 static HttpHeaderStat HttpHeaderStats
[] = {
276 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
278 static int HeaderEntryParsedCount
= 0;
284 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
286 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
288 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
289 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
292 * Module initialization routines
296 httpHeaderRegisterWithCacheManager(void)
298 Mgr::RegisterAction("http_headers",
299 "HTTP Header Statistics",
300 httpHeaderStoreReport
, 0, 1);
304 httpHeaderInitModule(void)
307 /* check that we have enough space for masks */
308 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
309 /* all headers must be described */
310 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
313 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
316 httpHeaderMaskInit(&ListHeadersMask
, 0);
318 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
320 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
322 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
324 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
326 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
328 httpHeaderMaskInit(&RequestHeadersMask
, 0);
330 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
332 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
334 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
336 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
338 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
340 /* init header stats */
341 assert(HttpHeaderStatCount
== hoReply
+ 1);
343 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
344 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
346 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
348 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
352 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
355 /* init dependent modules */
356 httpHdrCcInitModule();
358 httpHdrScInitModule();
360 httpHeaderRegisterWithCacheManager();
364 httpHeaderCleanModule(void)
366 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
368 httpHdrCcCleanModule();
369 httpHdrScCleanModule();
373 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
377 memset(hs
, 0, sizeof(HttpHeaderStat
));
379 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
380 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
381 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
382 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
386 * HttpHeader Implementation
389 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
391 httpHeaderMaskInit(&mask
, 0);
394 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
396 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
397 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
398 httpHeaderMaskInit(&mask
, 0);
401 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
403 httpHeaderMaskInit(&mask
, 0);
404 update(&other
, NULL
); // will update the mask as well
407 HttpHeader::~HttpHeader()
413 HttpHeader::operator =(const HttpHeader
&other
)
415 if (this != &other
) {
416 // we do not really care, but the caller probably does
417 assert(owner
== other
.owner
);
419 update(&other
, NULL
); // will update the mask as well
428 HttpHeaderPos pos
= HttpHeaderInitPos
;
431 assert(owner
> hoNone
&& owner
< hoEnd
);
432 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
434 PROF_start(HttpHeaderClean
);
437 * An unfortunate bug. The entries array is initialized
438 * such that count is set to zero. httpHeaderClean() seems to
439 * be called both when 'hdr' is created, and destroyed. Thus,
440 * we accumulate a large number of zero counts for 'hdr' before
441 * it is ever used. Can't think of a good way to fix it, except
442 * adding a state variable that indicates whether or not 'hdr'
443 * has been used. As a hack, just never count zero-sized header
447 if (owner
<= hoReply
) {
448 if (0 != entries
.count
)
449 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.count
);
451 ++ HttpHeaderStats
[owner
].destroyedCount
;
453 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
455 while ((e
= getEntry(&pos
))) {
456 /* tmp hack to try to avoid coredumps */
458 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
459 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
461 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
462 /* yes, this deletion leaves us in an inconsistent state */
466 } // if (owner <= hoReply)
468 httpHeaderMaskInit(&mask
, 0);
470 PROF_stop(HttpHeaderClean
);
473 /* append entries (also see httpHeaderUpdate) */
475 HttpHeader::append(const HttpHeader
* src
)
477 const HttpHeaderEntry
*e
;
478 HttpHeaderPos pos
= HttpHeaderInitPos
;
481 debugs(55, 7, "appending hdr: " << this << " += " << src
);
483 while ((e
= src
->getEntry(&pos
))) {
484 addEntry(e
->clone());
488 /* use fresh entries to replace old ones */
490 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
493 old
->update (fresh
, denied_mask
);
497 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
499 const HttpHeaderEntry
*e
;
500 HttpHeaderPos pos
= HttpHeaderInitPos
;
502 assert(this != fresh
);
504 while ((e
= fresh
->getEntry(&pos
))) {
505 /* deny bad guys (ok to check for HDR_OTHER) here */
507 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
510 if (e
->id
!= HDR_OTHER
)
513 delByName(e
->name
.termedBuf());
516 pos
= HttpHeaderInitPos
;
517 while ((e
= fresh
->getEntry(&pos
))) {
518 /* deny bad guys (ok to check for HDR_OTHER) here */
520 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
523 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
525 addEntry(e
->clone());
529 /* just handy in parsing: resets and returns false */
538 HttpHeader::parse(const char *header_start
, const char *header_end
)
540 const char *field_ptr
= header_start
;
541 HttpHeaderEntry
*e
, *e2
;
543 PROF_start(HttpHeaderParse
);
545 assert(header_start
&& header_end
);
546 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
547 ++ HttpHeaderStats
[owner
].parsedCount
;
550 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
551 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
552 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
556 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
557 * continuation lines start with a (single) space or tab */
558 while (field_ptr
< header_end
) {
559 const char *field_start
= field_ptr
;
560 const char *field_end
;
563 const char *this_line
= field_ptr
;
564 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
567 goto reset
; /* missing <LF> */
569 field_end
= field_ptr
;
571 ++field_ptr
; /* Move to next line */
573 if (field_end
> this_line
&& field_end
[-1] == '\r') {
574 --field_end
; /* Ignore CR LF */
576 if (owner
== hoRequest
&& field_end
> this_line
) {
578 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
583 debugs(55, DBG_IMPORTANT
, "WARNING: Rejecting HTTP request with a CR+ "
584 "header field to prevent request smuggling attacks: {" <<
585 getStringPrefix(header_start
, header_end
) << "}");
591 /* Barf on stray CR characters */
592 if (memchr(this_line
, '\r', field_end
- this_line
)) {
593 debugs(55, DBG_IMPORTANT
, "WARNING: suspicious CR characters in HTTP header {" <<
594 getStringPrefix(field_start
, field_end
) << "}");
596 if (Config
.onoff
.relaxed_header_parser
) {
597 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
599 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
607 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
608 debugs(55, DBG_IMPORTANT
, "WARNING: Blank continuation line in HTTP header {" <<
609 getStringPrefix(header_start
, header_end
) << "}");
612 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
614 if (field_start
== field_end
) {
615 if (field_ptr
< header_end
) {
616 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field near {" <<
617 getStringPrefix(field_start
, header_end
) << "}");
621 break; /* terminating blank line */
624 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
625 debugs(55, DBG_IMPORTANT
, "WARNING: unparseable HTTP header field {" <<
626 getStringPrefix(field_start
, field_end
) << "}");
627 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
628 " in {" << getStringPrefix(header_start
, header_end
) << "}");
630 if (Config
.onoff
.relaxed_header_parser
)
636 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
637 // if (e->value.cmp(e2->value.termedBuf()) != 0) {
638 if (e
->value
!= e2
->value
) {
640 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
641 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
643 if (!Config
.onoff
.relaxed_header_parser
) {
648 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
649 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
652 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
653 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
655 } else if (l1
> l2
) {
662 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
663 "NOTICE: found double content-length header");
665 if (Config
.onoff
.relaxed_header_parser
) {
675 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
676 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
677 "WARNING: found whitespace in HTTP header name {" <<
678 getStringPrefix(field_start
, field_end
) << "}");
680 if (!Config
.onoff
.relaxed_header_parser
) {
690 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
691 delById(HDR_CONTENT_LENGTH
);
694 PROF_stop(HttpHeaderParse
);
695 return 1; /* even if no fields where found, it is a valid header */
697 PROF_stop(HttpHeaderParse
);
701 /* packs all the entries using supplied packer */
703 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
705 HttpHeaderPos pos
= HttpHeaderInitPos
;
706 const HttpHeaderEntry
*e
;
708 debugs(55, 7, "packing hdr: (" << this << ")");
709 /* pack all entries one by one */
710 while ((e
= getEntry(&pos
))) {
711 if (!mask_sensitive_info
) {
716 case HDR_AUTHORIZATION
:
717 case HDR_PROXY_AUTHORIZATION
:
718 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
719 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
726 /* Pack in the "special" entries */
731 /* returns next valid entry */
733 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
736 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
738 for (++(*pos
); *pos
< (ssize_t
)entries
.count
; ++(*pos
)) {
739 if (entries
.items
[*pos
])
740 return (HttpHeaderEntry
*)entries
.items
[*pos
];
747 * returns a pointer to a specified entry if any
748 * note that we return one entry so it does not make much sense to ask for
752 HttpHeader::findEntry(http_hdr_type id
) const
754 HttpHeaderPos pos
= HttpHeaderInitPos
;
757 assert(!CBIT_TEST(ListHeadersMask
, id
));
759 /* check mask first */
761 if (!CBIT_TEST(mask
, id
))
764 /* looks like we must have it, do linear search */
765 while ((e
= getEntry(&pos
))) {
770 /* hm.. we thought it was there, but it was not found */
773 return NULL
; /* not reached */
777 * same as httpHeaderFindEntry
780 HttpHeader::findLastEntry(http_hdr_type id
) const
782 HttpHeaderPos pos
= HttpHeaderInitPos
;
784 HttpHeaderEntry
*result
= NULL
;
786 assert(!CBIT_TEST(ListHeadersMask
, id
));
788 /* check mask first */
790 if (!CBIT_TEST(mask
, id
))
793 /* looks like we must have it, do linear search */
794 while ((e
= getEntry(&pos
))) {
799 assert(result
); /* must be there! */
804 * deletes all fields with a given name if any, returns #fields deleted;
807 HttpHeader::delByName(const char *name
)
810 HttpHeaderPos pos
= HttpHeaderInitPos
;
812 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
813 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
815 while ((e
= getEntry(&pos
))) {
816 if (!e
->name
.caseCmp(name
))
819 CBIT_SET(mask
, e
->id
);
825 /* deletes all entries with a given id, returns the #entries deleted */
827 HttpHeader::delById(http_hdr_type id
)
830 HttpHeaderPos pos
= HttpHeaderInitPos
;
832 debugs(55, 8, this << " del-by-id " << id
);
834 assert(id
!= HDR_OTHER
); /* does not make sense */
836 if (!CBIT_TEST(mask
, id
))
839 while ((e
= getEntry(&pos
))) {
850 * deletes an entry at pos and leaves a gap; leaving a gap makes it
851 * possible to iterate(search) and delete fields at the same time
852 * NOTE: Does not update the header mask. Caller must follow up with
853 * a call to refreshMask() if headers_deleted was incremented.
856 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
859 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
860 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
861 entries
.items
[pos
] = NULL
;
862 /* decrement header length, allow for ": " and crlf */
863 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
870 * Compacts the header storage
873 HttpHeader::compact()
879 * Refreshes the header mask. Required after delAt() calls.
882 HttpHeader::refreshMask()
884 httpHeaderMaskInit(&mask
, 0);
885 debugs(55, 7, "refreshing the mask in hdr " << this);
886 HttpHeaderPos pos
= HttpHeaderInitPos
;
887 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
888 CBIT_SET(mask
, e
->id
);
893 * does not call e->clone() so one should not reuse "*e"
896 HttpHeader::addEntry(HttpHeaderEntry
* e
)
900 assert(e
->name
.size());
902 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
904 if (CBIT_TEST(mask
, e
->id
))
905 ++ Headers
[e
->id
].stat
.repCount
;
907 CBIT_SET(mask
, e
->id
);
909 entries
.push_back(e
);
911 /* increment header length, allow for ": " and crlf */
912 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
916 * does not call e->clone() so one should not reuse "*e"
919 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
924 debugs(55, 7, HERE
<< this << " adding entry: " << e
->id
<< " at " << entries
.count
);
926 if (CBIT_TEST(mask
, e
->id
))
927 ++ Headers
[e
->id
].stat
.repCount
;
929 CBIT_SET(mask
, e
->id
);
933 /* increment header length, allow for ": " and crlf */
934 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
938 HttpHeader::getList(http_hdr_type id
, String
*s
) const
941 HttpHeaderPos pos
= HttpHeaderInitPos
;
942 debugs(55, 9, this << " joining for id " << id
);
943 /* only fields from ListHeaders array can be "listed" */
944 assert(CBIT_TEST(ListHeadersMask
, id
));
946 if (!CBIT_TEST(mask
, id
))
949 while ((e
= getEntry(&pos
))) {
951 strListAdd(s
, e
->value
.termedBuf(), ',');
955 * note: we might get an empty (size==0) string if there was an "empty"
956 * header. This results in an empty length String, which may have a NULL
959 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
961 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
963 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
968 /* return a list of entries with the same id separated by ',' and ws */
970 HttpHeader::getList(http_hdr_type id
) const
973 HttpHeaderPos pos
= HttpHeaderInitPos
;
974 debugs(55, 9, this << "joining for id " << id
);
975 /* only fields from ListHeaders array can be "listed" */
976 assert(CBIT_TEST(ListHeadersMask
, id
));
978 if (!CBIT_TEST(mask
, id
))
983 while ((e
= getEntry(&pos
))) {
985 strListAdd(&s
, e
->value
.termedBuf(), ',');
989 * note: we might get an empty (size==0) string if there was an "empty"
990 * header. This results in an empty length String, which may have a NULL
993 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
995 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
997 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1002 /* return a string or list of entries with the same id separated by ',' and ws */
1004 HttpHeader::getStrOrList(http_hdr_type id
) const
1008 if (CBIT_TEST(ListHeadersMask
, id
))
1011 if ((e
= findEntry(id
)))
1018 * Returns the value of the specified header.
1021 HttpHeader::getByName(const char *name
) const
1024 HttpHeaderPos pos
= HttpHeaderInitPos
;
1029 /* First try the quick path */
1030 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1033 return getStrOrList(id
);
1037 /* Sorry, an unknown header name. Do linear search */
1038 while ((e
= getEntry(&pos
))) {
1039 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1040 strListAdd(&result
, e
->value
.termedBuf(), ',');
1048 * Returns a the value of the specified list member, if any.
1051 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1054 const char *pos
= NULL
;
1057 int mlen
= strlen(member
);
1061 header
= getByName(name
);
1065 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1066 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1067 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1076 * returns a the value of the specified list member, if any.
1079 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1082 const char *pos
= NULL
;
1085 int mlen
= strlen(member
);
1089 header
= getStrOrList(id
);
1092 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1093 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1094 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1103 /* test if a field is present */
1105 HttpHeader::has(http_hdr_type id
) const
1108 assert(id
!= HDR_OTHER
);
1109 debugs(55, 9, this << " lookup for " << id
);
1110 return CBIT_TEST(mask
, id
);
1114 HttpHeader::putInt(http_hdr_type id
, int number
)
1117 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1118 assert(number
>= 0);
1119 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1123 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1126 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1127 assert(number
>= 0);
1128 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1132 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1135 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1137 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1141 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1144 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1146 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1150 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1153 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1155 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1159 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1161 assert(auth_scheme
&& realm
);
1162 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1166 HttpHeader::putCc(const HttpHdrCc
* cc
)
1171 /* remove old directives if any */
1172 delById(HDR_CACHE_CONTROL
);
1175 packerToMemInit(&p
, &mb
);
1178 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1185 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1190 /* remove old directives if any */
1191 delById(HDR_CONTENT_RANGE
);
1194 packerToMemInit(&p
, &mb
);
1195 httpHdrContRangePackInto(cr
, &p
);
1197 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1204 HttpHeader::putRange(const HttpHdrRange
* range
)
1209 /* remove old directives if any */
1213 packerToMemInit(&p
, &mb
);
1214 range
->packInto(&p
);
1216 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1223 HttpHeader::putSc(HttpHdrSc
*sc
)
1228 /* remove old directives if any */
1229 delById(HDR_SURROGATE_CONTROL
);
1232 packerToMemInit(&p
, &mb
);
1235 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1242 HttpHeader::putWarning(const int code
, const char *const text
)
1245 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1246 putStr(HDR_WARNING
, buf
);
1249 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1251 HttpHeader::putExt(const char *name
, const char *value
)
1253 assert(name
&& value
);
1254 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1255 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1259 HttpHeader::getInt(http_hdr_type id
) const
1262 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1265 if ((e
= findEntry(id
)))
1272 HttpHeader::getInt64(http_hdr_type id
) const
1275 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1278 if ((e
= findEntry(id
)))
1279 return e
->getInt64();
1285 HttpHeader::getTime(http_hdr_type id
) const
1290 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1292 if ((e
= findEntry(id
))) {
1293 value
= parse_rfc1123(e
->value
.termedBuf());
1294 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1300 /* sync with httpHeaderGetLastStr */
1302 HttpHeader::getStr(http_hdr_type id
) const
1306 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1308 if ((e
= findEntry(id
))) {
1309 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1310 return e
->value
.termedBuf();
1318 HttpHeader::getLastStr(http_hdr_type id
) const
1322 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1324 if ((e
= findLastEntry(id
))) {
1325 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1326 return e
->value
.termedBuf();
1333 HttpHeader::getCc() const
1335 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1337 PROF_start(HttpHeader_getCc
);
1340 getList(HDR_CACHE_CONTROL
, &s
);
1342 HttpHdrCc
*cc
=new HttpHdrCc();
1344 if (!cc
->parse(s
)) {
1349 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1352 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1354 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1356 PROF_stop(HttpHeader_getCc
);
1362 HttpHeader::getRange() const
1364 HttpHdrRange
*r
= NULL
;
1366 /* some clients will send "Request-Range" _and_ *matching* "Range"
1367 * who knows, some clients might send Request-Range only;
1368 * this "if" should work correctly in both cases;
1369 * hopefully no clients send mismatched headers! */
1371 if ((e
= findEntry(HDR_RANGE
)) ||
1372 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1373 r
= HttpHdrRange::ParseCreate(&e
->value
);
1374 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1381 HttpHeader::getSc() const
1383 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1388 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1390 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1392 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1395 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1397 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1403 HttpHeader::getContRange() const
1405 HttpHdrContRange
*cr
= NULL
;
1408 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1409 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1410 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1417 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1421 assert(auth_scheme
);
1424 if (!field
) /* no authorization field */
1427 l
= strlen(auth_scheme
);
1429 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1434 if (!xisspace(*field
)) /* wrong scheme */
1437 /* skip white space */
1438 for (; field
&& xisspace(*field
); ++field
);
1440 if (!*field
) /* no authorization cookie */
1443 static char decodedAuthToken
[8192];
1444 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1445 decodedAuthToken
[decodedLen
] = '\0';
1446 return decodedAuthToken
;
1450 HttpHeader::getETag(http_hdr_type id
) const
1452 ETag etag
= {NULL
, -1};
1454 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1456 if ((e
= findEntry(id
)))
1457 etagParseInit(&etag
, e
->value
.termedBuf());
1463 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1467 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1468 memset(&tot
, 0, sizeof(tot
));
1470 if ((e
= findEntry(id
))) {
1471 const char *str
= e
->value
.termedBuf();
1472 /* try as an ETag */
1474 if (etagParseInit(&tot
.tag
, str
)) {
1475 tot
.valid
= tot
.tag
.str
!= NULL
;
1478 /* or maybe it is time? */
1479 tot
.time
= parse_rfc1123(str
);
1480 tot
.valid
= tot
.time
>= 0;
1485 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1493 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1498 if (id
!= HDR_OTHER
)
1499 name
= Headers
[id
].name
;
1505 ++ Headers
[id
].stat
.aliveCount
;
1507 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1510 HttpHeaderEntry::~HttpHeaderEntry()
1513 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1514 /* clean name if needed */
1516 if (id
== HDR_OTHER
)
1521 assert(Headers
[id
].stat
.aliveCount
);
1523 -- Headers
[id
].stat
.aliveCount
;
1528 /* parses and inits header entry, returns true/false */
1530 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1532 /* note: name_start == field_start */
1533 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1534 int name_len
= name_end
? name_end
- field_start
:0;
1535 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1536 /* note: value_end == field_end */
1538 ++ HeaderEntryParsedCount
;
1540 /* do we have a valid field name within this field? */
1542 if (!name_len
|| name_end
> field_end
)
1545 if (name_len
> 65534) {
1546 /* String must be LESS THAN 64K and it adds a terminating NULL */
1547 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1551 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1552 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1553 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1555 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1562 /* now we know we can parse it */
1564 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1566 /* is it a "known" field? */
1567 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1578 /* set field name */
1579 if (id
== HDR_OTHER
)
1580 name
.limitInit(field_start
, name_len
);
1582 name
= Headers
[id
].name
;
1584 /* trim field value */
1585 while (value_start
< field_end
&& xisspace(*value_start
))
1588 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1591 if (field_end
- value_start
> 65534) {
1592 /* String must be LESS THAN 64K and it adds a terminating NULL */
1593 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1595 if (id
== HDR_OTHER
)
1601 /* set field value */
1602 value
.limitInit(value_start
, field_end
- value_start
);
1604 ++ Headers
[id
].stat
.seenCount
;
1606 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1608 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1612 HttpHeaderEntry::clone() const
1614 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1618 HttpHeaderEntry::packInto(Packer
* p
) const
1621 packerAppend(p
, name
.rawBuf(), name
.size());
1622 packerAppend(p
, ": ", 2);
1623 packerAppend(p
, value
.rawBuf(), value
.size());
1624 packerAppend(p
, "\r\n", 2);
1628 HttpHeaderEntry::getInt() const
1631 assert (Headers
[id
].type
== ftInt
);
1633 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1634 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1635 /* XXX: Should we check ok - ie
1636 * return ok ? -1 : value;
1642 HttpHeaderEntry::getInt64() const
1645 assert (Headers
[id
].type
== ftInt64
);
1647 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1648 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1649 /* XXX: Should we check ok - ie
1650 * return ok ? -1 : value;
1656 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1658 ++ Headers
[id
].stat
.parsCount
;
1661 ++ Headers
[id
].stat
.errCount
;
1662 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1670 /* tmp variable used to pass stat info to dumpers */
1671 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1672 const HttpHeaderStat
*dump_stat
= NULL
;
1675 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1677 const int id
= (int) val
;
1678 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1679 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1680 int visible
= count
> 0;
1681 /* for entries with zero count, list only those that belong to current type of message */
1683 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1684 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1687 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1688 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1692 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1695 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1696 idx
, (int) val
, count
,
1697 xpercent(count
, dump_stat
->destroyedCount
));
1702 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1707 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1708 storeAppendPrintf(e
, "\nField type distribution\n");
1709 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1710 "id", "name", "count", "#/header");
1711 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1712 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1713 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1714 "id", "name", "count", "#/cc_field");
1715 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1716 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1717 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1718 "id", "name", "count", "#/sc_field");
1719 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1720 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1721 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1722 "id", "#flds", "count", "%total");
1723 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1728 httpHeaderStoreReport(StoreEntry
* e
)
1734 HttpHeaderStats
[0].parsedCount
=
1735 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1736 HttpHeaderStats
[0].ccParsedCount
=
1737 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1738 HttpHeaderStats
[0].destroyedCount
=
1739 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1740 HttpHeaderStats
[0].busyDestroyedCount
=
1741 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1743 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1744 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1745 storeAppendPrintf(e
, "%s\n", "<br>");
1748 /* field stats for all messages */
1749 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1751 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1752 "id", "name", "#alive", "%err", "%repeat");
1754 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1755 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1756 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1757 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1758 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1759 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1762 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1763 HttpHeaderStats
[hoRequest
].parsedCount
,
1764 HttpHeaderStats
[hoReply
].parsedCount
,
1765 HttpHeaderStats
[0].parsedCount
);
1766 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1770 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1773 for (int i
= 0; i
< end
; ++i
) {
1774 if (name_len
!= info
[i
].name
.size())
1777 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1786 httpHeaderIdByNameDef(const char *name
, int name_len
)
1789 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1791 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1795 httpHeaderNameById(int id
)
1798 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1800 assert(id
>= 0 && id
< HDR_ENUM_END
);
1802 return Headers
[id
].name
.termedBuf();
1806 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1809 const char *pos
= NULL
;
1812 int mlen
= strlen(member
);
1816 String
header (getStrOrList(id
));
1818 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1819 if (strncasecmp(item
, member
, mlen
) == 0
1820 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1830 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1833 const char *pos
= NULL
;
1836 int mlen
= strlen(member
);
1840 String
header (getByName(name
));
1842 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1843 if (strncasecmp(item
, member
, mlen
) == 0
1844 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1854 HttpHeader::removeHopByHopEntries()
1856 removeConnectionHeaderEntries();
1858 const HttpHeaderEntry
*e
;
1859 HttpHeaderPos pos
= HttpHeaderInitPos
;
1860 int headers_deleted
= 0;
1861 while ((e
= getEntry(&pos
))) {
1863 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1864 delAt(pos
, headers_deleted
);
1871 HttpHeader::removeConnectionHeaderEntries()
1873 if (has(HDR_CONNECTION
)) {
1874 /* anything that matches Connection list member will be deleted */
1875 String strConnection
;
1877 (void) getList(HDR_CONNECTION
, &strConnection
);
1878 const HttpHeaderEntry
*e
;
1879 HttpHeaderPos pos
= HttpHeaderInitPos
;
1881 * think: on-average-best nesting of the two loops (hdrEntry
1882 * and strListItem) @?@
1885 * maybe we should delete standard stuff ("keep-alive","close")
1886 * from strConnection first?
1889 int headers_deleted
= 0;
1890 while ((e
= getEntry(&pos
))) {
1891 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1892 delAt(pos
, headers_deleted
);
1894 if (headers_deleted
)