]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.cc
Verified and converted more String users.
[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
99524de7 464 delByName(e->name.unsafeBuf());
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) {
99524de7 581 if (e->value.cmp(e2->value.unsafeBuf()) != 0) {
3e62bd58 582 int64_t l1, l2;
efc26e8e 583 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 584 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start, header_end) << "}");
9331e4b4 585
586 if (!Config.onoff.relaxed_header_parser) {
eede25e7 587 delete e;
3a9cf2d5 588 goto reset;
9331e4b4 589 }
590
99524de7
FC
591 if (!httpHeaderParseOffset(e->value.unsafeBuf(), &l1)) {
592 debugs(55, 1, "WARNING: Unparseable content-length '" << e->value.unsafeBuf() << "'");
eede25e7 593 delete e;
9331e4b4 594 continue;
99524de7
FC
595 } else if (!httpHeaderParseOffset(e2->value.unsafeBuf(), &l2)) {
596 debugs(55, 1, "WARNING: Unparseable content-length '" << e2->value.unsafeBuf() << "'");
a9925b40 597 delById(e2->id);
9331e4b4 598 } else if (l1 > l2) {
a9925b40 599 delById(e2->id);
9331e4b4 600 } else {
eede25e7 601 delete e;
9331e4b4 602 continue;
603 }
52d3f198 604 } else {
efc26e8e 605 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 606 "NOTICE: found double content-length header");
b3123159 607
608 if (Config.onoff.relaxed_header_parser) {
eede25e7 609 delete e;
b3123159 610 continue;
611 } else {
eede25e7 612 delete e;
3a9cf2d5 613 goto reset;
b3123159 614 }
52d3f198 615 }
616 }
62e76326 617
99524de7 618 if (e->id == HDR_OTHER && stringHasWhitespace(e->name.unsafeBuf())) {
bf8fe701 619 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 620 "WARNING: found whitespace in HTTP header name {" <<
bf8fe701 621 getStringPrefix(field_start, field_end) << "}");
47ac2ebe 622
52d3f198 623 if (!Config.onoff.relaxed_header_parser) {
eede25e7 624 delete e;
3a9cf2d5 625 goto reset;
52d3f198 626 }
627 }
62e76326 628
a9925b40 629 addEntry(e);
cb69b4c7 630 }
62e76326 631
3a9cf2d5 632 PROF_stop(HttpHeaderParse);
9bea1d5b 633 return 1; /* even if no fields where found, it is a valid header */
3a9cf2d5 634reset:
635 PROF_stop(HttpHeaderParse);
636 return reset();
cb69b4c7 637}
638
99edd1c3 639/* packs all the entries using supplied packer */
cb69b4c7 640void
a9925b40 641HttpHeader::packInto(Packer * p) const
cb69b4c7 642{
9bea1d5b 643 HttpHeaderPos pos = HttpHeaderInitPos;
644 const HttpHeaderEntry *e;
a9925b40 645 assert(p);
efc26e8e 646 debugs(55, 7, "packing hdr: (" << this << ")");
9bea1d5b 647 /* pack all entries one by one */
a9925b40 648 while ((e = getEntry(&pos)))
eede25e7 649 e->packInto(p);
35b88ed2 650
651 /* Pack in the "special" entries */
652
653 /* Cache-Control */
cb69b4c7 654}
655
656/* returns next valid entry */
99edd1c3 657HttpHeaderEntry *
a9925b40 658HttpHeader::getEntry(HttpHeaderPos * pos) const
cb69b4c7 659{
a9925b40 660 assert(pos);
661 assert(*pos >= HttpHeaderInitPos && *pos < (ssize_t)entries.count);
62e76326 662
a9925b40 663 for ((*pos)++; *pos < (ssize_t)entries.count; (*pos)++) {
664 if (entries.items[*pos])
665 return (HttpHeaderEntry*)entries.items[*pos];
cb69b4c7 666 }
62e76326 667
9bea1d5b 668 return NULL;
cb69b4c7 669}
670
671/*
26ac0430 672 * returns a pointer to a specified entry if any
d8b249ef 673 * note that we return one entry so it does not make much sense to ask for
674 * "list" headers
cb69b4c7 675 */
de336bbe 676HttpHeaderEntry *
a9925b40 677HttpHeader::findEntry(http_hdr_type id) const
cb69b4c7 678{
9bea1d5b 679 HttpHeaderPos pos = HttpHeaderInitPos;
680 HttpHeaderEntry *e;
9bea1d5b 681 assert_eid(id);
682 assert(!CBIT_TEST(ListHeadersMask, id));
683
684 /* check mask first */
62e76326 685
a9925b40 686 if (!CBIT_TEST(mask, id))
62e76326 687 return NULL;
688
9bea1d5b 689 /* looks like we must have it, do linear search */
a9925b40 690 while ((e = getEntry(&pos))) {
62e76326 691 if (e->id == id)
692 return e;
cb69b4c7 693 }
62e76326 694
9bea1d5b 695 /* hm.. we thought it was there, but it was not found */
696 assert(0);
62e76326 697
9bea1d5b 698 return NULL; /* not reached */
cb69b4c7 699}
700
a622fff0 701/*
702 * same as httpHeaderFindEntry
703 */
a9925b40 704HttpHeaderEntry *
705HttpHeader::findLastEntry(http_hdr_type id) const
a622fff0 706{
9bea1d5b 707 HttpHeaderPos pos = HttpHeaderInitPos;
708 HttpHeaderEntry *e;
709 HttpHeaderEntry *result = NULL;
9bea1d5b 710 assert_eid(id);
711 assert(!CBIT_TEST(ListHeadersMask, id));
712
713 /* check mask first */
62e76326 714
a9925b40 715 if (!CBIT_TEST(mask, id))
62e76326 716 return NULL;
717
9bea1d5b 718 /* looks like we must have it, do linear search */
a9925b40 719 while ((e = getEntry(&pos))) {
62e76326 720 if (e->id == id)
721 result = e;
a622fff0 722 }
62e76326 723
9bea1d5b 724 assert(result); /* must be there! */
725 return result;
a622fff0 726}
727
cb69b4c7 728/*
26ac0430 729 * deletes all fields with a given name if any, returns #fields deleted;
cb69b4c7 730 */
2ac76861 731int
a9925b40 732HttpHeader::delByName(const char *name)
cb69b4c7 733{
9bea1d5b 734 int count = 0;
735 HttpHeaderPos pos = HttpHeaderInitPos;
736 HttpHeaderEntry *e;
a9925b40 737 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
efc26e8e 738 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
62e76326 739
a9925b40 740 while ((e = getEntry(&pos))) {
30abd221 741 if (!e->name.caseCmp(name))
ba9fb01d 742 delAt(pos, count);
743 else
a9925b40 744 CBIT_SET(mask, e->id);
cb69b4c7 745 }
62e76326 746
9bea1d5b 747 return count;
cb69b4c7 748}
749
2246b732 750/* deletes all entries with a given id, returns the #entries deleted */
751int
a9925b40 752HttpHeader::delById(http_hdr_type id)
d8b249ef 753{
9bea1d5b 754 int count = 0;
755 HttpHeaderPos pos = HttpHeaderInitPos;
756 HttpHeaderEntry *e;
efc26e8e 757 debugs(55, 8, this << " del-by-id " << id);
9bea1d5b 758 assert_eid(id);
e6ca7a9b 759 assert(id != HDR_OTHER); /* does not make sense */
62e76326 760
a9925b40 761 if (!CBIT_TEST(mask, id))
62e76326 762 return 0;
763
a9925b40 764 while ((e = getEntry(&pos))) {
ba9fb01d 765 if (e->id == id)
766 delAt(pos, count);
d8b249ef 767 }
62e76326 768
a9925b40 769 CBIT_CLR(mask, id);
9bea1d5b 770 assert(count);
771 return count;
d8b249ef 772}
d8b249ef 773
cb69b4c7 774/*
775 * deletes an entry at pos and leaves a gap; leaving a gap makes it
776 * possible to iterate(search) and delete fields at the same time
ba9fb01d 777 * NOTE: Does not update the header mask. Caller must follow up with
778 * a call to refreshMask() if headers_deleted was incremented.
cb69b4c7 779 */
2246b732 780void
ba9fb01d 781HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
cb69b4c7 782{
9bea1d5b 783 HttpHeaderEntry *e;
a9925b40 784 assert(pos >= HttpHeaderInitPos && pos < (ssize_t)entries.count);
785 e = (HttpHeaderEntry*)entries.items[pos];
786 entries.items[pos] = NULL;
9bea1d5b 787 /* decrement header length, allow for ": " and crlf */
a9925b40 788 len -= e->name.size() + 2 + e->value.size() + 2;
789 assert(len >= 0);
eede25e7 790 delete e;
ba9fb01d 791 ++headers_deleted;
cb69b4c7 792}
793
394499bd 794/*
795 * Compacts the header storage
796 */
797void
798HttpHeader::compact()
799{
800 entries.prune(NULL);
801}
802
ba9fb01d 803/*
804 * Refreshes the header mask. Required after delAt() calls.
805 */
806void
807HttpHeader::refreshMask()
808{
809 httpHeaderMaskInit(&mask, 0);
810 debugs(55, 7, "refreshing the mask in hdr " << this);
811 HttpHeaderPos pos = HttpHeaderInitPos;
812 while (HttpHeaderEntry *e = getEntry(&pos)) {
26ac0430 813 CBIT_SET(mask, e->id);
ba9fb01d 814 }
815}
99edd1c3 816
62e76326 817/* appends an entry;
eede25e7 818 * does not call e->clone() so one should not reuse "*e"
cb69b4c7 819 */
99edd1c3 820void
a9925b40 821HttpHeader::addEntry(HttpHeaderEntry * e)
cb69b4c7 822{
a9925b40 823 assert(e);
9bea1d5b 824 assert_eid(e->id);
59c50530 825 assert(e->name.size());
9bea1d5b 826
a9925b40 827 debugs(55, 9, this << " adding entry: " << e->id << " at " <<
828 entries.count);
62e76326 829
a9925b40 830 if (CBIT_TEST(mask, e->id))
62e76326 831 Headers[e->id].stat.repCount++;
9bea1d5b 832 else
a9925b40 833 CBIT_SET(mask, e->id);
62e76326 834
a9925b40 835 entries.push_back(e);
62e76326 836
9bea1d5b 837 /* increment header length, allow for ": " and crlf */
a9925b40 838 len += e->name.size() + 2 + e->value.size() + 2;
cb69b4c7 839}
840
bbe58ab5 841/* inserts an entry;
eede25e7 842 * does not call e->clone() so one should not reuse "*e"
bbe58ab5 843 */
844void
a9925b40 845HttpHeader::insertEntry(HttpHeaderEntry * e)
bbe58ab5 846{
a9925b40 847 assert(e);
bbe58ab5 848 assert_eid(e->id);
849
a9925b40 850 debugs(55, 7, this << " adding entry: " << e->id << " at " <<
851 entries.count);
bbe58ab5 852
a9925b40 853 if (CBIT_TEST(mask, e->id))
bbe58ab5 854 Headers[e->id].stat.repCount++;
855 else
a9925b40 856 CBIT_SET(mask, e->id);
bbe58ab5 857
a9925b40 858 entries.insert(e);
bbe58ab5 859
860 /* increment header length, allow for ": " and crlf */
a9925b40 861 len += e->name.size() + 2 + e->value.size() + 2;
bbe58ab5 862}
863
35b88ed2 864bool
30abd221 865HttpHeader::getList(http_hdr_type id, String *s) const
35b88ed2 866{
867 HttpHeaderEntry *e;
868 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 869 debugs(55, 9, this << " joining for id " << id);
35b88ed2 870 /* only fields from ListHeaders array can be "listed" */
871 assert(CBIT_TEST(ListHeadersMask, id));
872
873 if (!CBIT_TEST(mask, id))
874 return false;
875
876 while ((e = getEntry(&pos))) {
877 if (e->id == id)
99524de7 878 strListAdd(s, e->value.unsafeBuf(), ',');
35b88ed2 879 }
880
881 /*
882 * note: we might get an empty (size==0) string if there was an "empty"
883 * header. This results in an empty length String, which may have a NULL
884 * buffer.
885 */
886 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
887 if (!s->size())
888 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
889 else
890 debugs(55, 6, this << ": joined for id " << id << ": " << s);
891
892 return true;
893}
894
99edd1c3 895/* return a list of entries with the same id separated by ',' and ws */
30abd221 896String
a9925b40 897HttpHeader::getList(http_hdr_type id) const
cb69b4c7 898{
9bea1d5b 899 HttpHeaderEntry *e;
900 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 901 debugs(55, 9, this << "joining for id " << id);
9bea1d5b 902 /* only fields from ListHeaders array can be "listed" */
903 assert(CBIT_TEST(ListHeadersMask, id));
62e76326 904
a9925b40 905 if (!CBIT_TEST(mask, id))
30abd221 906 return String();
650c4b88 907
30abd221 908 String s;
62e76326 909
a9925b40 910 while ((e = getEntry(&pos))) {
62e76326 911 if (e->id == id)
99524de7 912 strListAdd(&s, e->value.unsafeBuf(), ',');
d35b9a94 913 }
62e76326 914
9bea1d5b 915 /*
948078e3 916 * note: we might get an empty (size==0) string if there was an "empty"
917 * header. This results in an empty length String, which may have a NULL
918 * buffer.
9bea1d5b 919 */
948078e3 920 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
528b2c61 921 if (!s.size())
948078e3 922 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
923 else
924 debugs(55, 6, this << ": joined for id " << id << ": " << s);
62e76326 925
9bea1d5b 926 return s;
cb69b4c7 927}
928
f66a9ef4 929/* return a string or list of entries with the same id separated by ',' and ws */
30abd221 930String
a9925b40 931HttpHeader::getStrOrList(http_hdr_type id) const
f66a9ef4 932{
c7327fa0 933 HttpHeaderEntry *e;
9bea1d5b 934
935 if (CBIT_TEST(ListHeadersMask, id))
a9925b40 936 return getList(id);
62e76326 937
a9925b40 938 if ((e = findEntry(id)))
62e76326 939 return e->value;
940
30abd221 941 return String();
f66a9ef4 942}
943
944/*
bd412580 945 * Returns the value of the specified header.
f66a9ef4 946 */
30abd221 947String
a9925b40 948HttpHeader::getByName(const char *name) const
f66a9ef4 949{
9bea1d5b 950 http_hdr_type id;
951 HttpHeaderPos pos = HttpHeaderInitPos;
952 HttpHeaderEntry *e;
9bea1d5b 953
9bea1d5b 954 assert(name);
955
956 /* First try the quick path */
957 id = httpHeaderIdByNameDef(name, strlen(name));
62e76326 958
9bea1d5b 959 if (id != -1)
a9925b40 960 return getStrOrList(id);
9bea1d5b 961
30abd221 962 String result;
650c4b88 963
9bea1d5b 964 /* Sorry, an unknown header name. Do linear search */
a9925b40 965 while ((e = getEntry(&pos))) {
30abd221 966 if (e->id == HDR_OTHER && e->name.caseCmp(name) == 0) {
99524de7 967 strListAdd(&result, e->value.unsafeBuf(), ',');
62e76326 968 }
f66a9ef4 969 }
62e76326 970
9bea1d5b 971 return result;
f66a9ef4 972}
cb69b4c7 973
14b463aa 974/*
372fdfbf 975 * Returns a the value of the specified list member, if any.
14b463aa 976 */
30abd221 977String
a9925b40 978HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
14b463aa 979{
30abd221 980 String header;
14b463aa 981 const char *pos = NULL;
982 const char *item;
983 int ilen;
984 int mlen = strlen(member);
985
14b463aa 986 assert(name);
987
a9925b40 988 header = getByName(name);
14b463aa 989
30abd221 990 String result;
650c4b88 991
14b463aa 992 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 993 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
994 result.append(item + mlen + 1, ilen - mlen - 1);
995 break;
996 }
14b463aa 997 }
62e76326 998
14b463aa 999 return result;
1000}
1001
1002/*
1003 * returns a the value of the specified list member, if any.
1004 */
30abd221 1005String
a9925b40 1006HttpHeader::getListMember(http_hdr_type id, const char *member, const char separator) const
14b463aa 1007{
30abd221 1008 String header;
14b463aa 1009 const char *pos = NULL;
1010 const char *item;
1011 int ilen;
1012 int mlen = strlen(member);
1013
14b463aa 1014 assert(id >= 0);
1015
a9925b40 1016 header = getStrOrList(id);
30abd221 1017 String result;
14b463aa 1018
1019 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1020 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1021 result.append(item + mlen + 1, ilen - mlen - 1);
1022 break;
1023 }
14b463aa 1024 }
62e76326 1025
30abd221 1026 header.clean();
14b463aa 1027 return result;
1028}
1029
cb69b4c7 1030/* test if a field is present */
2ac76861 1031int
a9925b40 1032HttpHeader::has(http_hdr_type id) const
cb69b4c7 1033{
9bea1d5b 1034 assert_eid(id);
1035 assert(id != HDR_OTHER);
efc26e8e 1036 debugs(55, 9, this << " lookup for " << id);
a9925b40 1037 return CBIT_TEST(mask, id);
cb69b4c7 1038}
1039
1040void
a9925b40 1041HttpHeader::putInt(http_hdr_type id, int number)
cb69b4c7 1042{
9bea1d5b 1043 assert_eid(id);
1044 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
1045 assert(number >= 0);
eede25e7 1046 addEntry(new HttpHeaderEntry(id, NULL, xitoa(number)));
cb69b4c7 1047}
1048
47f6e231 1049void
1050HttpHeader::putInt64(http_hdr_type id, int64_t number)
1051{
1052 assert_eid(id);
1053 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1054 assert(number >= 0);
1055 addEntry(new HttpHeaderEntry(id, NULL, xint64toa(number)));
1056}
1057
cb69b4c7 1058void
a9925b40 1059HttpHeader::putTime(http_hdr_type id, time_t htime)
cb69b4c7 1060{
9bea1d5b 1061 assert_eid(id);
1062 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
a1d6870f 1063 assert(htime >= 0);
eede25e7 1064 addEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
cb69b4c7 1065}
2ac76861 1066
bbe58ab5 1067void
a9925b40 1068HttpHeader::insertTime(http_hdr_type id, time_t htime)
bbe58ab5 1069{
1070 assert_eid(id);
1071 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
1072 assert(htime >= 0);
eede25e7 1073 insertEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
bbe58ab5 1074}
1075
cb69b4c7 1076void
a9925b40 1077HttpHeader::putStr(http_hdr_type id, const char *str)
cb69b4c7 1078{
9bea1d5b 1079 assert_eid(id);
1080 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
1081 assert(str);
eede25e7 1082 addEntry(new HttpHeaderEntry(id, NULL, str));
cb69b4c7 1083}
1084
63259c34 1085void
a9925b40 1086HttpHeader::putAuth(const char *auth_scheme, const char *realm)
63259c34 1087{
a9925b40 1088 assert(auth_scheme && realm);
1089 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
63259c34 1090}
1091
99edd1c3 1092void
a9925b40 1093HttpHeader::putCc(const HttpHdrCc * cc)
99edd1c3 1094{
9bea1d5b 1095 MemBuf mb;
1096 Packer p;
a9925b40 1097 assert(cc);
9bea1d5b 1098 /* remove old directives if any */
a9925b40 1099 delById(HDR_CACHE_CONTROL);
9bea1d5b 1100 /* pack into mb */
2fe7eff9 1101 mb.init();
9bea1d5b 1102 packerToMemInit(&p, &mb);
1103 httpHdrCcPackInto(cc, &p);
1104 /* put */
eede25e7 1105 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL, NULL, mb.buf));
9bea1d5b 1106 /* cleanup */
1107 packerClean(&p);
2fe7eff9 1108 mb.clean();
99edd1c3 1109}
1110
d192d11f 1111void
a9925b40 1112HttpHeader::putContRange(const HttpHdrContRange * cr)
d192d11f 1113{
9bea1d5b 1114 MemBuf mb;
1115 Packer p;
a9925b40 1116 assert(cr);
9bea1d5b 1117 /* remove old directives if any */
a9925b40 1118 delById(HDR_CONTENT_RANGE);
9bea1d5b 1119 /* pack into mb */
2fe7eff9 1120 mb.init();
9bea1d5b 1121 packerToMemInit(&p, &mb);
1122 httpHdrContRangePackInto(cr, &p);
1123 /* put */
eede25e7 1124 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE, NULL, mb.buf));
9bea1d5b 1125 /* cleanup */
1126 packerClean(&p);
2fe7eff9 1127 mb.clean();
d192d11f 1128}
1129
1130void
a9925b40 1131HttpHeader::putRange(const HttpHdrRange * range)
d192d11f 1132{
9bea1d5b 1133 MemBuf mb;
1134 Packer p;
a9925b40 1135 assert(range);
9bea1d5b 1136 /* remove old directives if any */
a9925b40 1137 delById(HDR_RANGE);
9bea1d5b 1138 /* pack into mb */
2fe7eff9 1139 mb.init();
9bea1d5b 1140 packerToMemInit(&p, &mb);
528b2c61 1141 range->packInto(&p);
9bea1d5b 1142 /* put */
eede25e7 1143 addEntry(new HttpHeaderEntry(HDR_RANGE, NULL, mb.buf));
9bea1d5b 1144 /* cleanup */
1145 packerClean(&p);
2fe7eff9 1146 mb.clean();
d192d11f 1147}
1148
43ae1d95 1149void
a9925b40 1150HttpHeader::putSc(HttpHdrSc *sc)
43ae1d95 1151{
1152 MemBuf mb;
1153 Packer p;
a9925b40 1154 assert(sc);
43ae1d95 1155 /* remove old directives if any */
a9925b40 1156 delById(HDR_RANGE);
43ae1d95 1157 /* pack into mb */
2fe7eff9 1158 mb.init();
43ae1d95 1159 packerToMemInit(&p, &mb);
1160 httpHdrScPackInto(sc, &p);
1161 /* put */
eede25e7 1162 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL, NULL, mb.buf));
43ae1d95 1163 /* cleanup */
1164 packerClean(&p);
2fe7eff9 1165 mb.clean();
43ae1d95 1166}
1167
cb69b4c7 1168/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1169void
a9925b40 1170HttpHeader::putExt(const char *name, const char *value)
cb69b4c7 1171{
9bea1d5b 1172 assert(name && value);
efc26e8e 1173 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
eede25e7 1174 addEntry(new HttpHeaderEntry(HDR_OTHER, name, value));
528b2c61 1175}
1176
1177int
a9925b40 1178HttpHeader::getInt(http_hdr_type id) const
528b2c61 1179{
9bea1d5b 1180 assert_eid(id);
1181 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
528b2c61 1182 HttpHeaderEntry *e;
62e76326 1183
a9925b40 1184 if ((e = findEntry(id)))
eede25e7 1185 return e->getInt();
62e76326 1186
528b2c61 1187 return -1;
7c525cc2 1188}
1189
47f6e231 1190int64_t
1191HttpHeader::getInt64(http_hdr_type id) const
1192{
1193 assert_eid(id);
1194 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1195 HttpHeaderEntry *e;
1196
1197 if ((e = findEntry(id)))
1198 return e->getInt64();
1199
1200 return -1;
1201}
1202
de336bbe 1203time_t
a9925b40 1204HttpHeader::getTime(http_hdr_type id) const
cb69b4c7 1205{
9bea1d5b 1206 HttpHeaderEntry *e;
1207 time_t value = -1;
1208 assert_eid(id);
1209 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
62e76326 1210
a9925b40 1211 if ((e = findEntry(id))) {
99524de7 1212 value = parse_rfc1123(e->value.unsafeBuf());
62e76326 1213 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
d8b249ef 1214 }
62e76326 1215
9bea1d5b 1216 return value;
cb69b4c7 1217}
1218
99edd1c3 1219/* sync with httpHeaderGetLastStr */
de336bbe 1220const char *
a9925b40 1221HttpHeader::getStr(http_hdr_type id) const
cb69b4c7 1222{
9bea1d5b 1223 HttpHeaderEntry *e;
1224 assert_eid(id);
1225 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1226
a9925b40 1227 if ((e = findEntry(id))) {
62e76326 1228 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
99524de7 1229 return e->value.unsafeBuf();
d8b249ef 1230 }
62e76326 1231
9bea1d5b 1232 return NULL;
cb69b4c7 1233}
1234
a622fff0 1235/* unusual */
1236const char *
a9925b40 1237HttpHeader::getLastStr(http_hdr_type id) const
a622fff0 1238{
9bea1d5b 1239 HttpHeaderEntry *e;
1240 assert_eid(id);
1241 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1242
a9925b40 1243 if ((e = findLastEntry(id))) {
62e76326 1244 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
99524de7 1245 return e->value.unsafeBuf();
a622fff0 1246 }
62e76326 1247
9bea1d5b 1248 return NULL;
a622fff0 1249}
1250
7faf2bdb 1251HttpHdrCc *
a9925b40 1252HttpHeader::getCc() const
cb69b4c7 1253{
9bea1d5b 1254 HttpHdrCc *cc;
30abd221 1255 String s;
62e76326 1256
a9925b40 1257 if (!CBIT_TEST(mask, HDR_CACHE_CONTROL))
62e76326 1258 return NULL;
35b88ed2 1259 PROF_start(HttpHeader_getCc);
62e76326 1260
35b88ed2 1261 getList(HDR_CACHE_CONTROL, &s);
62e76326 1262
9bea1d5b 1263 cc = httpHdrCcParseCreate(&s);
62e76326 1264
a9925b40 1265 HttpHeaderStats[owner].ccParsedCount++;
62e76326 1266
9bea1d5b 1267 if (cc)
a9925b40 1268 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
62e76326 1269
9bea1d5b 1270 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL, s, !cc);
62e76326 1271
35b88ed2 1272 PROF_stop(HttpHeader_getCc);
62e76326 1273
9bea1d5b 1274 return cc;
cb69b4c7 1275}
1276
02922e76 1277HttpHdrRange *
a9925b40 1278HttpHeader::getRange() const
02922e76 1279{
9bea1d5b 1280 HttpHdrRange *r = NULL;
1281 HttpHeaderEntry *e;
1282 /* some clients will send "Request-Range" _and_ *matching* "Range"
1283 * who knows, some clients might send Request-Range only;
1284 * this "if" should work correctly in both cases;
1285 * hopefully no clients send mismatched headers! */
62e76326 1286
a9925b40 1287 if ((e = findEntry(HDR_RANGE)) ||
1288 (e = findEntry(HDR_REQUEST_RANGE))) {
62e76326 1289 r = HttpHdrRange::ParseCreate(&e->value);
1290 httpHeaderNoteParsedEntry(e->id, e->value, !r);
d192d11f 1291 }
62e76326 1292
9bea1d5b 1293 return r;
02922e76 1294}
1295
43ae1d95 1296HttpHdrSc *
a9925b40 1297HttpHeader::getSc() const
43ae1d95 1298{
a9925b40 1299 if (!CBIT_TEST(mask, HDR_SURROGATE_CONTROL))
43ae1d95 1300 return NULL;
1301
30abd221 1302 String s;
26ac0430 1303
35b88ed2 1304 (void) getList(HDR_SURROGATE_CONTROL, &s);
43ae1d95 1305
1306 HttpHdrSc *sc = httpHdrScParseCreate(&s);
1307
a9925b40 1308 HttpHeaderStats[owner].ccParsedCount++;
43ae1d95 1309
1310 if (sc)
a9925b40 1311 httpHdrScUpdateStats(sc, &HttpHeaderStats[owner].scTypeDistr);
43ae1d95 1312
1313 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL, s, !sc);
1314
1315 return sc;
1316}
1317
d76fcfa7 1318HttpHdrContRange *
a9925b40 1319HttpHeader::getContRange() const
d76fcfa7 1320{
9bea1d5b 1321 HttpHdrContRange *cr = NULL;
1322 HttpHeaderEntry *e;
62e76326 1323
a9925b40 1324 if ((e = findEntry(HDR_CONTENT_RANGE))) {
99524de7 1325 cr = httpHdrContRangeParseCreate(e->value.unsafeBuf());
62e76326 1326 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
d8b249ef 1327 }
62e76326 1328
9bea1d5b 1329 return cr;
cb69b4c7 1330}
1331
99edd1c3 1332const char *
a9925b40 1333HttpHeader::getAuth(http_hdr_type id, const char *auth_scheme) const
cb69b4c7 1334{
9bea1d5b 1335 const char *field;
1336 int l;
a9925b40 1337 assert(auth_scheme);
1338 field = getStr(id);
62e76326 1339
9bea1d5b 1340 if (!field) /* no authorization field */
62e76326 1341 return NULL;
1342
9bea1d5b 1343 l = strlen(auth_scheme);
62e76326 1344
9bea1d5b 1345 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
62e76326 1346 return NULL;
1347
9bea1d5b 1348 field += l;
62e76326 1349
9bea1d5b 1350 if (!xisspace(*field)) /* wrong scheme */
62e76326 1351 return NULL;
1352
9bea1d5b 1353 /* skip white space */
1354 field += xcountws(field);
62e76326 1355
9bea1d5b 1356 if (!*field) /* no authorization cookie */
62e76326 1357 return NULL;
1358
9bea1d5b 1359 return base64_decode(field);
cb69b4c7 1360}
1361
a9771e51 1362ETag
a9925b40 1363HttpHeader::getETag(http_hdr_type id) const
a9771e51 1364{
26ac0430 1365 ETag etag = {NULL, -1};
9bea1d5b 1366 HttpHeaderEntry *e;
1367 assert(Headers[id].type == ftETag); /* must be of an appropriate type */
62e76326 1368
a9925b40 1369 if ((e = findEntry(id)))
99524de7 1370 etagParseInit(&etag, e->value.unsafeBuf());
62e76326 1371
9bea1d5b 1372 return etag;
a9771e51 1373}
1374
1375TimeOrTag
a9925b40 1376HttpHeader::getTimeOrTag(http_hdr_type id) const
a9771e51 1377{
9bea1d5b 1378 TimeOrTag tot;
1379 HttpHeaderEntry *e;
1380 assert(Headers[id].type == ftDate_1123_or_ETag); /* must be of an appropriate type */
1381 memset(&tot, 0, sizeof(tot));
62e76326 1382
a9925b40 1383 if ((e = findEntry(id))) {
99524de7 1384 const char *str = e->value.unsafeBuf();
62e76326 1385 /* try as an ETag */
1386
1387 if (etagParseInit(&tot.tag, str)) {
1388 tot.valid = tot.tag.str != NULL;
1389 tot.time = -1;
1390 } else {
1391 /* or maybe it is time? */
1392 tot.time = parse_rfc1123(str);
1393 tot.valid = tot.time >= 0;
1394 tot.tag.str = NULL;
1395 }
a9771e51 1396 }
62e76326 1397
9bea1d5b 1398 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
1399 return tot;
a9771e51 1400}
1401
cb69b4c7 1402/*
1403 * HttpHeaderEntry
1404 */
1405
eede25e7 1406HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId, const char *aName, const char *aValue)
cb69b4c7 1407{
eede25e7 1408 assert_eid(anId);
1409 id = anId;
62e76326 1410
9bea1d5b 1411 if (id != HDR_OTHER)
eede25e7 1412 name = Headers[id].name;
9bea1d5b 1413 else
eede25e7 1414 name = aName;
62e76326 1415
eede25e7 1416 value = aValue;
62e76326 1417
9bea1d5b 1418 Headers[id].stat.aliveCount++;
62e76326 1419
99524de7 1420 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name.unsafeBuf() << " : " << value.unsafeBuf());
eede25e7 1421}
62e76326 1422
eede25e7 1423HttpHeaderEntry::~HttpHeaderEntry()
2ac76861 1424{
eede25e7 1425 assert_eid(id);
99524de7 1426 debugs(55, 9, "destroying entry " << this << ": '" << name.unsafeBuf() << ": " << value.unsafeBuf() << "'");
30abd221 1427 /* clean name if needed */
1428
1429 if (id == HDR_OTHER)
1430 name.clean();
1431
1432 value.clean();
62e76326 1433
eede25e7 1434 assert(Headers[id].stat.aliveCount);
62e76326 1435
eede25e7 1436 Headers[id].stat.aliveCount--;
62e76326 1437
eede25e7 1438 id = HDR_BAD_HDR;
cb69b4c7 1439}
1440
eede25e7 1441/* parses and inits header entry, returns true/false */
cdce6c61 1442HttpHeaderEntry *
eede25e7 1443HttpHeaderEntry::parse(const char *field_start, const char *field_end)
cb69b4c7 1444{
9bea1d5b 1445 /* note: name_start == field_start */
52d3f198 1446 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
26ac0430 1447 int name_len = name_end ? name_end - field_start :0;
9bea1d5b 1448 const char *value_start = field_start + name_len + 1; /* skip ':' */
1449 /* note: value_end == field_end */
1450
1451 HeaderEntryParsedCount++;
1452
1453 /* do we have a valid field name within this field? */
62e76326 1454
9bea1d5b 1455 if (!name_len || name_end > field_end)
cdce6c61 1456 return NULL;
62e76326 1457
67746eea 1458 if (name_len > 65534) {
1459 /* String must be LESS THAN 64K and it adds a terminating NULL */
efc26e8e 1460 debugs(55, 1, "WARNING: ignoring header name of " << name_len << " bytes");
cdce6c61 1461 return NULL;
25acfb53 1462 }
62e76326 1463
52d3f198 1464 if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) {
bf8fe701 1465 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
1466 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end) << "'");
52d3f198 1467
1468 while (name_len > 0 && xisspace(field_start[name_len - 1]))
1469 name_len--;
1470
1471 if (!name_len)
cdce6c61 1472 return NULL;
52d3f198 1473 }
1474
9bea1d5b 1475 /* now we know we can parse it */
62e76326 1476
bf8fe701 1477 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end) << "'");
62e76326 1478
9bea1d5b 1479 /* is it a "known" field? */
cdce6c61 1480 http_hdr_type id = httpHeaderIdByName(field_start, name_len, Headers, HDR_ENUM_END);
1481
30abd221 1482 String name;
cdce6c61 1483
30abd221 1484 String value;
62e76326 1485
9bea1d5b 1486 if (id < 0)
62e76326 1487 id = HDR_OTHER;
1488
9bea1d5b 1489 assert_eid(id);
62e76326 1490
9bea1d5b 1491 /* set field name */
1492 if (id == HDR_OTHER)
eede25e7 1493 name.limitInit(field_start, name_len);
9bea1d5b 1494 else
eede25e7 1495 name = Headers[id].name;
62e76326 1496
9bea1d5b 1497 /* trim field value */
1498 while (value_start < field_end && xisspace(*value_start))
62e76326 1499 value_start++;
1500
52d3f198 1501 while (value_start < field_end && xisspace(field_end[-1]))
1502 field_end--;
1503
67746eea 1504 if (field_end - value_start > 65534) {
1505 /* String must be LESS THAN 64K and it adds a terminating NULL */
99524de7 1506 debugs(55, 1, "WARNING: ignoring '" << name.unsafeBuf() << "' header of " << (field_end - value_start) << " bytes");
62e76326 1507
eede25e7 1508 if (id == HDR_OTHER)
30abd221 1509 name.clean();
62e76326 1510
cdce6c61 1511 return NULL;
25acfb53 1512 }
62e76326 1513
9bea1d5b 1514 /* set field value */
eede25e7 1515 value.limitInit(value_start, field_end - value_start);
62e76326 1516
9bea1d5b 1517 Headers[id].stat.seenCount++;
62e76326 1518
9bea1d5b 1519 Headers[id].stat.aliveCount++;
62e76326 1520
99524de7 1521 debugs(55, 9, "parsed HttpHeaderEntry: '" << name.unsafeBuf() << ": " << value.unsafeBuf() << "'");
62e76326 1522
99524de7 1523 return new HttpHeaderEntry(id, name.unsafeBuf(), value.unsafeBuf());
cb69b4c7 1524}
1525
99edd1c3 1526HttpHeaderEntry *
eede25e7 1527HttpHeaderEntry::clone() const
cb69b4c7 1528{
99524de7 1529 return new HttpHeaderEntry(id, name.unsafeBuf(), value.unsafeBuf());
cb69b4c7 1530}
1531
de336bbe 1532void
eede25e7 1533HttpHeaderEntry::packInto(Packer * p) const
cb69b4c7 1534{
eede25e7 1535 assert(p);
99524de7 1536 packerAppend(p, name.unsafeBuf(), name.size());
9bea1d5b 1537 packerAppend(p, ": ", 2);
99524de7 1538 packerAppend(p, value.unsafeBuf(), value.size());
9bea1d5b 1539 packerAppend(p, "\r\n", 2);
cb69b4c7 1540}
1541
eede25e7 1542int
1543HttpHeaderEntry::getInt() const
1544{
1545 assert_eid (id);
1546 assert (Headers[id].type == ftInt);
1547 int val = -1;
99524de7 1548 int ok = httpHeaderParseInt(value.unsafeBuf(), &val);
eede25e7 1549 httpHeaderNoteParsedEntry(id, value, !ok);
1550 /* XXX: Should we check ok - ie
1551 * return ok ? -1 : value;
1552 */
1553 return val;
1554}
1555
47f6e231 1556int64_t
1557HttpHeaderEntry::getInt64() const
1558{
1559 assert_eid (id);
1560 assert (Headers[id].type == ftInt64);
1561 int64_t val = -1;
99524de7 1562 int ok = httpHeaderParseOffset(value.unsafeBuf(), &val);
47f6e231 1563 httpHeaderNoteParsedEntry(id, value, !ok);
1564 /* XXX: Should we check ok - ie
1565 * return ok ? -1 : value;
1566 */
1567 return val;
1568}
1569
cb69b4c7 1570static void
30abd221 1571httpHeaderNoteParsedEntry(http_hdr_type id, String const &context, int error)
cb69b4c7 1572{
9bea1d5b 1573 Headers[id].stat.parsCount++;
62e76326 1574
9bea1d5b 1575 if (error) {
62e76326 1576 Headers[id].stat.errCount++;
99524de7 1577 debugs(55, 2, "cannot parse hdr field: '" << Headers[id].name.unsafeBuf() << ": " << context.unsafeBuf() << "'");
cb69b4c7 1578 }
cb69b4c7 1579}
1580
cb69b4c7 1581/*
12cf1be2 1582 * Reports
cb69b4c7 1583 */
cb69b4c7 1584
fcd2d3ef 1585/* tmp variable used to pass stat info to dumpers */
0cdcddb9 1586extern const HttpHeaderStat *dump_stat; /* argh! */
fcd2d3ef 1587const HttpHeaderStat *dump_stat = NULL;
1588
cb69b4c7 1589static void
9bea1d5b 1590httpHeaderFieldStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1591{
9bea1d5b 1592 const int id = (int) val;
1593 const int valid_id = id >= 0 && id < HDR_ENUM_END;
99524de7 1594 const char *name = valid_id ? Headers[id].name.unsafeBuf() : "INVALID";
9bea1d5b 1595 int visible = count > 0;
1596 /* for entries with zero count, list only those that belong to current type of message */
62e76326 1597
9bea1d5b 1598 if (!visible && valid_id && dump_stat->owner_mask)
62e76326 1599 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1600
9bea1d5b 1601 if (visible)
62e76326 1602 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1603 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
cb69b4c7 1604}
1605
12cf1be2 1606static void
9bea1d5b 1607httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1608{
9bea1d5b 1609 if (count)
62e76326 1610 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1611 idx, (int) val, count,
1612 xpercent(count, dump_stat->destroyedCount));
cb69b4c7 1613}
1614
1615
1616static void
9bea1d5b 1617httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
12cf1be2 1618{
9bea1d5b 1619 assert(hs && e);
1620
1621 dump_stat = hs;
1622 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1623 storeAppendPrintf(e, "\nField type distribution\n");
1624 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1625 "id", "name", "count", "#/header");
9bea1d5b 1626 statHistDump(&hs->fieldTypeDistr, e, httpHeaderFieldStatDumper);
1627 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1628 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1629 "id", "name", "count", "#/cc_field");
9bea1d5b 1630 statHistDump(&hs->ccTypeDistr, e, httpHdrCcStatDumper);
43ae1d95 1631 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1632 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1633 "id", "name", "count", "#/sc_field");
1634 statHistDump(&hs->scTypeDistr, e, httpHdrScStatDumper);
9bea1d5b 1635 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1636 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
62e76326 1637 "id", "#flds", "count", "%total");
9bea1d5b 1638 statHistDump(&hs->hdrUCountDistr, e, httpHeaderFldsPerHdrDumper);
1639 dump_stat = NULL;
cb69b4c7 1640}
1641
12cf1be2 1642void
9bea1d5b 1643httpHeaderStoreReport(StoreEntry * e)
cb69b4c7 1644{
9bea1d5b 1645 int i;
1646 http_hdr_type ht;
1647 assert(e);
1648
1649 HttpHeaderStats[0].parsedCount =
62e76326 1650 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
9bea1d5b 1651 HttpHeaderStats[0].ccParsedCount =
62e76326 1652 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
9bea1d5b 1653 HttpHeaderStats[0].destroyedCount =
62e76326 1654 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
9bea1d5b 1655 HttpHeaderStats[0].busyDestroyedCount =
62e76326 1656 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
9bea1d5b 1657
1658 for (i = 1; i < HttpHeaderStatCount; i++) {
62e76326 1659 httpHeaderStatDump(HttpHeaderStats + i, e);
1660 storeAppendPrintf(e, "%s\n", "<br>");
12cf1be2 1661 }
62e76326 1662
9bea1d5b 1663 /* field stats for all messages */
1664 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
62e76326 1665
077fe581 1666 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
62e76326 1667 "id", "name", "#alive", "%err", "%repeat");
1668
e6ccf245 1669 for (ht = (http_hdr_type)0; ht < HDR_ENUM_END; ++ht) {
62e76326 1670 HttpHeaderFieldInfo *f = Headers + ht;
077fe581 1671 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
99524de7 1672 f->id, f->name.unsafeBuf(), f->stat.aliveCount,
62e76326 1673 xpercent(f->stat.errCount, f->stat.parsCount),
1674 xpercent(f->stat.repCount, f->stat.seenCount));
12cf1be2 1675 }
62e76326 1676
9bea1d5b 1677 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
62e76326 1678 HttpHeaderStats[hoRequest].parsedCount,
1679 HttpHeaderStats[hoReply].parsedCount,
1680 HttpHeaderStats[0].parsedCount);
9bea1d5b 1681 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
cb69b4c7 1682}
97474590 1683
e6ccf245 1684http_hdr_type
30abd221 1685httpHeaderIdByName(const char *name, int name_len, const HttpHeaderFieldInfo * info, int end)
97474590 1686{
9bea1d5b 1687 int i;
62e76326 1688
9bea1d5b 1689 for (i = 0; i < end; ++i) {
30abd221 1690 if (name_len >= 0 && name_len != info[i].name.size())
62e76326 1691 continue;
1692
99524de7 1693 if (!strncasecmp(name, info[i].name.unsafeBuf(),
62e76326 1694 name_len < 0 ? info[i].name.size() + 1 : name_len))
1695 return info[i].id;
97474590 1696 }
62e76326 1697
e6ccf245 1698 return HDR_BAD_HDR;
97474590 1699}
1700
e6ccf245 1701http_hdr_type
9bea1d5b 1702httpHeaderIdByNameDef(const char *name, int name_len)
97474590 1703{
9bea1d5b 1704 if (!Headers)
62e76326 1705 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1706
9bea1d5b 1707 return httpHeaderIdByName(name, name_len, Headers, HDR_ENUM_END);
97474590 1708}
efd900cb 1709
1710const char *
9bea1d5b 1711httpHeaderNameById(int id)
efd900cb 1712{
9bea1d5b 1713 if (!Headers)
62e76326 1714 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1715
9bea1d5b 1716 assert(id >= 0 && id < HDR_ENUM_END);
62e76326 1717
99524de7 1718 return Headers[id].name.unsafeBuf();
efd900cb 1719}
0353e724 1720
924f73bc 1721int
a9925b40 1722HttpHeader::hasListMember(http_hdr_type id, const char *member, const char separator) const
924f73bc 1723{
1724 int result = 0;
1725 const char *pos = NULL;
1726 const char *item;
1727 int ilen;
1728 int mlen = strlen(member);
1729
924f73bc 1730 assert(id >= 0);
1731
30abd221 1732 String header (getStrOrList(id));
924f73bc 1733
9d0cdbb9 1734 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1735 if (strncmp(item, member, mlen) == 0
1736 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1737 result = 1;
1738 break;
1739 }
1740 }
1741
1742 return result;
1743}
1744
1745int
a9925b40 1746HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
9d0cdbb9 1747{
1748 int result = 0;
1749 const char *pos = NULL;
1750 const char *item;
1751 int ilen;
1752 int mlen = strlen(member);
1753
9d0cdbb9 1754 assert(name);
1755
30abd221 1756 String header (getByName(name));
9d0cdbb9 1757
924f73bc 1758 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1759 if (strncmp(item, member, mlen) == 0
1760 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1761 result = 1;
1762 break;
1763 }
1764 }
1765
1766 return result;
1767}
1768
2cdeea82 1769void
1770HttpHeader::removeHopByHopEntries()
1771{
1772 removeConnectionHeaderEntries();
26ac0430 1773
dcf3665b
AJ
1774 const HttpHeaderEntry *e;
1775 HttpHeaderPos pos = HttpHeaderInitPos;
1776 int headers_deleted = 0;
1777 while ((e = getEntry(&pos))) {
26ac0430
AJ
1778 int id = e->id;
1779 if (CBIT_TEST(HopByHopHeadersMask, id)) {
1780 delAt(pos, headers_deleted);
1781 CBIT_CLR(mask, id);
1782 }
dcf3665b 1783 }
2cdeea82 1784}
1785
924f73bc 1786void
1787HttpHeader::removeConnectionHeaderEntries()
1788{
a9925b40 1789 if (has(HDR_CONNECTION)) {
924f73bc 1790 /* anything that matches Connection list member will be deleted */
30abd221 1791 String strConnection;
26ac0430
AJ
1792
1793 (void) getList(HDR_CONNECTION, &strConnection);
924f73bc 1794 const HttpHeaderEntry *e;
1795 HttpHeaderPos pos = HttpHeaderInitPos;
1796 /*
1797 * think: on-average-best nesting of the two loops (hdrEntry
1798 * and strListItem) @?@
1799 */
1800 /*
1801 * maybe we should delete standard stuff ("keep-alive","close")
1802 * from strConnection first?
1803 */
1804
ba9fb01d 1805 int headers_deleted = 0;
a9925b40 1806 while ((e = getEntry(&pos))) {
99524de7 1807 if (strListIsMember(&strConnection, e->name.unsafeBuf(), ','))
ba9fb01d 1808 delAt(pos, headers_deleted);
924f73bc 1809 }
ba9fb01d 1810 if (headers_deleted)
1811 refreshMask();
924f73bc 1812 }
1813}