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