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