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