]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.cc
Updates for running on squid-cache.org
[thirdparty/squid.git] / src / HttpHeader.cc
CommitLineData
4f7e9dbb 1
cb69b4c7 2/*
2cdeea82 3 * $Id: HttpHeader.cc,v 1.139 2008/01/22 19:53:03 rousskov Exp $
cb69b4c7 4 *
123abbe1 5 * DEBUG: section 55 HTTP Header
cb69b4c7 6 * AUTHOR: Alex Rousskov
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
cb69b4c7 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
cb69b4c7 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
cb69b4c7 34 */
35
36#include "squid.h"
62ee09ca 37#include "CacheManager.h"
e6ccf245 38#include "Store.h"
39#include "HttpHeader.h"
528b2c61 40#include "HttpHdrContRange.h"
25b6a907 41#include "HttpHdrSc.h"
0eb49b6d 42#include "MemBuf.h"
cb69b4c7 43
44/*
2ac76861 45 * On naming conventions:
46 *
47 * HTTP/1.1 defines message-header as
48 *
49 * message-header = field-name ":" [ field-value ] CRLF
50 * field-name = token
51 * field-value = *( field-content | LWS )
52 *
53 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
54 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
55 *
56 * HttpHeader is an object that represents all message-headers in a message.
57 * HttpHeader does not manage start-line.
58 *
d8b249ef 59 * HttpHeader is implemented as a collection of header "entries".
60 * An entry is a (field_id, field_name, field_value) triplet.
2ac76861 61 */
cb69b4c7 62
63
cb69b4c7 64/*
65 * local constants and vars
66 */
67
68/*
69 * A table with major attributes for every known field.
70 * We calculate name lengths and reorganize this array on start up.
71 * After reorganization, field id can be used as an index to the table.
72 */
de336bbe 73static const HttpHeaderFieldAttrs HeadersAttrs[] =
62e76326 74 {
75 {"Accept", HDR_ACCEPT, ftStr},
76
77 {"Accept-Charset", HDR_ACCEPT_CHARSET, ftStr},
78 {"Accept-Encoding", HDR_ACCEPT_ENCODING, ftStr},
79 {"Accept-Language", HDR_ACCEPT_LANGUAGE, ftStr},
80 {"Accept-Ranges", HDR_ACCEPT_RANGES, ftStr},
81 {"Age", HDR_AGE, ftInt},
82 {"Allow", HDR_ALLOW, ftStr},
83 {"Authorization", HDR_AUTHORIZATION, ftStr}, /* for now */
84 {"Cache-Control", HDR_CACHE_CONTROL, ftPCc},
85 {"Connection", HDR_CONNECTION, ftStr},
86 {"Content-Base", HDR_CONTENT_BASE, ftStr},
87 {"Content-Encoding", HDR_CONTENT_ENCODING, ftStr},
88 {"Content-Language", HDR_CONTENT_LANGUAGE, ftStr},
47f6e231 89 {"Content-Length", HDR_CONTENT_LENGTH, ftInt64},
62e76326 90 {"Content-Location", HDR_CONTENT_LOCATION, ftStr},
91 {"Content-MD5", HDR_CONTENT_MD5, ftStr}, /* for now */
92 {"Content-Range", HDR_CONTENT_RANGE, ftPContRange},
93 {"Content-Type", HDR_CONTENT_TYPE, ftStr},
94 {"Cookie", HDR_COOKIE, ftStr},
95 {"Date", HDR_DATE, ftDate_1123},
96 {"ETag", HDR_ETAG, ftETag},
97 {"Expires", HDR_EXPIRES, ftDate_1123},
98 {"From", HDR_FROM, ftStr},
99 {"Host", HDR_HOST, ftStr},
100 {"If-Match", HDR_IF_MATCH, ftStr}, /* for now */
101 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE, ftDate_1123},
102 {"If-None-Match", HDR_IF_NONE_MATCH, ftStr}, /* for now */
103 {"If-Range", HDR_IF_RANGE, ftDate_1123_or_ETag},
1d7ab0f4 104 {"Keep-Alive", HDR_KEEP_ALIVE, ftStr},
62e76326 105 {"Last-Modified", HDR_LAST_MODIFIED, ftDate_1123},
106 {"Link", HDR_LINK, ftStr},
107 {"Location", HDR_LOCATION, ftStr},
108 {"Max-Forwards", HDR_MAX_FORWARDS, ftInt},
109 {"Mime-Version", HDR_MIME_VERSION, ftStr}, /* for now */
110 {"Pragma", HDR_PRAGMA, ftStr},
111 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE, ftStr},
112 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO, ftStr},
113 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION, ftStr},
114 {"Proxy-Connection", HDR_PROXY_CONNECTION, ftStr},
d67acb4e 115 {"Proxy-support", HDR_PROXY_SUPPORT, ftStr},
62e76326 116 {"Public", HDR_PUBLIC, ftStr},
117 {"Range", HDR_RANGE, ftPRange},
118 {"Referer", HDR_REFERER, ftStr},
119 {"Request-Range", HDR_REQUEST_RANGE, ftPRange}, /* usually matches HDR_RANGE */
120 {"Retry-After", HDR_RETRY_AFTER, ftStr}, /* for now (ftDate_1123 or ftInt!) */
121 {"Server", HDR_SERVER, ftStr},
122 {"Set-Cookie", HDR_SET_COOKIE, ftStr},
1d7ab0f4 123 {"TE", HDR_TE, ftStr},
62e76326 124 {"Title", HDR_TITLE, ftStr},
1d7ab0f4 125 {"Trailers", HDR_TRAILERS, ftStr},
62e76326 126 {"Transfer-Encoding", HDR_TRANSFER_ENCODING, ftStr},
127 {"Upgrade", HDR_UPGRADE, ftStr}, /* for now */
128 {"User-Agent", HDR_USER_AGENT, ftStr},
129 {"Vary", HDR_VARY, ftStr}, /* for now */
130 {"Via", HDR_VIA, ftStr}, /* for now */
131 {"Warning", HDR_WARNING, ftStr}, /* for now */
132 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE, ftStr},
133 {"Authentication-Info", HDR_AUTHENTICATION_INFO, ftStr},
134 {"X-Cache", HDR_X_CACHE, ftStr},
135 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP, ftStr},
136 {"X-Forwarded-For", HDR_X_FORWARDED_FOR, ftStr},
137 {"X-Request-URI", HDR_X_REQUEST_URI, ftStr},
138 {"X-Squid-Error", HDR_X_SQUID_ERROR, ftStr},
139 {"Negotiate", HDR_NEGOTIATE, ftStr},
f66a9ef4 140#if X_ACCELERATOR_VARY
62e76326 141 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY, ftStr},
f66a9ef4 142#endif
43ae1d95 143 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY, ftStr},
144 {"Surrogate-Control", HDR_SURROGATE_CONTROL, ftPSc},
62e76326 145 {"Front-End-Https", HDR_FRONT_END_HTTPS, ftStr},
146 {"Other:", HDR_OTHER, ftStr} /* ':' will not allow matches */
147 };
148
de336bbe 149static HttpHeaderFieldInfo *Headers = NULL;
cb69b4c7 150
e6ccf245 151http_hdr_type &operator++ (http_hdr_type &aHeader)
152{
1f1ae50a 153 int tmp = (int)aHeader;
154 aHeader = (http_hdr_type)(++tmp);
e6ccf245 155 return aHeader;
156}
157
158
cb69b4c7 159/*
160 * headers with field values defined as #(values) in HTTP/1.1
d8b249ef 161 * Headers that are currently not recognized, are commented out.
cb69b4c7 162 */
b644367b 163static HttpHeaderMask ListHeadersMask; /* set run-time using ListHeadersArr */
de336bbe 164static http_hdr_type ListHeadersArr[] =
62e76326 165 {
166 HDR_ACCEPT,
167 HDR_ACCEPT_CHARSET, HDR_ACCEPT_ENCODING, HDR_ACCEPT_LANGUAGE,
168 HDR_ACCEPT_RANGES, HDR_ALLOW,
169 HDR_CACHE_CONTROL,
170 HDR_CONTENT_ENCODING,
171 HDR_CONTENT_LANGUAGE,
172 HDR_CONNECTION,
173 HDR_IF_MATCH, HDR_IF_NONE_MATCH,
174 HDR_LINK, HDR_PRAGMA,
175 HDR_PROXY_CONNECTION,
d67acb4e 176 HDR_PROXY_SUPPORT,
62e76326 177 HDR_TRANSFER_ENCODING,
178 HDR_UPGRADE,
179 HDR_VARY,
180 HDR_VIA,
181 /* HDR_WARNING, */
182 HDR_WWW_AUTHENTICATE,
183 HDR_AUTHENTICATION_INFO,
184 HDR_PROXY_AUTHENTICATION_INFO,
185 /* HDR_EXPECT, HDR_TE, HDR_TRAILER */
f66a9ef4 186#if X_ACCELERATOR_VARY
62e76326 187 HDR_X_ACCELERATOR_VARY,
f66a9ef4 188#endif
43ae1d95 189 HDR_SURROGATE_CAPABILITY,
190 HDR_SURROGATE_CONTROL,
62e76326 191 HDR_X_FORWARDED_FOR
192 };
99edd1c3 193
194/* general-headers */
195static http_hdr_type GeneralHeadersArr[] =
62e76326 196 {
197 HDR_CACHE_CONTROL, HDR_CONNECTION, HDR_DATE, HDR_PRAGMA,
198 HDR_TRANSFER_ENCODING,
199 HDR_UPGRADE,
200 /* HDR_TRAILER, */
43ae1d95 201 HDR_VIA,
62e76326 202 };
99edd1c3 203
204/* entity-headers */
205static http_hdr_type EntityHeadersArr[] =
62e76326 206 {
207 HDR_ALLOW, HDR_CONTENT_BASE, HDR_CONTENT_ENCODING, HDR_CONTENT_LANGUAGE,
208 HDR_CONTENT_LENGTH, HDR_CONTENT_LOCATION, HDR_CONTENT_MD5,
209 HDR_CONTENT_RANGE, HDR_CONTENT_TYPE, HDR_ETAG, HDR_EXPIRES, HDR_LAST_MODIFIED, HDR_LINK,
210 HDR_OTHER
211 };
cb69b4c7 212
b644367b 213static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */
de336bbe 214static http_hdr_type ReplyHeadersArr[] =
62e76326 215 {
216 HDR_ACCEPT, HDR_ACCEPT_CHARSET, HDR_ACCEPT_ENCODING, HDR_ACCEPT_LANGUAGE,
217 HDR_ACCEPT_RANGES, HDR_AGE,
218 HDR_LOCATION, HDR_MAX_FORWARDS,
219 HDR_MIME_VERSION, HDR_PUBLIC, HDR_RETRY_AFTER, HDR_SERVER, HDR_SET_COOKIE,
220 HDR_VARY,
221 HDR_WARNING, HDR_PROXY_CONNECTION, HDR_X_CACHE,
222 HDR_X_CACHE_LOOKUP,
223 HDR_X_REQUEST_URI,
f66a9ef4 224#if X_ACCELERATOR_VARY
62e76326 225 HDR_X_ACCELERATOR_VARY,
f66a9ef4 226#endif
43ae1d95 227 HDR_X_SQUID_ERROR,
228 HDR_SURROGATE_CONTROL
62e76326 229 };
cb69b4c7 230
b644367b 231static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */
de336bbe 232static http_hdr_type RequestHeadersArr[] =
62e76326 233 {
234 HDR_AUTHORIZATION, HDR_FROM, HDR_HOST,
235 HDR_IF_MATCH, HDR_IF_MODIFIED_SINCE, HDR_IF_NONE_MATCH,
236 HDR_IF_RANGE, HDR_MAX_FORWARDS, HDR_PROXY_CONNECTION,
237 HDR_PROXY_AUTHORIZATION, HDR_RANGE, HDR_REFERER, HDR_REQUEST_RANGE,
43ae1d95 238 HDR_USER_AGENT, HDR_X_FORWARDED_FOR, HDR_SURROGATE_CAPABILITY
62e76326 239 };
cb69b4c7 240
dcf3665b 241static HttpHeaderMask HopByHopHeadersMask;
2cdeea82 242static http_hdr_type HopByHopHeadersArr[] =
243 {
dcf3665b
AJ
244 HDR_CONNECTION, HDR_KEEP_ALIVE, /*HDR_PROXY_AUTHENTICATE,*/ HDR_PROXY_AUTHORIZATION,
245 HDR_TE, HDR_TRAILERS, HDR_TRANSFER_ENCODING, HDR_UPGRADE, HDR_PROXY_CONNECTION
2cdeea82 246 };
247
12cf1be2 248/* header accounting */
2ac76861 249static HttpHeaderStat HttpHeaderStats[] =
62e76326 250 {
251 {"all"},
9f4d4f65 252#if USE_HTCP
62e76326 253 {"HTCP reply"},
9f4d4f65 254#endif
62e76326 255 {"request"},
256 {"reply"}
257 };
9bea1d5b 258static int HttpHeaderStatCount = countof(HttpHeaderStats);
12cf1be2 259
7faf2bdb 260static int HeaderEntryParsedCount = 0;
cb69b4c7 261
12cf1be2 262/*
263 * local routines
264 */
cb69b4c7 265
266#define assert_eid(id) assert((id) >= 0 && (id) < HDR_ENUM_END)
267
30abd221 268static void httpHeaderNoteParsedEntry(http_hdr_type id, String const &value, int error);
cb69b4c7 269
9bea1d5b 270static void httpHeaderStatInit(HttpHeaderStat * hs, const char *label);
271static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e);
7c525cc2 272
cb69b4c7 273/*
274 * Module initialization routines
275 */
276
6b7d87bb
FC
277static void
278httpHeaderRegisterWithCacheManager(void)
279{
280 CacheManager::GetInstance()->
281 registerAction("http_headers",
282 "HTTP Header Statistics",
283 httpHeaderStoreReport, 0, 1);
284}
285
cb69b4c7 286void
9bea1d5b 287httpHeaderInitModule(void)
cb69b4c7 288{
9bea1d5b 289 int i;
290 /* check that we have enough space for masks */
291 assert(8 * sizeof(HttpHeaderMask) >= HDR_ENUM_END);
292 /* all headers must be described */
293 assert(countof(HeadersAttrs) == HDR_ENUM_END);
62e76326 294
9bea1d5b 295 if (!Headers)
62e76326 296 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
297
9bea1d5b 298 /* create masks */
299 httpHeaderMaskInit(&ListHeadersMask, 0);
62e76326 300
8abf232c 301 httpHeaderCalcMask(&ListHeadersMask, ListHeadersArr, countof(ListHeadersArr));
62e76326 302
9bea1d5b 303 httpHeaderMaskInit(&ReplyHeadersMask, 0);
62e76326 304
8abf232c 305 httpHeaderCalcMask(&ReplyHeadersMask, ReplyHeadersArr, countof(ReplyHeadersArr));
62e76326 306
8abf232c 307 httpHeaderCalcMask(&ReplyHeadersMask, GeneralHeadersArr, countof(GeneralHeadersArr));
62e76326 308
8abf232c 309 httpHeaderCalcMask(&ReplyHeadersMask, EntityHeadersArr, countof(EntityHeadersArr));
62e76326 310
9bea1d5b 311 httpHeaderMaskInit(&RequestHeadersMask, 0);
62e76326 312
8abf232c 313 httpHeaderCalcMask(&RequestHeadersMask, RequestHeadersArr, countof(RequestHeadersArr));
62e76326 314
8abf232c 315 httpHeaderCalcMask(&RequestHeadersMask, GeneralHeadersArr, countof(GeneralHeadersArr));
62e76326 316
8abf232c 317 httpHeaderCalcMask(&RequestHeadersMask, EntityHeadersArr, countof(EntityHeadersArr));
62e76326 318
dcf3665b
AJ
319 httpHeaderCalcMask(&HopByHopHeadersMask, HopByHopHeadersArr, countof(HopByHopHeadersArr));
320
9bea1d5b 321 /* init header stats */
322 assert(HttpHeaderStatCount == hoReply + 1);
62e76326 323
9bea1d5b 324 for (i = 0; i < HttpHeaderStatCount; i++)
62e76326 325 httpHeaderStatInit(HttpHeaderStats + i, HttpHeaderStats[i].label);
326
9bea1d5b 327 HttpHeaderStats[hoRequest].owner_mask = &RequestHeadersMask;
62e76326 328
9bea1d5b 329 HttpHeaderStats[hoReply].owner_mask = &ReplyHeadersMask;
62e76326 330
86aebcda 331#if USE_HTCP
62e76326 332
9bea1d5b 333 HttpHeaderStats[hoHtcpReply].owner_mask = &ReplyHeadersMask;
62e76326 334
86aebcda 335#endif
9bea1d5b 336 /* init dependent modules */
337 httpHdrCcInitModule();
62e76326 338
43ae1d95 339 httpHdrScInitModule();
ea391f18
FC
340
341 httpHeaderRegisterWithCacheManager();
62ee09ca 342}
43ae1d95 343
7021844c 344void
9bea1d5b 345httpHeaderCleanModule(void)
7021844c 346{
9bea1d5b 347 httpHeaderDestroyFieldsInfo(Headers, HDR_ENUM_END);
348 Headers = NULL;
349 httpHdrCcCleanModule();
43ae1d95 350 httpHdrScCleanModule();
7021844c 351}
352
12cf1be2 353static void
9bea1d5b 354httpHeaderStatInit(HttpHeaderStat * hs, const char *label)
12cf1be2 355{
9bea1d5b 356 assert(hs);
357 assert(label);
358 memset(hs, 0, sizeof(HttpHeaderStat));
359 hs->label = label;
360 statHistEnumInit(&hs->hdrUCountDistr, 32); /* not a real enum */
361 statHistEnumInit(&hs->fieldTypeDistr, HDR_ENUM_END);
362 statHistEnumInit(&hs->ccTypeDistr, CC_ENUM_END);
43ae1d95 363 statHistEnumInit(&hs->scTypeDistr, SC_ENUM_END);
cb69b4c7 364}
365
cb69b4c7 366/*
367 * HttpHeader Implementation
368 */
369
75faaa7a 370HttpHeader::HttpHeader() : owner (hoNone), len (0)
cb69b4c7 371{
75faaa7a 372 httpHeaderMaskInit(&mask, 0);
373}
374
375HttpHeader::HttpHeader(http_hdr_owner_type const &anOwner) : owner (anOwner), len (0)
376{
75faaa7a 377 assert(anOwner > hoNone && anOwner <= hoReply);
efc26e8e 378 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner);
75faaa7a 379 httpHeaderMaskInit(&mask, 0);
380}
381
382HttpHeader::~HttpHeader()
383{
519e0948 384 clean();
cb69b4c7 385}
386
387void
519e0948 388HttpHeader::clean()
cb69b4c7 389{
9bea1d5b 390 HttpHeaderPos pos = HttpHeaderInitPos;
391 HttpHeaderEntry *e;
392
519e0948 393 assert(owner > hoNone && owner <= hoReply);
efc26e8e 394 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner);
9bea1d5b 395
3a9cf2d5 396 PROF_start(HttpHeaderClean);
397
51328da8 398 /*
519e0948 399 * An unfortunate bug. The entries array is initialized
51328da8 400 * such that count is set to zero. httpHeaderClean() seems to
401 * be called both when 'hdr' is created, and destroyed. Thus,
402 * we accumulate a large number of zero counts for 'hdr' before
403 * it is ever used. Can't think of a good way to fix it, except
404 * adding a state variable that indicates whether or not 'hdr'
405 * has been used. As a hack, just never count zero-sized header
406 * arrays.
407 */
408
519e0948 409 if (0 != entries.count)
410 statHistCount(&HttpHeaderStats[owner].hdrUCountDistr, entries.count);
51328da8 411
519e0948 412 HttpHeaderStats[owner].destroyedCount++;
51328da8 413
519e0948 414 HttpHeaderStats[owner].busyDestroyedCount += entries.count > 0;
62e76326 415
a9925b40 416 while ((e = getEntry(&pos))) {
62e76326 417 /* tmp hack to try to avoid coredumps */
418
419 if (e->id < 0 || e->id >= HDR_ENUM_END) {
efc26e8e 420 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos << "] is invalid (" << e->id << "). Ignored.");
62e76326 421 } else {
519e0948 422 statHistCount(&HttpHeaderStats[owner].fieldTypeDistr, e->id);
eede25e7 423 /* yes, this deletion leaves us in an inconsistent state */
424 delete e;
62e76326 425 }
12cf1be2 426 }
519e0948 427 entries.clean();
428 httpHeaderMaskInit(&mask, 0);
3a9cf2d5 429 PROF_stop(HttpHeaderClean);
cb69b4c7 430}
431
2246b732 432/* append entries (also see httpHeaderUpdate) */
433void
a9925b40 434HttpHeader::append(const HttpHeader * src)
2246b732 435{
9bea1d5b 436 const HttpHeaderEntry *e;
437 HttpHeaderPos pos = HttpHeaderInitPos;
a9925b40 438 assert(src);
439 assert(src != this);
efc26e8e 440 debugs(55, 7, "appending hdr: " << this << " += " << src);
9bea1d5b 441
a9925b40 442 while ((e = src->getEntry(&pos))) {
eede25e7 443 addEntry(e->clone());
2246b732 444 }
445}
446
d8b249ef 447/* use fresh entries to replace old ones */
cb69b4c7 448void
9bea1d5b 449httpHeaderUpdate(HttpHeader * old, const HttpHeader * fresh, const HttpHeaderMask * denied_mask)
924f73bc 450{
451 assert (old);
452 old->update (fresh, denied_mask);
453}
454
455void
456HttpHeader::update (HttpHeader const *fresh, HttpHeaderMask const *denied_mask)
cb69b4c7 457{
9bea1d5b 458 const HttpHeaderEntry *e;
459 HttpHeaderPos pos = HttpHeaderInitPos;
a9925b40 460 assert(fresh);
924f73bc 461 assert(this != fresh);
9bea1d5b 462
a9925b40 463 while ((e = fresh->getEntry(&pos))) {
62e76326 464 /* deny bad guys (ok to check for HDR_OTHER) here */
465
466 if (denied_mask && CBIT_TEST(*denied_mask, e->id))
467 continue;
468
ba9fb01d 469 if (e->id != HDR_OTHER)
470 delById(e->id);
471 else
30abd221 472 delByName(e->name.buf());
9917847f 473 }
474
475 pos = HttpHeaderInitPos;
476 while ((e = fresh->getEntry(&pos))) {
477 /* deny bad guys (ok to check for HDR_OTHER) here */
478
479 if (denied_mask && CBIT_TEST(*denied_mask, e->id))
480 continue;
481
482 debugs(55, 7, "Updating header '" << HeadersAttrs[e->id].name << "' in cached entry");
62e76326 483
eede25e7 484 addEntry(e->clone());
cb69b4c7 485 }
cb69b4c7 486}
487
488/* just handy in parsing: resets and returns false */
9f4d4f65 489int
a9925b40 490HttpHeader::reset()
2ac76861 491{
d71e8477 492 http_hdr_owner_type ho;
a9925b40 493 ho = owner;
494 clean();
495 *this = HttpHeader(ho);
9bea1d5b 496 return 0;
cb69b4c7 497}
498
cb69b4c7 499int
a9925b40 500HttpHeader::parse(const char *header_start, const char *header_end)
cb69b4c7 501{
52d3f198 502 const char *field_ptr = header_start;
503 HttpHeaderEntry *e, *e2;
9bea1d5b 504
3a9cf2d5 505 PROF_start(HttpHeaderParse);
506
9bea1d5b 507 assert(header_start && header_end);
efc26e8e 508 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl << getStringPrefix(header_start, header_end));
a9925b40 509 HttpHeaderStats[owner].parsedCount++;
62e76326 510
52d3f198 511 if (memchr(header_start, '\0', header_end - header_start)) {
efc26e8e 512 debugs(55, 1, "WARNING: HTTP header contains NULL characters {" <<
513 getStringPrefix(header_start, header_end) << "}");
3a9cf2d5 514 goto reset;
52d3f198 515 }
516
517 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
518 * continuation lines start with a (single) space or tab */
519 while (field_ptr < header_end) {
520 const char *field_start = field_ptr;
62e76326 521 const char *field_end;
62e76326 522
523 do {
52d3f198 524 const char *this_line = field_ptr;
525 field_ptr = (const char *)memchr(field_ptr, '\n', header_end - field_ptr);
526
527 if (!field_ptr)
3a9cf2d5 528 goto reset; /* missing <LF> */
52d3f198 529
530 field_end = field_ptr;
531
532 field_ptr++; /* Move to next line */
533
534 if (field_end > this_line && field_end[-1] == '\r') {
535 field_end--; /* Ignore CR LF */
536 /* Ignore CR CR LF in relaxed mode */
537
538 if (Config.onoff.relaxed_header_parser && field_end > this_line + 1 && field_end[-1] == '\r') {
efc26e8e 539 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
540 "WARNING: Double CR characters in HTTP header {" << getStringPrefix(field_start, field_end) << "}");
52d3f198 541 field_end--;
542 }
543 }
544
545 /* Barf on stray CR characters */
546 if (memchr(this_line, '\r', field_end - this_line)) {
efc26e8e 547 debugs(55, 1, "WARNING: suspicious CR characters in HTTP header {" <<
548 getStringPrefix(field_start, field_end) << "}");
52d3f198 549
550 if (Config.onoff.relaxed_header_parser) {
551 char *p = (char *) this_line; /* XXX Warning! This destroys original header content and violates specifications somewhat */
552
553 while ((p = (char *)memchr(p, '\r', field_end - p)) != NULL)
554 *p++ = ' ';
555 } else
3a9cf2d5 556 goto reset;
52d3f198 557 }
558
559 if (this_line + 1 == field_end && this_line > field_start) {
efc26e8e 560 debugs(55, 1, "WARNING: Blank continuation line in HTTP header {" <<
561 getStringPrefix(header_start, header_end) << "}");
3a9cf2d5 562 goto reset;
52d3f198 563 }
564 } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t'));
565
566 if (field_start == field_end) {
567 if (field_ptr < header_end) {
efc26e8e 568 debugs(55, 1, "WARNING: unparseable HTTP header field near {" <<
569 getStringPrefix(field_start, header_end) << "}");
3a9cf2d5 570 goto reset;
52d3f198 571 }
572
573 break; /* terminating blank line */
574 }
62e76326 575
cdce6c61 576 if ((e = HttpHeaderEntry::parse(field_start, field_end)) == NULL) {
efc26e8e 577 debugs(55, 1, "WARNING: unparseable HTTP header field {" <<
578 getStringPrefix(field_start, field_end) << "}");
579 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
580 " in {" << getStringPrefix(header_start, header_end) << "}");
52d3f198 581
582 if (Config.onoff.relaxed_header_parser)
583 continue;
584 else
3a9cf2d5 585 goto reset;
47ac2ebe 586 }
62e76326 587
a9925b40 588 if (e->id == HDR_CONTENT_LENGTH && (e2 = findEntry(e->id)) != NULL) {
30abd221 589 if (e->value.cmp(e2->value.buf()) != 0) {
3e62bd58 590 int64_t l1, l2;
efc26e8e 591 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
592 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start, header_end) << "}");
9331e4b4 593
594 if (!Config.onoff.relaxed_header_parser) {
eede25e7 595 delete e;
3a9cf2d5 596 goto reset;
9331e4b4 597 }
598
3e62bd58 599 if (!httpHeaderParseOffset(e->value.buf(), &l1)) {
30abd221 600 debugs(55, 1, "WARNING: Unparseable content-length '" << e->value.buf() << "'");
eede25e7 601 delete e;
9331e4b4 602 continue;
3e62bd58 603 } else if (!httpHeaderParseOffset(e2->value.buf(), &l2)) {
30abd221 604 debugs(55, 1, "WARNING: Unparseable content-length '" << e2->value.buf() << "'");
a9925b40 605 delById(e2->id);
9331e4b4 606 } else if (l1 > l2) {
a9925b40 607 delById(e2->id);
9331e4b4 608 } else {
eede25e7 609 delete e;
9331e4b4 610 continue;
611 }
52d3f198 612 } else {
efc26e8e 613 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
614 "NOTICE: found double content-length header");
b3123159 615
616 if (Config.onoff.relaxed_header_parser) {
eede25e7 617 delete e;
b3123159 618 continue;
619 } else {
eede25e7 620 delete e;
3a9cf2d5 621 goto reset;
b3123159 622 }
52d3f198 623 }
624 }
62e76326 625
30abd221 626 if (e->id == HDR_OTHER && stringHasWhitespace(e->name.buf())) {
bf8fe701 627 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
628 "WARNING: found whitespace in HTTP header name {" <<
629 getStringPrefix(field_start, field_end) << "}");
47ac2ebe 630
52d3f198 631 if (!Config.onoff.relaxed_header_parser) {
eede25e7 632 delete e;
3a9cf2d5 633 goto reset;
52d3f198 634 }
635 }
62e76326 636
a9925b40 637 addEntry(e);
cb69b4c7 638 }
62e76326 639
3a9cf2d5 640 PROF_stop(HttpHeaderParse);
9bea1d5b 641 return 1; /* even if no fields where found, it is a valid header */
3a9cf2d5 642reset:
643 PROF_stop(HttpHeaderParse);
644 return reset();
cb69b4c7 645}
646
99edd1c3 647/* packs all the entries using supplied packer */
cb69b4c7 648void
a9925b40 649HttpHeader::packInto(Packer * p) const
cb69b4c7 650{
9bea1d5b 651 HttpHeaderPos pos = HttpHeaderInitPos;
652 const HttpHeaderEntry *e;
a9925b40 653 assert(p);
efc26e8e 654 debugs(55, 7, "packing hdr: (" << this << ")");
9bea1d5b 655 /* pack all entries one by one */
a9925b40 656 while ((e = getEntry(&pos)))
eede25e7 657 e->packInto(p);
35b88ed2 658
659 /* Pack in the "special" entries */
660
661 /* Cache-Control */
cb69b4c7 662}
663
664/* returns next valid entry */
99edd1c3 665HttpHeaderEntry *
a9925b40 666HttpHeader::getEntry(HttpHeaderPos * pos) const
cb69b4c7 667{
a9925b40 668 assert(pos);
669 assert(*pos >= HttpHeaderInitPos && *pos < (ssize_t)entries.count);
62e76326 670
a9925b40 671 for ((*pos)++; *pos < (ssize_t)entries.count; (*pos)++) {
672 if (entries.items[*pos])
673 return (HttpHeaderEntry*)entries.items[*pos];
cb69b4c7 674 }
62e76326 675
9bea1d5b 676 return NULL;
cb69b4c7 677}
678
679/*
d8b249ef 680 * returns a pointer to a specified entry if any
681 * note that we return one entry so it does not make much sense to ask for
682 * "list" headers
cb69b4c7 683 */
de336bbe 684HttpHeaderEntry *
a9925b40 685HttpHeader::findEntry(http_hdr_type id) const
cb69b4c7 686{
9bea1d5b 687 HttpHeaderPos pos = HttpHeaderInitPos;
688 HttpHeaderEntry *e;
9bea1d5b 689 assert_eid(id);
690 assert(!CBIT_TEST(ListHeadersMask, id));
691
692 /* check mask first */
62e76326 693
a9925b40 694 if (!CBIT_TEST(mask, id))
62e76326 695 return NULL;
696
9bea1d5b 697 /* looks like we must have it, do linear search */
a9925b40 698 while ((e = getEntry(&pos))) {
62e76326 699 if (e->id == id)
700 return e;
cb69b4c7 701 }
62e76326 702
9bea1d5b 703 /* hm.. we thought it was there, but it was not found */
704 assert(0);
62e76326 705
9bea1d5b 706 return NULL; /* not reached */
cb69b4c7 707}
708
a622fff0 709/*
710 * same as httpHeaderFindEntry
711 */
a9925b40 712HttpHeaderEntry *
713HttpHeader::findLastEntry(http_hdr_type id) const
a622fff0 714{
9bea1d5b 715 HttpHeaderPos pos = HttpHeaderInitPos;
716 HttpHeaderEntry *e;
717 HttpHeaderEntry *result = NULL;
9bea1d5b 718 assert_eid(id);
719 assert(!CBIT_TEST(ListHeadersMask, id));
720
721 /* check mask first */
62e76326 722
a9925b40 723 if (!CBIT_TEST(mask, id))
62e76326 724 return NULL;
725
9bea1d5b 726 /* looks like we must have it, do linear search */
a9925b40 727 while ((e = getEntry(&pos))) {
62e76326 728 if (e->id == id)
729 result = e;
a622fff0 730 }
62e76326 731
9bea1d5b 732 assert(result); /* must be there! */
733 return result;
a622fff0 734}
735
cb69b4c7 736/*
d8b249ef 737 * deletes all fields with a given name if any, returns #fields deleted;
cb69b4c7 738 */
2ac76861 739int
a9925b40 740HttpHeader::delByName(const char *name)
cb69b4c7 741{
9bea1d5b 742 int count = 0;
743 HttpHeaderPos pos = HttpHeaderInitPos;
744 HttpHeaderEntry *e;
a9925b40 745 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
efc26e8e 746 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
62e76326 747
a9925b40 748 while ((e = getEntry(&pos))) {
30abd221 749 if (!e->name.caseCmp(name))
ba9fb01d 750 delAt(pos, count);
751 else
a9925b40 752 CBIT_SET(mask, e->id);
cb69b4c7 753 }
62e76326 754
9bea1d5b 755 return count;
cb69b4c7 756}
757
2246b732 758/* deletes all entries with a given id, returns the #entries deleted */
759int
a9925b40 760HttpHeader::delById(http_hdr_type id)
d8b249ef 761{
9bea1d5b 762 int count = 0;
763 HttpHeaderPos pos = HttpHeaderInitPos;
764 HttpHeaderEntry *e;
efc26e8e 765 debugs(55, 8, this << " del-by-id " << id);
9bea1d5b 766 assert_eid(id);
e6ca7a9b 767 assert(id != HDR_OTHER); /* does not make sense */
62e76326 768
a9925b40 769 if (!CBIT_TEST(mask, id))
62e76326 770 return 0;
771
a9925b40 772 while ((e = getEntry(&pos))) {
ba9fb01d 773 if (e->id == id)
774 delAt(pos, count);
d8b249ef 775 }
62e76326 776
a9925b40 777 CBIT_CLR(mask, id);
9bea1d5b 778 assert(count);
779 return count;
d8b249ef 780}
d8b249ef 781
cb69b4c7 782/*
783 * deletes an entry at pos and leaves a gap; leaving a gap makes it
784 * possible to iterate(search) and delete fields at the same time
ba9fb01d 785 * NOTE: Does not update the header mask. Caller must follow up with
786 * a call to refreshMask() if headers_deleted was incremented.
cb69b4c7 787 */
2246b732 788void
ba9fb01d 789HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
cb69b4c7 790{
9bea1d5b 791 HttpHeaderEntry *e;
a9925b40 792 assert(pos >= HttpHeaderInitPos && pos < (ssize_t)entries.count);
793 e = (HttpHeaderEntry*)entries.items[pos];
794 entries.items[pos] = NULL;
9bea1d5b 795 /* decrement header length, allow for ": " and crlf */
a9925b40 796 len -= e->name.size() + 2 + e->value.size() + 2;
797 assert(len >= 0);
eede25e7 798 delete e;
ba9fb01d 799 ++headers_deleted;
cb69b4c7 800}
801
394499bd 802/*
803 * Compacts the header storage
804 */
805void
806HttpHeader::compact()
807{
808 entries.prune(NULL);
809}
810
ba9fb01d 811/*
812 * Refreshes the header mask. Required after delAt() calls.
813 */
814void
815HttpHeader::refreshMask()
816{
817 httpHeaderMaskInit(&mask, 0);
818 debugs(55, 7, "refreshing the mask in hdr " << this);
819 HttpHeaderPos pos = HttpHeaderInitPos;
820 while (HttpHeaderEntry *e = getEntry(&pos)) {
821 CBIT_SET(mask, e->id);
822 }
823}
99edd1c3 824
62e76326 825/* appends an entry;
eede25e7 826 * does not call e->clone() so one should not reuse "*e"
cb69b4c7 827 */
99edd1c3 828void
a9925b40 829HttpHeader::addEntry(HttpHeaderEntry * e)
cb69b4c7 830{
a9925b40 831 assert(e);
9bea1d5b 832 assert_eid(e->id);
59c50530 833 assert(e->name.size());
9bea1d5b 834
a9925b40 835 debugs(55, 9, this << " adding entry: " << e->id << " at " <<
836 entries.count);
62e76326 837
a9925b40 838 if (CBIT_TEST(mask, e->id))
62e76326 839 Headers[e->id].stat.repCount++;
9bea1d5b 840 else
a9925b40 841 CBIT_SET(mask, e->id);
62e76326 842
a9925b40 843 entries.push_back(e);
62e76326 844
9bea1d5b 845 /* increment header length, allow for ": " and crlf */
a9925b40 846 len += e->name.size() + 2 + e->value.size() + 2;
cb69b4c7 847}
848
bbe58ab5 849/* inserts an entry;
eede25e7 850 * does not call e->clone() so one should not reuse "*e"
bbe58ab5 851 */
852void
a9925b40 853HttpHeader::insertEntry(HttpHeaderEntry * e)
bbe58ab5 854{
a9925b40 855 assert(e);
bbe58ab5 856 assert_eid(e->id);
857
a9925b40 858 debugs(55, 7, this << " adding entry: " << e->id << " at " <<
859 entries.count);
bbe58ab5 860
a9925b40 861 if (CBIT_TEST(mask, e->id))
bbe58ab5 862 Headers[e->id].stat.repCount++;
863 else
a9925b40 864 CBIT_SET(mask, e->id);
bbe58ab5 865
a9925b40 866 entries.insert(e);
bbe58ab5 867
868 /* increment header length, allow for ": " and crlf */
a9925b40 869 len += e->name.size() + 2 + e->value.size() + 2;
bbe58ab5 870}
871
35b88ed2 872bool
30abd221 873HttpHeader::getList(http_hdr_type id, String *s) const
35b88ed2 874{
875 HttpHeaderEntry *e;
876 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 877 debugs(55, 9, this << " joining for id " << id);
35b88ed2 878 /* only fields from ListHeaders array can be "listed" */
879 assert(CBIT_TEST(ListHeadersMask, id));
880
881 if (!CBIT_TEST(mask, id))
882 return false;
883
884 while ((e = getEntry(&pos))) {
885 if (e->id == id)
30abd221 886 strListAdd(s, e->value.buf(), ',');
35b88ed2 887 }
888
889 /*
890 * note: we might get an empty (size==0) string if there was an "empty"
891 * header. This results in an empty length String, which may have a NULL
892 * buffer.
893 */
894 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
895 if (!s->size())
896 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
897 else
898 debugs(55, 6, this << ": joined for id " << id << ": " << s);
899
900 return true;
901}
902
99edd1c3 903/* return a list of entries with the same id separated by ',' and ws */
30abd221 904String
a9925b40 905HttpHeader::getList(http_hdr_type id) const
cb69b4c7 906{
9bea1d5b 907 HttpHeaderEntry *e;
908 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 909 debugs(55, 9, this << "joining for id " << id);
9bea1d5b 910 /* only fields from ListHeaders array can be "listed" */
911 assert(CBIT_TEST(ListHeadersMask, id));
62e76326 912
a9925b40 913 if (!CBIT_TEST(mask, id))
30abd221 914 return String();
650c4b88 915
30abd221 916 String s;
62e76326 917
a9925b40 918 while ((e = getEntry(&pos))) {
62e76326 919 if (e->id == id)
30abd221 920 strListAdd(&s, e->value.buf(), ',');
d35b9a94 921 }
62e76326 922
9bea1d5b 923 /*
948078e3 924 * note: we might get an empty (size==0) string if there was an "empty"
925 * header. This results in an empty length String, which may have a NULL
926 * buffer.
9bea1d5b 927 */
948078e3 928 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
528b2c61 929 if (!s.size())
948078e3 930 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
931 else
932 debugs(55, 6, this << ": joined for id " << id << ": " << s);
62e76326 933
9bea1d5b 934 return s;
cb69b4c7 935}
936
f66a9ef4 937/* return a string or list of entries with the same id separated by ',' and ws */
30abd221 938String
a9925b40 939HttpHeader::getStrOrList(http_hdr_type id) const
f66a9ef4 940{
c7327fa0 941 HttpHeaderEntry *e;
9bea1d5b 942
943 if (CBIT_TEST(ListHeadersMask, id))
a9925b40 944 return getList(id);
62e76326 945
a9925b40 946 if ((e = findEntry(id)))
62e76326 947 return e->value;
948
30abd221 949 return String();
f66a9ef4 950}
951
952/*
bd412580 953 * Returns the value of the specified header.
f66a9ef4 954 */
30abd221 955String
a9925b40 956HttpHeader::getByName(const char *name) const
f66a9ef4 957{
9bea1d5b 958 http_hdr_type id;
959 HttpHeaderPos pos = HttpHeaderInitPos;
960 HttpHeaderEntry *e;
9bea1d5b 961
9bea1d5b 962 assert(name);
963
964 /* First try the quick path */
965 id = httpHeaderIdByNameDef(name, strlen(name));
62e76326 966
9bea1d5b 967 if (id != -1)
a9925b40 968 return getStrOrList(id);
9bea1d5b 969
30abd221 970 String result;
650c4b88 971
9bea1d5b 972 /* Sorry, an unknown header name. Do linear search */
a9925b40 973 while ((e = getEntry(&pos))) {
30abd221 974 if (e->id == HDR_OTHER && e->name.caseCmp(name) == 0) {
975 strListAdd(&result, e->value.buf(), ',');
62e76326 976 }
f66a9ef4 977 }
62e76326 978
9bea1d5b 979 return result;
f66a9ef4 980}
cb69b4c7 981
14b463aa 982/*
372fdfbf 983 * Returns a the value of the specified list member, if any.
14b463aa 984 */
30abd221 985String
a9925b40 986HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
14b463aa 987{
30abd221 988 String header;
14b463aa 989 const char *pos = NULL;
990 const char *item;
991 int ilen;
992 int mlen = strlen(member);
993
14b463aa 994 assert(name);
995
a9925b40 996 header = getByName(name);
14b463aa 997
30abd221 998 String result;
650c4b88 999
14b463aa 1000 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1001 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1002 result.append(item + mlen + 1, ilen - mlen - 1);
1003 break;
1004 }
14b463aa 1005 }
62e76326 1006
14b463aa 1007 return result;
1008}
1009
1010/*
1011 * returns a the value of the specified list member, if any.
1012 */
30abd221 1013String
a9925b40 1014HttpHeader::getListMember(http_hdr_type id, const char *member, const char separator) const
14b463aa 1015{
30abd221 1016 String header;
14b463aa 1017 const char *pos = NULL;
1018 const char *item;
1019 int ilen;
1020 int mlen = strlen(member);
1021
14b463aa 1022 assert(id >= 0);
1023
a9925b40 1024 header = getStrOrList(id);
30abd221 1025 String result;
14b463aa 1026
1027 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1028 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1029 result.append(item + mlen + 1, ilen - mlen - 1);
1030 break;
1031 }
14b463aa 1032 }
62e76326 1033
30abd221 1034 header.clean();
14b463aa 1035 return result;
1036}
1037
cb69b4c7 1038/* test if a field is present */
2ac76861 1039int
a9925b40 1040HttpHeader::has(http_hdr_type id) const
cb69b4c7 1041{
9bea1d5b 1042 assert_eid(id);
1043 assert(id != HDR_OTHER);
efc26e8e 1044 debugs(55, 9, this << " lookup for " << id);
a9925b40 1045 return CBIT_TEST(mask, id);
cb69b4c7 1046}
1047
1048void
a9925b40 1049HttpHeader::putInt(http_hdr_type id, int number)
cb69b4c7 1050{
9bea1d5b 1051 assert_eid(id);
1052 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
1053 assert(number >= 0);
eede25e7 1054 addEntry(new HttpHeaderEntry(id, NULL, xitoa(number)));
cb69b4c7 1055}
1056
47f6e231 1057void
1058HttpHeader::putInt64(http_hdr_type id, int64_t number)
1059{
1060 assert_eid(id);
1061 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1062 assert(number >= 0);
1063 addEntry(new HttpHeaderEntry(id, NULL, xint64toa(number)));
1064}
1065
cb69b4c7 1066void
a9925b40 1067HttpHeader::putTime(http_hdr_type id, time_t htime)
cb69b4c7 1068{
9bea1d5b 1069 assert_eid(id);
1070 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
a1d6870f 1071 assert(htime >= 0);
eede25e7 1072 addEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
cb69b4c7 1073}
2ac76861 1074
bbe58ab5 1075void
a9925b40 1076HttpHeader::insertTime(http_hdr_type id, time_t htime)
bbe58ab5 1077{
1078 assert_eid(id);
1079 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
1080 assert(htime >= 0);
eede25e7 1081 insertEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
bbe58ab5 1082}
1083
cb69b4c7 1084void
a9925b40 1085HttpHeader::putStr(http_hdr_type id, const char *str)
cb69b4c7 1086{
9bea1d5b 1087 assert_eid(id);
1088 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
1089 assert(str);
eede25e7 1090 addEntry(new HttpHeaderEntry(id, NULL, str));
cb69b4c7 1091}
1092
63259c34 1093void
a9925b40 1094HttpHeader::putAuth(const char *auth_scheme, const char *realm)
63259c34 1095{
a9925b40 1096 assert(auth_scheme && realm);
1097 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
63259c34 1098}
1099
99edd1c3 1100void
a9925b40 1101HttpHeader::putCc(const HttpHdrCc * cc)
99edd1c3 1102{
9bea1d5b 1103 MemBuf mb;
1104 Packer p;
a9925b40 1105 assert(cc);
9bea1d5b 1106 /* remove old directives if any */
a9925b40 1107 delById(HDR_CACHE_CONTROL);
9bea1d5b 1108 /* pack into mb */
2fe7eff9 1109 mb.init();
9bea1d5b 1110 packerToMemInit(&p, &mb);
1111 httpHdrCcPackInto(cc, &p);
1112 /* put */
eede25e7 1113 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL, NULL, mb.buf));
9bea1d5b 1114 /* cleanup */
1115 packerClean(&p);
2fe7eff9 1116 mb.clean();
99edd1c3 1117}
1118
d192d11f 1119void
a9925b40 1120HttpHeader::putContRange(const HttpHdrContRange * cr)
d192d11f 1121{
9bea1d5b 1122 MemBuf mb;
1123 Packer p;
a9925b40 1124 assert(cr);
9bea1d5b 1125 /* remove old directives if any */
a9925b40 1126 delById(HDR_CONTENT_RANGE);
9bea1d5b 1127 /* pack into mb */
2fe7eff9 1128 mb.init();
9bea1d5b 1129 packerToMemInit(&p, &mb);
1130 httpHdrContRangePackInto(cr, &p);
1131 /* put */
eede25e7 1132 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE, NULL, mb.buf));
9bea1d5b 1133 /* cleanup */
1134 packerClean(&p);
2fe7eff9 1135 mb.clean();
d192d11f 1136}
1137
1138void
a9925b40 1139HttpHeader::putRange(const HttpHdrRange * range)
d192d11f 1140{
9bea1d5b 1141 MemBuf mb;
1142 Packer p;
a9925b40 1143 assert(range);
9bea1d5b 1144 /* remove old directives if any */
a9925b40 1145 delById(HDR_RANGE);
9bea1d5b 1146 /* pack into mb */
2fe7eff9 1147 mb.init();
9bea1d5b 1148 packerToMemInit(&p, &mb);
528b2c61 1149 range->packInto(&p);
9bea1d5b 1150 /* put */
eede25e7 1151 addEntry(new HttpHeaderEntry(HDR_RANGE, NULL, mb.buf));
9bea1d5b 1152 /* cleanup */
1153 packerClean(&p);
2fe7eff9 1154 mb.clean();
d192d11f 1155}
1156
43ae1d95 1157void
a9925b40 1158HttpHeader::putSc(HttpHdrSc *sc)
43ae1d95 1159{
1160 MemBuf mb;
1161 Packer p;
a9925b40 1162 assert(sc);
43ae1d95 1163 /* remove old directives if any */
a9925b40 1164 delById(HDR_RANGE);
43ae1d95 1165 /* pack into mb */
2fe7eff9 1166 mb.init();
43ae1d95 1167 packerToMemInit(&p, &mb);
1168 httpHdrScPackInto(sc, &p);
1169 /* put */
eede25e7 1170 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL, NULL, mb.buf));
43ae1d95 1171 /* cleanup */
1172 packerClean(&p);
2fe7eff9 1173 mb.clean();
43ae1d95 1174}
1175
cb69b4c7 1176/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1177void
a9925b40 1178HttpHeader::putExt(const char *name, const char *value)
cb69b4c7 1179{
9bea1d5b 1180 assert(name && value);
efc26e8e 1181 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
eede25e7 1182 addEntry(new HttpHeaderEntry(HDR_OTHER, name, value));
528b2c61 1183}
1184
1185int
a9925b40 1186HttpHeader::getInt(http_hdr_type id) const
528b2c61 1187{
9bea1d5b 1188 assert_eid(id);
1189 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
528b2c61 1190 HttpHeaderEntry *e;
62e76326 1191
a9925b40 1192 if ((e = findEntry(id)))
eede25e7 1193 return e->getInt();
62e76326 1194
528b2c61 1195 return -1;
7c525cc2 1196}
1197
47f6e231 1198int64_t
1199HttpHeader::getInt64(http_hdr_type id) const
1200{
1201 assert_eid(id);
1202 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1203 HttpHeaderEntry *e;
1204
1205 if ((e = findEntry(id)))
1206 return e->getInt64();
1207
1208 return -1;
1209}
1210
de336bbe 1211time_t
a9925b40 1212HttpHeader::getTime(http_hdr_type id) const
cb69b4c7 1213{
9bea1d5b 1214 HttpHeaderEntry *e;
1215 time_t value = -1;
1216 assert_eid(id);
1217 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
62e76326 1218
a9925b40 1219 if ((e = findEntry(id))) {
30abd221 1220 value = parse_rfc1123(e->value.buf());
62e76326 1221 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
d8b249ef 1222 }
62e76326 1223
9bea1d5b 1224 return value;
cb69b4c7 1225}
1226
99edd1c3 1227/* sync with httpHeaderGetLastStr */
de336bbe 1228const char *
a9925b40 1229HttpHeader::getStr(http_hdr_type id) const
cb69b4c7 1230{
9bea1d5b 1231 HttpHeaderEntry *e;
1232 assert_eid(id);
1233 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1234
a9925b40 1235 if ((e = findEntry(id))) {
62e76326 1236 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
30abd221 1237 return e->value.buf();
d8b249ef 1238 }
62e76326 1239
9bea1d5b 1240 return NULL;
cb69b4c7 1241}
1242
a622fff0 1243/* unusual */
1244const char *
a9925b40 1245HttpHeader::getLastStr(http_hdr_type id) const
a622fff0 1246{
9bea1d5b 1247 HttpHeaderEntry *e;
1248 assert_eid(id);
1249 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1250
a9925b40 1251 if ((e = findLastEntry(id))) {
62e76326 1252 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
30abd221 1253 return e->value.buf();
a622fff0 1254 }
62e76326 1255
9bea1d5b 1256 return NULL;
a622fff0 1257}
1258
7faf2bdb 1259HttpHdrCc *
a9925b40 1260HttpHeader::getCc() const
cb69b4c7 1261{
9bea1d5b 1262 HttpHdrCc *cc;
30abd221 1263 String s;
62e76326 1264
a9925b40 1265 if (!CBIT_TEST(mask, HDR_CACHE_CONTROL))
62e76326 1266 return NULL;
35b88ed2 1267 PROF_start(HttpHeader_getCc);
62e76326 1268
35b88ed2 1269 getList(HDR_CACHE_CONTROL, &s);
62e76326 1270
9bea1d5b 1271 cc = httpHdrCcParseCreate(&s);
62e76326 1272
a9925b40 1273 HttpHeaderStats[owner].ccParsedCount++;
62e76326 1274
9bea1d5b 1275 if (cc)
a9925b40 1276 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
62e76326 1277
9bea1d5b 1278 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL, s, !cc);
62e76326 1279
35b88ed2 1280 PROF_stop(HttpHeader_getCc);
62e76326 1281
9bea1d5b 1282 return cc;
cb69b4c7 1283}
1284
02922e76 1285HttpHdrRange *
a9925b40 1286HttpHeader::getRange() const
02922e76 1287{
9bea1d5b 1288 HttpHdrRange *r = NULL;
1289 HttpHeaderEntry *e;
1290 /* some clients will send "Request-Range" _and_ *matching* "Range"
1291 * who knows, some clients might send Request-Range only;
1292 * this "if" should work correctly in both cases;
1293 * hopefully no clients send mismatched headers! */
62e76326 1294
a9925b40 1295 if ((e = findEntry(HDR_RANGE)) ||
1296 (e = findEntry(HDR_REQUEST_RANGE))) {
62e76326 1297 r = HttpHdrRange::ParseCreate(&e->value);
1298 httpHeaderNoteParsedEntry(e->id, e->value, !r);
d192d11f 1299 }
62e76326 1300
9bea1d5b 1301 return r;
02922e76 1302}
1303
43ae1d95 1304HttpHdrSc *
a9925b40 1305HttpHeader::getSc() const
43ae1d95 1306{
a9925b40 1307 if (!CBIT_TEST(mask, HDR_SURROGATE_CONTROL))
43ae1d95 1308 return NULL;
1309
30abd221 1310 String s;
35b88ed2 1311
1312 (void) getList(HDR_SURROGATE_CONTROL, &s);
43ae1d95 1313
1314 HttpHdrSc *sc = httpHdrScParseCreate(&s);
1315
a9925b40 1316 HttpHeaderStats[owner].ccParsedCount++;
43ae1d95 1317
1318 if (sc)
a9925b40 1319 httpHdrScUpdateStats(sc, &HttpHeaderStats[owner].scTypeDistr);
43ae1d95 1320
1321 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL, s, !sc);
1322
1323 return sc;
1324}
1325
d76fcfa7 1326HttpHdrContRange *
a9925b40 1327HttpHeader::getContRange() const
d76fcfa7 1328{
9bea1d5b 1329 HttpHdrContRange *cr = NULL;
1330 HttpHeaderEntry *e;
62e76326 1331
a9925b40 1332 if ((e = findEntry(HDR_CONTENT_RANGE))) {
30abd221 1333 cr = httpHdrContRangeParseCreate(e->value.buf());
62e76326 1334 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
d8b249ef 1335 }
62e76326 1336
9bea1d5b 1337 return cr;
cb69b4c7 1338}
1339
99edd1c3 1340const char *
a9925b40 1341HttpHeader::getAuth(http_hdr_type id, const char *auth_scheme) const
cb69b4c7 1342{
9bea1d5b 1343 const char *field;
1344 int l;
a9925b40 1345 assert(auth_scheme);
1346 field = getStr(id);
62e76326 1347
9bea1d5b 1348 if (!field) /* no authorization field */
62e76326 1349 return NULL;
1350
9bea1d5b 1351 l = strlen(auth_scheme);
62e76326 1352
9bea1d5b 1353 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
62e76326 1354 return NULL;
1355
9bea1d5b 1356 field += l;
62e76326 1357
9bea1d5b 1358 if (!xisspace(*field)) /* wrong scheme */
62e76326 1359 return NULL;
1360
9bea1d5b 1361 /* skip white space */
1362 field += xcountws(field);
62e76326 1363
9bea1d5b 1364 if (!*field) /* no authorization cookie */
62e76326 1365 return NULL;
1366
9bea1d5b 1367 return base64_decode(field);
cb69b4c7 1368}
1369
a9771e51 1370ETag
a9925b40 1371HttpHeader::getETag(http_hdr_type id) const
a9771e51 1372{
9bea1d5b 1373 ETag etag =
62e76326 1374 {NULL, -1};
9bea1d5b 1375 HttpHeaderEntry *e;
1376 assert(Headers[id].type == ftETag); /* must be of an appropriate type */
62e76326 1377
a9925b40 1378 if ((e = findEntry(id)))
30abd221 1379 etagParseInit(&etag, e->value.buf());
62e76326 1380
9bea1d5b 1381 return etag;
a9771e51 1382}
1383
1384TimeOrTag
a9925b40 1385HttpHeader::getTimeOrTag(http_hdr_type id) const
a9771e51 1386{
9bea1d5b 1387 TimeOrTag tot;
1388 HttpHeaderEntry *e;
1389 assert(Headers[id].type == ftDate_1123_or_ETag); /* must be of an appropriate type */
1390 memset(&tot, 0, sizeof(tot));
62e76326 1391
a9925b40 1392 if ((e = findEntry(id))) {
30abd221 1393 const char *str = e->value.buf();
62e76326 1394 /* try as an ETag */
1395
1396 if (etagParseInit(&tot.tag, str)) {
1397 tot.valid = tot.tag.str != NULL;
1398 tot.time = -1;
1399 } else {
1400 /* or maybe it is time? */
1401 tot.time = parse_rfc1123(str);
1402 tot.valid = tot.time >= 0;
1403 tot.tag.str = NULL;
1404 }
a9771e51 1405 }
62e76326 1406
9bea1d5b 1407 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
1408 return tot;
a9771e51 1409}
1410
cb69b4c7 1411/*
1412 * HttpHeaderEntry
1413 */
1414
eede25e7 1415HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId, const char *aName, const char *aValue)
cb69b4c7 1416{
eede25e7 1417 assert_eid(anId);
1418 id = anId;
62e76326 1419
9bea1d5b 1420 if (id != HDR_OTHER)
eede25e7 1421 name = Headers[id].name;
9bea1d5b 1422 else
eede25e7 1423 name = aName;
62e76326 1424
eede25e7 1425 value = aValue;
62e76326 1426
9bea1d5b 1427 Headers[id].stat.aliveCount++;
62e76326 1428
30abd221 1429 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name.buf() << " : " << value.buf());
eede25e7 1430}
62e76326 1431
eede25e7 1432HttpHeaderEntry::~HttpHeaderEntry()
2ac76861 1433{
eede25e7 1434 assert_eid(id);
30abd221 1435 debugs(55, 9, "destroying entry " << this << ": '" << name.buf() << ": " << value.buf() << "'");
1436 /* clean name if needed */
1437
1438 if (id == HDR_OTHER)
1439 name.clean();
1440
1441 value.clean();
62e76326 1442
eede25e7 1443 assert(Headers[id].stat.aliveCount);
62e76326 1444
eede25e7 1445 Headers[id].stat.aliveCount--;
62e76326 1446
eede25e7 1447 id = HDR_BAD_HDR;
cb69b4c7 1448}
1449
eede25e7 1450/* parses and inits header entry, returns true/false */
cdce6c61 1451HttpHeaderEntry *
eede25e7 1452HttpHeaderEntry::parse(const char *field_start, const char *field_end)
cb69b4c7 1453{
9bea1d5b 1454 /* note: name_start == field_start */
52d3f198 1455 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
1456 int name_len = name_end ? name_end - field_start : 0;
9bea1d5b 1457 const char *value_start = field_start + name_len + 1; /* skip ':' */
1458 /* note: value_end == field_end */
1459
1460 HeaderEntryParsedCount++;
1461
1462 /* do we have a valid field name within this field? */
62e76326 1463
9bea1d5b 1464 if (!name_len || name_end > field_end)
cdce6c61 1465 return NULL;
62e76326 1466
67746eea 1467 if (name_len > 65534) {
1468 /* String must be LESS THAN 64K and it adds a terminating NULL */
efc26e8e 1469 debugs(55, 1, "WARNING: ignoring header name of " << name_len << " bytes");
cdce6c61 1470 return NULL;
25acfb53 1471 }
62e76326 1472
52d3f198 1473 if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) {
bf8fe701 1474 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
1475 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end) << "'");
52d3f198 1476
1477 while (name_len > 0 && xisspace(field_start[name_len - 1]))
1478 name_len--;
1479
1480 if (!name_len)
cdce6c61 1481 return NULL;
52d3f198 1482 }
1483
9bea1d5b 1484 /* now we know we can parse it */
62e76326 1485
bf8fe701 1486 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end) << "'");
62e76326 1487
9bea1d5b 1488 /* is it a "known" field? */
cdce6c61 1489 http_hdr_type id = httpHeaderIdByName(field_start, name_len, Headers, HDR_ENUM_END);
1490
30abd221 1491 String name;
cdce6c61 1492
30abd221 1493 String value;
62e76326 1494
9bea1d5b 1495 if (id < 0)
62e76326 1496 id = HDR_OTHER;
1497
9bea1d5b 1498 assert_eid(id);
62e76326 1499
9bea1d5b 1500 /* set field name */
1501 if (id == HDR_OTHER)
eede25e7 1502 name.limitInit(field_start, name_len);
9bea1d5b 1503 else
eede25e7 1504 name = Headers[id].name;
62e76326 1505
9bea1d5b 1506 /* trim field value */
1507 while (value_start < field_end && xisspace(*value_start))
62e76326 1508 value_start++;
1509
52d3f198 1510 while (value_start < field_end && xisspace(field_end[-1]))
1511 field_end--;
1512
67746eea 1513 if (field_end - value_start > 65534) {
1514 /* String must be LESS THAN 64K and it adds a terminating NULL */
30abd221 1515 debugs(55, 1, "WARNING: ignoring '" << name.buf() << "' header of " << (field_end - value_start) << " bytes");
62e76326 1516
eede25e7 1517 if (id == HDR_OTHER)
30abd221 1518 name.clean();
62e76326 1519
cdce6c61 1520 return NULL;
25acfb53 1521 }
62e76326 1522
9bea1d5b 1523 /* set field value */
eede25e7 1524 value.limitInit(value_start, field_end - value_start);
62e76326 1525
9bea1d5b 1526 Headers[id].stat.seenCount++;
62e76326 1527
9bea1d5b 1528 Headers[id].stat.aliveCount++;
62e76326 1529
30abd221 1530 debugs(55, 9, "parsed HttpHeaderEntry: '" << name.buf() << ": " << value.buf() << "'");
62e76326 1531
30abd221 1532 return new HttpHeaderEntry(id, name.buf(), value.buf());
cb69b4c7 1533}
1534
99edd1c3 1535HttpHeaderEntry *
eede25e7 1536HttpHeaderEntry::clone() const
cb69b4c7 1537{
30abd221 1538 return new HttpHeaderEntry(id, name.buf(), value.buf());
cb69b4c7 1539}
1540
de336bbe 1541void
eede25e7 1542HttpHeaderEntry::packInto(Packer * p) const
cb69b4c7 1543{
eede25e7 1544 assert(p);
30abd221 1545 packerAppend(p, name.buf(), name.size());
9bea1d5b 1546 packerAppend(p, ": ", 2);
30abd221 1547 packerAppend(p, value.buf(), value.size());
9bea1d5b 1548 packerAppend(p, "\r\n", 2);
cb69b4c7 1549}
1550
eede25e7 1551int
1552HttpHeaderEntry::getInt() const
1553{
1554 assert_eid (id);
1555 assert (Headers[id].type == ftInt);
1556 int val = -1;
30abd221 1557 int ok = httpHeaderParseInt(value.buf(), &val);
eede25e7 1558 httpHeaderNoteParsedEntry(id, value, !ok);
1559 /* XXX: Should we check ok - ie
1560 * return ok ? -1 : value;
1561 */
1562 return val;
1563}
1564
47f6e231 1565int64_t
1566HttpHeaderEntry::getInt64() const
1567{
1568 assert_eid (id);
1569 assert (Headers[id].type == ftInt64);
1570 int64_t val = -1;
1571 int ok = httpHeaderParseOffset(value.buf(), &val);
1572 httpHeaderNoteParsedEntry(id, value, !ok);
1573 /* XXX: Should we check ok - ie
1574 * return ok ? -1 : value;
1575 */
1576 return val;
1577}
1578
cb69b4c7 1579static void
30abd221 1580httpHeaderNoteParsedEntry(http_hdr_type id, String const &context, int error)
cb69b4c7 1581{
9bea1d5b 1582 Headers[id].stat.parsCount++;
62e76326 1583
9bea1d5b 1584 if (error) {
62e76326 1585 Headers[id].stat.errCount++;
30abd221 1586 debugs(55, 2, "cannot parse hdr field: '" << Headers[id].name.buf() << ": " << context.buf() << "'");
cb69b4c7 1587 }
cb69b4c7 1588}
1589
cb69b4c7 1590/*
12cf1be2 1591 * Reports
cb69b4c7 1592 */
cb69b4c7 1593
fcd2d3ef 1594/* tmp variable used to pass stat info to dumpers */
0cdcddb9 1595extern const HttpHeaderStat *dump_stat; /* argh! */
fcd2d3ef 1596const HttpHeaderStat *dump_stat = NULL;
1597
cb69b4c7 1598static void
9bea1d5b 1599httpHeaderFieldStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1600{
9bea1d5b 1601 const int id = (int) val;
1602 const int valid_id = id >= 0 && id < HDR_ENUM_END;
30abd221 1603 const char *name = valid_id ? Headers[id].name.buf() : "INVALID";
9bea1d5b 1604 int visible = count > 0;
1605 /* for entries with zero count, list only those that belong to current type of message */
62e76326 1606
9bea1d5b 1607 if (!visible && valid_id && dump_stat->owner_mask)
62e76326 1608 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1609
9bea1d5b 1610 if (visible)
62e76326 1611 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1612 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
cb69b4c7 1613}
1614
12cf1be2 1615static void
9bea1d5b 1616httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1617{
9bea1d5b 1618 if (count)
62e76326 1619 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1620 idx, (int) val, count,
1621 xpercent(count, dump_stat->destroyedCount));
cb69b4c7 1622}
1623
1624
1625static void
9bea1d5b 1626httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
12cf1be2 1627{
9bea1d5b 1628 assert(hs && e);
1629
1630 dump_stat = hs;
1631 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1632 storeAppendPrintf(e, "\nField type distribution\n");
1633 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1634 "id", "name", "count", "#/header");
9bea1d5b 1635 statHistDump(&hs->fieldTypeDistr, e, httpHeaderFieldStatDumper);
1636 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1637 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1638 "id", "name", "count", "#/cc_field");
9bea1d5b 1639 statHistDump(&hs->ccTypeDistr, e, httpHdrCcStatDumper);
43ae1d95 1640 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1641 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1642 "id", "name", "count", "#/sc_field");
1643 statHistDump(&hs->scTypeDistr, e, httpHdrScStatDumper);
9bea1d5b 1644 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1645 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
62e76326 1646 "id", "#flds", "count", "%total");
9bea1d5b 1647 statHistDump(&hs->hdrUCountDistr, e, httpHeaderFldsPerHdrDumper);
1648 dump_stat = NULL;
cb69b4c7 1649}
1650
12cf1be2 1651void
9bea1d5b 1652httpHeaderStoreReport(StoreEntry * e)
cb69b4c7 1653{
9bea1d5b 1654 int i;
1655 http_hdr_type ht;
1656 assert(e);
1657
1658 HttpHeaderStats[0].parsedCount =
62e76326 1659 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
9bea1d5b 1660 HttpHeaderStats[0].ccParsedCount =
62e76326 1661 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
9bea1d5b 1662 HttpHeaderStats[0].destroyedCount =
62e76326 1663 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
9bea1d5b 1664 HttpHeaderStats[0].busyDestroyedCount =
62e76326 1665 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
9bea1d5b 1666
1667 for (i = 1; i < HttpHeaderStatCount; i++) {
62e76326 1668 httpHeaderStatDump(HttpHeaderStats + i, e);
1669 storeAppendPrintf(e, "%s\n", "<br>");
12cf1be2 1670 }
62e76326 1671
9bea1d5b 1672 /* field stats for all messages */
1673 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
62e76326 1674
077fe581 1675 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
62e76326 1676 "id", "name", "#alive", "%err", "%repeat");
1677
e6ccf245 1678 for (ht = (http_hdr_type)0; ht < HDR_ENUM_END; ++ht) {
62e76326 1679 HttpHeaderFieldInfo *f = Headers + ht;
077fe581 1680 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
30abd221 1681 f->id, f->name.buf(), f->stat.aliveCount,
62e76326 1682 xpercent(f->stat.errCount, f->stat.parsCount),
1683 xpercent(f->stat.repCount, f->stat.seenCount));
12cf1be2 1684 }
62e76326 1685
9bea1d5b 1686 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
62e76326 1687 HttpHeaderStats[hoRequest].parsedCount,
1688 HttpHeaderStats[hoReply].parsedCount,
1689 HttpHeaderStats[0].parsedCount);
9bea1d5b 1690 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
cb69b4c7 1691}
97474590 1692
e6ccf245 1693http_hdr_type
30abd221 1694httpHeaderIdByName(const char *name, int name_len, const HttpHeaderFieldInfo * info, int end)
97474590 1695{
9bea1d5b 1696 int i;
62e76326 1697
9bea1d5b 1698 for (i = 0; i < end; ++i) {
30abd221 1699 if (name_len >= 0 && name_len != info[i].name.size())
62e76326 1700 continue;
1701
30abd221 1702 if (!strncasecmp(name, info[i].name.buf(),
62e76326 1703 name_len < 0 ? info[i].name.size() + 1 : name_len))
1704 return info[i].id;
97474590 1705 }
62e76326 1706
e6ccf245 1707 return HDR_BAD_HDR;
97474590 1708}
1709
e6ccf245 1710http_hdr_type
9bea1d5b 1711httpHeaderIdByNameDef(const char *name, int name_len)
97474590 1712{
9bea1d5b 1713 if (!Headers)
62e76326 1714 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1715
9bea1d5b 1716 return httpHeaderIdByName(name, name_len, Headers, HDR_ENUM_END);
97474590 1717}
efd900cb 1718
1719const char *
9bea1d5b 1720httpHeaderNameById(int id)
efd900cb 1721{
9bea1d5b 1722 if (!Headers)
62e76326 1723 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1724
9bea1d5b 1725 assert(id >= 0 && id < HDR_ENUM_END);
62e76326 1726
30abd221 1727 return Headers[id].name.buf();
efd900cb 1728}
0353e724 1729
924f73bc 1730int
a9925b40 1731HttpHeader::hasListMember(http_hdr_type id, const char *member, const char separator) const
924f73bc 1732{
1733 int result = 0;
1734 const char *pos = NULL;
1735 const char *item;
1736 int ilen;
1737 int mlen = strlen(member);
1738
924f73bc 1739 assert(id >= 0);
1740
30abd221 1741 String header (getStrOrList(id));
924f73bc 1742
9d0cdbb9 1743 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1744 if (strncmp(item, member, mlen) == 0
1745 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1746 result = 1;
1747 break;
1748 }
1749 }
1750
1751 return result;
1752}
1753
1754int
a9925b40 1755HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
9d0cdbb9 1756{
1757 int result = 0;
1758 const char *pos = NULL;
1759 const char *item;
1760 int ilen;
1761 int mlen = strlen(member);
1762
9d0cdbb9 1763 assert(name);
1764
30abd221 1765 String header (getByName(name));
9d0cdbb9 1766
924f73bc 1767 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1768 if (strncmp(item, member, mlen) == 0
1769 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1770 result = 1;
1771 break;
1772 }
1773 }
1774
1775 return result;
1776}
1777
2cdeea82 1778void
1779HttpHeader::removeHopByHopEntries()
1780{
1781 removeConnectionHeaderEntries();
1782
dcf3665b
AJ
1783 const HttpHeaderEntry *e;
1784 HttpHeaderPos pos = HttpHeaderInitPos;
1785 int headers_deleted = 0;
1786 while ((e = getEntry(&pos))) {
1787 int id = e->id;
1788 if(CBIT_TEST(HopByHopHeadersMask, id)){
1789 delAt(pos, headers_deleted);
1790 CBIT_CLR(mask, id);
1791 }
1792 }
2cdeea82 1793}
1794
924f73bc 1795void
1796HttpHeader::removeConnectionHeaderEntries()
1797{
a9925b40 1798 if (has(HDR_CONNECTION)) {
924f73bc 1799 /* anything that matches Connection list member will be deleted */
30abd221 1800 String strConnection;
1801
1802 (void) getList(HDR_CONNECTION, &strConnection);
924f73bc 1803 const HttpHeaderEntry *e;
1804 HttpHeaderPos pos = HttpHeaderInitPos;
1805 /*
1806 * think: on-average-best nesting of the two loops (hdrEntry
1807 * and strListItem) @?@
1808 */
1809 /*
1810 * maybe we should delete standard stuff ("keep-alive","close")
1811 * from strConnection first?
1812 */
1813
ba9fb01d 1814 int headers_deleted = 0;
a9925b40 1815 while ((e = getEntry(&pos))) {
30abd221 1816 if (strListIsMember(&strConnection, e->name.buf(), ','))
ba9fb01d 1817 delAt(pos, headers_deleted);
924f73bc 1818 }
ba9fb01d 1819 if (headers_deleted)
1820 refreshMask();
924f73bc 1821 }
1822}