2 * DEBUG: section 55 HTTP Header
3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "HttpHdrCc.h"
36 #include "HttpHdrContRange.h"
37 #include "HttpHdrSc.h"
38 #include "HttpHeader.h"
39 #include "HttpHeaderFieldInfo.h"
40 #include "HttpHeaderStat.h"
41 #include "HttpHeaderTools.h"
43 #include "mgr/Registration.h"
44 #include "profiler/Profiler.h"
46 #include "SquidConfig.h"
47 #include "SquidString.h"
51 #include "TimeOrTag.h"
55 /* XXX: the whole set of API managing the entries vector should be rethought
56 * after the parse4r-ng effort is complete.
60 * On naming conventions:
62 * HTTP/1.1 defines message-header as
64 * message-header = field-name ":" [ field-value ] CRLF
66 * field-value = *( field-content | LWS )
68 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
69 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
71 * HttpHeader is an object that represents all message-headers in a message.
72 * HttpHeader does not manage start-line.
74 * HttpHeader is implemented as a collection of header "entries".
75 * An entry is a (field_id, field_name, field_value) triplet.
79 * local constants and vars
83 * A table with major attributes for every known field.
84 * We calculate name lengths and reorganize this array on start up.
85 * After reorganization, field id can be used as an index to the table.
87 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
88 {"Accept", HDR_ACCEPT
, ftStr
},
90 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
91 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
92 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
93 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
94 {"Age", HDR_AGE
, ftInt
},
95 {"Allow", HDR_ALLOW
, ftStr
},
96 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
97 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
98 {"Connection", HDR_CONNECTION
, ftStr
},
99 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
100 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
101 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
102 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
103 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
104 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
105 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
106 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
107 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
108 {"Cookie", HDR_COOKIE
, ftStr
},
109 {"Cookie2", HDR_COOKIE2
, ftStr
},
110 {"Date", HDR_DATE
, ftDate_1123
},
111 {"ETag", HDR_ETAG
, ftETag
},
112 {"Expect", HDR_EXPECT
, ftStr
},
113 {"Expires", HDR_EXPIRES
, ftDate_1123
},
114 {"From", HDR_FROM
, ftStr
},
115 {"Host", HDR_HOST
, ftStr
},
116 {"HTTP2-Settings", HDR_HTTP2_SETTINGS
, ftStr
}, /* for now */
117 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
118 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
119 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
120 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
121 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
122 {"Key", HDR_KEY
, ftStr
},
123 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
124 {"Link", HDR_LINK
, ftStr
},
125 {"Location", HDR_LOCATION
, ftStr
},
126 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
127 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
128 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
129 {"Origin", HDR_ORIGIN
, ftStr
},
130 {"Pragma", HDR_PRAGMA
, ftStr
},
131 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
132 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
133 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
134 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
135 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
136 {"Public", HDR_PUBLIC
, ftStr
},
137 {"Range", HDR_RANGE
, ftPRange
},
138 {"Referer", HDR_REFERER
, ftStr
},
139 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
140 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!) */
141 {"Server", HDR_SERVER
, ftStr
},
142 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
143 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
144 {"TE", HDR_TE
, ftStr
},
145 {"Title", HDR_TITLE
, ftStr
},
146 {"Trailer", HDR_TRAILER
, ftStr
},
147 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
148 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
149 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
150 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
151 {"User-Agent", HDR_USER_AGENT
, ftStr
},
152 {"Vary", HDR_VARY
, ftStr
}, /* for now */
153 {"Via", HDR_VIA
, ftStr
}, /* for now */
154 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
155 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
156 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
157 {"X-Cache", HDR_X_CACHE
, ftStr
},
158 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
159 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
160 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
161 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
162 #if X_ACCELERATOR_VARY
163 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
166 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
168 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
169 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
170 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
171 {"Other:", HDR_OTHER
, ftStr
} /* ':' will not allow matches */
174 static HttpHeaderFieldInfo
*Headers
= NULL
;
176 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
178 int tmp
= (int)aHeader
;
179 aHeader
= (http_hdr_type
)(++tmp
);
184 * headers with field values defined as #(values) in HTTP/1.1
185 * Headers that are currently not recognized, are commented out.
187 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
188 static http_hdr_type ListHeadersArr
[] = {
190 HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
191 HDR_ACCEPT_RANGES
, HDR_ALLOW
,
193 HDR_CONTENT_ENCODING
,
194 HDR_CONTENT_LANGUAGE
,
197 HDR_IF_MATCH
, HDR_IF_NONE_MATCH
,
199 HDR_LINK
, HDR_PRAGMA
,
200 HDR_PROXY_CONNECTION
,
202 HDR_TRANSFER_ENCODING
,
207 HDR_WWW_AUTHENTICATE
,
208 HDR_AUTHENTICATION_INFO
,
209 HDR_PROXY_AUTHENTICATION_INFO
,
210 /* HDR_TE, HDR_TRAILER */
211 #if X_ACCELERATOR_VARY
212 HDR_X_ACCELERATOR_VARY
,
217 HDR_SURROGATE_CAPABILITY
,
218 HDR_SURROGATE_CONTROL
,
222 /* general-headers */
223 static http_hdr_type GeneralHeadersArr
[] = {
224 HDR_CACHE_CONTROL
, HDR_CONNECTION
, HDR_DATE
, HDR_PRAGMA
,
225 HDR_TRANSFER_ENCODING
,
232 static http_hdr_type EntityHeadersArr
[] = {
233 HDR_ALLOW
, HDR_CONTENT_BASE
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
,
234 HDR_CONTENT_LENGTH
, HDR_CONTENT_LOCATION
, HDR_CONTENT_MD5
,
235 HDR_CONTENT_RANGE
, HDR_CONTENT_TYPE
, HDR_ETAG
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, HDR_LINK
,
239 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
240 static http_hdr_type ReplyHeadersArr
[] = {
241 HDR_ACCEPT
, HDR_ACCEPT_CHARSET
, HDR_ACCEPT_ENCODING
, HDR_ACCEPT_LANGUAGE
,
242 HDR_ACCEPT_RANGES
, HDR_AGE
,
244 HDR_LOCATION
, HDR_MAX_FORWARDS
,
245 HDR_MIME_VERSION
, HDR_PUBLIC
, HDR_RETRY_AFTER
, HDR_SERVER
, HDR_SET_COOKIE
, HDR_SET_COOKIE2
,
248 HDR_WARNING
, HDR_PROXY_CONNECTION
, HDR_X_CACHE
,
251 #if X_ACCELERATOR_VARY
252 HDR_X_ACCELERATOR_VARY
,
258 HDR_SURROGATE_CONTROL
261 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
262 static http_hdr_type RequestHeadersArr
[] = {
263 HDR_AUTHORIZATION
, HDR_FROM
, HDR_HOST
,
265 HDR_IF_MATCH
, HDR_IF_MODIFIED_SINCE
, HDR_IF_NONE_MATCH
,
266 HDR_IF_RANGE
, HDR_MAX_FORWARDS
,
268 HDR_PROXY_CONNECTION
,
269 HDR_PROXY_AUTHORIZATION
, HDR_RANGE
, HDR_REFERER
, HDR_REQUEST_RANGE
,
270 HDR_USER_AGENT
, HDR_X_FORWARDED_FOR
, HDR_SURROGATE_CAPABILITY
273 static HttpHeaderMask HopByHopHeadersMask
;
274 static http_hdr_type HopByHopHeadersArr
[] = {
275 HDR_CONNECTION
, HDR_HTTP2_SETTINGS
, HDR_KEEP_ALIVE
, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION
,
276 HDR_TE
, HDR_TRAILER
, HDR_TRANSFER_ENCODING
, HDR_UPGRADE
, HDR_PROXY_CONNECTION
279 /* header accounting */
280 static HttpHeaderStat HttpHeaderStats
[] = {
288 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
290 static int HeaderEntryParsedCount
= 0;
293 * forward declarations and local routines
297 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
299 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
301 static void httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
);
302 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
304 /** store report about current header usage and other stats */
305 static void httpHeaderStoreReport(StoreEntry
* e
);
308 * Module initialization routines
312 httpHeaderRegisterWithCacheManager(void)
314 Mgr::RegisterAction("http_headers",
315 "HTTP Header Statistics",
316 httpHeaderStoreReport
, 0, 1);
320 httpHeaderInitModule(void)
323 /* check that we have enough space for masks */
324 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
325 /* all headers must be described */
326 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
329 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
332 httpHeaderMaskInit(&ListHeadersMask
, 0);
334 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
336 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
338 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
340 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
342 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
344 httpHeaderMaskInit(&RequestHeadersMask
, 0);
346 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
348 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
350 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
352 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
354 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
356 /* init header stats */
357 assert(HttpHeaderStatCount
== hoReply
+ 1);
359 for (i
= 0; i
< HttpHeaderStatCount
; ++i
)
360 httpHeaderStatInit(HttpHeaderStats
+ i
, HttpHeaderStats
[i
].label
);
362 HttpHeaderStats
[hoRequest
].owner_mask
= &RequestHeadersMask
;
364 HttpHeaderStats
[hoReply
].owner_mask
= &ReplyHeadersMask
;
368 HttpHeaderStats
[hoHtcpReply
].owner_mask
= &ReplyHeadersMask
;
371 /* init dependent modules */
372 httpHdrCcInitModule();
374 httpHdrScInitModule();
376 httpHeaderRegisterWithCacheManager();
380 httpHeaderCleanModule(void)
382 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
384 httpHdrCcCleanModule();
385 httpHdrScCleanModule();
389 httpHeaderStatInit(HttpHeaderStat
* hs
, const char *label
)
393 memset(hs
, 0, sizeof(HttpHeaderStat
));
395 hs
->hdrUCountDistr
.enumInit(32); /* not a real enum */
396 hs
->fieldTypeDistr
.enumInit(HDR_ENUM_END
);
397 hs
->ccTypeDistr
.enumInit(CC_ENUM_END
);
398 hs
->scTypeDistr
.enumInit(SC_ENUM_END
);
402 * HttpHeader Implementation
405 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
407 httpHeaderMaskInit(&mask
, 0);
410 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
412 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
413 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
414 httpHeaderMaskInit(&mask
, 0);
417 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
419 httpHeaderMaskInit(&mask
, 0);
420 update(&other
, NULL
); // will update the mask as well
423 HttpHeader::~HttpHeader()
429 HttpHeader::operator =(const HttpHeader
&other
)
431 if (this != &other
) {
432 // we do not really care, but the caller probably does
433 assert(owner
== other
.owner
);
435 update(&other
, NULL
); // will update the mask as well
445 assert(owner
> hoNone
&& owner
< hoEnd
);
446 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
448 PROF_start(HttpHeaderClean
);
450 if (owner
<= hoReply
) {
452 * An unfortunate bug. The entries array is initialized
453 * such that count is set to zero. httpHeaderClean() seems to
454 * be called both when 'hdr' is created, and destroyed. Thus,
455 * we accumulate a large number of zero counts for 'hdr' before
456 * it is ever used. Can't think of a good way to fix it, except
457 * adding a state variable that indicates whether or not 'hdr'
458 * has been used. As a hack, just never count zero-sized header
461 if (!entries
.empty())
462 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
464 ++ HttpHeaderStats
[owner
].destroyedCount
;
466 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
467 } // if (owner <= hoReply)
469 for (std::vector
<HttpHeaderEntry
*>::iterator i
= entries
.begin(); i
!= entries
.end(); ++i
) {
470 HttpHeaderEntry
*e
= *i
;
473 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
474 debugs(55, DBG_CRITICAL
, "BUG: invalid entry (" << e
->id
<< "). Ignored.");
476 if (owner
<= hoReply
)
477 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
483 httpHeaderMaskInit(&mask
, 0);
485 PROF_stop(HttpHeaderClean
);
488 /* append entries (also see httpHeaderUpdate) */
490 HttpHeader::append(const HttpHeader
* src
)
492 const HttpHeaderEntry
*e
;
493 HttpHeaderPos pos
= HttpHeaderInitPos
;
496 debugs(55, 7, "appending hdr: " << this << " += " << src
);
498 while ((e
= src
->getEntry(&pos
))) {
499 addEntry(e
->clone());
503 /* use fresh entries to replace old ones */
505 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
508 old
->update (fresh
, denied_mask
);
512 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
514 const HttpHeaderEntry
*e
;
515 HttpHeaderPos pos
= HttpHeaderInitPos
;
517 assert(this != fresh
);
519 while ((e
= fresh
->getEntry(&pos
))) {
520 /* deny bad guys (ok to check for HDR_OTHER) here */
522 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
525 if (e
->id
!= HDR_OTHER
)
528 delByName(e
->name
.termedBuf());
531 pos
= HttpHeaderInitPos
;
532 while ((e
= fresh
->getEntry(&pos
))) {
533 /* deny bad guys (ok to check for HDR_OTHER) here */
535 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
538 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
540 addEntry(e
->clone());
544 /* just handy in parsing: resets and returns false */
553 HttpHeader::parse(const char *header_start
, const char *header_end
)
555 const char *field_ptr
= header_start
;
556 HttpHeaderEntry
*e
, *e2
;
557 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
559 PROF_start(HttpHeaderParse
);
561 assert(header_start
&& header_end
);
562 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, header_end
));
563 ++ HttpHeaderStats
[owner
].parsedCount
;
566 if ((nulpos
= (char*)memchr(header_start
, '\0', header_end
- header_start
))) {
567 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
568 getStringPrefix(header_start
, nulpos
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, header_end
));
569 PROF_stop(HttpHeaderParse
);
573 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
574 * continuation lines start with a (single) space or tab */
575 while (field_ptr
< header_end
) {
576 const char *field_start
= field_ptr
;
577 const char *field_end
;
580 const char *this_line
= field_ptr
;
581 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
585 PROF_stop(HttpHeaderParse
);
589 field_end
= field_ptr
;
591 ++field_ptr
; /* Move to next line */
593 if (field_end
> this_line
&& field_end
[-1] == '\r') {
594 --field_end
; /* Ignore CR LF */
596 if (owner
== hoRequest
&& field_end
> this_line
) {
598 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
603 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
604 "header field to prevent request smuggling attacks: {" <<
605 getStringPrefix(header_start
, header_end
) << "}");
606 PROF_stop(HttpHeaderParse
);
612 /* Barf on stray CR characters */
613 if (memchr(this_line
, '\r', field_end
- this_line
)) {
614 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
615 getStringPrefix(field_start
, field_end
) << "}");
617 if (Config
.onoff
.relaxed_header_parser
) {
618 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
620 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
625 PROF_stop(HttpHeaderParse
);
630 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
631 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
632 getStringPrefix(header_start
, header_end
) << "}");
633 PROF_stop(HttpHeaderParse
);
636 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
638 if (field_start
== field_end
) {
639 if (field_ptr
< header_end
) {
640 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field near {" <<
641 getStringPrefix(field_start
, header_end
) << "}");
642 PROF_stop(HttpHeaderParse
);
646 break; /* terminating blank line */
649 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
650 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field {" <<
651 getStringPrefix(field_start
, field_end
) << "}");
652 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, header_end
) << "}");
654 if (Config
.onoff
.relaxed_header_parser
)
657 PROF_stop(HttpHeaderParse
);
661 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
662 if (e
->value
!= e2
->value
) {
664 debugs(55, warnOnError
, "WARNING: found two conflicting content-length headers in {" <<
665 getStringPrefix(header_start
, header_end
) << "}");
667 if (!Config
.onoff
.relaxed_header_parser
) {
669 PROF_stop(HttpHeaderParse
);
673 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
674 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
677 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
678 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
680 } else if (l1
> l2
) {
687 debugs(55, warnOnError
, "NOTICE: found double content-length header");
690 if (Config
.onoff
.relaxed_header_parser
)
693 PROF_stop(HttpHeaderParse
);
698 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
699 debugs(55, warnOnError
, "WARNING: found whitespace in HTTP header name {" <<
700 getStringPrefix(field_start
, field_end
) << "}");
702 if (!Config
.onoff
.relaxed_header_parser
) {
704 PROF_stop(HttpHeaderParse
);
713 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
714 delById(HDR_CONTENT_LENGTH
);
717 PROF_stop(HttpHeaderParse
);
718 return 1; /* even if no fields where found, it is a valid header */
721 /* packs all the entries using supplied packer */
723 HttpHeader::packInto(Packer
* p
, bool mask_sensitive_info
) const
725 HttpHeaderPos pos
= HttpHeaderInitPos
;
726 const HttpHeaderEntry
*e
;
728 debugs(55, 7, "packing hdr: (" << this << ")");
729 /* pack all entries one by one */
730 while ((e
= getEntry(&pos
))) {
731 if (!mask_sensitive_info
) {
736 case HDR_AUTHORIZATION
:
737 case HDR_PROXY_AUTHORIZATION
:
738 packerAppend(p
, e
->name
.rawBuf(), e
->name
.size());
739 packerAppend(p
, ": ** NOT DISPLAYED **\r\n", 23);
746 /* Pack in the "special" entries */
751 /* returns next valid entry */
753 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
756 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
758 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
760 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
767 * returns a pointer to a specified entry if any
768 * note that we return one entry so it does not make much sense to ask for
772 HttpHeader::findEntry(http_hdr_type id
) const
774 HttpHeaderPos pos
= HttpHeaderInitPos
;
777 assert(!CBIT_TEST(ListHeadersMask
, id
));
779 /* check mask first */
781 if (!CBIT_TEST(mask
, id
))
784 /* looks like we must have it, do linear search */
785 while ((e
= getEntry(&pos
))) {
790 /* hm.. we thought it was there, but it was not found */
793 return NULL
; /* not reached */
797 * same as httpHeaderFindEntry
800 HttpHeader::findLastEntry(http_hdr_type id
) const
802 HttpHeaderPos pos
= HttpHeaderInitPos
;
804 HttpHeaderEntry
*result
= NULL
;
806 assert(!CBIT_TEST(ListHeadersMask
, id
));
808 /* check mask first */
810 if (!CBIT_TEST(mask
, id
))
813 /* looks like we must have it, do linear search */
814 while ((e
= getEntry(&pos
))) {
819 assert(result
); /* must be there! */
824 * deletes all fields with a given name if any, returns #fields deleted;
827 HttpHeader::delByName(const char *name
)
830 HttpHeaderPos pos
= HttpHeaderInitPos
;
832 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
833 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
835 while ((e
= getEntry(&pos
))) {
836 if (!e
->name
.caseCmp(name
))
839 CBIT_SET(mask
, e
->id
);
845 /* deletes all entries with a given id, returns the #entries deleted */
847 HttpHeader::delById(http_hdr_type id
)
850 HttpHeaderPos pos
= HttpHeaderInitPos
;
852 debugs(55, 8, this << " del-by-id " << id
);
854 assert(id
!= HDR_OTHER
); /* does not make sense */
856 if (!CBIT_TEST(mask
, id
))
859 while ((e
= getEntry(&pos
))) {
870 * deletes an entry at pos and leaves a gap; leaving a gap makes it
871 * possible to iterate(search) and delete fields at the same time
872 * NOTE: Does not update the header mask. Caller must follow up with
873 * a call to refreshMask() if headers_deleted was incremented.
876 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
879 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
880 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
882 /* decrement header length, allow for ": " and crlf */
883 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
890 * Compacts the header storage
893 HttpHeader::compact()
895 // TODO: optimize removal, or possibly make it so that's not needed.
896 std::vector
<HttpHeaderEntry
*>::iterator newend
;
897 newend
= std::remove(entries
.begin(), entries
.end(), static_cast<HttpHeaderEntry
*>(NULL
));
898 entries
.resize(newend
-entries
.begin());
902 * Refreshes the header mask. Required after delAt() calls.
905 HttpHeader::refreshMask()
907 httpHeaderMaskInit(&mask
, 0);
908 debugs(55, 7, "refreshing the mask in hdr " << this);
909 HttpHeaderPos pos
= HttpHeaderInitPos
;
910 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
911 CBIT_SET(mask
, e
->id
);
916 * does not call e->clone() so one should not reuse "*e"
919 HttpHeader::addEntry(HttpHeaderEntry
* e
)
923 assert(e
->name
.size());
925 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
927 if (CBIT_TEST(mask
, e
->id
))
928 ++ Headers
[e
->id
].stat
.repCount
;
930 CBIT_SET(mask
, e
->id
);
932 entries
.push_back(e
);
934 /* increment header length, allow for ": " and crlf */
935 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
939 * does not call e->clone() so one should not reuse "*e"
942 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
947 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
949 if (CBIT_TEST(mask
, e
->id
))
950 ++ Headers
[e
->id
].stat
.repCount
;
952 CBIT_SET(mask
, e
->id
);
954 entries
.insert(entries
.begin(),e
);
956 /* increment header length, allow for ": " and crlf */
957 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
961 HttpHeader::getList(http_hdr_type id
, String
*s
) const
964 HttpHeaderPos pos
= HttpHeaderInitPos
;
965 debugs(55, 9, this << " joining for id " << id
);
966 /* only fields from ListHeaders array can be "listed" */
967 assert(CBIT_TEST(ListHeadersMask
, id
));
969 if (!CBIT_TEST(mask
, id
))
972 while ((e
= getEntry(&pos
))) {
974 strListAdd(s
, e
->value
.termedBuf(), ',');
978 * note: we might get an empty (size==0) string if there was an "empty"
979 * header. This results in an empty length String, which may have a NULL
982 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
984 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
986 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
991 /* return a list of entries with the same id separated by ',' and ws */
993 HttpHeader::getList(http_hdr_type id
) const
996 HttpHeaderPos pos
= HttpHeaderInitPos
;
997 debugs(55, 9, this << "joining for id " << id
);
998 /* only fields from ListHeaders array can be "listed" */
999 assert(CBIT_TEST(ListHeadersMask
, id
));
1001 if (!CBIT_TEST(mask
, id
))
1006 while ((e
= getEntry(&pos
))) {
1008 strListAdd(&s
, e
->value
.termedBuf(), ',');
1012 * note: we might get an empty (size==0) string if there was an "empty"
1013 * header. This results in an empty length String, which may have a NULL
1016 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1018 debugs(55, 3, "empty list header: " << Headers
[id
].name
<< "(" << id
<< ")");
1020 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1025 /* return a string or list of entries with the same id separated by ',' and ws */
1027 HttpHeader::getStrOrList(http_hdr_type id
) const
1031 if (CBIT_TEST(ListHeadersMask
, id
))
1034 if ((e
= findEntry(id
)))
1041 * Returns the value of the specified header and/or an undefined String.
1044 HttpHeader::getByName(const char *name
) const
1047 // ignore presence: return undefined string if an empty header is present
1048 (void)getByNameIfPresent(name
, result
);
1053 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
1056 HttpHeaderPos pos
= HttpHeaderInitPos
;
1061 /* First try the quick path */
1062 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1067 result
= getStrOrList(id
);
1071 /* Sorry, an unknown header name. Do linear search */
1073 while ((e
= getEntry(&pos
))) {
1074 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1076 strListAdd(&result
, e
->value
.termedBuf(), ',');
1084 * Returns a the value of the specified list member, if any.
1087 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1090 const char *pos
= NULL
;
1093 int mlen
= strlen(member
);
1097 header
= getByName(name
);
1101 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1102 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1103 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1112 * returns a the value of the specified list member, if any.
1115 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1118 const char *pos
= NULL
;
1121 int mlen
= strlen(member
);
1125 header
= getStrOrList(id
);
1128 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1129 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1130 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1139 /* test if a field is present */
1141 HttpHeader::has(http_hdr_type id
) const
1144 assert(id
!= HDR_OTHER
);
1145 debugs(55, 9, this << " lookup for " << id
);
1146 return CBIT_TEST(mask
, id
);
1150 HttpHeader::putInt(http_hdr_type id
, int number
)
1153 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1154 assert(number
>= 0);
1155 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1159 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1162 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1163 assert(number
>= 0);
1164 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1168 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1171 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1173 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1177 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1180 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1182 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1186 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1189 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1191 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1195 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1197 assert(auth_scheme
&& realm
);
1198 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1202 HttpHeader::putCc(const HttpHdrCc
* cc
)
1207 /* remove old directives if any */
1208 delById(HDR_CACHE_CONTROL
);
1211 packerToMemInit(&p
, &mb
);
1214 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1221 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1226 /* remove old directives if any */
1227 delById(HDR_CONTENT_RANGE
);
1230 packerToMemInit(&p
, &mb
);
1231 httpHdrContRangePackInto(cr
, &p
);
1233 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1240 HttpHeader::putRange(const HttpHdrRange
* range
)
1245 /* remove old directives if any */
1249 packerToMemInit(&p
, &mb
);
1250 range
->packInto(&p
);
1252 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1259 HttpHeader::putSc(HttpHdrSc
*sc
)
1264 /* remove old directives if any */
1265 delById(HDR_SURROGATE_CONTROL
);
1268 packerToMemInit(&p
, &mb
);
1271 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1278 HttpHeader::putWarning(const int code
, const char *const text
)
1281 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1282 putStr(HDR_WARNING
, buf
);
1285 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1287 HttpHeader::putExt(const char *name
, const char *value
)
1289 assert(name
&& value
);
1290 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1291 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1295 HttpHeader::getInt(http_hdr_type id
) const
1298 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1301 if ((e
= findEntry(id
)))
1308 HttpHeader::getInt64(http_hdr_type id
) const
1311 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1314 if ((e
= findEntry(id
)))
1315 return e
->getInt64();
1321 HttpHeader::getTime(http_hdr_type id
) const
1326 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1328 if ((e
= findEntry(id
))) {
1329 value
= parse_rfc1123(e
->value
.termedBuf());
1330 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1336 /* sync with httpHeaderGetLastStr */
1338 HttpHeader::getStr(http_hdr_type id
) const
1342 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1344 if ((e
= findEntry(id
))) {
1345 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1346 return e
->value
.termedBuf();
1354 HttpHeader::getLastStr(http_hdr_type id
) const
1358 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1360 if ((e
= findLastEntry(id
))) {
1361 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1362 return e
->value
.termedBuf();
1369 HttpHeader::getCc() const
1371 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1373 PROF_start(HttpHeader_getCc
);
1376 getList(HDR_CACHE_CONTROL
, &s
);
1378 HttpHdrCc
*cc
=new HttpHdrCc();
1380 if (!cc
->parse(s
)) {
1385 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1388 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1390 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1392 PROF_stop(HttpHeader_getCc
);
1398 HttpHeader::getRange() const
1400 HttpHdrRange
*r
= NULL
;
1402 /* some clients will send "Request-Range" _and_ *matching* "Range"
1403 * who knows, some clients might send Request-Range only;
1404 * this "if" should work correctly in both cases;
1405 * hopefully no clients send mismatched headers! */
1407 if ((e
= findEntry(HDR_RANGE
)) ||
1408 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1409 r
= HttpHdrRange::ParseCreate(&e
->value
);
1410 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1417 HttpHeader::getSc() const
1419 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1424 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1426 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1428 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1431 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1433 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1439 HttpHeader::getContRange() const
1441 HttpHdrContRange
*cr
= NULL
;
1444 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1445 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1446 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1453 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1457 assert(auth_scheme
);
1460 if (!field
) /* no authorization field */
1463 l
= strlen(auth_scheme
);
1465 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1470 if (!xisspace(*field
)) /* wrong scheme */
1473 /* skip white space */
1474 for (; field
&& xisspace(*field
); ++field
);
1476 if (!*field
) /* no authorization cookie */
1479 static char decodedAuthToken
[8192];
1480 const int decodedLen
= base64_decode(decodedAuthToken
, sizeof(decodedAuthToken
)-1, field
);
1481 decodedAuthToken
[decodedLen
] = '\0';
1482 return decodedAuthToken
;
1486 HttpHeader::getETag(http_hdr_type id
) const
1488 ETag etag
= {NULL
, -1};
1490 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1492 if ((e
= findEntry(id
)))
1493 etagParseInit(&etag
, e
->value
.termedBuf());
1499 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1503 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1504 memset(&tot
, 0, sizeof(tot
));
1506 if ((e
= findEntry(id
))) {
1507 const char *str
= e
->value
.termedBuf();
1508 /* try as an ETag */
1510 if (etagParseInit(&tot
.tag
, str
)) {
1511 tot
.valid
= tot
.tag
.str
!= NULL
;
1514 /* or maybe it is time? */
1515 tot
.time
= parse_rfc1123(str
);
1516 tot
.valid
= tot
.time
>= 0;
1521 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1529 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1534 if (id
!= HDR_OTHER
)
1535 name
= Headers
[id
].name
;
1541 ++ Headers
[id
].stat
.aliveCount
;
1543 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1546 HttpHeaderEntry::~HttpHeaderEntry()
1549 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1550 /* clean name if needed */
1552 if (id
== HDR_OTHER
)
1557 assert(Headers
[id
].stat
.aliveCount
);
1559 -- Headers
[id
].stat
.aliveCount
;
1564 /* parses and inits header entry, returns true/false */
1566 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1568 /* note: name_start == field_start */
1569 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1570 int name_len
= name_end
? name_end
- field_start
:0;
1571 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1572 /* note: value_end == field_end */
1574 ++ HeaderEntryParsedCount
;
1576 /* do we have a valid field name within this field? */
1578 if (!name_len
|| name_end
> field_end
)
1581 if (name_len
> 65534) {
1582 /* String must be LESS THAN 64K and it adds a terminating NULL */
1583 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1587 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1588 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1589 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
) << "'");
1591 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1598 /* now we know we can parse it */
1600 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
) << "'");
1602 /* is it a "known" field? */
1603 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1614 /* set field name */
1615 if (id
== HDR_OTHER
)
1616 name
.limitInit(field_start
, name_len
);
1618 name
= Headers
[id
].name
;
1620 /* trim field value */
1621 while (value_start
< field_end
&& xisspace(*value_start
))
1624 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1627 if (field_end
- value_start
> 65534) {
1628 /* String must be LESS THAN 64K and it adds a terminating NULL */
1629 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1631 if (id
== HDR_OTHER
)
1637 /* set field value */
1638 value
.limitInit(value_start
, field_end
- value_start
);
1640 ++ Headers
[id
].stat
.seenCount
;
1642 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1644 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1648 HttpHeaderEntry::clone() const
1650 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1654 HttpHeaderEntry::packInto(Packer
* p
) const
1657 packerAppend(p
, name
.rawBuf(), name
.size());
1658 packerAppend(p
, ": ", 2);
1659 packerAppend(p
, value
.rawBuf(), value
.size());
1660 packerAppend(p
, "\r\n", 2);
1664 HttpHeaderEntry::getInt() const
1667 assert (Headers
[id
].type
== ftInt
);
1669 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1670 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1671 /* XXX: Should we check ok - ie
1672 * return ok ? -1 : value;
1678 HttpHeaderEntry::getInt64() const
1681 assert (Headers
[id
].type
== ftInt64
);
1683 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1684 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1685 /* XXX: Should we check ok - ie
1686 * return ok ? -1 : value;
1692 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1694 ++ Headers
[id
].stat
.parsCount
;
1697 ++ Headers
[id
].stat
.errCount
;
1698 debugs(55, 2, "cannot parse hdr field: '" << Headers
[id
].name
<< ": " << context
<< "'");
1706 /* tmp variable used to pass stat info to dumpers */
1707 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1708 const HttpHeaderStat
*dump_stat
= NULL
;
1711 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1713 const int id
= (int) val
;
1714 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1715 const char *name
= valid_id
? Headers
[id
].name
.termedBuf() : "INVALID";
1716 int visible
= count
> 0;
1717 /* for entries with zero count, list only those that belong to current type of message */
1719 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1720 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1723 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1724 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1728 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
1731 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1732 idx
, (int) val
, count
,
1733 xpercent(count
, dump_stat
->destroyedCount
));
1737 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1742 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1743 storeAppendPrintf(e
, "\nField type distribution\n");
1744 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1745 "id", "name", "count", "#/header");
1746 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1747 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1748 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1749 "id", "name", "count", "#/cc_field");
1750 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1751 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1752 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1753 "id", "name", "count", "#/sc_field");
1754 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1755 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1756 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1757 "id", "#flds", "count", "%total");
1758 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1759 storeAppendPrintf(e
, "\n");
1764 httpHeaderStoreReport(StoreEntry
* e
)
1770 HttpHeaderStats
[0].parsedCount
=
1771 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1772 HttpHeaderStats
[0].ccParsedCount
=
1773 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1774 HttpHeaderStats
[0].destroyedCount
=
1775 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1776 HttpHeaderStats
[0].busyDestroyedCount
=
1777 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1779 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1780 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1783 /* field stats for all messages */
1784 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1786 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1787 "id", "name", "#alive", "%err", "%repeat");
1789 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1790 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1791 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1792 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1793 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1794 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1797 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1798 HttpHeaderStats
[hoRequest
].parsedCount
,
1799 HttpHeaderStats
[hoReply
].parsedCount
,
1800 HttpHeaderStats
[0].parsedCount
);
1801 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1805 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1808 for (int i
= 0; i
< end
; ++i
) {
1809 if (name_len
!= info
[i
].name
.size())
1812 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1821 httpHeaderIdByNameDef(const char *name
, int name_len
)
1824 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1826 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1830 httpHeaderNameById(int id
)
1833 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1835 assert(id
>= 0 && id
< HDR_ENUM_END
);
1837 return Headers
[id
].name
.termedBuf();
1841 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1844 const char *pos
= NULL
;
1847 int mlen
= strlen(member
);
1851 String
header (getStrOrList(id
));
1853 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1854 if (strncasecmp(item
, member
, mlen
) == 0
1855 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1865 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1868 const char *pos
= NULL
;
1871 int mlen
= strlen(member
);
1875 String
header (getByName(name
));
1877 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1878 if (strncasecmp(item
, member
, mlen
) == 0
1879 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1889 HttpHeader::removeHopByHopEntries()
1891 removeConnectionHeaderEntries();
1893 const HttpHeaderEntry
*e
;
1894 HttpHeaderPos pos
= HttpHeaderInitPos
;
1895 int headers_deleted
= 0;
1896 while ((e
= getEntry(&pos
))) {
1898 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
1899 delAt(pos
, headers_deleted
);
1906 HttpHeader::removeConnectionHeaderEntries()
1908 if (has(HDR_CONNECTION
)) {
1909 /* anything that matches Connection list member will be deleted */
1910 String strConnection
;
1912 (void) getList(HDR_CONNECTION
, &strConnection
);
1913 const HttpHeaderEntry
*e
;
1914 HttpHeaderPos pos
= HttpHeaderInitPos
;
1916 * think: on-average-best nesting of the two loops (hdrEntry
1917 * and strListItem) @?@
1920 * maybe we should delete standard stuff ("keep-alive","close")
1921 * from strConnection first?
1924 int headers_deleted
= 0;
1925 while ((e
= getEntry(&pos
))) {
1926 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
1927 delAt(pos
, headers_deleted
);
1929 if (headers_deleted
)