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