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