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