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