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