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