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