]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.cc
Merged from trunk
[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
f7f3304a 36#include "squid-old.h"
25f98340 37#include "base64.h"
528b2c61 38#include "HttpHdrContRange.h"
7ebe76de 39#include "HttpHdrCc.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"
25f98340 45#include "rfc1123.h"
00a7574e 46#include "StatHist.h"
8822ebee 47#include "Store.h"
81a94152 48#include "TimeOrTag.h"
cb69b4c7 49
50/*
2ac76861 51 * On naming conventions:
26ac0430
AJ
52 *
53 * HTTP/1.1 defines message-header as
54 *
2ac76861 55 * message-header = field-name ":" [ field-value ] CRLF
56 * field-name = token
57 * field-value = *( field-content | LWS )
26ac0430 58 *
2ac76861 59 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
60 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
26ac0430 61 *
2ac76861 62 * HttpHeader is an object that represents all message-headers in a message.
63 * HttpHeader does not manage start-line.
26ac0430 64 *
d8b249ef 65 * HttpHeader is implemented as a collection of header "entries".
66 * An entry is a (field_id, field_name, field_value) triplet.
2ac76861 67 */
cb69b4c7 68
69
cb69b4c7 70/*
71 * local constants and vars
72 */
73
74/*
26ac0430
AJ
75 * A table with major attributes for every known field.
76 * We calculate name lengths and reorganize this array on start up.
cb69b4c7 77 * After reorganization, field id can be used as an index to the table.
78 */
26ac0430
AJ
79static const HttpHeaderFieldAttrs HeadersAttrs[] = {
80 {"Accept", HDR_ACCEPT, ftStr},
81
82 {"Accept-Charset", HDR_ACCEPT_CHARSET, ftStr},
83 {"Accept-Encoding", HDR_ACCEPT_ENCODING, ftStr},
84 {"Accept-Language", HDR_ACCEPT_LANGUAGE, ftStr},
85 {"Accept-Ranges", HDR_ACCEPT_RANGES, ftStr},
86 {"Age", HDR_AGE, ftInt},
87 {"Allow", HDR_ALLOW, ftStr},
88 {"Authorization", HDR_AUTHORIZATION, ftStr}, /* for now */
89 {"Cache-Control", HDR_CACHE_CONTROL, ftPCc},
90 {"Connection", HDR_CONNECTION, ftStr},
91 {"Content-Base", HDR_CONTENT_BASE, ftStr},
a585e13d 92 {"Content-Disposition", HDR_CONTENT_DISPOSITION, ftStr}, /* for now */
26ac0430
AJ
93 {"Content-Encoding", HDR_CONTENT_ENCODING, ftStr},
94 {"Content-Language", HDR_CONTENT_LANGUAGE, ftStr},
95 {"Content-Length", HDR_CONTENT_LENGTH, ftInt64},
96 {"Content-Location", HDR_CONTENT_LOCATION, ftStr},
97 {"Content-MD5", HDR_CONTENT_MD5, ftStr}, /* for now */
98 {"Content-Range", HDR_CONTENT_RANGE, ftPContRange},
99 {"Content-Type", HDR_CONTENT_TYPE, ftStr},
100 {"Cookie", HDR_COOKIE, ftStr},
f89d4012 101 {"Cookie2", HDR_COOKIE2, ftStr},
26ac0430
AJ
102 {"Date", HDR_DATE, ftDate_1123},
103 {"ETag", HDR_ETAG, ftETag},
52b601ff 104 {"Expect", HDR_EXPECT, ftStr},
debd58cb 105 {"Expires", HDR_EXPIRES, ftDate_1123},
26ac0430
AJ
106 {"From", HDR_FROM, ftStr},
107 {"Host", HDR_HOST, ftStr},
108 {"If-Match", HDR_IF_MATCH, ftStr}, /* for now */
109 {"If-Modified-Since", HDR_IF_MODIFIED_SINCE, ftDate_1123},
110 {"If-None-Match", HDR_IF_NONE_MATCH, ftStr}, /* for now */
111 {"If-Range", HDR_IF_RANGE, ftDate_1123_or_ETag},
112 {"Keep-Alive", HDR_KEEP_ALIVE, ftStr},
113 {"Last-Modified", HDR_LAST_MODIFIED, ftDate_1123},
114 {"Link", HDR_LINK, ftStr},
115 {"Location", HDR_LOCATION, ftStr},
ef064e01 116 {"Max-Forwards", HDR_MAX_FORWARDS, ftInt64},
26ac0430 117 {"Mime-Version", HDR_MIME_VERSION, ftStr}, /* for now */
debd58cb 118 {"Negotiate", HDR_NEGOTIATE, ftStr},
3865965d 119 {"Origin", HDR_ORIGIN, ftStr},
26ac0430
AJ
120 {"Pragma", HDR_PRAGMA, ftStr},
121 {"Proxy-Authenticate", HDR_PROXY_AUTHENTICATE, ftStr},
122 {"Proxy-Authentication-Info", HDR_PROXY_AUTHENTICATION_INFO, ftStr},
123 {"Proxy-Authorization", HDR_PROXY_AUTHORIZATION, ftStr},
124 {"Proxy-Connection", HDR_PROXY_CONNECTION, ftStr},
125 {"Proxy-support", HDR_PROXY_SUPPORT, ftStr},
126 {"Public", HDR_PUBLIC, ftStr},
127 {"Range", HDR_RANGE, ftPRange},
128 {"Referer", HDR_REFERER, ftStr},
129 {"Request-Range", HDR_REQUEST_RANGE, ftPRange}, /* usually matches HDR_RANGE */
130 {"Retry-After", HDR_RETRY_AFTER, ftStr}, /* for now (ftDate_1123 or ftInt!) */
131 {"Server", HDR_SERVER, ftStr},
132 {"Set-Cookie", HDR_SET_COOKIE, ftStr},
f89d4012 133 {"Set-Cookie2", HDR_SET_COOKIE2, ftStr},
26ac0430
AJ
134 {"TE", HDR_TE, ftStr},
135 {"Title", HDR_TITLE, ftStr},
a1651bad 136 {"Trailer", HDR_TRAILER, ftStr},
26ac0430 137 {"Transfer-Encoding", HDR_TRANSFER_ENCODING, ftStr},
af48ea68
AJ
138 {"Translate", HDR_TRANSLATE, ftStr}, /* for now. may need to crop */
139 {"Unless-Modified-Since", HDR_UNLESS_MODIFIED_SINCE, ftStr}, /* for now ignore. may need to crop */
26ac0430
AJ
140 {"Upgrade", HDR_UPGRADE, ftStr}, /* for now */
141 {"User-Agent", HDR_USER_AGENT, ftStr},
142 {"Vary", HDR_VARY, ftStr}, /* for now */
143 {"Via", HDR_VIA, ftStr}, /* for now */
144 {"Warning", HDR_WARNING, ftStr}, /* for now */
145 {"WWW-Authenticate", HDR_WWW_AUTHENTICATE, ftStr},
146 {"Authentication-Info", HDR_AUTHENTICATION_INFO, ftStr},
147 {"X-Cache", HDR_X_CACHE, ftStr},
148 {"X-Cache-Lookup", HDR_X_CACHE_LOOKUP, ftStr},
149 {"X-Forwarded-For", HDR_X_FORWARDED_FOR, ftStr},
150 {"X-Request-URI", HDR_X_REQUEST_URI, ftStr},
151 {"X-Squid-Error", HDR_X_SQUID_ERROR, ftStr},
f66a9ef4 152#if X_ACCELERATOR_VARY
26ac0430 153 {"X-Accelerator-Vary", HDR_X_ACCELERATOR_VARY, ftStr},
a22e6cd3
AR
154#endif
155#if USE_ADAPTATION
156 {"X-Next-Services", HDR_X_NEXT_SERVICES, ftStr},
f66a9ef4 157#endif
26ac0430
AJ
158 {"Surrogate-Capability", HDR_SURROGATE_CAPABILITY, ftStr},
159 {"Surrogate-Control", HDR_SURROGATE_CONTROL, ftPSc},
160 {"Front-End-Https", HDR_FRONT_END_HTTPS, ftStr},
161 {"Other:", HDR_OTHER, ftStr} /* ':' will not allow matches */
162};
62e76326 163
de336bbe 164static HttpHeaderFieldInfo *Headers = NULL;
cb69b4c7 165
e6ccf245 166http_hdr_type &operator++ (http_hdr_type &aHeader)
167{
1f1ae50a 168 int tmp = (int)aHeader;
169 aHeader = (http_hdr_type)(++tmp);
e6ccf245 170 return aHeader;
171}
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
9bea1d5b 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
02259ff8 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
CT
458 if (e->id < 0 || e->id >= HDR_ENUM_END) {
459 debugs(55, 0, "HttpHeader::clean BUG: entry[" << pos << "] is invalid (" << e->id << "). Ignored.");
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));
a9925b40 547 HttpHeaderStats[owner].parsedCount++;
62e76326 548
1d8a896b 549 char *nulpos;
6d8633ea 550 if ((nulpos = (char*)memchr(header_start, '\0', header_end - header_start))) {
26ac0430 551 debugs(55, 1, "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
571 field_ptr++; /* Move to next line */
572
573 if (field_end > this_line && field_end[-1] == '\r') {
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) {
583 debugs(55, 1, "WARNING: Rejecting HTTP request with a CR+ "
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)) {
efc26e8e 593 debugs(55, 1, "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
599 while ((p = (char *)memchr(p, '\r', field_end - p)) != NULL)
600 *p++ = ' ';
601 } else
3a9cf2d5 602 goto reset;
52d3f198 603 }
604
605 if (this_line + 1 == field_end && this_line > field_start) {
efc26e8e 606 debugs(55, 1, "WARNING: Blank continuation line in HTTP header {" <<
26ac0430 607 getStringPrefix(header_start, header_end) << "}");
3a9cf2d5 608 goto reset;
52d3f198 609 }
610 } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t'));
611
612 if (field_start == field_end) {
613 if (field_ptr < header_end) {
efc26e8e 614 debugs(55, 1, "WARNING: unparseable HTTP header field near {" <<
26ac0430 615 getStringPrefix(field_start, header_end) << "}");
3a9cf2d5 616 goto reset;
52d3f198 617 }
618
619 break; /* terminating blank line */
620 }
62e76326 621
cdce6c61 622 if ((e = HttpHeaderEntry::parse(field_start, field_end)) == NULL) {
26ac0430
AJ
623 debugs(55, 1, "WARNING: unparseable HTTP header field {" <<
624 getStringPrefix(field_start, field_end) << "}");
efc26e8e 625 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 626 " in {" << getStringPrefix(header_start, header_end) << "}");
52d3f198 627
628 if (Config.onoff.relaxed_header_parser)
629 continue;
630 else
3a9cf2d5 631 goto reset;
47ac2ebe 632 }
62e76326 633
a9925b40 634 if (e->id == HDR_CONTENT_LENGTH && (e2 = findEntry(e->id)) != NULL) {
9d7a89a5
FC
635// if (e->value.cmp(e2->value.termedBuf()) != 0) {
636 if (e->value != e2->value) {
3e62bd58 637 int64_t l1, l2;
efc26e8e 638 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 639 "WARNING: found two conflicting content-length headers in {" << getStringPrefix(header_start, header_end) << "}");
9331e4b4 640
641 if (!Config.onoff.relaxed_header_parser) {
eede25e7 642 delete e;
3a9cf2d5 643 goto reset;
9331e4b4 644 }
645
9d7a89a5
FC
646 if (!httpHeaderParseOffset(e->value.termedBuf(), &l1)) {
647 debugs(55, 1, "WARNING: Unparseable content-length '" << e->value << "'");
eede25e7 648 delete e;
9331e4b4 649 continue;
9d7a89a5
FC
650 } else if (!httpHeaderParseOffset(e2->value.termedBuf(), &l2)) {
651 debugs(55, 1, "WARNING: Unparseable content-length '" << e2->value << "'");
a9925b40 652 delById(e2->id);
9331e4b4 653 } else if (l1 > l2) {
a9925b40 654 delById(e2->id);
9331e4b4 655 } else {
eede25e7 656 delete e;
9331e4b4 657 continue;
658 }
52d3f198 659 } else {
efc26e8e 660 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 661 "NOTICE: found double content-length header");
b3123159 662
663 if (Config.onoff.relaxed_header_parser) {
eede25e7 664 delete e;
b3123159 665 continue;
666 } else {
eede25e7 667 delete e;
3a9cf2d5 668 goto reset;
b3123159 669 }
52d3f198 670 }
671 }
62e76326 672
9d7a89a5 673 if (e->id == HDR_OTHER && stringHasWhitespace(e->name.termedBuf())) {
bf8fe701 674 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
26ac0430 675 "WARNING: found whitespace in HTTP header name {" <<
bf8fe701 676 getStringPrefix(field_start, field_end) << "}");
47ac2ebe 677
52d3f198 678 if (!Config.onoff.relaxed_header_parser) {
eede25e7 679 delete e;
3a9cf2d5 680 goto reset;
52d3f198 681 }
682 }
62e76326 683
a9925b40 684 addEntry(e);
cb69b4c7 685 }
62e76326 686
c3d0ba0c
AR
687 if (chunked()) {
688 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
689 delById(HDR_CONTENT_LENGTH);
690 }
691
3a9cf2d5 692 PROF_stop(HttpHeaderParse);
9bea1d5b 693 return 1; /* even if no fields where found, it is a valid header */
3a9cf2d5 694reset:
695 PROF_stop(HttpHeaderParse);
696 return reset();
cb69b4c7 697}
698
99edd1c3 699/* packs all the entries using supplied packer */
cb69b4c7 700void
d8f6c79c 701HttpHeader::packInto(Packer * p, bool mask_sensitive_info) const
cb69b4c7 702{
9bea1d5b 703 HttpHeaderPos pos = HttpHeaderInitPos;
704 const HttpHeaderEntry *e;
a9925b40 705 assert(p);
efc26e8e 706 debugs(55, 7, "packing hdr: (" << this << ")");
9bea1d5b 707 /* pack all entries one by one */
d8f6c79c
FC
708 while ((e = getEntry(&pos))) {
709 if (!mask_sensitive_info) {
710 e->packInto(p);
711 continue;
712 }
713 switch (e->id) {
d3823ea7
A
714 case HDR_AUTHORIZATION:
715 case HDR_PROXY_AUTHORIZATION:
716 packerAppend(p, e->name.rawBuf(), e->name.size());
717 packerAppend(p, ": ** NOT DISPLAYED **\r\n", 23);
718 break;
719 default:
720 e->packInto(p);
721 break;
d8f6c79c
FC
722 }
723 }
35b88ed2 724 /* Pack in the "special" entries */
725
726 /* Cache-Control */
cb69b4c7 727}
728
729/* returns next valid entry */
99edd1c3 730HttpHeaderEntry *
a9925b40 731HttpHeader::getEntry(HttpHeaderPos * pos) const
cb69b4c7 732{
a9925b40 733 assert(pos);
734 assert(*pos >= HttpHeaderInitPos && *pos < (ssize_t)entries.count);
62e76326 735
a9925b40 736 for ((*pos)++; *pos < (ssize_t)entries.count; (*pos)++) {
737 if (entries.items[*pos])
738 return (HttpHeaderEntry*)entries.items[*pos];
cb69b4c7 739 }
62e76326 740
9bea1d5b 741 return NULL;
cb69b4c7 742}
743
744/*
26ac0430 745 * returns a pointer to a specified entry if any
d8b249ef 746 * note that we return one entry so it does not make much sense to ask for
747 * "list" headers
cb69b4c7 748 */
de336bbe 749HttpHeaderEntry *
a9925b40 750HttpHeader::findEntry(http_hdr_type id) const
cb69b4c7 751{
9bea1d5b 752 HttpHeaderPos pos = HttpHeaderInitPos;
753 HttpHeaderEntry *e;
9bea1d5b 754 assert_eid(id);
755 assert(!CBIT_TEST(ListHeadersMask, id));
756
757 /* check mask first */
62e76326 758
a9925b40 759 if (!CBIT_TEST(mask, id))
62e76326 760 return NULL;
761
9bea1d5b 762 /* looks like we must have it, do linear search */
a9925b40 763 while ((e = getEntry(&pos))) {
62e76326 764 if (e->id == id)
765 return e;
cb69b4c7 766 }
62e76326 767
9bea1d5b 768 /* hm.. we thought it was there, but it was not found */
769 assert(0);
62e76326 770
9bea1d5b 771 return NULL; /* not reached */
cb69b4c7 772}
773
a622fff0 774/*
775 * same as httpHeaderFindEntry
776 */
a9925b40 777HttpHeaderEntry *
778HttpHeader::findLastEntry(http_hdr_type id) const
a622fff0 779{
9bea1d5b 780 HttpHeaderPos pos = HttpHeaderInitPos;
781 HttpHeaderEntry *e;
782 HttpHeaderEntry *result = NULL;
9bea1d5b 783 assert_eid(id);
784 assert(!CBIT_TEST(ListHeadersMask, id));
785
786 /* check mask first */
62e76326 787
a9925b40 788 if (!CBIT_TEST(mask, id))
62e76326 789 return NULL;
790
9bea1d5b 791 /* looks like we must have it, do linear search */
a9925b40 792 while ((e = getEntry(&pos))) {
62e76326 793 if (e->id == id)
794 result = e;
a622fff0 795 }
62e76326 796
9bea1d5b 797 assert(result); /* must be there! */
798 return result;
a622fff0 799}
800
cb69b4c7 801/*
26ac0430 802 * deletes all fields with a given name if any, returns #fields deleted;
cb69b4c7 803 */
2ac76861 804int
a9925b40 805HttpHeader::delByName(const char *name)
cb69b4c7 806{
9bea1d5b 807 int count = 0;
808 HttpHeaderPos pos = HttpHeaderInitPos;
809 HttpHeaderEntry *e;
a9925b40 810 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
efc26e8e 811 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
62e76326 812
a9925b40 813 while ((e = getEntry(&pos))) {
30abd221 814 if (!e->name.caseCmp(name))
ba9fb01d 815 delAt(pos, count);
816 else
a9925b40 817 CBIT_SET(mask, e->id);
cb69b4c7 818 }
62e76326 819
9bea1d5b 820 return count;
cb69b4c7 821}
822
2246b732 823/* deletes all entries with a given id, returns the #entries deleted */
824int
a9925b40 825HttpHeader::delById(http_hdr_type id)
d8b249ef 826{
9bea1d5b 827 int count = 0;
828 HttpHeaderPos pos = HttpHeaderInitPos;
829 HttpHeaderEntry *e;
efc26e8e 830 debugs(55, 8, this << " del-by-id " << id);
9bea1d5b 831 assert_eid(id);
e6ca7a9b 832 assert(id != HDR_OTHER); /* does not make sense */
62e76326 833
a9925b40 834 if (!CBIT_TEST(mask, id))
62e76326 835 return 0;
836
a9925b40 837 while ((e = getEntry(&pos))) {
ba9fb01d 838 if (e->id == id)
839 delAt(pos, count);
d8b249ef 840 }
62e76326 841
a9925b40 842 CBIT_CLR(mask, id);
9bea1d5b 843 assert(count);
844 return count;
d8b249ef 845}
d8b249ef 846
cb69b4c7 847/*
848 * deletes an entry at pos and leaves a gap; leaving a gap makes it
849 * possible to iterate(search) and delete fields at the same time
ba9fb01d 850 * NOTE: Does not update the header mask. Caller must follow up with
851 * a call to refreshMask() if headers_deleted was incremented.
cb69b4c7 852 */
2246b732 853void
ba9fb01d 854HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
cb69b4c7 855{
9bea1d5b 856 HttpHeaderEntry *e;
a9925b40 857 assert(pos >= HttpHeaderInitPos && pos < (ssize_t)entries.count);
858 e = (HttpHeaderEntry*)entries.items[pos];
859 entries.items[pos] = NULL;
9bea1d5b 860 /* decrement header length, allow for ": " and crlf */
a9925b40 861 len -= e->name.size() + 2 + e->value.size() + 2;
862 assert(len >= 0);
eede25e7 863 delete e;
ba9fb01d 864 ++headers_deleted;
cb69b4c7 865}
866
394499bd 867/*
868 * Compacts the header storage
869 */
870void
871HttpHeader::compact()
872{
873 entries.prune(NULL);
874}
875
ba9fb01d 876/*
877 * Refreshes the header mask. Required after delAt() calls.
878 */
879void
880HttpHeader::refreshMask()
881{
882 httpHeaderMaskInit(&mask, 0);
883 debugs(55, 7, "refreshing the mask in hdr " << this);
884 HttpHeaderPos pos = HttpHeaderInitPos;
885 while (HttpHeaderEntry *e = getEntry(&pos)) {
26ac0430 886 CBIT_SET(mask, e->id);
ba9fb01d 887 }
888}
99edd1c3 889
62e76326 890/* appends an entry;
eede25e7 891 * does not call e->clone() so one should not reuse "*e"
cb69b4c7 892 */
99edd1c3 893void
a9925b40 894HttpHeader::addEntry(HttpHeaderEntry * e)
cb69b4c7 895{
a9925b40 896 assert(e);
9bea1d5b 897 assert_eid(e->id);
59c50530 898 assert(e->name.size());
9bea1d5b 899
30c48b1a 900 debugs(55, 7, HERE << this << " adding entry: " << e->id << " at " << entries.count);
62e76326 901
a9925b40 902 if (CBIT_TEST(mask, e->id))
62e76326 903 Headers[e->id].stat.repCount++;
9bea1d5b 904 else
a9925b40 905 CBIT_SET(mask, e->id);
62e76326 906
a9925b40 907 entries.push_back(e);
62e76326 908
9bea1d5b 909 /* increment header length, allow for ": " and crlf */
a9925b40 910 len += e->name.size() + 2 + e->value.size() + 2;
cb69b4c7 911}
912
bbe58ab5 913/* inserts an entry;
eede25e7 914 * does not call e->clone() so one should not reuse "*e"
bbe58ab5 915 */
916void
a9925b40 917HttpHeader::insertEntry(HttpHeaderEntry * e)
bbe58ab5 918{
a9925b40 919 assert(e);
bbe58ab5 920 assert_eid(e->id);
921
30c48b1a 922 debugs(55, 7, HERE << this << " adding entry: " << e->id << " at " << entries.count);
bbe58ab5 923
a9925b40 924 if (CBIT_TEST(mask, e->id))
bbe58ab5 925 Headers[e->id].stat.repCount++;
926 else
a9925b40 927 CBIT_SET(mask, e->id);
bbe58ab5 928
a9925b40 929 entries.insert(e);
bbe58ab5 930
931 /* increment header length, allow for ": " and crlf */
a9925b40 932 len += e->name.size() + 2 + e->value.size() + 2;
bbe58ab5 933}
934
35b88ed2 935bool
30abd221 936HttpHeader::getList(http_hdr_type id, String *s) const
35b88ed2 937{
938 HttpHeaderEntry *e;
939 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 940 debugs(55, 9, this << " joining for id " << id);
35b88ed2 941 /* only fields from ListHeaders array can be "listed" */
942 assert(CBIT_TEST(ListHeadersMask, id));
943
944 if (!CBIT_TEST(mask, id))
945 return false;
946
947 while ((e = getEntry(&pos))) {
948 if (e->id == id)
9d7a89a5 949 strListAdd(s, e->value.termedBuf(), ',');
35b88ed2 950 }
951
952 /*
953 * note: we might get an empty (size==0) string if there was an "empty"
954 * header. This results in an empty length String, which may have a NULL
955 * buffer.
956 */
957 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
958 if (!s->size())
959 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
960 else
961 debugs(55, 6, this << ": joined for id " << id << ": " << s);
962
963 return true;
964}
965
99edd1c3 966/* return a list of entries with the same id separated by ',' and ws */
30abd221 967String
a9925b40 968HttpHeader::getList(http_hdr_type id) const
cb69b4c7 969{
9bea1d5b 970 HttpHeaderEntry *e;
971 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 972 debugs(55, 9, this << "joining for id " << id);
9bea1d5b 973 /* only fields from ListHeaders array can be "listed" */
974 assert(CBIT_TEST(ListHeadersMask, id));
62e76326 975
a9925b40 976 if (!CBIT_TEST(mask, id))
30abd221 977 return String();
650c4b88 978
30abd221 979 String s;
62e76326 980
a9925b40 981 while ((e = getEntry(&pos))) {
62e76326 982 if (e->id == id)
9d7a89a5 983 strListAdd(&s, e->value.termedBuf(), ',');
d35b9a94 984 }
62e76326 985
9bea1d5b 986 /*
948078e3 987 * note: we might get an empty (size==0) string if there was an "empty"
988 * header. This results in an empty length String, which may have a NULL
989 * buffer.
9bea1d5b 990 */
948078e3 991 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
528b2c61 992 if (!s.size())
948078e3 993 debugs(55, 3, "empty list header: " << Headers[id].name << "(" << id << ")");
994 else
995 debugs(55, 6, this << ": joined for id " << id << ": " << s);
62e76326 996
9bea1d5b 997 return s;
cb69b4c7 998}
999
f66a9ef4 1000/* return a string or list of entries with the same id separated by ',' and ws */
30abd221 1001String
a9925b40 1002HttpHeader::getStrOrList(http_hdr_type id) const
f66a9ef4 1003{
c7327fa0 1004 HttpHeaderEntry *e;
9bea1d5b 1005
1006 if (CBIT_TEST(ListHeadersMask, id))
a9925b40 1007 return getList(id);
62e76326 1008
a9925b40 1009 if ((e = findEntry(id)))
62e76326 1010 return e->value;
1011
30abd221 1012 return String();
f66a9ef4 1013}
1014
1015/*
bd412580 1016 * Returns the value of the specified header.
f66a9ef4 1017 */
30abd221 1018String
a9925b40 1019HttpHeader::getByName(const char *name) const
f66a9ef4 1020{
9bea1d5b 1021 http_hdr_type id;
1022 HttpHeaderPos pos = HttpHeaderInitPos;
1023 HttpHeaderEntry *e;
9bea1d5b 1024
9bea1d5b 1025 assert(name);
1026
1027 /* First try the quick path */
1028 id = httpHeaderIdByNameDef(name, strlen(name));
62e76326 1029
9bea1d5b 1030 if (id != -1)
a9925b40 1031 return getStrOrList(id);
9bea1d5b 1032
30abd221 1033 String result;
650c4b88 1034
9bea1d5b 1035 /* Sorry, an unknown header name. Do linear search */
a9925b40 1036 while ((e = getEntry(&pos))) {
30abd221 1037 if (e->id == HDR_OTHER && e->name.caseCmp(name) == 0) {
9d7a89a5 1038 strListAdd(&result, e->value.termedBuf(), ',');
62e76326 1039 }
f66a9ef4 1040 }
62e76326 1041
9bea1d5b 1042 return result;
f66a9ef4 1043}
cb69b4c7 1044
14b463aa 1045/*
372fdfbf 1046 * Returns a the value of the specified list member, if any.
14b463aa 1047 */
30abd221 1048String
a9925b40 1049HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
14b463aa 1050{
30abd221 1051 String header;
14b463aa 1052 const char *pos = NULL;
1053 const char *item;
1054 int ilen;
1055 int mlen = strlen(member);
1056
14b463aa 1057 assert(name);
1058
a9925b40 1059 header = getByName(name);
14b463aa 1060
30abd221 1061 String result;
650c4b88 1062
14b463aa 1063 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1064 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1065 result.append(item + mlen + 1, ilen - mlen - 1);
1066 break;
1067 }
14b463aa 1068 }
62e76326 1069
14b463aa 1070 return result;
1071}
1072
1073/*
1074 * returns a the value of the specified list member, if any.
1075 */
30abd221 1076String
a9925b40 1077HttpHeader::getListMember(http_hdr_type id, const char *member, const char separator) const
14b463aa 1078{
30abd221 1079 String header;
14b463aa 1080 const char *pos = NULL;
1081 const char *item;
1082 int ilen;
1083 int mlen = strlen(member);
1084
14b463aa 1085 assert(id >= 0);
1086
a9925b40 1087 header = getStrOrList(id);
30abd221 1088 String result;
14b463aa 1089
1090 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 1091 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
1092 result.append(item + mlen + 1, ilen - mlen - 1);
1093 break;
1094 }
14b463aa 1095 }
62e76326 1096
30abd221 1097 header.clean();
14b463aa 1098 return result;
1099}
1100
cb69b4c7 1101/* test if a field is present */
2ac76861 1102int
a9925b40 1103HttpHeader::has(http_hdr_type id) const
cb69b4c7 1104{
9bea1d5b 1105 assert_eid(id);
1106 assert(id != HDR_OTHER);
efc26e8e 1107 debugs(55, 9, this << " lookup for " << id);
a9925b40 1108 return CBIT_TEST(mask, id);
cb69b4c7 1109}
1110
1111void
a9925b40 1112HttpHeader::putInt(http_hdr_type id, int number)
cb69b4c7 1113{
9bea1d5b 1114 assert_eid(id);
1115 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
1116 assert(number >= 0);
eede25e7 1117 addEntry(new HttpHeaderEntry(id, NULL, xitoa(number)));
cb69b4c7 1118}
1119
47f6e231 1120void
1121HttpHeader::putInt64(http_hdr_type id, int64_t number)
1122{
1123 assert_eid(id);
1124 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1125 assert(number >= 0);
1126 addEntry(new HttpHeaderEntry(id, NULL, xint64toa(number)));
1127}
1128
cb69b4c7 1129void
a9925b40 1130HttpHeader::putTime(http_hdr_type id, time_t htime)
cb69b4c7 1131{
9bea1d5b 1132 assert_eid(id);
1133 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
a1d6870f 1134 assert(htime >= 0);
eede25e7 1135 addEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
cb69b4c7 1136}
2ac76861 1137
bbe58ab5 1138void
a9925b40 1139HttpHeader::insertTime(http_hdr_type id, time_t htime)
bbe58ab5 1140{
1141 assert_eid(id);
1142 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
1143 assert(htime >= 0);
eede25e7 1144 insertEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
bbe58ab5 1145}
1146
cb69b4c7 1147void
a9925b40 1148HttpHeader::putStr(http_hdr_type id, const char *str)
cb69b4c7 1149{
9bea1d5b 1150 assert_eid(id);
1151 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
1152 assert(str);
eede25e7 1153 addEntry(new HttpHeaderEntry(id, NULL, str));
cb69b4c7 1154}
1155
63259c34 1156void
a9925b40 1157HttpHeader::putAuth(const char *auth_scheme, const char *realm)
63259c34 1158{
a9925b40 1159 assert(auth_scheme && realm);
1160 httpHeaderPutStrf(this, HDR_WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
63259c34 1161}
1162
99edd1c3 1163void
a9925b40 1164HttpHeader::putCc(const HttpHdrCc * cc)
99edd1c3 1165{
9bea1d5b 1166 MemBuf mb;
1167 Packer p;
a9925b40 1168 assert(cc);
9bea1d5b 1169 /* remove old directives if any */
a9925b40 1170 delById(HDR_CACHE_CONTROL);
9bea1d5b 1171 /* pack into mb */
2fe7eff9 1172 mb.init();
9bea1d5b 1173 packerToMemInit(&p, &mb);
4ce6e3b5 1174 cc->packInto(&p);
9bea1d5b 1175 /* put */
eede25e7 1176 addEntry(new HttpHeaderEntry(HDR_CACHE_CONTROL, NULL, mb.buf));
9bea1d5b 1177 /* cleanup */
1178 packerClean(&p);
2fe7eff9 1179 mb.clean();
99edd1c3 1180}
1181
d192d11f 1182void
a9925b40 1183HttpHeader::putContRange(const HttpHdrContRange * cr)
d192d11f 1184{
9bea1d5b 1185 MemBuf mb;
1186 Packer p;
a9925b40 1187 assert(cr);
9bea1d5b 1188 /* remove old directives if any */
a9925b40 1189 delById(HDR_CONTENT_RANGE);
9bea1d5b 1190 /* pack into mb */
2fe7eff9 1191 mb.init();
9bea1d5b 1192 packerToMemInit(&p, &mb);
1193 httpHdrContRangePackInto(cr, &p);
1194 /* put */
eede25e7 1195 addEntry(new HttpHeaderEntry(HDR_CONTENT_RANGE, NULL, mb.buf));
9bea1d5b 1196 /* cleanup */
1197 packerClean(&p);
2fe7eff9 1198 mb.clean();
d192d11f 1199}
1200
1201void
a9925b40 1202HttpHeader::putRange(const HttpHdrRange * range)
d192d11f 1203{
9bea1d5b 1204 MemBuf mb;
1205 Packer p;
a9925b40 1206 assert(range);
9bea1d5b 1207 /* remove old directives if any */
a9925b40 1208 delById(HDR_RANGE);
9bea1d5b 1209 /* pack into mb */
2fe7eff9 1210 mb.init();
9bea1d5b 1211 packerToMemInit(&p, &mb);
528b2c61 1212 range->packInto(&p);
9bea1d5b 1213 /* put */
eede25e7 1214 addEntry(new HttpHeaderEntry(HDR_RANGE, NULL, mb.buf));
9bea1d5b 1215 /* cleanup */
1216 packerClean(&p);
2fe7eff9 1217 mb.clean();
d192d11f 1218}
1219
43ae1d95 1220void
a9925b40 1221HttpHeader::putSc(HttpHdrSc *sc)
43ae1d95 1222{
1223 MemBuf mb;
1224 Packer p;
a9925b40 1225 assert(sc);
43ae1d95 1226 /* remove old directives if any */
f48978e6 1227 delById(HDR_SURROGATE_CONTROL);
43ae1d95 1228 /* pack into mb */
2fe7eff9 1229 mb.init();
43ae1d95 1230 packerToMemInit(&p, &mb);
45a58345 1231 sc->packInto(&p);
43ae1d95 1232 /* put */
eede25e7 1233 addEntry(new HttpHeaderEntry(HDR_SURROGATE_CONTROL, NULL, mb.buf));
43ae1d95 1234 /* cleanup */
1235 packerClean(&p);
2fe7eff9 1236 mb.clean();
43ae1d95 1237}
1238
bcfba8bd
AR
1239void
1240HttpHeader::putWarning(const int code, const char *const text)
1241{
1242 char buf[512];
1243 snprintf(buf, sizeof(buf), "%i %s \"%s\"", code, visible_appname_string, text);
1244 putStr(HDR_WARNING, buf);
1245}
1246
cb69b4c7 1247/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1248void
a9925b40 1249HttpHeader::putExt(const char *name, const char *value)
cb69b4c7 1250{
9bea1d5b 1251 assert(name && value);
efc26e8e 1252 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
eede25e7 1253 addEntry(new HttpHeaderEntry(HDR_OTHER, name, value));
528b2c61 1254}
1255
1256int
a9925b40 1257HttpHeader::getInt(http_hdr_type id) const
528b2c61 1258{
9bea1d5b 1259 assert_eid(id);
1260 assert(Headers[id].type == ftInt); /* must be of an appropriate type */
528b2c61 1261 HttpHeaderEntry *e;
62e76326 1262
a9925b40 1263 if ((e = findEntry(id)))
eede25e7 1264 return e->getInt();
62e76326 1265
528b2c61 1266 return -1;
7c525cc2 1267}
1268
47f6e231 1269int64_t
1270HttpHeader::getInt64(http_hdr_type id) const
1271{
1272 assert_eid(id);
1273 assert(Headers[id].type == ftInt64); /* must be of an appropriate type */
1274 HttpHeaderEntry *e;
1275
1276 if ((e = findEntry(id)))
1277 return e->getInt64();
1278
1279 return -1;
1280}
1281
de336bbe 1282time_t
a9925b40 1283HttpHeader::getTime(http_hdr_type id) const
cb69b4c7 1284{
9bea1d5b 1285 HttpHeaderEntry *e;
1286 time_t value = -1;
1287 assert_eid(id);
1288 assert(Headers[id].type == ftDate_1123); /* must be of an appropriate type */
62e76326 1289
a9925b40 1290 if ((e = findEntry(id))) {
9d7a89a5 1291 value = parse_rfc1123(e->value.termedBuf());
62e76326 1292 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
d8b249ef 1293 }
62e76326 1294
9bea1d5b 1295 return value;
cb69b4c7 1296}
1297
99edd1c3 1298/* sync with httpHeaderGetLastStr */
de336bbe 1299const char *
a9925b40 1300HttpHeader::getStr(http_hdr_type id) const
cb69b4c7 1301{
9bea1d5b 1302 HttpHeaderEntry *e;
1303 assert_eid(id);
1304 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1305
a9925b40 1306 if ((e = findEntry(id))) {
62e76326 1307 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
9d7a89a5 1308 return e->value.termedBuf();
d8b249ef 1309 }
62e76326 1310
9bea1d5b 1311 return NULL;
cb69b4c7 1312}
1313
a622fff0 1314/* unusual */
1315const char *
a9925b40 1316HttpHeader::getLastStr(http_hdr_type id) const
a622fff0 1317{
9bea1d5b 1318 HttpHeaderEntry *e;
1319 assert_eid(id);
1320 assert(Headers[id].type == ftStr); /* must be of an appropriate type */
62e76326 1321
a9925b40 1322 if ((e = findLastEntry(id))) {
62e76326 1323 httpHeaderNoteParsedEntry(e->id, e->value, 0); /* no errors are possible */
9d7a89a5 1324 return e->value.termedBuf();
a622fff0 1325 }
62e76326 1326
9bea1d5b 1327 return NULL;
a622fff0 1328}
1329
7faf2bdb 1330HttpHdrCc *
a9925b40 1331HttpHeader::getCc() const
cb69b4c7 1332{
a9925b40 1333 if (!CBIT_TEST(mask, HDR_CACHE_CONTROL))
62e76326 1334 return NULL;
35b88ed2 1335 PROF_start(HttpHeader_getCc);
62e76326 1336
c57b8f05
FC
1337 String s;
1338 getList(HDR_CACHE_CONTROL, &s);
62e76326 1339
c57b8f05 1340 HttpHdrCc *cc=new HttpHdrCc();
7ebe76de
FC
1341
1342 if (!cc->parse(s)) {
cf66f10a
FC
1343 delete cc;
1344 cc = NULL;
1345 }
62e76326 1346
a9925b40 1347 HttpHeaderStats[owner].ccParsedCount++;
62e76326 1348
9bea1d5b 1349 if (cc)
a9925b40 1350 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
62e76326 1351
9bea1d5b 1352 httpHeaderNoteParsedEntry(HDR_CACHE_CONTROL, s, !cc);
62e76326 1353
35b88ed2 1354 PROF_stop(HttpHeader_getCc);
62e76326 1355
9bea1d5b 1356 return cc;
cb69b4c7 1357}
1358
02922e76 1359HttpHdrRange *
a9925b40 1360HttpHeader::getRange() const
02922e76 1361{
9bea1d5b 1362 HttpHdrRange *r = NULL;
1363 HttpHeaderEntry *e;
1364 /* some clients will send "Request-Range" _and_ *matching* "Range"
1365 * who knows, some clients might send Request-Range only;
1366 * this "if" should work correctly in both cases;
1367 * hopefully no clients send mismatched headers! */
62e76326 1368
a9925b40 1369 if ((e = findEntry(HDR_RANGE)) ||
1370 (e = findEntry(HDR_REQUEST_RANGE))) {
62e76326 1371 r = HttpHdrRange::ParseCreate(&e->value);
1372 httpHeaderNoteParsedEntry(e->id, e->value, !r);
d192d11f 1373 }
62e76326 1374
9bea1d5b 1375 return r;
02922e76 1376}
1377
43ae1d95 1378HttpHdrSc *
a9925b40 1379HttpHeader::getSc() const
43ae1d95 1380{
a9925b40 1381 if (!CBIT_TEST(mask, HDR_SURROGATE_CONTROL))
43ae1d95 1382 return NULL;
1383
30abd221 1384 String s;
26ac0430 1385
35b88ed2 1386 (void) getList(HDR_SURROGATE_CONTROL, &s);
43ae1d95 1387
45a58345 1388 HttpHdrSc *sc = httpHdrScParseCreate(s);
43ae1d95 1389
45a58345 1390 ++HttpHeaderStats[owner].ccParsedCount;
43ae1d95 1391
1392 if (sc)
45a58345 1393 sc->updateStats(&HttpHeaderStats[owner].scTypeDistr);
43ae1d95 1394
1395 httpHeaderNoteParsedEntry(HDR_SURROGATE_CONTROL, s, !sc);
1396
1397 return sc;
1398}
1399
d76fcfa7 1400HttpHdrContRange *
a9925b40 1401HttpHeader::getContRange() const
d76fcfa7 1402{
9bea1d5b 1403 HttpHdrContRange *cr = NULL;
1404 HttpHeaderEntry *e;
62e76326 1405
a9925b40 1406 if ((e = findEntry(HDR_CONTENT_RANGE))) {
9d7a89a5 1407 cr = httpHdrContRangeParseCreate(e->value.termedBuf());
62e76326 1408 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
d8b249ef 1409 }
62e76326 1410
9bea1d5b 1411 return cr;
cb69b4c7 1412}
1413
99edd1c3 1414const char *
a9925b40 1415HttpHeader::getAuth(http_hdr_type id, const char *auth_scheme) const
cb69b4c7 1416{
9bea1d5b 1417 const char *field;
1418 int l;
a9925b40 1419 assert(auth_scheme);
1420 field = getStr(id);
62e76326 1421
9bea1d5b 1422 if (!field) /* no authorization field */
62e76326 1423 return NULL;
1424
9bea1d5b 1425 l = strlen(auth_scheme);
62e76326 1426
9bea1d5b 1427 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
62e76326 1428 return NULL;
1429
9bea1d5b 1430 field += l;
62e76326 1431
9bea1d5b 1432 if (!xisspace(*field)) /* wrong scheme */
62e76326 1433 return NULL;
1434
9bea1d5b 1435 /* skip white space */
d97aa9ff 1436 for (; field && xisspace(*field); field++);
62e76326 1437
9bea1d5b 1438 if (!*field) /* no authorization cookie */
62e76326 1439 return NULL;
1440
8bdd0cec
AJ
1441 static char decodedAuthToken[8192];
1442 const int decodedLen = base64_decode(decodedAuthToken, sizeof(decodedAuthToken)-1, field);
1443 decodedAuthToken[decodedLen] = '\0';
1444 return decodedAuthToken;
cb69b4c7 1445}
1446
a9771e51 1447ETag
a9925b40 1448HttpHeader::getETag(http_hdr_type id) const
a9771e51 1449{
26ac0430 1450 ETag etag = {NULL, -1};
9bea1d5b 1451 HttpHeaderEntry *e;
1452 assert(Headers[id].type == ftETag); /* must be of an appropriate type */
62e76326 1453
a9925b40 1454 if ((e = findEntry(id)))
9d7a89a5 1455 etagParseInit(&etag, e->value.termedBuf());
62e76326 1456
9bea1d5b 1457 return etag;
a9771e51 1458}
1459
1460TimeOrTag
a9925b40 1461HttpHeader::getTimeOrTag(http_hdr_type id) const
a9771e51 1462{
9bea1d5b 1463 TimeOrTag tot;
1464 HttpHeaderEntry *e;
1465 assert(Headers[id].type == ftDate_1123_or_ETag); /* must be of an appropriate type */
1466 memset(&tot, 0, sizeof(tot));
62e76326 1467
a9925b40 1468 if ((e = findEntry(id))) {
9d7a89a5 1469 const char *str = e->value.termedBuf();
62e76326 1470 /* try as an ETag */
1471
1472 if (etagParseInit(&tot.tag, str)) {
1473 tot.valid = tot.tag.str != NULL;
1474 tot.time = -1;
1475 } else {
1476 /* or maybe it is time? */
1477 tot.time = parse_rfc1123(str);
1478 tot.valid = tot.time >= 0;
1479 tot.tag.str = NULL;
1480 }
a9771e51 1481 }
62e76326 1482
9bea1d5b 1483 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
1484 return tot;
a9771e51 1485}
1486
cb69b4c7 1487/*
1488 * HttpHeaderEntry
1489 */
1490
eede25e7 1491HttpHeaderEntry::HttpHeaderEntry(http_hdr_type anId, const char *aName, const char *aValue)
cb69b4c7 1492{
eede25e7 1493 assert_eid(anId);
1494 id = anId;
62e76326 1495
9bea1d5b 1496 if (id != HDR_OTHER)
eede25e7 1497 name = Headers[id].name;
9bea1d5b 1498 else
eede25e7 1499 name = aName;
62e76326 1500
eede25e7 1501 value = aValue;
62e76326 1502
9bea1d5b 1503 Headers[id].stat.aliveCount++;
62e76326 1504
9d7a89a5 1505 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name << " : " << value );
eede25e7 1506}
62e76326 1507
eede25e7 1508HttpHeaderEntry::~HttpHeaderEntry()
2ac76861 1509{
eede25e7 1510 assert_eid(id);
9d7a89a5 1511 debugs(55, 9, "destroying entry " << this << ": '" << name << ": " << value << "'");
30abd221 1512 /* clean name if needed */
1513
1514 if (id == HDR_OTHER)
1515 name.clean();
1516
1517 value.clean();
62e76326 1518
eede25e7 1519 assert(Headers[id].stat.aliveCount);
62e76326 1520
eede25e7 1521 Headers[id].stat.aliveCount--;
62e76326 1522
eede25e7 1523 id = HDR_BAD_HDR;
cb69b4c7 1524}
1525
eede25e7 1526/* parses and inits header entry, returns true/false */
cdce6c61 1527HttpHeaderEntry *
eede25e7 1528HttpHeaderEntry::parse(const char *field_start, const char *field_end)
cb69b4c7 1529{
9bea1d5b 1530 /* note: name_start == field_start */
52d3f198 1531 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
26ac0430 1532 int name_len = name_end ? name_end - field_start :0;
9bea1d5b 1533 const char *value_start = field_start + name_len + 1; /* skip ':' */
1534 /* note: value_end == field_end */
1535
1536 HeaderEntryParsedCount++;
1537
1538 /* do we have a valid field name within this field? */
62e76326 1539
9bea1d5b 1540 if (!name_len || name_end > field_end)
cdce6c61 1541 return NULL;
62e76326 1542
67746eea 1543 if (name_len > 65534) {
1544 /* String must be LESS THAN 64K and it adds a terminating NULL */
efc26e8e 1545 debugs(55, 1, "WARNING: ignoring header name of " << name_len << " bytes");
cdce6c61 1546 return NULL;
25acfb53 1547 }
62e76326 1548
52d3f198 1549 if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) {
bf8fe701 1550 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
1551 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end) << "'");
52d3f198 1552
1553 while (name_len > 0 && xisspace(field_start[name_len - 1]))
1554 name_len--;
1555
1556 if (!name_len)
cdce6c61 1557 return NULL;
52d3f198 1558 }
1559
9bea1d5b 1560 /* now we know we can parse it */
62e76326 1561
bf8fe701 1562 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end) << "'");
62e76326 1563
9bea1d5b 1564 /* is it a "known" field? */
cdce6c61 1565 http_hdr_type id = httpHeaderIdByName(field_start, name_len, Headers, HDR_ENUM_END);
1566
30abd221 1567 String name;
cdce6c61 1568
30abd221 1569 String value;
62e76326 1570
9bea1d5b 1571 if (id < 0)
62e76326 1572 id = HDR_OTHER;
1573
9bea1d5b 1574 assert_eid(id);
62e76326 1575
9bea1d5b 1576 /* set field name */
1577 if (id == HDR_OTHER)
eede25e7 1578 name.limitInit(field_start, name_len);
9bea1d5b 1579 else
eede25e7 1580 name = Headers[id].name;
62e76326 1581
9bea1d5b 1582 /* trim field value */
1583 while (value_start < field_end && xisspace(*value_start))
62e76326 1584 value_start++;
1585
52d3f198 1586 while (value_start < field_end && xisspace(field_end[-1]))
1587 field_end--;
1588
67746eea 1589 if (field_end - value_start > 65534) {
1590 /* String must be LESS THAN 64K and it adds a terminating NULL */
9d7a89a5 1591 debugs(55, 1, "WARNING: ignoring '" << name << "' header of " << (field_end - value_start) << " bytes");
62e76326 1592
eede25e7 1593 if (id == HDR_OTHER)
30abd221 1594 name.clean();
62e76326 1595
cdce6c61 1596 return NULL;
25acfb53 1597 }
62e76326 1598
9bea1d5b 1599 /* set field value */
eede25e7 1600 value.limitInit(value_start, field_end - value_start);
62e76326 1601
9bea1d5b 1602 Headers[id].stat.seenCount++;
62e76326 1603
9d7a89a5 1604 debugs(55, 9, "parsed HttpHeaderEntry: '" << name << ": " << value << "'");
62e76326 1605
9d7a89a5 1606 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1607}
1608
99edd1c3 1609HttpHeaderEntry *
eede25e7 1610HttpHeaderEntry::clone() const
cb69b4c7 1611{
9d7a89a5 1612 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1613}
1614
de336bbe 1615void
eede25e7 1616HttpHeaderEntry::packInto(Packer * p) const
cb69b4c7 1617{
eede25e7 1618 assert(p);
9d7a89a5 1619 packerAppend(p, name.rawBuf(), name.size());
9bea1d5b 1620 packerAppend(p, ": ", 2);
9d7a89a5 1621 packerAppend(p, value.rawBuf(), value.size());
9bea1d5b 1622 packerAppend(p, "\r\n", 2);
cb69b4c7 1623}
1624
eede25e7 1625int
1626HttpHeaderEntry::getInt() const
1627{
1628 assert_eid (id);
1629 assert (Headers[id].type == ftInt);
1630 int val = -1;
9d7a89a5 1631 int ok = httpHeaderParseInt(value.termedBuf(), &val);
eede25e7 1632 httpHeaderNoteParsedEntry(id, value, !ok);
1633 /* XXX: Should we check ok - ie
1634 * return ok ? -1 : value;
1635 */
1636 return val;
1637}
1638
47f6e231 1639int64_t
1640HttpHeaderEntry::getInt64() const
1641{
1642 assert_eid (id);
1643 assert (Headers[id].type == ftInt64);
1644 int64_t val = -1;
9d7a89a5 1645 int ok = httpHeaderParseOffset(value.termedBuf(), &val);
47f6e231 1646 httpHeaderNoteParsedEntry(id, value, !ok);
1647 /* XXX: Should we check ok - ie
1648 * return ok ? -1 : value;
1649 */
1650 return val;
1651}
1652
cb69b4c7 1653static void
30abd221 1654httpHeaderNoteParsedEntry(http_hdr_type id, String const &context, int error)
cb69b4c7 1655{
9bea1d5b 1656 Headers[id].stat.parsCount++;
62e76326 1657
9bea1d5b 1658 if (error) {
62e76326 1659 Headers[id].stat.errCount++;
9d7a89a5 1660 debugs(55, 2, "cannot parse hdr field: '" << Headers[id].name << ": " << context << "'");
cb69b4c7 1661 }
cb69b4c7 1662}
1663
cb69b4c7 1664/*
12cf1be2 1665 * Reports
cb69b4c7 1666 */
cb69b4c7 1667
fcd2d3ef 1668/* tmp variable used to pass stat info to dumpers */
0cdcddb9 1669extern const HttpHeaderStat *dump_stat; /* argh! */
fcd2d3ef 1670const HttpHeaderStat *dump_stat = NULL;
1671
59a09b98 1672void
9bea1d5b 1673httpHeaderFieldStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1674{
9bea1d5b 1675 const int id = (int) val;
1676 const int valid_id = id >= 0 && id < HDR_ENUM_END;
9d7a89a5 1677 const char *name = valid_id ? Headers[id].name.termedBuf() : "INVALID";
9bea1d5b 1678 int visible = count > 0;
1679 /* for entries with zero count, list only those that belong to current type of message */
62e76326 1680
9bea1d5b 1681 if (!visible && valid_id && dump_stat->owner_mask)
62e76326 1682 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1683
9bea1d5b 1684 if (visible)
62e76326 1685 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1686 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
cb69b4c7 1687}
1688
12cf1be2 1689static void
9bea1d5b 1690httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double size, int count)
cb69b4c7 1691{
9bea1d5b 1692 if (count)
62e76326 1693 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1694 idx, (int) val, count,
1695 xpercent(count, dump_stat->destroyedCount));
cb69b4c7 1696}
1697
1698
1699static void
9bea1d5b 1700httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
12cf1be2 1701{
9bea1d5b 1702 assert(hs && e);
1703
1704 dump_stat = hs;
1705 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1706 storeAppendPrintf(e, "\nField type distribution\n");
1707 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1708 "id", "name", "count", "#/header");
96886986 1709 hs->fieldTypeDistr.dump(e, httpHeaderFieldStatDumper);
9bea1d5b 1710 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1711 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1712 "id", "name", "count", "#/cc_field");
96886986 1713 hs->ccTypeDistr.dump(e, httpHdrCcStatDumper);
43ae1d95 1714 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1715 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1716 "id", "name", "count", "#/sc_field");
96886986 1717 hs->scTypeDistr.dump(e, httpHdrScStatDumper);
9bea1d5b 1718 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1719 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
62e76326 1720 "id", "#flds", "count", "%total");
96886986 1721 hs->hdrUCountDistr.dump(e, httpHeaderFldsPerHdrDumper);
9bea1d5b 1722 dump_stat = NULL;
cb69b4c7 1723}
1724
12cf1be2 1725void
9bea1d5b 1726httpHeaderStoreReport(StoreEntry * e)
cb69b4c7 1727{
9bea1d5b 1728 int i;
1729 http_hdr_type ht;
1730 assert(e);
1731
1732 HttpHeaderStats[0].parsedCount =
62e76326 1733 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
9bea1d5b 1734 HttpHeaderStats[0].ccParsedCount =
62e76326 1735 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
9bea1d5b 1736 HttpHeaderStats[0].destroyedCount =
62e76326 1737 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
9bea1d5b 1738 HttpHeaderStats[0].busyDestroyedCount =
62e76326 1739 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
9bea1d5b 1740
1741 for (i = 1; i < HttpHeaderStatCount; i++) {
62e76326 1742 httpHeaderStatDump(HttpHeaderStats + i, e);
1743 storeAppendPrintf(e, "%s\n", "<br>");
12cf1be2 1744 }
62e76326 1745
9bea1d5b 1746 /* field stats for all messages */
1747 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
62e76326 1748
077fe581 1749 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
62e76326 1750 "id", "name", "#alive", "%err", "%repeat");
1751
e6ccf245 1752 for (ht = (http_hdr_type)0; ht < HDR_ENUM_END; ++ht) {
62e76326 1753 HttpHeaderFieldInfo *f = Headers + ht;
077fe581 1754 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
9d7a89a5 1755 f->id, f->name.termedBuf(), f->stat.aliveCount,
62e76326 1756 xpercent(f->stat.errCount, f->stat.parsCount),
1757 xpercent(f->stat.repCount, f->stat.seenCount));
12cf1be2 1758 }
62e76326 1759
9bea1d5b 1760 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
62e76326 1761 HttpHeaderStats[hoRequest].parsedCount,
1762 HttpHeaderStats[hoReply].parsedCount,
1763 HttpHeaderStats[0].parsedCount);
9bea1d5b 1764 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
cb69b4c7 1765}
97474590 1766
e6ccf245 1767http_hdr_type
9b558d8a 1768httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * info, int end)
97474590 1769{
1545d2d1
AJ
1770 if (name_len > 0) {
1771 for (int i = 0; i < end; ++i) {
1772 if (name_len != info[i].name.size())
1773 continue;
62e76326 1774
1545d2d1
AJ
1775 if (!strncasecmp(name, info[i].name.rawBuf(), name_len))
1776 return info[i].id;
1777 }
97474590 1778 }
62e76326 1779
e6ccf245 1780 return HDR_BAD_HDR;
97474590 1781}
1782
e6ccf245 1783http_hdr_type
9bea1d5b 1784httpHeaderIdByNameDef(const char *name, int name_len)
97474590 1785{
9bea1d5b 1786 if (!Headers)
62e76326 1787 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1788
9bea1d5b 1789 return httpHeaderIdByName(name, name_len, Headers, HDR_ENUM_END);
97474590 1790}
efd900cb 1791
1792const char *
9bea1d5b 1793httpHeaderNameById(int id)
efd900cb 1794{
9bea1d5b 1795 if (!Headers)
62e76326 1796 Headers = httpHeaderBuildFieldsInfo(HeadersAttrs, HDR_ENUM_END);
1797
9bea1d5b 1798 assert(id >= 0 && id < HDR_ENUM_END);
62e76326 1799
9d7a89a5 1800 return Headers[id].name.termedBuf();
efd900cb 1801}
0353e724 1802
924f73bc 1803int
a9925b40 1804HttpHeader::hasListMember(http_hdr_type id, const char *member, const char separator) const
924f73bc 1805{
1806 int result = 0;
1807 const char *pos = NULL;
1808 const char *item;
1809 int ilen;
1810 int mlen = strlen(member);
1811
924f73bc 1812 assert(id >= 0);
1813
30abd221 1814 String header (getStrOrList(id));
924f73bc 1815
9d0cdbb9 1816 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1817 if (strncasecmp(item, member, mlen) == 0
9d0cdbb9 1818 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1819 result = 1;
1820 break;
1821 }
1822 }
1823
1824 return result;
1825}
1826
1827int
a9925b40 1828HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
9d0cdbb9 1829{
1830 int result = 0;
1831 const char *pos = NULL;
1832 const char *item;
1833 int ilen;
1834 int mlen = strlen(member);
1835
9d0cdbb9 1836 assert(name);
1837
30abd221 1838 String header (getByName(name));
9d0cdbb9 1839
924f73bc 1840 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1841 if (strncasecmp(item, member, mlen) == 0
924f73bc 1842 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1843 result = 1;
1844 break;
1845 }
1846 }
1847
1848 return result;
1849}
1850
2cdeea82 1851void
1852HttpHeader::removeHopByHopEntries()
1853{
1854 removeConnectionHeaderEntries();
26ac0430 1855
dcf3665b
AJ
1856 const HttpHeaderEntry *e;
1857 HttpHeaderPos pos = HttpHeaderInitPos;
1858 int headers_deleted = 0;
1859 while ((e = getEntry(&pos))) {
26ac0430
AJ
1860 int id = e->id;
1861 if (CBIT_TEST(HopByHopHeadersMask, id)) {
1862 delAt(pos, headers_deleted);
1863 CBIT_CLR(mask, id);
1864 }
dcf3665b 1865 }
2cdeea82 1866}
1867
924f73bc 1868void
1869HttpHeader::removeConnectionHeaderEntries()
1870{
a9925b40 1871 if (has(HDR_CONNECTION)) {
924f73bc 1872 /* anything that matches Connection list member will be deleted */
30abd221 1873 String strConnection;
26ac0430
AJ
1874
1875 (void) getList(HDR_CONNECTION, &strConnection);
924f73bc 1876 const HttpHeaderEntry *e;
1877 HttpHeaderPos pos = HttpHeaderInitPos;
1878 /*
1879 * think: on-average-best nesting of the two loops (hdrEntry
1880 * and strListItem) @?@
1881 */
1882 /*
1883 * maybe we should delete standard stuff ("keep-alive","close")
1884 * from strConnection first?
1885 */
1886
ba9fb01d 1887 int headers_deleted = 0;
a9925b40 1888 while ((e = getEntry(&pos))) {
9d7a89a5 1889 if (strListIsMember(&strConnection, e->name.termedBuf(), ','))
ba9fb01d 1890 delAt(pos, headers_deleted);
924f73bc 1891 }
ba9fb01d 1892 if (headers_deleted)
1893 refreshMask();
924f73bc 1894 }
1895}