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