3 * $Id: HttpHeader.cc,v 1.139 2008/01/22 19:53:03 rousskov Exp $
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.
37 #include "CacheManager.h"
39 #include "HttpHeader.h"
40 #include "HttpHdrContRange.h"
41 #include "HttpHdrSc.h"
45 * On naming conventions:
47 * HTTP/1.1 defines message-header as
49 * message-header = field-name ":" [ field-value ] CRLF
51 * field-value = *( field-content | LWS )
53 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
54 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
56 * HttpHeader is an object that represents all message-headers in a message.
57 * HttpHeader does not manage start-line.
59 * HttpHeader is implemented as a collection of header "entries".
60 * An entry is a (field_id, field_name, field_value) triplet.
65 * local constants and vars
69 * A table with major attributes for every known field.
70 * We calculate name lengths and reorganize this array on start up.
71 * After reorganization, field id can be used as an index to the table.
73 static const HttpHeaderFieldAttrs HeadersAttrs
[] =
75 {"Accept", HDR_ACCEPT
, ftStr
},
77 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
78 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
79 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
80 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
81 {"Age", HDR_AGE
, ftInt
},
82 {"Allow", HDR_ALLOW
, ftStr
},
83 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
84 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
85 {"Connection", HDR_CONNECTION
, ftStr
},
86 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
87 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
88 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
89 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
90 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
91 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
92 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
93 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
94 {"Cookie", HDR_COOKIE
, ftStr
},
95 {"Date", HDR_DATE
, ftDate_1123
},
96 {"ETag", HDR_ETAG
, ftETag
},
97 {"Expires", HDR_EXPIRES
, ftDate_1123
},
98 {"From", HDR_FROM
, ftStr
},
99 {"Host", HDR_HOST
, ftStr
},
100 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
101 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
102 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
103 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
104 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
105 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
106 {"Link", HDR_LINK
, ftStr
},
107 {"Location", HDR_LOCATION
, ftStr
},
108 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt
},
109 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
110 {"Pragma", HDR_PRAGMA
, ftStr
},
111 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
112 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
113 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
114 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
115 {"Public", HDR_PUBLIC
, ftStr
},
116 {"Range", HDR_RANGE
, ftPRange
},
117 {"Referer", HDR_REFERER
, ftStr
},
118 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
119 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
120 {"Server", HDR_SERVER
, ftStr
},
121 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
122 {"TE", HDR_TE
, ftStr
},
123 {"Title", HDR_TITLE
, ftStr
},
124 {"Trailers", HDR_TRAILERS
, ftStr
},
125 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
126 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
127 {"User-Agent", HDR_USER_AGENT
, ftStr
},
128 {"Vary", HDR_VARY
, ftStr
}, /* for now */
129 {"Via", HDR_VIA
, ftStr
}, /* for now */
130 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
131 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
132 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
133 {"X-Cache", HDR_X_CACHE
, ftStr
},
134 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
135 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
136 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
137 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
138 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
139 #if X_ACCELERATOR_VARY
140 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
142 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
143 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
144 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
145 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
148 static HttpHeaderFieldInfo
*Headers
= NULL
;
150 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
152 int tmp
= (int)aHeader
;
153 aHeader
= (http_hdr_type
)(++tmp
);
159 * headers with field values defined as #(values) in HTTP/1.1
160 * Headers that are currently not recognized, are commented out.
162 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
163 static http_hdr_type ListHeadersArr
[] =
166 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
167 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
169 HDR_CONTENT_ENCODING
,
170 HDR_CONTENT_LANGUAGE
,
172 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
173 HDR_LINK
, HDR_PRAGMA
,
174 HDR_PROXY_CONNECTION
,
175 HDR_TRANSFER_ENCODING
,
180 HDR_WWW_AUTHENTICATE
,
181 HDR_AUTHENTICATION_INFO
,
182 HDR_PROXY_AUTHENTICATION_INFO
,
183 /* HDR_EXPECT, HDR_TE, HDR_TRAILER */
184 #if X_ACCELERATOR_VARY
185 HDR_X_ACCELERATOR_VARY
,
187 HDR_SURROGATE_CAPABILITY
,
188 HDR_SURROGATE_CONTROL
,
192 /* general-headers */
193 static http_hdr_type GeneralHeadersArr
[] =
195 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
196 HDR_TRANSFER_ENCODING
,
203 static http_hdr_type EntityHeadersArr
[] =
205 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
206 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
207 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
211 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
212 static http_hdr_type ReplyHeadersArr
[] =
214 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
215 HDR_ACCEPT_RANGES
, HDR_AGE
,
216 HDR_LOCATION
, HDR_MAX_FORWARDS
,
217 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
,
219 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
222 #if X_ACCELERATOR_VARY
223 HDR_X_ACCELERATOR_VARY
,
226 HDR_SURROGATE_CONTROL
229 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
230 static http_hdr_type RequestHeadersArr
[] =
232 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
233 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
234 HDR_IF_RANGE
, HDR_MAX_FORWARDS
, HDR_PROXY_CONNECTION
,
235 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
236 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
239 static HttpHeaderMask HopByHopHeadersMask
;
240 static http_hdr_type HopByHopHeadersArr
[] =
242 HDR_CONNECTION
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
243 HDR_TE
, HDR_TRAILERS
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
246 /* header accounting */
247 static HttpHeaderStat HttpHeaderStats
[] =
256 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
258 static int HeaderEntryParsedCount
= 0;
264 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
266 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
268 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
269 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
272 * Module initialization routines
276 httpHeaderRegisterWithCacheManager(void)
278 CacheManager::GetInstance()->
279 registerAction("http_headers",
280 "HTTP Header Statistics",
281 httpHeaderStoreReport
, 0, 1);
285 httpHeaderInitModule(void)
288 /* check that we have enough space for masks */
289 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
290 /* all headers must be described */
291 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
294 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
297 httpHeaderMaskInit(&ListHeadersMask
, 0);
299 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
301 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
303 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
305 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
307 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
309 httpHeaderMaskInit(&RequestHeadersMask
, 0);
311 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
313 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
315 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
317 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
319 /* init header stats */
320 assert(HttpHeaderStatCount
== hoReply
+ 1);
322 for (i
= 0; i
< HttpHeaderStatCount
; i
++)
323 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
325 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
327 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
331 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
334 /* init dependent modules */
335 httpHdrCcInitModule();
337 httpHdrScInitModule();
339 httpHeaderRegisterWithCacheManager();
343 httpHeaderCleanModule(void)
345 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
347 httpHdrCcCleanModule();
348 httpHdrScCleanModule();
352 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
356 memset(hs
, 0, sizeof(HttpHeaderStat
));
358 statHistEnumInit(&hs
->hdrUCountDistr
, 32); /* not a real enum */
359 statHistEnumInit(&hs
->fieldTypeDistr
, HDR_ENUM_END
);
360 statHistEnumInit(&hs
->ccTypeDistr
, CC_ENUM_END
);
361 statHistEnumInit(&hs
->scTypeDistr
, SC_ENUM_END
);
365 * HttpHeader Implementation
368 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
370 httpHeaderMaskInit(&mask
, 0);
373 HttpHeader::HttpHeader(http_hdr_owner_type
const &anOwner
) : owner (anOwner
), len (0)
375 assert(anOwner
> hoNone
&& anOwner
<= hoReply
);
376 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
377 httpHeaderMaskInit(&mask
, 0);
380 HttpHeader::~HttpHeader()
388 HttpHeaderPos pos
= HttpHeaderInitPos
;
391 assert(owner
> hoNone
&& owner
<= hoReply
);
392 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
394 PROF_start(HttpHeaderClean
);
397 * An unfortunate bug. The entries array is initialized
398 * such that count is set to zero. httpHeaderClean() seems to
399 * be called both when 'hdr' is created, and destroyed. Thus,
400 * we accumulate a large number of zero counts for 'hdr' before
401 * it is ever used. Can't think of a good way to fix it, except
402 * adding a state variable that indicates whether or not 'hdr'
403 * has been used. As a hack, just never count zero-sized header
407 if (0 != entries
.count
)
408 statHistCount(&HttpHeaderStats
[owner
].hdrUCountDistr
, entries
.count
);
410 HttpHeaderStats
[owner
].destroyedCount
++;
412 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.count
> 0;
414 while ((e
= getEntry(&pos
))) {
415 /* tmp hack to try to avoid coredumps */
417 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
418 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos
<< "] is invalid (" << e
->id
<< "). Ignored.");
420 statHistCount(&HttpHeaderStats
[owner
].fieldTypeDistr
, e
->id
);
421 /* yes, this deletion leaves us in an inconsistent state */
426 httpHeaderMaskInit(&mask
, 0);
427 PROF_stop(HttpHeaderClean
);
430 /* append entries (also see httpHeaderUpdate) */
432 HttpHeader::append(const HttpHeader
* src
)
434 const HttpHeaderEntry
*e
;
435 HttpHeaderPos pos
= HttpHeaderInitPos
;
438 debugs(55, 7, "appending hdr: " << this << " += " << src
);
440 while ((e
= src
->getEntry(&pos
))) {
441 addEntry(e
->clone());
445 /* use fresh entries to replace old ones */
447 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
450 old
->update (fresh
, denied_mask
);
454 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
456 const HttpHeaderEntry
*e
;
457 HttpHeaderPos pos
= HttpHeaderInitPos
;
459 assert(this != fresh
);
461 while ((e
= fresh
->getEntry(&pos
))) {
462 /* deny bad guys (ok to check for HDR_OTHER) here */
464 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
467 if (e
->id
!= HDR_OTHER
)
470 delByName(e
->name
.buf());
473 pos
= HttpHeaderInitPos
;
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 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
482 addEntry(e
->clone());
486 /* just handy in parsing: resets and returns false */
490 http_hdr_owner_type ho
;
493 *this = HttpHeader(ho
);
498 HttpHeader::parse(const char *header_start
, const char *header_end
)
500 const char *field_ptr
= header_start
;
501 HttpHeaderEntry
*e
, *e2
;
503 PROF_start(HttpHeaderParse
);
505 assert(header_start
&& header_end
);
506 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
507 HttpHeaderStats
[owner
].parsedCount
++;
509 if (memchr(header_start
, '\0', header_end
- header_start
)) {
510 debugs(55, 1, "WARNING: HTTP header contains NULL characters {" <<
511 getStringPrefix(header_start
, header_end
) << "}");
515 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
516 * continuation lines start with a (single) space or tab */
517 while (field_ptr
< header_end
) {
518 const char *field_start
= field_ptr
;
519 const char *field_end
;
522 const char *this_line
= field_ptr
;
523 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
526 goto reset
; /* missing <LF> */
528 field_end
= field_ptr
;
530 field_ptr
++; /* Move to next line */
532 if (field_end
> this_line
&& field_end
[-1] == '\r') {
533 field_end
--; /* Ignore CR LF */
534 /* Ignore CR CR LF in relaxed mode */
536 if (Config
.onoff
.relaxed_header_parser
&& field_end
> this_line
+ 1 && field_end
[-1] == '\r') {
537 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
538 "WARNING: Double CR characters in HTTP header {" << getStringPrefix(field_start
, field_end
) << "}");
543 /* Barf on stray CR characters */
544 if (memchr(this_line
, '\r', field_end
- this_line
)) {
545 debugs(55, 1, "WARNING: suspicious CR characters in HTTP header {" <<
546 getStringPrefix(field_start
, field_end
) << "}");
548 if (Config
.onoff
.relaxed_header_parser
) {
549 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
551 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
)
557 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
558 debugs(55, 1, "WARNING: Blank continuation line in HTTP header {" <<
559 getStringPrefix(header_start
, header_end
) << "}");
562 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
564 if (field_start
== field_end
) {
565 if (field_ptr
< header_end
) {
566 debugs(55, 1, "WARNING: unparseable HTTP header field near {" <<
567 getStringPrefix(field_start
, header_end
) << "}");
571 break; /* terminating blank line */
574 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
575 debugs(55, 1, "WARNING: unparseable HTTP header field {" <<
576 getStringPrefix(field_start
, field_end
) << "}");
577 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
578 " in {" << getStringPrefix(header_start
, header_end
) << "}");
580 if (Config
.onoff
.relaxed_header_parser
)
586 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
587 if (e
->value
.cmp(e2
->value
.buf()) != 0) {
589 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
590 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start
, header_end
) << "}");
592 if (!Config
.onoff
.relaxed_header_parser
) {
597 if (!httpHeaderParseOffset(e
->value
.buf(), &l1
)) {
598 debugs(55, 1, "WARNING: Unparseable content-length '" << e
->value
.buf() << "'");
601 } else if (!httpHeaderParseOffset(e2
->value
.buf(), &l2
)) {
602 debugs(55, 1, "WARNING: Unparseable content-length '" << e2
->value
.buf() << "'");
604 } else if (l1
> l2
) {
611 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
612 "NOTICE: found double content-length header");
614 if (Config
.onoff
.relaxed_header_parser
) {
624 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.buf())) {
625 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
626 "WARNING: found whitespace in HTTP header name {" <<
627 getStringPrefix(field_start
, field_end
) << "}");
629 if (!Config
.onoff
.relaxed_header_parser
) {
638 PROF_stop(HttpHeaderParse
);
639 return 1; /* even if no fields where found, it is a valid header */
641 PROF_stop(HttpHeaderParse
);
645 /* packs all the entries using supplied packer */
647 HttpHeader::packInto(Packer
* p
) const
649 HttpHeaderPos pos
= HttpHeaderInitPos
;
650 const HttpHeaderEntry
*e
;
652 debugs(55, 7, "packing hdr: (" << this << ")");
653 /* pack all entries one by one */
654 while ((e
= getEntry(&pos
)))
657 /* Pack in the "special" entries */
662 /* returns next valid entry */
664 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
667 assert(*pos
>= HttpHeaderInitPos
&& *pos
< (ssize_t
)entries
.count
);
669 for ((*pos
)++; *pos
< (ssize_t
)entries
.count
; (*pos
)++) {
670 if (entries
.items
[*pos
])
671 return (HttpHeaderEntry
*)entries
.items
[*pos
];
678 * returns a pointer to a specified entry if any
679 * note that we return one entry so it does not make much sense to ask for
683 HttpHeader::findEntry(http_hdr_type id
) const
685 HttpHeaderPos pos
= HttpHeaderInitPos
;
688 assert(!CBIT_TEST(ListHeadersMask
, id
));
690 /* check mask first */
692 if (!CBIT_TEST(mask
, id
))
695 /* looks like we must have it, do linear search */
696 while ((e
= getEntry(&pos
))) {
701 /* hm.. we thought it was there, but it was not found */
704 return NULL
; /* not reached */
708 * same as httpHeaderFindEntry
711 HttpHeader::findLastEntry(http_hdr_type id
) const
713 HttpHeaderPos pos
= HttpHeaderInitPos
;
715 HttpHeaderEntry
*result
= NULL
;
717 assert(!CBIT_TEST(ListHeadersMask
, id
));
719 /* check mask first */
721 if (!CBIT_TEST(mask
, id
))
724 /* looks like we must have it, do linear search */
725 while ((e
= getEntry(&pos
))) {
730 assert(result
); /* must be there! */
735 * deletes all fields with a given name if any, returns #fields deleted;
738 HttpHeader::delByName(const char *name
)
741 HttpHeaderPos pos
= HttpHeaderInitPos
;
743 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
744 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
746 while ((e
= getEntry(&pos
))) {
747 if (!e
->name
.caseCmp(name
))
750 CBIT_SET(mask
, e
->id
);
756 /* deletes all entries with a given id, returns the #entries deleted */
758 HttpHeader::delById(http_hdr_type id
)
761 HttpHeaderPos pos
= HttpHeaderInitPos
;
763 debugs(55, 8, this << " del-by-id " << id
);
765 assert(id
!= HDR_OTHER
); /* does not make sense */
767 if (!CBIT_TEST(mask
, id
))
770 while ((e
= getEntry(&pos
))) {
781 * deletes an entry at pos and leaves a gap; leaving a gap makes it
782 * possible to iterate(search) and delete fields at the same time
783 * NOTE: Does not update the header mask. Caller must follow up with
784 * a call to refreshMask() if headers_deleted was incremented.
787 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
790 assert(pos
>= HttpHeaderInitPos
&& pos
< (ssize_t
)entries
.count
);
791 e
= (HttpHeaderEntry
*)entries
.items
[pos
];
792 entries
.items
[pos
] = NULL
;
793 /* decrement header length, allow for ": " and crlf */
794 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
801 * Compacts the header storage
804 HttpHeader::compact()
810 * Refreshes the header mask. Required after delAt() calls.
813 HttpHeader::refreshMask()
815 httpHeaderMaskInit(&mask
, 0);
816 debugs(55, 7, "refreshing the mask in hdr " << this);
817 HttpHeaderPos pos
= HttpHeaderInitPos
;
818 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
819 CBIT_SET(mask
, e
->id
);
824 * does not call e->clone() so one should not reuse "*e"
827 HttpHeader::addEntry(HttpHeaderEntry
* e
)
831 assert(e
->name
.size());
833 debugs(55, 9, this << " adding entry: " << e
->id
<< " at " <<
836 if (CBIT_TEST(mask
, e
->id
))
837 Headers
[e
->id
].stat
.repCount
++;
839 CBIT_SET(mask
, e
->id
);
841 entries
.push_back(e
);
843 /* increment header length, allow for ": " and crlf */
844 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
848 * does not call e->clone() so one should not reuse "*e"
851 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
856 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " <<
859 if (CBIT_TEST(mask
, e
->id
))
860 Headers
[e
->id
].stat
.repCount
++;
862 CBIT_SET(mask
, e
->id
);
866 /* increment header length, allow for ": " and crlf */
867 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
871 HttpHeader::getList(http_hdr_type id
, String
*s
) const
874 HttpHeaderPos pos
= HttpHeaderInitPos
;
875 debugs(55, 9, this << " joining for id " << id
);
876 /* only fields from ListHeaders array can be "listed" */
877 assert(CBIT_TEST(ListHeadersMask
, id
));
879 if (!CBIT_TEST(mask
, id
))
882 while ((e
= getEntry(&pos
))) {
884 strListAdd(s
, e
->value
.buf(), ',');
888 * note: we might get an empty (size==0) string if there was an "empty"
889 * header. This results in an empty length String, which may have a NULL
892 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
894 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
896 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
901 /* return a list of entries with the same id separated by ',' and ws */
903 HttpHeader::getList(http_hdr_type id
) const
906 HttpHeaderPos pos
= HttpHeaderInitPos
;
907 debugs(55, 9, this << "joining for id " << id
);
908 /* only fields from ListHeaders array can be "listed" */
909 assert(CBIT_TEST(ListHeadersMask
, id
));
911 if (!CBIT_TEST(mask
, id
))
916 while ((e
= getEntry(&pos
))) {
918 strListAdd(&s
, e
->value
.buf(), ',');
922 * note: we might get an empty (size==0) string if there was an "empty"
923 * header. This results in an empty length String, which may have a NULL
926 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
928 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
930 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
935 /* return a string or list of entries with the same id separated by ',' and ws */
937 HttpHeader::getStrOrList(http_hdr_type id
) const
941 if (CBIT_TEST(ListHeadersMask
, id
))
944 if ((e
= findEntry(id
)))
951 * Returns the value of the specified header.
954 HttpHeader::getByName(const char *name
) const
957 HttpHeaderPos pos
= HttpHeaderInitPos
;
962 /* First try the quick path */
963 id
= httpHeaderIdByNameDef(name
, strlen(name
));
966 return getStrOrList(id
);
970 /* Sorry, an unknown header name. Do linear search */
971 while ((e
= getEntry(&pos
))) {
972 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
973 strListAdd(&result
, e
->value
.buf(), ',');
981 * Returns a the value of the specified list member, if any.
984 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
987 const char *pos
= NULL
;
990 int mlen
= strlen(member
);
994 header
= getByName(name
);
998 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
999 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1000 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1009 * returns a the value of the specified list member, if any.
1012 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1015 const char *pos
= NULL
;
1018 int mlen
= strlen(member
);
1022 header
= getStrOrList(id
);
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 /* test if a field is present */
1038 HttpHeader::has(http_hdr_type id
) const
1041 assert(id
!= HDR_OTHER
);
1042 debugs(55, 9, this << " lookup for " << id
);
1043 return CBIT_TEST(mask
, id
);
1047 HttpHeader::putInt(http_hdr_type id
, int number
)
1050 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1051 assert(number
>= 0);
1052 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1056 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1059 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1060 assert(number
>= 0);
1061 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1065 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1068 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1070 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1074 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1077 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1079 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1083 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1086 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1088 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1092 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1094 assert(auth_scheme
&& realm
);
1095 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1099 HttpHeader::putCc(const HttpHdrCc
* cc
)
1104 /* remove old directives if any */
1105 delById(HDR_CACHE_CONTROL
);
1108 packerToMemInit(&p
, &mb
);
1109 httpHdrCcPackInto(cc
, &p
);
1111 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1118 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1123 /* remove old directives if any */
1124 delById(HDR_CONTENT_RANGE
);
1127 packerToMemInit(&p
, &mb
);
1128 httpHdrContRangePackInto(cr
, &p
);
1130 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1137 HttpHeader::putRange(const HttpHdrRange
* range
)
1142 /* remove old directives if any */
1146 packerToMemInit(&p
, &mb
);
1147 range
->packInto(&p
);
1149 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1156 HttpHeader::putSc(HttpHdrSc
*sc
)
1161 /* remove old directives if any */
1165 packerToMemInit(&p
, &mb
);
1166 httpHdrScPackInto(sc
, &p
);
1168 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1174 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1176 HttpHeader::putExt(const char *name
, const char *value
)
1178 assert(name
&& value
);
1179 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1180 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1184 HttpHeader::getInt(http_hdr_type id
) const
1187 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1190 if ((e
= findEntry(id
)))
1197 HttpHeader::getInt64(http_hdr_type id
) const
1200 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1203 if ((e
= findEntry(id
)))
1204 return e
->getInt64();
1210 HttpHeader::getTime(http_hdr_type id
) const
1215 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1217 if ((e
= findEntry(id
))) {
1218 value
= parse_rfc1123(e
->value
.buf());
1219 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1225 /* sync with httpHeaderGetLastStr */
1227 HttpHeader::getStr(http_hdr_type id
) const
1231 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1233 if ((e
= findEntry(id
))) {
1234 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1235 return e
->value
.buf();
1243 HttpHeader::getLastStr(http_hdr_type id
) const
1247 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1249 if ((e
= findLastEntry(id
))) {
1250 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1251 return e
->value
.buf();
1258 HttpHeader::getCc() const
1263 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1265 PROF_start(HttpHeader_getCc
);
1267 getList(HDR_CACHE_CONTROL
, &s
);
1269 cc
= httpHdrCcParseCreate(&s
);
1271 HttpHeaderStats
[owner
].ccParsedCount
++;
1274 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1276 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1278 PROF_stop(HttpHeader_getCc
);
1284 HttpHeader::getRange() const
1286 HttpHdrRange
*r
= NULL
;
1288 /* some clients will send "Request-Range" _and_ *matching* "Range"
1289 * who knows, some clients might send Request-Range only;
1290 * this "if" should work correctly in both cases;
1291 * hopefully no clients send mismatched headers! */
1293 if ((e
= findEntry(HDR_RANGE
)) ||
1294 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1295 r
= HttpHdrRange::ParseCreate(&e
->value
);
1296 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1303 HttpHeader::getSc() const
1305 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1310 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1312 HttpHdrSc
*sc
= httpHdrScParseCreate(&s
);
1314 HttpHeaderStats
[owner
].ccParsedCount
++;
1317 httpHdrScUpdateStats(sc
, &HttpHeaderStats
[owner
].scTypeDistr
);
1319 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1325 HttpHeader::getContRange() const
1327 HttpHdrContRange
*cr
= NULL
;
1330 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1331 cr
= httpHdrContRangeParseCreate(e
->value
.buf());
1332 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1339 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1343 assert(auth_scheme
);
1346 if (!field
) /* no authorization field */
1349 l
= strlen(auth_scheme
);
1351 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1356 if (!xisspace(*field
)) /* wrong scheme */
1359 /* skip white space */
1360 field
+= xcountws(field
);
1362 if (!*field
) /* no authorization cookie */
1365 return base64_decode(field
);
1369 HttpHeader::getETag(http_hdr_type id
) const
1374 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1376 if ((e
= findEntry(id
)))
1377 etagParseInit(&etag
, e
->value
.buf());
1383 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1387 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1388 memset(&tot
, 0, sizeof(tot
));
1390 if ((e
= findEntry(id
))) {
1391 const char *str
= e
->value
.buf();
1392 /* try as an ETag */
1394 if (etagParseInit(&tot
.tag
, str
)) {
1395 tot
.valid
= tot
.tag
.str
!= NULL
;
1398 /* or maybe it is time? */
1399 tot
.time
= parse_rfc1123(str
);
1400 tot
.valid
= tot
.time
>= 0;
1405 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1413 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1418 if (id
!= HDR_OTHER
)
1419 name
= Headers
[id
].name
;
1425 Headers
[id
].stat
.aliveCount
++;
1427 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
.buf() << " : " << value
.buf());
1430 HttpHeaderEntry::~HttpHeaderEntry()
1433 debugs(55, 9, "destroying entry " << this << ": '" << name
.buf() << ": " << value
.buf() << "'");
1434 /* clean name if needed */
1436 if (id
== HDR_OTHER
)
1441 assert(Headers
[id
].stat
.aliveCount
);
1443 Headers
[id
].stat
.aliveCount
--;
1448 /* parses and inits header entry, returns true/false */
1450 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1452 /* note: name_start == field_start */
1453 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1454 int name_len
= name_end
? name_end
- field_start
: 0;
1455 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1456 /* note: value_end == field_end */
1458 HeaderEntryParsedCount
++;
1460 /* do we have a valid field name within this field? */
1462 if (!name_len
|| name_end
> field_end
)
1465 if (name_len
> 65534) {
1466 /* String must be LESS THAN 64K and it adds a terminating NULL */
1467 debugs(55, 1, "WARNING: ignoring header name of " << name_len
<< " bytes");
1471 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1472 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1473 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1475 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1482 /* now we know we can parse it */
1484 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1486 /* is it a "known" field? */
1487 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1498 /* set field name */
1499 if (id
== HDR_OTHER
)
1500 name
.limitInit(field_start
, name_len
);
1502 name
= Headers
[id
].name
;
1504 /* trim field value */
1505 while (value_start
< field_end
&& xisspace(*value_start
))
1508 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1511 if (field_end
- value_start
> 65534) {
1512 /* String must be LESS THAN 64K and it adds a terminating NULL */
1513 debugs(55, 1, "WARNING: ignoring '" << name
.buf() << "' header of " << (field_end
- value_start
) << " bytes");
1515 if (id
== HDR_OTHER
)
1521 /* set field value */
1522 value
.limitInit(value_start
, field_end
- value_start
);
1524 Headers
[id
].stat
.seenCount
++;
1526 Headers
[id
].stat
.aliveCount
++;
1528 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
.buf() << ": " << value
.buf() << "'");
1530 return new HttpHeaderEntry(id
, name
.buf(), value
.buf());
1534 HttpHeaderEntry::clone() const
1536 return new HttpHeaderEntry(id
, name
.buf(), value
.buf());
1540 HttpHeaderEntry::packInto(Packer
* p
) const
1543 packerAppend(p
, name
.buf(), name
.size());
1544 packerAppend(p
, ": ", 2);
1545 packerAppend(p
, value
.buf(), value
.size());
1546 packerAppend(p
, "\r\n", 2);
1550 HttpHeaderEntry::getInt() const
1553 assert (Headers
[id
].type
== ftInt
);
1555 int ok
= httpHeaderParseInt(value
.buf(), &val
);
1556 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1557 /* XXX: Should we check ok - ie
1558 * return ok ? -1 : value;
1564 HttpHeaderEntry::getInt64() const
1567 assert (Headers
[id
].type
== ftInt64
);
1569 int ok
= httpHeaderParseOffset(value
.buf(), &val
);
1570 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1571 /* XXX: Should we check ok - ie
1572 * return ok ? -1 : value;
1578 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1580 Headers
[id
].stat
.parsCount
++;
1583 Headers
[id
].stat
.errCount
++;
1584 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
.buf() << ": " << context
.buf() << "'");
1592 /* tmp variable used to pass stat info to dumpers */
1593 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1594 const HttpHeaderStat
*dump_stat
= NULL
;
1597 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1599 const int id
= (int) val
;
1600 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1601 const char *name
= valid_id
? Headers
[id
].name
.buf() : "INVALID";
1602 int visible
= count
> 0;
1603 /* for entries with zero count, list only those that belong to current type of message */
1605 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1606 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1609 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1610 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1614 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1617 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1618 idx
, (int) val
, count
,
1619 xpercent(count
, dump_stat
->destroyedCount
));
1624 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1629 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1630 storeAppendPrintf(e
, "\nField type distribution\n");
1631 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1632 "id", "name", "count", "#/header");
1633 statHistDump(&hs
->fieldTypeDistr
, e
, httpHeaderFieldStatDumper
);
1634 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1635 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1636 "id", "name", "count", "#/cc_field");
1637 statHistDump(&hs
->ccTypeDistr
, e
, httpHdrCcStatDumper
);
1638 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1639 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1640 "id", "name", "count", "#/sc_field");
1641 statHistDump(&hs
->scTypeDistr
, e
, httpHdrScStatDumper
);
1642 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1643 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1644 "id", "#flds", "count", "%total");
1645 statHistDump(&hs
->hdrUCountDistr
, e
, httpHeaderFldsPerHdrDumper
);
1650 httpHeaderStoreReport(StoreEntry
* e
)
1656 HttpHeaderStats
[0].parsedCount
=
1657 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1658 HttpHeaderStats
[0].ccParsedCount
=
1659 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1660 HttpHeaderStats
[0].destroyedCount
=
1661 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1662 HttpHeaderStats
[0].busyDestroyedCount
=
1663 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1665 for (i
= 1; i
< HttpHeaderStatCount
; i
++) {
1666 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1667 storeAppendPrintf(e
, "%s\n", "<br>");
1670 /* field stats for all messages */
1671 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1673 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1674 "id", "name", "#alive", "%err", "%repeat");
1676 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1677 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1678 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1679 f
->id
, f
->name
.buf(), f
->stat
.aliveCount
,
1680 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1681 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1684 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1685 HttpHeaderStats
[hoRequest
].parsedCount
,
1686 HttpHeaderStats
[hoReply
].parsedCount
,
1687 HttpHeaderStats
[0].parsedCount
);
1688 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1692 httpHeaderIdByName(const char *name
, int name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1696 for (i
= 0; i
< end
; ++i
) {
1697 if (name_len
>= 0 && name_len
!= info
[i
].name
.size())
1700 if (!strncasecmp(name
, info
[i
].name
.buf(),
1701 name_len
< 0 ? info
[i
].name
.size() + 1 : name_len
))
1709 httpHeaderIdByNameDef(const char *name
, int name_len
)
1712 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1714 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1718 httpHeaderNameById(int id
)
1721 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1723 assert(id
>= 0 && id
< HDR_ENUM_END
);
1725 return Headers
[id
].name
.buf();
1729 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1732 const char *pos
= NULL
;
1735 int mlen
= strlen(member
);
1739 String
header (getStrOrList(id
));
1741 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1742 if (strncmp(item
, member
, mlen
) == 0
1743 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1753 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1756 const char *pos
= NULL
;
1759 int mlen
= strlen(member
);
1763 String
header (getByName(name
));
1765 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1766 if (strncmp(item
, member
, mlen
) == 0
1767 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1777 HttpHeader::removeHopByHopEntries()
1779 removeConnectionHeaderEntries();
1781 const HttpHeaderEntry
*e
;
1782 HttpHeaderPos pos
= HttpHeaderInitPos
;
1783 int headers_deleted
= 0;
1784 while ((e
= getEntry(&pos
))) {
1786 if(CBIT_TEST(HopByHopHeadersMask
, id
)){
1787 delAt(pos
, headers_deleted
);
1794 HttpHeader::removeConnectionHeaderEntries()
1796 if (has(HDR_CONNECTION
)) {
1797 /* anything that matches Connection list member will be deleted */
1798 String strConnection
;
1800 (void) getList(HDR_CONNECTION
, &strConnection
);
1801 const HttpHeaderEntry
*e
;
1802 HttpHeaderPos pos
= HttpHeaderInitPos
;
1804 * think: on-average-best nesting of the two loops (hdrEntry
1805 * and strListItem) @?@
1808 * maybe we should delete standard stuff ("keep-alive","close")
1809 * from strConnection first?
1812 int headers_deleted
= 0;
1813 while ((e
= getEntry(&pos
))) {
1814 if (strListIsMember(&strConnection
, e
->name
.buf(), ','))
1815 delAt(pos
, headers_deleted
);
1817 if (headers_deleted
)