2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 55 HTTP Header */
12 #include "base/LookupTable.h"
15 #include "HttpHdrCc.h"
16 #include "HttpHdrContRange.h"
17 #include "HttpHdrSc.h"
18 #include "HttpHeader.h"
19 #include "HttpHeaderFieldInfo.h"
20 #include "HttpHeaderStat.h"
21 #include "HttpHeaderTools.h"
23 #include "mgr/Registration.h"
24 #include "profiler/Profiler.h"
26 #include "SquidConfig.h"
27 #include "SquidString.h"
31 #include "TimeOrTag.h"
36 /* XXX: the whole set of API managing the entries vector should be rethought
37 * after the parse4r-ng effort is complete.
41 * On naming conventions:
43 * HTTP/1.1 defines message-header as
45 * message-header = field-name ":" [ field-value ] CRLF
47 * field-value = *( field-content | LWS )
49 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
50 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
52 * HttpHeader is an object that represents all message-headers in a message.
53 * HttpHeader does not manage start-line.
55 * HttpHeader is implemented as a collection of header "entries".
56 * An entry is a (field_id, field_name, field_value) triplet.
60 * local constants and vars
64 * A table with major attributes for every known field.
65 * We calculate name lengths and reorganize this array on start up.
66 * After reorganization, field id can be used as an index to the table.
68 static const HttpHeaderFieldAttrs HeadersAttrs
[] = {
69 HttpHeaderFieldAttrs("Accept", HDR_ACCEPT
, ftStr
),
70 HttpHeaderFieldAttrs("Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
),
71 HttpHeaderFieldAttrs("Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
),
72 HttpHeaderFieldAttrs("Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
),
73 HttpHeaderFieldAttrs("Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
),
74 HttpHeaderFieldAttrs("Age", HDR_AGE
, ftInt
),
75 HttpHeaderFieldAttrs("Allow", HDR_ALLOW
, ftStr
),
76 HttpHeaderFieldAttrs("Alternate-Protocol", HDR_ALTERNATE_PROTOCOL
, ftStr
),
77 HttpHeaderFieldAttrs("Authorization", HDR_AUTHORIZATION
, ftStr
), /* for now */
78 HttpHeaderFieldAttrs("Cache-Control", HDR_CACHE_CONTROL
, ftPCc
),
79 HttpHeaderFieldAttrs("Connection", HDR_CONNECTION
, ftStr
),
80 HttpHeaderFieldAttrs("Content-Base", HDR_CONTENT_BASE
, ftStr
),
81 HttpHeaderFieldAttrs("Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
), /* for now */
82 HttpHeaderFieldAttrs("Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
),
83 HttpHeaderFieldAttrs("Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
),
84 HttpHeaderFieldAttrs("Content-Length", HDR_CONTENT_LENGTH
, ftInt64
),
85 HttpHeaderFieldAttrs("Content-Location", HDR_CONTENT_LOCATION
, ftStr
),
86 HttpHeaderFieldAttrs("Content-MD5", HDR_CONTENT_MD5
, ftStr
), /* for now */
87 HttpHeaderFieldAttrs("Content-Range", HDR_CONTENT_RANGE
, ftPContRange
),
88 HttpHeaderFieldAttrs("Content-Type", HDR_CONTENT_TYPE
, ftStr
),
89 HttpHeaderFieldAttrs("Cookie", HDR_COOKIE
, ftStr
),
90 HttpHeaderFieldAttrs("Cookie2", HDR_COOKIE2
, ftStr
),
91 HttpHeaderFieldAttrs("Date", HDR_DATE
, ftDate_1123
),
92 HttpHeaderFieldAttrs("ETag", HDR_ETAG
, ftETag
),
93 HttpHeaderFieldAttrs("Expect", HDR_EXPECT
, ftStr
),
94 HttpHeaderFieldAttrs("Expires", HDR_EXPIRES
, ftDate_1123
),
95 HttpHeaderFieldAttrs("Forwarded", HDR_FORWARDED
, ftStr
),
96 HttpHeaderFieldAttrs("From", HDR_FROM
, ftStr
),
97 HttpHeaderFieldAttrs("Host", HDR_HOST
, ftStr
),
98 HttpHeaderFieldAttrs("HTTP2-Settings", HDR_HTTP2_SETTINGS
, ftStr
), /* for now */
99 HttpHeaderFieldAttrs("If-Match", HDR_IF_MATCH
, ftStr
), /* for now */
100 HttpHeaderFieldAttrs("If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
),
101 HttpHeaderFieldAttrs("If-None-Match", HDR_IF_NONE_MATCH
, ftStr
), /* for now */
102 HttpHeaderFieldAttrs("If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
),
103 HttpHeaderFieldAttrs("If-Unmodified-Since", HDR_IF_UNMODIFIED_SINCE
, ftDate_1123
),
104 HttpHeaderFieldAttrs("Keep-Alive", HDR_KEEP_ALIVE
, ftStr
),
105 HttpHeaderFieldAttrs("Key", HDR_KEY
, ftStr
),
106 HttpHeaderFieldAttrs("Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
),
107 HttpHeaderFieldAttrs("Link", HDR_LINK
, ftStr
),
108 HttpHeaderFieldAttrs("Location", HDR_LOCATION
, ftStr
),
109 HttpHeaderFieldAttrs("Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
),
110 HttpHeaderFieldAttrs("Mime-Version", HDR_MIME_VERSION
, ftStr
), /* for now */
111 HttpHeaderFieldAttrs("Negotiate", HDR_NEGOTIATE
, ftStr
),
112 HttpHeaderFieldAttrs("Origin", HDR_ORIGIN
, ftStr
),
113 HttpHeaderFieldAttrs("Pragma", HDR_PRAGMA
, ftStr
),
114 HttpHeaderFieldAttrs("Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
),
115 HttpHeaderFieldAttrs("Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
),
116 HttpHeaderFieldAttrs("Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
),
117 HttpHeaderFieldAttrs("Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
),
118 HttpHeaderFieldAttrs("Proxy-support", HDR_PROXY_SUPPORT
, ftStr
),
119 HttpHeaderFieldAttrs("Public", HDR_PUBLIC
, ftStr
),
120 HttpHeaderFieldAttrs("Range", HDR_RANGE
, ftPRange
),
121 HttpHeaderFieldAttrs("Referer", HDR_REFERER
, ftStr
),
122 HttpHeaderFieldAttrs("Request-Range", HDR_REQUEST_RANGE
, ftPRange
), /* usually matches HDR_RANGE */
123 HttpHeaderFieldAttrs("Retry-After", HDR_RETRY_AFTER
, ftStr
), /* for now (ftDate_1123 or ftInt!) */
124 HttpHeaderFieldAttrs("Server", HDR_SERVER
, ftStr
),
125 HttpHeaderFieldAttrs("Set-Cookie", HDR_SET_COOKIE
, ftStr
),
126 HttpHeaderFieldAttrs("Set-Cookie2", HDR_SET_COOKIE2
, ftStr
),
127 HttpHeaderFieldAttrs("TE", HDR_TE
, ftStr
),
128 HttpHeaderFieldAttrs("Title", HDR_TITLE
, ftStr
),
129 HttpHeaderFieldAttrs("Trailer", HDR_TRAILER
, ftStr
),
130 HttpHeaderFieldAttrs("Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
),
131 HttpHeaderFieldAttrs("Translate", HDR_TRANSLATE
, ftStr
), /* for now. may need to crop */
132 HttpHeaderFieldAttrs("Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
), /* for now ignore. may need to crop */
133 HttpHeaderFieldAttrs("Upgrade", HDR_UPGRADE
, ftStr
), /* for now */
134 HttpHeaderFieldAttrs("User-Agent", HDR_USER_AGENT
, ftStr
),
135 HttpHeaderFieldAttrs("Vary", HDR_VARY
, ftStr
), /* for now */
136 HttpHeaderFieldAttrs("Via", HDR_VIA
, ftStr
), /* for now */
137 HttpHeaderFieldAttrs("Warning", HDR_WARNING
, ftStr
), /* for now */
138 HttpHeaderFieldAttrs("WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
),
139 HttpHeaderFieldAttrs("Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
),
140 HttpHeaderFieldAttrs("X-Cache", HDR_X_CACHE
, ftStr
),
141 HttpHeaderFieldAttrs("X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
),
142 HttpHeaderFieldAttrs("X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
),
143 HttpHeaderFieldAttrs("X-Request-URI", HDR_X_REQUEST_URI
, ftStr
),
144 HttpHeaderFieldAttrs("X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
),
145 #if X_ACCELERATOR_VARY
146 HttpHeaderFieldAttrs("X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
),
149 HttpHeaderFieldAttrs("X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
),
151 HttpHeaderFieldAttrs("Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
),
152 HttpHeaderFieldAttrs("Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
),
153 HttpHeaderFieldAttrs("Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
),
154 HttpHeaderFieldAttrs("FTP-Command", HDR_FTP_COMMAND
, ftStr
),
155 HttpHeaderFieldAttrs("FTP-Arguments", HDR_FTP_ARGUMENTS
, ftStr
),
156 HttpHeaderFieldAttrs("FTP-Pre", HDR_FTP_PRE
, ftStr
),
157 HttpHeaderFieldAttrs("FTP-Status", HDR_FTP_STATUS
, ftInt
),
158 HttpHeaderFieldAttrs("FTP-Reason", HDR_FTP_REASON
, ftStr
),
159 HttpHeaderFieldAttrs("Other:", HDR_OTHER
, ftStr
) /* ':' will not allow matches */
162 struct HeaderTableRecord
{
167 // Note: MUST be sorted by value of http_hdr_type.
168 // invariant: for each index in headerTable, (int)headerTable[index] = index
169 static const HeaderTableRecord headerTable
[] = {
170 {"Accept", HDR_ACCEPT
, ftStr
},
171 {"Accept-Charset", HDR_ACCEPT_CHARSET
, ftStr
},
172 {"Accept-Encoding", HDR_ACCEPT_ENCODING
, ftStr
},
173 {"Accept-Language", HDR_ACCEPT_LANGUAGE
, ftStr
},
174 {"Accept-Ranges", HDR_ACCEPT_RANGES
, ftStr
},
175 {"Age", HDR_AGE
, ftInt
},
176 {"Allow", HDR_ALLOW
, ftStr
},
177 {"Alternate-Protocol", HDR_ALTERNATE_PROTOCOL
, ftStr
},
178 {"Authorization", HDR_AUTHORIZATION
, ftStr
}, /* for now */
179 {"Cache-Control", HDR_CACHE_CONTROL
, ftPCc
},
180 {"Connection", HDR_CONNECTION
, ftStr
},
181 {"Content-Base", HDR_CONTENT_BASE
, ftStr
},
182 {"Content-Disposition", HDR_CONTENT_DISPOSITION
, ftStr
}, /* for now */
183 {"Content-Encoding", HDR_CONTENT_ENCODING
, ftStr
},
184 {"Content-Language", HDR_CONTENT_LANGUAGE
, ftStr
},
185 {"Content-Length", HDR_CONTENT_LENGTH
, ftInt64
},
186 {"Content-Location", HDR_CONTENT_LOCATION
, ftStr
},
187 {"Content-MD5", HDR_CONTENT_MD5
, ftStr
}, /* for now */
188 {"Content-Range", HDR_CONTENT_RANGE
, ftPContRange
},
189 {"Content-Type", HDR_CONTENT_TYPE
, ftStr
},
190 {"Cookie", HDR_COOKIE
, ftStr
},
191 {"Cookie2", HDR_COOKIE2
, ftStr
},
192 {"Date", HDR_DATE
, ftDate_1123
},
193 {"ETag", HDR_ETAG
, ftETag
},
194 {"Expect", HDR_EXPECT
, ftStr
},
195 {"Expires", HDR_EXPIRES
, ftDate_1123
},
196 {"Forwarded", HDR_FORWARDED
, ftStr
},
197 {"From", HDR_FROM
, ftStr
},
198 {"Host", HDR_HOST
, ftStr
},
199 {"HTTP2-Settings", HDR_HTTP2_SETTINGS
, ftStr
}, /* for now */
200 {"If-Match", HDR_IF_MATCH
, ftStr
}, /* for now */
201 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE
, ftDate_1123
},
202 {"If-None-Match", HDR_IF_NONE_MATCH
, ftStr
}, /* for now */
203 {"If-Range", HDR_IF_RANGE
, ftDate_1123_or_ETag
},
204 {"If-Unmodified-Since", HDR_IF_UNMODIFIED_SINCE
, ftDate_1123
},
205 {"Keep-Alive", HDR_KEEP_ALIVE
, ftStr
},
206 {"Key", HDR_KEY
, ftStr
},
207 {"Last-Modified", HDR_LAST_MODIFIED
, ftDate_1123
},
208 {"Link", HDR_LINK
, ftStr
},
209 {"Location", HDR_LOCATION
, ftStr
},
210 {"Max-Forwards", HDR_MAX_FORWARDS
, ftInt64
},
211 {"Mime-Version", HDR_MIME_VERSION
, ftStr
}, /* for now */
212 {"Negotiate", HDR_NEGOTIATE
, ftStr
},
213 {"Origin", HDR_ORIGIN
, ftStr
},
214 {"Pragma", HDR_PRAGMA
, ftStr
},
215 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE
, ftStr
},
216 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO
, ftStr
},
217 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION
, ftStr
},
218 {"Proxy-Connection", HDR_PROXY_CONNECTION
, ftStr
},
219 {"Proxy-support", HDR_PROXY_SUPPORT
, ftStr
},
220 {"Public", HDR_PUBLIC
, ftStr
},
221 {"Range", HDR_RANGE
, ftPRange
},
222 {"Referer", HDR_REFERER
, ftStr
},
223 {"Request-Range", HDR_REQUEST_RANGE
, ftPRange
}, /* usually matches HDR_RANGE */
224 {"Retry-After", HDR_RETRY_AFTER
, ftStr
}, /* for now (ftDate_1123 or ftInt!} */
225 {"Server", HDR_SERVER
, ftStr
},
226 {"Set-Cookie", HDR_SET_COOKIE
, ftStr
},
227 {"Set-Cookie2", HDR_SET_COOKIE2
, ftStr
},
228 {"TE", HDR_TE
, ftStr
},
229 {"Title", HDR_TITLE
, ftStr
},
230 {"Trailer", HDR_TRAILER
, ftStr
},
231 {"Transfer-Encoding", HDR_TRANSFER_ENCODING
, ftStr
},
232 {"Translate", HDR_TRANSLATE
, ftStr
}, /* for now. may need to crop */
233 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE
, ftStr
}, /* for now ignore. may need to crop */
234 {"Upgrade", HDR_UPGRADE
, ftStr
}, /* for now */
235 {"User-Agent", HDR_USER_AGENT
, ftStr
},
236 {"Vary", HDR_VARY
, ftStr
}, /* for now */
237 {"Via", HDR_VIA
, ftStr
}, /* for now */
238 {"Warning", HDR_WARNING
, ftStr
}, /* for now */
239 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE
, ftStr
},
240 {"Authentication-Info", HDR_AUTHENTICATION_INFO
, ftStr
},
241 {"X-Cache", HDR_X_CACHE
, ftStr
},
242 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP
, ftStr
},
243 {"X-Forwarded-For", HDR_X_FORWARDED_FOR
, ftStr
},
244 {"X-Request-URI", HDR_X_REQUEST_URI
, ftStr
},
245 {"X-Squid-Error", HDR_X_SQUID_ERROR
, ftStr
},
246 #if X_ACCELERATOR_VARY
247 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY
, ftStr
},
250 {"X-Next-Services", HDR_X_NEXT_SERVICES
, ftStr
},
252 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY
, ftStr
},
253 {"Surrogate-Control", HDR_SURROGATE_CONTROL
, ftPSc
},
254 {"Front-End-Https", HDR_FRONT_END_HTTPS
, ftStr
},
255 {"FTP-Command", HDR_FTP_COMMAND
, ftStr
},
256 {"FTP-Arguments", HDR_FTP_ARGUMENTS
, ftStr
},
257 {"FTP-Pre", HDR_FTP_PRE
, ftStr
},
258 {"FTP-Status", HDR_FTP_STATUS
, ftInt
},
259 {"FTP-Reason", HDR_FTP_REASON
, ftStr
},
260 {nullptr, HDR_OTHER
} /* ':' will not allow matches */
263 static HttpHeaderFieldInfo
*Headers
= NULL
;
264 LookupTable
<http_hdr_type
, HeaderTableRecord
> headerLookupTable(HDR_OTHER
, headerTable
);
265 std::vector
<HttpHeaderFieldStat
> headerStatsTable(HDR_OTHER
);
267 http_hdr_type
&operator++ (http_hdr_type
&aHeader
)
269 int tmp
= (int)aHeader
;
270 aHeader
= (http_hdr_type
)(++tmp
);
275 * headers with field values defined as #(values) in HTTP/1.1
276 * Headers that are currently not recognized, are commented out.
278 static HttpHeaderMask ListHeadersMask
; /* set run-time using ListHeadersArr */
279 static http_hdr_type ListHeadersArr
[] = {
287 HDR_CONTENT_ENCODING
,
288 HDR_CONTENT_LANGUAGE
,
296 HDR_PROXY_CONNECTION
,
298 HDR_TRANSFER_ENCODING
,
303 HDR_WWW_AUTHENTICATE
,
304 HDR_AUTHENTICATION_INFO
,
305 HDR_PROXY_AUTHENTICATION_INFO
,
306 /* HDR_TE, HDR_TRAILER */
307 #if X_ACCELERATOR_VARY
308 HDR_X_ACCELERATOR_VARY
,
313 HDR_SURROGATE_CAPABILITY
,
314 HDR_SURROGATE_CONTROL
,
319 /* general-headers */
320 static http_hdr_type GeneralHeadersArr
[] = {
328 HDR_PROXY_CONNECTION
,
329 HDR_TRANSFER_ENCODING
,
336 static http_hdr_type EntityHeadersArr
[] = {
339 HDR_CONTENT_ENCODING
,
340 HDR_CONTENT_LANGUAGE
,
342 HDR_CONTENT_LOCATION
,
353 /* request-only headers */
354 static HttpHeaderMask RequestHeadersMask
; /* set run-time using RequestHeaders */
355 static http_hdr_type RequestHeadersArr
[] = {
366 HDR_IF_MODIFIED_SINCE
,
369 HDR_IF_UNMODIFIED_SINCE
,
372 HDR_PROXY_AUTHORIZATION
,
378 HDR_SURROGATE_CAPABILITY
381 /* reply-only headers */
382 static HttpHeaderMask ReplyHeadersMask
; /* set run-time using ReplyHeaders */
383 static http_hdr_type ReplyHeadersArr
[] = {
389 HDR_PROXY_AUTHENTICATE
,
397 HDR_WWW_AUTHENTICATE
,
401 #if X_ACCELERATOR_VARY
402 HDR_X_ACCELERATOR_VARY
,
408 HDR_SURROGATE_CONTROL
411 /* hop-by-hop headers */
412 static HttpHeaderMask HopByHopHeadersMask
;
413 static http_hdr_type HopByHopHeadersArr
[] = {
414 HDR_ALTERNATE_PROTOCOL
,
418 /*HDR_PROXY_AUTHENTICATE, // removal handled specially for peer login */
419 HDR_PROXY_AUTHORIZATION
,
422 HDR_TRANSFER_ENCODING
,
427 /* header accounting */
428 // NP: keep in sync with enum http_hdr_owner_type
429 static HttpHeaderStat HttpHeaderStats
[] = {
430 HttpHeaderStat(/*hoNone*/ "all", NULL
),
432 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask
),
434 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask
),
435 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask
)
441 static int HttpHeaderStatCount
= countof(HttpHeaderStats
);
443 static int HeaderEntryParsedCount
= 0;
446 * forward declarations and local routines
450 #define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
452 static void httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &value
, int error
);
454 static void httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
);
456 /** store report about current header usage and other stats */
457 static void httpHeaderStoreReport(StoreEntry
* e
);
460 * Module initialization routines
464 httpHeaderRegisterWithCacheManager(void)
466 Mgr::RegisterAction("http_headers",
467 "HTTP Header Statistics",
468 httpHeaderStoreReport
, 0, 1);
472 httpHeaderInitModule(void)
474 /* check that we have enough space for masks */
475 assert(8 * sizeof(HttpHeaderMask
) >= HDR_ENUM_END
);
476 /* all headers must be described */
477 assert(countof(HeadersAttrs
) == HDR_ENUM_END
);
480 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
481 // use headerLookupTable in place of Headers
484 httpHeaderMaskInit(&ListHeadersMask
, 0);
485 httpHeaderCalcMask(&ListHeadersMask
, ListHeadersArr
, countof(ListHeadersArr
));
487 httpHeaderMaskInit(&ReplyHeadersMask
, 0);
488 httpHeaderCalcMask(&ReplyHeadersMask
, ReplyHeadersArr
, countof(ReplyHeadersArr
));
489 httpHeaderCalcMask(&ReplyHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
490 httpHeaderCalcMask(&ReplyHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
492 httpHeaderMaskInit(&RequestHeadersMask
, 0);
493 httpHeaderCalcMask(&RequestHeadersMask
, RequestHeadersArr
, countof(RequestHeadersArr
));
494 httpHeaderCalcMask(&RequestHeadersMask
, GeneralHeadersArr
, countof(GeneralHeadersArr
));
495 httpHeaderCalcMask(&RequestHeadersMask
, EntityHeadersArr
, countof(EntityHeadersArr
));
497 httpHeaderMaskInit(&HopByHopHeadersMask
, 0);
498 httpHeaderCalcMask(&HopByHopHeadersMask
, HopByHopHeadersArr
, countof(HopByHopHeadersArr
));
500 /* header stats initialized by class constructor */
501 assert(HttpHeaderStatCount
== hoReply
+ 1);
503 /* init dependent modules */
504 httpHdrCcInitModule();
505 httpHdrScInitModule();
507 httpHeaderRegisterWithCacheManager();
511 httpHeaderCleanModule(void)
513 httpHeaderDestroyFieldsInfo(Headers
, HDR_ENUM_END
);
515 httpHdrCcCleanModule();
516 httpHdrScCleanModule();
520 * HttpHeader Implementation
523 HttpHeader::HttpHeader() : owner (hoNone
), len (0)
525 httpHeaderMaskInit(&mask
, 0);
528 HttpHeader::HttpHeader(const http_hdr_owner_type anOwner
): owner(anOwner
), len(0)
530 assert(anOwner
> hoNone
&& anOwner
< hoEnd
);
531 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner
);
532 httpHeaderMaskInit(&mask
, 0);
535 HttpHeader::HttpHeader(const HttpHeader
&other
): owner(other
.owner
), len(other
.len
)
537 httpHeaderMaskInit(&mask
, 0);
538 update(&other
, NULL
); // will update the mask as well
541 HttpHeader::~HttpHeader()
547 HttpHeader::operator =(const HttpHeader
&other
)
549 if (this != &other
) {
550 // we do not really care, but the caller probably does
551 assert(owner
== other
.owner
);
553 update(&other
, NULL
); // will update the mask as well
563 assert(owner
> hoNone
&& owner
< hoEnd
);
564 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner
);
566 PROF_start(HttpHeaderClean
);
568 if (owner
<= hoReply
) {
570 * An unfortunate bug. The entries array is initialized
571 * such that count is set to zero. httpHeaderClean() seems to
572 * be called both when 'hdr' is created, and destroyed. Thus,
573 * we accumulate a large number of zero counts for 'hdr' before
574 * it is ever used. Can't think of a good way to fix it, except
575 * adding a state variable that indicates whether or not 'hdr'
576 * has been used. As a hack, just never count zero-sized header
579 if (!entries
.empty())
580 HttpHeaderStats
[owner
].hdrUCountDistr
.count(entries
.size());
582 ++ HttpHeaderStats
[owner
].destroyedCount
;
584 HttpHeaderStats
[owner
].busyDestroyedCount
+= entries
.size() > 0;
585 } // if (owner <= hoReply)
587 for (std::vector
<HttpHeaderEntry
*>::iterator i
= entries
.begin(); i
!= entries
.end(); ++i
) {
588 HttpHeaderEntry
*e
= *i
;
591 if (e
->id
< 0 || e
->id
>= HDR_ENUM_END
) {
592 debugs(55, DBG_CRITICAL
, "BUG: invalid entry (" << e
->id
<< "). Ignored.");
594 if (owner
<= hoReply
)
595 HttpHeaderStats
[owner
].fieldTypeDistr
.count(e
->id
);
601 httpHeaderMaskInit(&mask
, 0);
603 PROF_stop(HttpHeaderClean
);
606 /* append entries (also see httpHeaderUpdate) */
608 HttpHeader::append(const HttpHeader
* src
)
610 const HttpHeaderEntry
*e
;
611 HttpHeaderPos pos
= HttpHeaderInitPos
;
614 debugs(55, 7, "appending hdr: " << this << " += " << src
);
616 while ((e
= src
->getEntry(&pos
))) {
617 addEntry(e
->clone());
621 /* use fresh entries to replace old ones */
623 httpHeaderUpdate(HttpHeader
* old
, const HttpHeader
* fresh
, const HttpHeaderMask
* denied_mask
)
626 old
->update (fresh
, denied_mask
);
630 HttpHeader::update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
)
632 const HttpHeaderEntry
*e
;
633 HttpHeaderPos pos
= HttpHeaderInitPos
;
635 assert(this != fresh
);
637 while ((e
= fresh
->getEntry(&pos
))) {
638 /* deny bad guys (ok to check for HDR_OTHER) here */
640 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
643 if (e
->id
!= HDR_OTHER
)
646 delByName(e
->name
.termedBuf());
649 pos
= HttpHeaderInitPos
;
650 while ((e
= fresh
->getEntry(&pos
))) {
651 /* deny bad guys (ok to check for HDR_OTHER) here */
653 if (denied_mask
&& CBIT_TEST(*denied_mask
, e
->id
))
656 debugs(55, 7, "Updating header '" << HeadersAttrs
[e
->id
].name
<< "' in cached entry");
658 addEntry(e
->clone());
662 /* just handy in parsing: resets and returns false */
671 HttpHeader::parse(const char *header_start
, size_t hdrLen
)
673 const char *field_ptr
= header_start
;
674 const char *header_end
= header_start
+ hdrLen
; // XXX: remove
675 HttpHeaderEntry
*e
, *e2
;
676 int warnOnError
= (Config
.onoff
.relaxed_header_parser
<= 0 ? DBG_IMPORTANT
: 2);
678 PROF_start(HttpHeaderParse
);
680 assert(header_start
&& header_end
);
681 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl
<< getStringPrefix(header_start
, hdrLen
));
682 ++ HttpHeaderStats
[owner
].parsedCount
;
685 if ((nulpos
= (char*)memchr(header_start
, '\0', hdrLen
))) {
686 debugs(55, DBG_IMPORTANT
, "WARNING: HTTP header contains NULL characters {" <<
687 getStringPrefix(header_start
, nulpos
-header_start
) << "}\nNULL\n{" << getStringPrefix(nulpos
+1, hdrLen
-(nulpos
-header_start
)-1));
688 PROF_stop(HttpHeaderParse
);
692 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
693 * continuation lines start with a (single) space or tab */
694 while (field_ptr
< header_end
) {
695 const char *field_start
= field_ptr
;
696 const char *field_end
;
699 const char *this_line
= field_ptr
;
700 field_ptr
= (const char *)memchr(field_ptr
, '\n', header_end
- field_ptr
);
704 PROF_stop(HttpHeaderParse
);
708 field_end
= field_ptr
;
710 ++field_ptr
; /* Move to next line */
712 if (field_end
> this_line
&& field_end
[-1] == '\r') {
713 --field_end
; /* Ignore CR LF */
715 if (owner
== hoRequest
&& field_end
> this_line
) {
717 for (const char *p
= this_line
; p
< field_end
&& cr_only
; ++p
) {
722 debugs(55, DBG_IMPORTANT
, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
723 "header field to prevent request smuggling attacks: {" <<
724 getStringPrefix(header_start
, hdrLen
) << "}");
725 PROF_stop(HttpHeaderParse
);
731 /* Barf on stray CR characters */
732 if (memchr(this_line
, '\r', field_end
- this_line
)) {
733 debugs(55, warnOnError
, "WARNING: suspicious CR characters in HTTP header {" <<
734 getStringPrefix(field_start
, field_end
-field_start
) << "}");
736 if (Config
.onoff
.relaxed_header_parser
) {
737 char *p
= (char *) this_line
; /* XXX Warning! This destroys original header content and violates specifications somewhat */
739 while ((p
= (char *)memchr(p
, '\r', field_end
- p
)) != NULL
) {
744 PROF_stop(HttpHeaderParse
);
749 if (this_line
+ 1 == field_end
&& this_line
> field_start
) {
750 debugs(55, warnOnError
, "WARNING: Blank continuation line in HTTP header {" <<
751 getStringPrefix(header_start
, hdrLen
) << "}");
752 PROF_stop(HttpHeaderParse
);
755 } while (field_ptr
< header_end
&& (*field_ptr
== ' ' || *field_ptr
== '\t'));
757 if (field_start
== field_end
) {
758 if (field_ptr
< header_end
) {
759 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field near {" <<
760 getStringPrefix(field_start
, hdrLen
-(field_start
-header_start
)) << "}");
761 PROF_stop(HttpHeaderParse
);
765 break; /* terminating blank line */
768 if ((e
= HttpHeaderEntry::parse(field_start
, field_end
)) == NULL
) {
769 debugs(55, warnOnError
, "WARNING: unparseable HTTP header field {" <<
770 getStringPrefix(field_start
, field_end
-field_start
) << "}");
771 debugs(55, warnOnError
, " in {" << getStringPrefix(header_start
, hdrLen
) << "}");
773 if (Config
.onoff
.relaxed_header_parser
)
776 PROF_stop(HttpHeaderParse
);
780 if (e
->id
== HDR_CONTENT_LENGTH
&& (e2
= findEntry(e
->id
)) != NULL
) {
781 if (e
->value
!= e2
->value
) {
783 debugs(55, warnOnError
, "WARNING: found two conflicting content-length headers in {" <<
784 getStringPrefix(header_start
, hdrLen
) << "}");
786 if (!Config
.onoff
.relaxed_header_parser
) {
788 PROF_stop(HttpHeaderParse
);
792 if (!httpHeaderParseOffset(e
->value
.termedBuf(), &l1
)) {
793 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e
->value
<< "'");
796 } else if (!httpHeaderParseOffset(e2
->value
.termedBuf(), &l2
)) {
797 debugs(55, DBG_IMPORTANT
, "WARNING: Unparseable content-length '" << e2
->value
<< "'");
799 } else if (l1
> l2
) {
806 debugs(55, warnOnError
, "NOTICE: found double content-length header");
809 if (Config
.onoff
.relaxed_header_parser
)
812 PROF_stop(HttpHeaderParse
);
817 if (e
->id
== HDR_OTHER
&& stringHasWhitespace(e
->name
.termedBuf())) {
818 debugs(55, warnOnError
, "WARNING: found whitespace in HTTP header name {" <<
819 getStringPrefix(field_start
, field_end
-field_start
) << "}");
821 if (!Config
.onoff
.relaxed_header_parser
) {
823 PROF_stop(HttpHeaderParse
);
832 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
833 delById(HDR_CONTENT_LENGTH
);
836 PROF_stop(HttpHeaderParse
);
837 return 1; /* even if no fields where found, it is a valid header */
840 /* packs all the entries using supplied packer */
842 HttpHeader::packInto(Packable
* p
, bool mask_sensitive_info
) const
844 HttpHeaderPos pos
= HttpHeaderInitPos
;
845 const HttpHeaderEntry
*e
;
847 debugs(55, 7, this << " into " << p
<<
848 (mask_sensitive_info
? " while masking" : ""));
849 /* pack all entries one by one */
850 while ((e
= getEntry(&pos
))) {
851 if (!mask_sensitive_info
) {
856 bool maskThisEntry
= false;
858 case HDR_AUTHORIZATION
:
859 case HDR_PROXY_AUTHORIZATION
:
860 maskThisEntry
= true;
863 case HDR_FTP_ARGUMENTS
:
864 if (const HttpHeaderEntry
*cmd
= findEntry(HDR_FTP_COMMAND
))
865 maskThisEntry
= (cmd
->value
== "PASS");
872 p
->append(e
->name
.rawBuf(), e
->name
.size());
873 p
->append(": ** NOT DISPLAYED **\r\n", 23);
879 /* Pack in the "special" entries */
884 /* returns next valid entry */
886 HttpHeader::getEntry(HttpHeaderPos
* pos
) const
889 assert(*pos
>= HttpHeaderInitPos
&& *pos
< static_cast<ssize_t
>(entries
.size()));
891 for (++(*pos
); *pos
< static_cast<ssize_t
>(entries
.size()); ++(*pos
)) {
893 return static_cast<HttpHeaderEntry
*>(entries
[*pos
]);
900 * returns a pointer to a specified entry if any
901 * note that we return one entry so it does not make much sense to ask for
905 HttpHeader::findEntry(http_hdr_type id
) const
907 HttpHeaderPos pos
= HttpHeaderInitPos
;
910 assert(!CBIT_TEST(ListHeadersMask
, id
));
912 /* check mask first */
914 if (!CBIT_TEST(mask
, id
))
917 /* looks like we must have it, do linear search */
918 while ((e
= getEntry(&pos
))) {
923 /* hm.. we thought it was there, but it was not found */
926 return NULL
; /* not reached */
930 * same as httpHeaderFindEntry
933 HttpHeader::findLastEntry(http_hdr_type id
) const
935 HttpHeaderPos pos
= HttpHeaderInitPos
;
937 HttpHeaderEntry
*result
= NULL
;
939 assert(!CBIT_TEST(ListHeadersMask
, id
));
941 /* check mask first */
943 if (!CBIT_TEST(mask
, id
))
946 /* looks like we must have it, do linear search */
947 while ((e
= getEntry(&pos
))) {
952 assert(result
); /* must be there! */
957 * deletes all fields with a given name if any, returns #fields deleted;
960 HttpHeader::delByName(const char *name
)
963 HttpHeaderPos pos
= HttpHeaderInitPos
;
965 httpHeaderMaskInit(&mask
, 0); /* temporal inconsistency */
966 debugs(55, 9, "deleting '" << name
<< "' fields in hdr " << this);
968 while ((e
= getEntry(&pos
))) {
969 if (!e
->name
.caseCmp(name
))
972 CBIT_SET(mask
, e
->id
);
978 /* deletes all entries with a given id, returns the #entries deleted */
980 HttpHeader::delById(http_hdr_type id
)
983 HttpHeaderPos pos
= HttpHeaderInitPos
;
985 debugs(55, 8, this << " del-by-id " << id
);
987 assert(id
!= HDR_OTHER
); /* does not make sense */
989 if (!CBIT_TEST(mask
, id
))
992 while ((e
= getEntry(&pos
))) {
1003 * deletes an entry at pos and leaves a gap; leaving a gap makes it
1004 * possible to iterate(search) and delete fields at the same time
1005 * NOTE: Does not update the header mask. Caller must follow up with
1006 * a call to refreshMask() if headers_deleted was incremented.
1009 HttpHeader::delAt(HttpHeaderPos pos
, int &headers_deleted
)
1012 assert(pos
>= HttpHeaderInitPos
&& pos
< static_cast<ssize_t
>(entries
.size()));
1013 e
= static_cast<HttpHeaderEntry
*>(entries
[pos
]);
1014 entries
[pos
] = NULL
;
1015 /* decrement header length, allow for ": " and crlf */
1016 len
-= e
->name
.size() + 2 + e
->value
.size() + 2;
1023 * Compacts the header storage
1026 HttpHeader::compact()
1028 // TODO: optimize removal, or possibly make it so that's not needed.
1029 std::vector
<HttpHeaderEntry
*>::iterator newend
;
1030 newend
= std::remove(entries
.begin(), entries
.end(), static_cast<HttpHeaderEntry
*>(NULL
));
1031 entries
.resize(newend
-entries
.begin());
1035 * Refreshes the header mask. Required after delAt() calls.
1038 HttpHeader::refreshMask()
1040 httpHeaderMaskInit(&mask
, 0);
1041 debugs(55, 7, "refreshing the mask in hdr " << this);
1042 HttpHeaderPos pos
= HttpHeaderInitPos
;
1043 while (HttpHeaderEntry
*e
= getEntry(&pos
)) {
1044 CBIT_SET(mask
, e
->id
);
1048 /* appends an entry;
1049 * does not call e->clone() so one should not reuse "*e"
1052 HttpHeader::addEntry(HttpHeaderEntry
* e
)
1056 assert(e
->name
.size());
1058 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
1060 if (CBIT_TEST(mask
, e
->id
)) {
1061 ++ Headers
[e
->id
].stat
.repCount
;
1062 ++ headerStatsTable
.at(e
->id
).repCount
; //TODO: use operator[]
1064 CBIT_SET(mask
, e
->id
);
1067 entries
.push_back(e
);
1069 /* increment header length, allow for ": " and crlf */
1070 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
1073 /* inserts an entry;
1074 * does not call e->clone() so one should not reuse "*e"
1077 HttpHeader::insertEntry(HttpHeaderEntry
* e
)
1082 debugs(55, 7, this << " adding entry: " << e
->id
<< " at " << entries
.size());
1084 if (CBIT_TEST(mask
, e
->id
)) {
1085 ++ Headers
[e
->id
].stat
.repCount
;
1086 ++ headerStatsTable
.at(e
->id
).repCount
;
1088 CBIT_SET(mask
, e
->id
);
1091 entries
.insert(entries
.begin(),e
);
1093 /* increment header length, allow for ": " and crlf */
1094 len
+= e
->name
.size() + 2 + e
->value
.size() + 2;
1098 HttpHeader::getList(http_hdr_type id
, String
*s
) const
1101 HttpHeaderPos pos
= HttpHeaderInitPos
;
1102 debugs(55, 9, this << " joining for id " << id
);
1103 /* only fields from ListHeaders array can be "listed" */
1104 assert(CBIT_TEST(ListHeadersMask
, id
));
1106 if (!CBIT_TEST(mask
, id
))
1109 while ((e
= getEntry(&pos
))) {
1111 strListAdd(s
, e
->value
.termedBuf(), ',');
1115 * note: we might get an empty (size==0) string if there was an "empty"
1116 * header. This results in an empty length String, which may have a NULL
1119 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1121 debugs(55, 3, "empty list header: " << headerTable
[id
].name
<< "(" << id
<< ")");
1123 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1128 /* return a list of entries with the same id separated by ',' and ws */
1130 HttpHeader::getList(http_hdr_type id
) const
1133 HttpHeaderPos pos
= HttpHeaderInitPos
;
1134 debugs(55, 9, this << "joining for id " << id
);
1135 /* only fields from ListHeaders array can be "listed" */
1136 assert(CBIT_TEST(ListHeadersMask
, id
));
1138 if (!CBIT_TEST(mask
, id
))
1143 while ((e
= getEntry(&pos
))) {
1145 strListAdd(&s
, e
->value
.termedBuf(), ',');
1149 * note: we might get an empty (size==0) string if there was an "empty"
1150 * header. This results in an empty length String, which may have a NULL
1153 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
1155 debugs(55, 3, "empty list header: " << headerTable
[id
].name
<< "(" << id
<< ")");
1157 debugs(55, 6, this << ": joined for id " << id
<< ": " << s
);
1162 /* return a string or list of entries with the same id separated by ',' and ws */
1164 HttpHeader::getStrOrList(http_hdr_type id
) const
1168 if (CBIT_TEST(ListHeadersMask
, id
))
1171 if ((e
= findEntry(id
)))
1178 * Returns the value of the specified header and/or an undefined String.
1181 HttpHeader::getByName(const char *name
) const
1184 // ignore presence: return undefined string if an empty header is present
1185 (void)getByNameIfPresent(name
, result
);
1190 HttpHeader::getByNameIfPresent(const char *name
, String
&result
) const
1193 HttpHeaderPos pos
= HttpHeaderInitPos
;
1198 /* First try the quick path */
1199 id
= httpHeaderIdByNameDef(name
, strlen(name
));
1204 result
= getStrOrList(id
);
1208 /* Sorry, an unknown header name. Do linear search */
1210 while ((e
= getEntry(&pos
))) {
1211 if (e
->id
== HDR_OTHER
&& e
->name
.caseCmp(name
) == 0) {
1213 strListAdd(&result
, e
->value
.termedBuf(), ',');
1221 * Returns a the value of the specified list member, if any.
1224 HttpHeader::getByNameListMember(const char *name
, const char *member
, const char separator
) const
1227 const char *pos
= NULL
;
1230 int mlen
= strlen(member
);
1234 header
= getByName(name
);
1238 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1239 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1240 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1249 * returns a the value of the specified list member, if any.
1252 HttpHeader::getListMember(http_hdr_type id
, const char *member
, const char separator
) const
1255 const char *pos
= NULL
;
1258 int mlen
= strlen(member
);
1262 header
= getStrOrList(id
);
1265 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1266 if (strncmp(item
, member
, mlen
) == 0 && item
[mlen
] == '=') {
1267 result
.append(item
+ mlen
+ 1, ilen
- mlen
- 1);
1276 /* test if a field is present */
1278 HttpHeader::has(http_hdr_type id
) const
1281 assert(id
!= HDR_OTHER
);
1282 debugs(55, 9, this << " lookup for " << id
);
1283 return CBIT_TEST(mask
, id
);
1287 HttpHeader::putInt(http_hdr_type id
, int number
)
1290 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1291 assert(number
>= 0);
1292 addEntry(new HttpHeaderEntry(id
, NULL
, xitoa(number
)));
1296 HttpHeader::putInt64(http_hdr_type id
, int64_t number
)
1299 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1300 assert(number
>= 0);
1301 addEntry(new HttpHeaderEntry(id
, NULL
, xint64toa(number
)));
1305 HttpHeader::putTime(http_hdr_type id
, time_t htime
)
1308 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1310 addEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1314 HttpHeader::insertTime(http_hdr_type id
, time_t htime
)
1317 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1319 insertEntry(new HttpHeaderEntry(id
, NULL
, mkrfc1123(htime
)));
1323 HttpHeader::putStr(http_hdr_type id
, const char *str
)
1326 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1328 addEntry(new HttpHeaderEntry(id
, NULL
, str
));
1332 HttpHeader::putAuth(const char *auth_scheme
, const char *realm
)
1334 assert(auth_scheme
&& realm
);
1335 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE
, "%s realm=\"%s\"", auth_scheme
, realm
);
1339 HttpHeader::putCc(const HttpHdrCc
* cc
)
1342 /* remove old directives if any */
1343 delById(HDR_CACHE_CONTROL
);
1349 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL
, NULL
, mb
.buf
));
1355 HttpHeader::putContRange(const HttpHdrContRange
* cr
)
1358 /* remove old directives if any */
1359 delById(HDR_CONTENT_RANGE
);
1363 httpHdrContRangePackInto(cr
, &mb
);
1365 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE
, NULL
, mb
.buf
));
1371 HttpHeader::putRange(const HttpHdrRange
* range
)
1374 /* remove old directives if any */
1379 range
->packInto(&mb
);
1381 addEntry(new HttpHeaderEntry(HDR_RANGE
, NULL
, mb
.buf
));
1387 HttpHeader::putSc(HttpHdrSc
*sc
)
1390 /* remove old directives if any */
1391 delById(HDR_SURROGATE_CONTROL
);
1397 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL
, NULL
, mb
.buf
));
1403 HttpHeader::putWarning(const int code
, const char *const text
)
1406 snprintf(buf
, sizeof(buf
), "%i %s \"%s\"", code
, visible_appname_string
, text
);
1407 putStr(HDR_WARNING
, buf
);
1410 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1412 HttpHeader::putExt(const char *name
, const char *value
)
1414 assert(name
&& value
);
1415 debugs(55, 8, this << " adds ext entry " << name
<< " : " << value
);
1416 addEntry(new HttpHeaderEntry(HDR_OTHER
, name
, value
));
1420 HttpHeader::getInt(http_hdr_type id
) const
1423 assert(Headers
[id
].type
== ftInt
); /* must be of an appropriate type */
1426 if ((e
= findEntry(id
)))
1433 HttpHeader::getInt64(http_hdr_type id
) const
1436 assert(Headers
[id
].type
== ftInt64
); /* must be of an appropriate type */
1439 if ((e
= findEntry(id
)))
1440 return e
->getInt64();
1446 HttpHeader::getTime(http_hdr_type id
) const
1451 assert(Headers
[id
].type
== ftDate_1123
); /* must be of an appropriate type */
1453 if ((e
= findEntry(id
))) {
1454 value
= parse_rfc1123(e
->value
.termedBuf());
1455 httpHeaderNoteParsedEntry(e
->id
, e
->value
, value
< 0);
1461 /* sync with httpHeaderGetLastStr */
1463 HttpHeader::getStr(http_hdr_type id
) const
1467 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1469 if ((e
= findEntry(id
))) {
1470 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1471 return e
->value
.termedBuf();
1479 HttpHeader::getLastStr(http_hdr_type id
) const
1483 assert(Headers
[id
].type
== ftStr
); /* must be of an appropriate type */
1485 if ((e
= findLastEntry(id
))) {
1486 httpHeaderNoteParsedEntry(e
->id
, e
->value
, 0); /* no errors are possible */
1487 return e
->value
.termedBuf();
1494 HttpHeader::getCc() const
1496 if (!CBIT_TEST(mask
, HDR_CACHE_CONTROL
))
1498 PROF_start(HttpHeader_getCc
);
1501 getList(HDR_CACHE_CONTROL
, &s
);
1503 HttpHdrCc
*cc
=new HttpHdrCc();
1505 if (!cc
->parse(s
)) {
1510 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1513 httpHdrCcUpdateStats(cc
, &HttpHeaderStats
[owner
].ccTypeDistr
);
1515 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL
, s
, !cc
);
1517 PROF_stop(HttpHeader_getCc
);
1523 HttpHeader::getRange() const
1525 HttpHdrRange
*r
= NULL
;
1527 /* some clients will send "Request-Range" _and_ *matching* "Range"
1528 * who knows, some clients might send Request-Range only;
1529 * this "if" should work correctly in both cases;
1530 * hopefully no clients send mismatched headers! */
1532 if ((e
= findEntry(HDR_RANGE
)) ||
1533 (e
= findEntry(HDR_REQUEST_RANGE
))) {
1534 r
= HttpHdrRange::ParseCreate(&e
->value
);
1535 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !r
);
1542 HttpHeader::getSc() const
1544 if (!CBIT_TEST(mask
, HDR_SURROGATE_CONTROL
))
1549 (void) getList(HDR_SURROGATE_CONTROL
, &s
);
1551 HttpHdrSc
*sc
= httpHdrScParseCreate(s
);
1553 ++ HttpHeaderStats
[owner
].ccParsedCount
;
1556 sc
->updateStats(&HttpHeaderStats
[owner
].scTypeDistr
);
1558 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL
, s
, !sc
);
1564 HttpHeader::getContRange() const
1566 HttpHdrContRange
*cr
= NULL
;
1569 if ((e
= findEntry(HDR_CONTENT_RANGE
))) {
1570 cr
= httpHdrContRangeParseCreate(e
->value
.termedBuf());
1571 httpHeaderNoteParsedEntry(e
->id
, e
->value
, !cr
);
1578 HttpHeader::getAuth(http_hdr_type id
, const char *auth_scheme
) const
1582 assert(auth_scheme
);
1585 if (!field
) /* no authorization field */
1588 l
= strlen(auth_scheme
);
1590 if (!l
|| strncasecmp(field
, auth_scheme
, l
)) /* wrong scheme */
1595 if (!xisspace(*field
)) /* wrong scheme */
1598 /* skip white space */
1599 for (; field
&& xisspace(*field
); ++field
);
1601 if (!*field
) /* no authorization cookie */
1604 static char decodedAuthToken
[8192];
1605 struct base64_decode_ctx ctx
;
1606 base64_decode_init(&ctx
);
1607 size_t decodedLen
= 0;
1608 if (!base64_decode_update(&ctx
, &decodedLen
, reinterpret_cast<uint8_t*>(decodedAuthToken
), strlen(field
), reinterpret_cast<const uint8_t*>(field
)) ||
1609 !base64_decode_final(&ctx
)) {
1612 decodedAuthToken
[decodedLen
] = '\0';
1613 return decodedAuthToken
;
1617 HttpHeader::getETag(http_hdr_type id
) const
1619 ETag etag
= {NULL
, -1};
1621 assert(Headers
[id
].type
== ftETag
); /* must be of an appropriate type */
1623 if ((e
= findEntry(id
)))
1624 etagParseInit(&etag
, e
->value
.termedBuf());
1630 HttpHeader::getTimeOrTag(http_hdr_type id
) const
1634 assert(Headers
[id
].type
== ftDate_1123_or_ETag
); /* must be of an appropriate type */
1635 memset(&tot
, 0, sizeof(tot
));
1637 if ((e
= findEntry(id
))) {
1638 const char *str
= e
->value
.termedBuf();
1639 /* try as an ETag */
1641 if (etagParseInit(&tot
.tag
, str
)) {
1642 tot
.valid
= tot
.tag
.str
!= NULL
;
1645 /* or maybe it is time? */
1646 tot
.time
= parse_rfc1123(str
);
1647 tot
.valid
= tot
.time
>= 0;
1652 assert(tot
.time
< 0 || !tot
.tag
.str
); /* paranoid */
1660 HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId
, const char *aName
, const char *aValue
)
1665 if (id
!= HDR_OTHER
)
1666 name
= headerTable
[id
].name
;
1672 ++ Headers
[id
].stat
.aliveCount
;
1673 ++ headerStatsTable
.at(id
).aliveCount
;
1675 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name
<< " : " << value
);
1678 HttpHeaderEntry::~HttpHeaderEntry()
1681 debugs(55, 9, "destroying entry " << this << ": '" << name
<< ": " << value
<< "'");
1683 assert(Headers
[id
].stat
.aliveCount
); // is this really needed?
1685 -- Headers
[id
].stat
.aliveCount
;
1690 /* parses and inits header entry, returns true/false */
1692 HttpHeaderEntry::parse(const char *field_start
, const char *field_end
)
1694 /* note: name_start == field_start */
1695 const char *name_end
= (const char *)memchr(field_start
, ':', field_end
- field_start
);
1696 int name_len
= name_end
? name_end
- field_start
:0;
1697 const char *value_start
= field_start
+ name_len
+ 1; /* skip ':' */
1698 /* note: value_end == field_end */
1700 ++ HeaderEntryParsedCount
;
1702 /* do we have a valid field name within this field? */
1704 if (!name_len
|| name_end
> field_end
)
1707 if (name_len
> 65534) {
1708 /* String must be LESS THAN 64K and it adds a terminating NULL */
1709 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring header name of " << name_len
<< " bytes");
1713 if (Config
.onoff
.relaxed_header_parser
&& xisspace(field_start
[name_len
- 1])) {
1714 debugs(55, Config
.onoff
.relaxed_header_parser
<= 0 ? 1 : 2,
1715 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1717 while (name_len
> 0 && xisspace(field_start
[name_len
- 1]))
1724 /* now we know we can parse it */
1726 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start
, field_end
-field_start
) << "'");
1728 /* is it a "known" field? */
1729 http_hdr_type id
= httpHeaderIdByName(field_start
, name_len
, Headers
, HDR_ENUM_END
);
1740 /* set field name */
1741 if (id
== HDR_OTHER
)
1742 name
.limitInit(field_start
, name_len
);
1744 name
= headerTable
[id
].name
;
1746 /* trim field value */
1747 while (value_start
< field_end
&& xisspace(*value_start
))
1750 while (value_start
< field_end
&& xisspace(field_end
[-1]))
1753 if (field_end
- value_start
> 65534) {
1754 /* String must be LESS THAN 64K and it adds a terminating NULL */
1755 debugs(55, DBG_IMPORTANT
, "WARNING: ignoring '" << name
<< "' header of " << (field_end
- value_start
) << " bytes");
1757 if (id
== HDR_OTHER
)
1763 /* set field value */
1764 value
.limitInit(value_start
, field_end
- value_start
);
1766 ++ Headers
[id
].stat
.seenCount
;
1767 ++ headerStatsTable
[id
].seenCount
;
1769 debugs(55, 9, "parsed HttpHeaderEntry: '" << name
<< ": " << value
<< "'");
1771 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1775 HttpHeaderEntry::clone() const
1777 return new HttpHeaderEntry(id
, name
.termedBuf(), value
.termedBuf());
1781 HttpHeaderEntry::packInto(Packable
* p
) const
1784 p
->append(name
.rawBuf(), name
.size());
1786 p
->append(value
.rawBuf(), value
.size());
1787 p
->append("\r\n", 2);
1791 HttpHeaderEntry::getInt() const
1794 assert (Headers
[id
].type
== ftInt
);
1796 int ok
= httpHeaderParseInt(value
.termedBuf(), &val
);
1797 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1798 /* XXX: Should we check ok - ie
1799 * return ok ? -1 : value;
1805 HttpHeaderEntry::getInt64() const
1808 assert (Headers
[id
].type
== ftInt64
);
1810 int ok
= httpHeaderParseOffset(value
.termedBuf(), &val
);
1811 httpHeaderNoteParsedEntry(id
, value
, !ok
);
1812 /* XXX: Should we check ok - ie
1813 * return ok ? -1 : value;
1819 httpHeaderNoteParsedEntry(http_hdr_type id
, String
const &context
, int error
)
1821 ++ Headers
[id
].stat
.parsCount
;
1822 ++ headerStatsTable
.at(id
).parsCount
;
1825 ++ Headers
[id
].stat
.errCount
;
1826 ++ headerStatsTable
.at(id
).errCount
;
1827 debugs(55, 2, "cannot parse hdr field: '" << headerTable
[id
].name
<< ": " << context
<< "'");
1835 /* tmp variable used to pass stat info to dumpers */
1836 extern const HttpHeaderStat
*dump_stat
; /* argh! */
1837 const HttpHeaderStat
*dump_stat
= NULL
;
1840 httpHeaderFieldStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
1842 const int id
= (int) val
;
1843 const int valid_id
= id
>= 0 && id
< HDR_ENUM_END
;
1844 const char *name
= valid_id
? headerTable
[id
].name
: "INVALID";
1845 int visible
= count
> 0;
1846 /* for entries with zero count, list only those that belong to current type of message */
1848 if (!visible
&& valid_id
&& dump_stat
->owner_mask
)
1849 visible
= CBIT_TEST(*dump_stat
->owner_mask
, id
);
1852 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
1853 id
, name
, count
, xdiv(count
, dump_stat
->busyDestroyedCount
));
1857 httpHeaderFldsPerHdrDumper(StoreEntry
* sentry
, int idx
, double val
, double, int count
)
1860 storeAppendPrintf(sentry
, "%2d\t %5d\t %5d\t %6.2f\n",
1861 idx
, (int) val
, count
,
1862 xpercent(count
, dump_stat
->destroyedCount
));
1866 httpHeaderStatDump(const HttpHeaderStat
* hs
, StoreEntry
* e
)
1871 storeAppendPrintf(e
, "\nHeader Stats: %s\n", hs
->label
);
1872 storeAppendPrintf(e
, "\nField type distribution\n");
1873 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1874 "id", "name", "count", "#/header");
1875 hs
->fieldTypeDistr
.dump(e
, httpHeaderFieldStatDumper
);
1876 storeAppendPrintf(e
, "\nCache-control directives distribution\n");
1877 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1878 "id", "name", "count", "#/cc_field");
1879 hs
->ccTypeDistr
.dump(e
, httpHdrCcStatDumper
);
1880 storeAppendPrintf(e
, "\nSurrogate-control directives distribution\n");
1881 storeAppendPrintf(e
, "%2s\t %-20s\t %5s\t %6s\n",
1882 "id", "name", "count", "#/sc_field");
1883 hs
->scTypeDistr
.dump(e
, httpHdrScStatDumper
);
1884 storeAppendPrintf(e
, "\nNumber of fields per header distribution\n");
1885 storeAppendPrintf(e
, "%2s\t %-5s\t %5s\t %6s\n",
1886 "id", "#flds", "count", "%total");
1887 hs
->hdrUCountDistr
.dump(e
, httpHeaderFldsPerHdrDumper
);
1888 storeAppendPrintf(e
, "\n");
1893 httpHeaderStoreReport(StoreEntry
* e
)
1899 HttpHeaderStats
[0].parsedCount
=
1900 HttpHeaderStats
[hoRequest
].parsedCount
+ HttpHeaderStats
[hoReply
].parsedCount
;
1901 HttpHeaderStats
[0].ccParsedCount
=
1902 HttpHeaderStats
[hoRequest
].ccParsedCount
+ HttpHeaderStats
[hoReply
].ccParsedCount
;
1903 HttpHeaderStats
[0].destroyedCount
=
1904 HttpHeaderStats
[hoRequest
].destroyedCount
+ HttpHeaderStats
[hoReply
].destroyedCount
;
1905 HttpHeaderStats
[0].busyDestroyedCount
=
1906 HttpHeaderStats
[hoRequest
].busyDestroyedCount
+ HttpHeaderStats
[hoReply
].busyDestroyedCount
;
1908 for (i
= 1; i
< HttpHeaderStatCount
; ++i
) {
1909 httpHeaderStatDump(HttpHeaderStats
+ i
, e
);
1912 /* field stats for all messages */
1913 storeAppendPrintf(e
, "\nHttp Fields Stats (replies and requests)\n");
1915 storeAppendPrintf(e
, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1916 "id", "name", "#alive", "%err", "%repeat");
1918 for (ht
= (http_hdr_type
)0; ht
< HDR_ENUM_END
; ++ht
) {
1919 HttpHeaderFieldInfo
*f
= Headers
+ ht
;
1920 storeAppendPrintf(e
, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1921 f
->id
, f
->name
.termedBuf(), f
->stat
.aliveCount
,
1922 xpercent(f
->stat
.errCount
, f
->stat
.parsCount
),
1923 xpercent(f
->stat
.repCount
, f
->stat
.seenCount
));
1926 storeAppendPrintf(e
, "Headers Parsed: %d + %d = %d\n",
1927 HttpHeaderStats
[hoRequest
].parsedCount
,
1928 HttpHeaderStats
[hoReply
].parsedCount
,
1929 HttpHeaderStats
[0].parsedCount
);
1930 storeAppendPrintf(e
, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount
);
1934 httpHeaderIdByName(const char *name
, size_t name_len
, const HttpHeaderFieldInfo
* info
, int end
)
1937 for (int i
= 0; i
< end
; ++i
) {
1938 if (name_len
!= info
[i
].name
.size())
1941 if (!strncasecmp(name
, info
[i
].name
.rawBuf(), name_len
))
1950 httpHeaderIdByNameDef(const char *name
, int name_len
)
1953 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1955 return httpHeaderIdByName(name
, name_len
, Headers
, HDR_ENUM_END
);
1959 httpHeaderNameById(int id
)
1962 Headers
= httpHeaderBuildFieldsInfo(HeadersAttrs
, HDR_ENUM_END
);
1964 assert(id
>= 0 && id
< HDR_ENUM_END
);
1966 return headerTable
[id
].name
;
1970 HttpHeader::hasListMember(http_hdr_type id
, const char *member
, const char separator
) const
1973 const char *pos
= NULL
;
1976 int mlen
= strlen(member
);
1980 String
header (getStrOrList(id
));
1982 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
1983 if (strncasecmp(item
, member
, mlen
) == 0
1984 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
1994 HttpHeader::hasByNameListMember(const char *name
, const char *member
, const char separator
) const
1997 const char *pos
= NULL
;
2000 int mlen
= strlen(member
);
2004 String
header (getByName(name
));
2006 while (strListGetItem(&header
, separator
, &item
, &ilen
, &pos
)) {
2007 if (strncasecmp(item
, member
, mlen
) == 0
2008 && (item
[mlen
] == '=' || item
[mlen
] == separator
|| item
[mlen
] == ';' || item
[mlen
] == '\0')) {
2018 HttpHeader::removeHopByHopEntries()
2020 removeConnectionHeaderEntries();
2022 const HttpHeaderEntry
*e
;
2023 HttpHeaderPos pos
= HttpHeaderInitPos
;
2024 int headers_deleted
= 0;
2025 while ((e
= getEntry(&pos
))) {
2027 if (CBIT_TEST(HopByHopHeadersMask
, id
)) {
2028 delAt(pos
, headers_deleted
);
2035 HttpHeader::removeConnectionHeaderEntries()
2037 if (has(HDR_CONNECTION
)) {
2038 /* anything that matches Connection list member will be deleted */
2039 String strConnection
;
2041 (void) getList(HDR_CONNECTION
, &strConnection
);
2042 const HttpHeaderEntry
*e
;
2043 HttpHeaderPos pos
= HttpHeaderInitPos
;
2045 * think: on-average-best nesting of the two loops (hdrEntry
2046 * and strListItem) @?@
2049 * maybe we should delete standard stuff ("keep-alive","close")
2050 * from strConnection first?
2053 int headers_deleted
= 0;
2054 while ((e
= getEntry(&pos
))) {
2055 if (strListIsMember(&strConnection
, e
->name
.termedBuf(), ','))
2056 delAt(pos
, headers_deleted
);
2058 if (headers_deleted
)