]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.cc
ICAP: Initial support for trailers
[thirdparty/squid.git] / src / HttpHeader.cc
CommitLineData
cb69b4c7 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
cb69b4c7 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
cb69b4c7 7 */
8
bbc27441
AJ
9/* DEBUG: section 55 HTTP Header */
10
582c2af2 11#include "squid.h"
81ab22b6 12#include "base/EnumIterator.h"
25f98340 13#include "base64.h"
67679543 14#include "globals.h"
a1b9ec20 15#include "http/ContentLengthInterpreter.h"
7ebe76de 16#include "HttpHdrCc.h"
582c2af2 17#include "HttpHdrContRange.h"
1da82544 18#include "HttpHdrScTarget.h" // also includes HttpHdrSc.h
8822ebee 19#include "HttpHeader.h"
79cb238d 20#include "HttpHeaderFieldInfo.h"
e1656dc4 21#include "HttpHeaderStat.h"
a5bac1d2 22#include "HttpHeaderTools.h"
0eb49b6d 23#include "MemBuf.h"
8822ebee 24#include "mgr/Registration.h"
69c698a3 25#include "mime_header.h"
582c2af2 26#include "profiler/Profiler.h"
25f98340 27#include "rfc1123.h"
602d9612 28#include "SquidConfig.h"
00a7574e 29#include "StatHist.h"
8822ebee 30#include "Store.h"
28204b3b 31#include "StrList.h"
81a94152 32#include "TimeOrTag.h"
ed6e9fb9 33#include "util.h"
cb69b4c7 34
42865d25
FC
35#include <algorithm>
36
6fda1515
FC
37/* XXX: the whole set of API managing the entries vector should be rethought
38 * after the parse4r-ng effort is complete.
39 */
40
cb69b4c7 41/*
2ac76861 42 * On naming conventions:
26ac0430
AJ
43 *
44 * HTTP/1.1 defines message-header as
45 *
2ac76861 46 * message-header = field-name ":" [ field-value ] CRLF
47 * field-name = token
48 * field-value = *( field-content | LWS )
26ac0430 49 *
2ac76861 50 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
51 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
26ac0430 52 *
2ac76861 53 * HttpHeader is an object that represents all message-headers in a message.
54 * HttpHeader does not manage start-line.
26ac0430 55 *
d8b249ef 56 * HttpHeader is implemented as a collection of header "entries".
57 * An entry is a (field_id, field_name, field_value) triplet.
2ac76861 58 */
cb69b4c7 59
cb69b4c7 60/*
61 * local constants and vars
62 */
63
789217a2 64// statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted
81ab22b6 65std::vector<HttpHeaderFieldStat> headerStatsTable(Http::HdrType::enumEnd_);
cb69b4c7 66
81ab22b6 67/* request-only headers. Used for cachemgr */
f53969cc 68static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */
16d3204a 69
81ab22b6 70/* reply-only headers. Used for cachemgr */
f53969cc 71static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */
2cdeea82 72
12cf1be2 73/* header accounting */
26735116 74// NP: keep in sync with enum http_hdr_owner_type
26ac0430 75static HttpHeaderStat HttpHeaderStats[] = {
95cc1e3e 76 HttpHeaderStat(/*hoNone*/ "all", NULL),
9f4d4f65 77#if USE_HTCP
95cc1e3e 78 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask),
9f4d4f65 79#endif
95cc1e3e
FC
80 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask),
81 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask)
26735116
AJ
82#if USE_OPENSSL
83 /* hoErrorDetail */
84#endif
85 /* hoEnd */
26ac0430 86};
9bea1d5b 87static int HttpHeaderStatCount = countof(HttpHeaderStats);
12cf1be2 88
7faf2bdb 89static int HeaderEntryParsedCount = 0;
cb69b4c7 90
12cf1be2 91/*
f734cda2 92 * forward declarations and local routines
12cf1be2 93 */
cb69b4c7 94
f734cda2 95class StoreEntry;
cb69b4c7 96
81ab22b6
FC
97// update parse statistics for header id; if error is true also account
98// for errors and write to debug log what happened
99static void httpHeaderNoteParsedEntry(Http::HdrType id, String const &value, bool error);
73b2aa87 100static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e);
f734cda2
FC
101/** store report about current header usage and other stats */
102static void httpHeaderStoreReport(StoreEntry * e);
103
cb69b4c7 104/*
105 * Module initialization routines
106 */
107
6b7d87bb
FC
108static void
109httpHeaderRegisterWithCacheManager(void)
110{
8822ebee 111 Mgr::RegisterAction("http_headers",
d9fc6862
A
112 "HTTP Header Statistics",
113 httpHeaderStoreReport, 0, 1);
6b7d87bb
FC
114}
115
cb69b4c7 116void
9bea1d5b 117httpHeaderInitModule(void)
cb69b4c7 118{
9bea1d5b 119 /* check that we have enough space for masks */
81ab22b6
FC
120 assert(8 * sizeof(HttpHeaderMask) >= Http::HdrType::enumEnd_);
121
122 // masks are needed for stats page still
123 for (auto h : WholeEnum<Http::HdrType>()) {
124 if (Http::HeaderLookupTable.lookup(h).request)
125 CBIT_SET(RequestHeadersMask,h);
126 if (Http::HeaderLookupTable.lookup(h).reply)
127 CBIT_SET(ReplyHeadersMask,h);
128 }
dcf3665b 129
26735116 130 /* header stats initialized by class constructor */
9bea1d5b 131 assert(HttpHeaderStatCount == hoReply + 1);
16d3204a 132
9bea1d5b 133 /* init dependent modules */
134 httpHdrCcInitModule();
43ae1d95 135 httpHdrScInitModule();
ea391f18
FC
136
137 httpHeaderRegisterWithCacheManager();
62ee09ca 138}
43ae1d95 139
cb69b4c7 140/*
141 * HttpHeader Implementation
142 */
143
3e42b356 144HttpHeader::HttpHeader() : owner (hoNone), len (0), conflictingContentLength_(false)
cb69b4c7 145{
75faaa7a 146 httpHeaderMaskInit(&mask, 0);
147}
148
3e42b356 149HttpHeader::HttpHeader(const http_hdr_owner_type anOwner): owner(anOwner), len(0), conflictingContentLength_(false)
75faaa7a 150{
02259ff8 151 assert(anOwner > hoNone && anOwner < hoEnd);
efc26e8e 152 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner);
75faaa7a 153 httpHeaderMaskInit(&mask, 0);
154}
155
3e42b356 156HttpHeader::HttpHeader(const HttpHeader &other): owner(other.owner), len(other.len), conflictingContentLength_(false)
5e5fa5b1
AR
157{
158 httpHeaderMaskInit(&mask, 0);
2d4f252d 159 update(&other); // will update the mask as well
5e5fa5b1
AR
160}
161
75faaa7a 162HttpHeader::~HttpHeader()
163{
519e0948 164 clean();
cb69b4c7 165}
166
5e5fa5b1
AR
167HttpHeader &
168HttpHeader::operator =(const HttpHeader &other)
169{
170 if (this != &other) {
171 // we do not really care, but the caller probably does
172 assert(owner == other.owner);
173 clean();
2d4f252d 174 update(&other); // will update the mask as well
5e5fa5b1 175 len = other.len;
3e42b356 176 conflictingContentLength_ = other.conflictingContentLength_;
5e5fa5b1
AR
177 }
178 return *this;
179}
180
cb69b4c7 181void
519e0948 182HttpHeader::clean()
cb69b4c7 183{
9bea1d5b 184
02259ff8 185 assert(owner > hoNone && owner < hoEnd);
efc26e8e 186 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner);
9bea1d5b 187
3a9cf2d5 188 PROF_start(HttpHeaderClean);
189
0de73a38 190 if (owner <= hoReply) {
c594d76c
A
191 /*
192 * An unfortunate bug. The entries array is initialized
193 * such that count is set to zero. httpHeaderClean() seems to
194 * be called both when 'hdr' is created, and destroyed. Thus,
195 * we accumulate a large number of zero counts for 'hdr' before
196 * it is ever used. Can't think of a good way to fix it, except
197 * adding a state variable that indicates whether or not 'hdr'
198 * has been used. As a hack, just never count zero-sized header
199 * arrays.
200 */
4c9eadc2
FC
201 if (!entries.empty())
202 HttpHeaderStats[owner].hdrUCountDistr.count(entries.size());
51328da8 203
95dc7ff4 204 ++ HttpHeaderStats[owner].destroyedCount;
51328da8 205
4c9eadc2 206 HttpHeaderStats[owner].busyDestroyedCount += entries.size() > 0;
0de73a38 207 } // if (owner <= hoReply)
62e76326 208
81ab22b6 209 for (HttpHeaderEntry *e : entries) {
1da82544 210 if (e == nullptr)
6fda1515 211 continue;
1da82544 212 if (!Http::any_valid_header(e->id)) {
6fda1515 213 debugs(55, DBG_CRITICAL, "BUG: invalid entry (" << e->id << "). Ignored.");
c594d76c
A
214 } else {
215 if (owner <= hoReply)
f30f7998 216 HttpHeaderStats[owner].fieldTypeDistr.count(e->id);
c594d76c 217 delete e;
62e76326 218 }
c594d76c 219 }
6fda1515 220
c33a88ca 221 entries.clear();
519e0948 222 httpHeaderMaskInit(&mask, 0);
5e5fa5b1 223 len = 0;
3e42b356 224 conflictingContentLength_ = false;
3a9cf2d5 225 PROF_stop(HttpHeaderClean);
cb69b4c7 226}
227
2246b732 228/* append entries (also see httpHeaderUpdate) */
229void
a9925b40 230HttpHeader::append(const HttpHeader * src)
2246b732 231{
a9925b40 232 assert(src);
233 assert(src != this);
efc26e8e 234 debugs(55, 7, "appending hdr: " << this << " += " << src);
9bea1d5b 235
81ab22b6
FC
236 for (auto e : src->entries) {
237 if (e)
238 addEntry(e->clone());
2246b732 239 }
240}
241
1a210de4
EB
242/// check whether the fresh header has any new/changed updatable fields
243bool
244HttpHeader::needUpdate(HttpHeader const *fresh) const
245{
246 for (const auto e: fresh->entries) {
d747ab54 247 if (!e || skipUpdateHeader(e->id))
1a210de4
EB
248 continue;
249 String value;
250 const char *name = e->name.termedBuf();
251 if (!getByNameIfPresent(name, strlen(name), value) ||
252 (value != fresh->getByName(name)))
253 return true;
254 }
255 return false;
256}
257
924f73bc 258void
2d4f252d
EB
259HttpHeader::updateWarnings()
260{
261 int count = 0;
262 HttpHeaderPos pos = HttpHeaderInitPos;
263
264 // RFC 7234, section 4.3.4: delete 1xx warnings and retain 2xx warnings
265 while (HttpHeaderEntry *e = getEntry(&pos)) {
266 if (e->id == Http::HdrType::WARNING && (e->getInt()/100 == 1) )
267 delAt(pos, count);
268 }
269}
270
271bool
272HttpHeader::skipUpdateHeader(const Http::HdrType id) const
273{
274 // RFC 7234, section 4.3.4: use fields other from Warning for update
275 return id == Http::HdrType::WARNING;
276}
277
1a210de4 278bool
2d4f252d 279HttpHeader::update(HttpHeader const *fresh)
cb69b4c7 280{
a9925b40 281 assert(fresh);
924f73bc 282 assert(this != fresh);
9bea1d5b 283
1a210de4
EB
284 // Optimization: Finding whether a header field changed is expensive
285 // and probably not worth it except for collapsed revalidation needs.
286 if (Config.onoff.collapsed_forwarding && !needUpdate(fresh))
287 return false;
288
2d4f252d
EB
289 updateWarnings();
290
1a210de4
EB
291 const HttpHeaderEntry *e;
292 HttpHeaderPos pos = HttpHeaderInitPos;
293
a9925b40 294 while ((e = fresh->getEntry(&pos))) {
789217a2 295 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
62e76326 296
2d4f252d 297 if (skipUpdateHeader(e->id))
62e76326 298 continue;
299
789217a2 300 if (e->id != Http::HdrType::OTHER)
ba9fb01d 301 delById(e->id);
302 else
9d7a89a5 303 delByName(e->name.termedBuf());
9917847f 304 }
305
306 pos = HttpHeaderInitPos;
307 while ((e = fresh->getEntry(&pos))) {
789217a2 308 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
9917847f 309
2d4f252d 310 if (skipUpdateHeader(e->id))
9917847f 311 continue;
312
81ab22b6 313 debugs(55, 7, "Updating header '" << Http::HeaderLookupTable.lookup(e->id).name << "' in cached entry");
62e76326 314
eede25e7 315 addEntry(e->clone());
cb69b4c7 316 }
1a210de4 317 return true;
cb69b4c7 318}
319
69c698a3
EB
320bool
321HttpHeader::Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end)
322{
323 /*
324 * parse_start points to the first line of HTTP message *headers*,
325 * not including the request or status lines
326 */
327 const size_t end = headersEnd(*parse_start, l);
328
329 if (end) {
330 *blk_start = *parse_start;
331 *blk_end = *parse_start + end - 1;
332 assert(**blk_end == '\n');
333 // Point blk_end to the first character after the last header field.
334 // In other words, blk_end should point to the CR?LF header terminator.
335 if (end > 1 && *(*blk_end - 1) == '\r')
336 --(*blk_end);
337 *parse_start += end;
338 }
339 return end;
340}
341
342int
343HttpHeader::parse(const char *buf, size_t buf_len, bool atEnd, size_t &hdr_sz)
344{
345 const char *parse_start = buf;
346 const char *blk_start, *blk_end;
347 hdr_sz = 0;
348
349 if (!Isolate(&parse_start, buf_len, &blk_start, &blk_end)) {
350 // XXX: do not parse non-isolated headers even if the connection is closed.
351 // Treat unterminated headers as "partial headers" framing errors.
352 if (!atEnd)
353 return 0;
354 blk_start = parse_start;
355 blk_end = blk_start + strlen(blk_start);
356 }
357
358 if (parse(blk_start, blk_end - blk_start)) {
359 hdr_sz = parse_start - buf;
360 return 1;
361 }
362 return -1;
363}
364
cb69b4c7 365int
784619e6 366HttpHeader::parse(const char *header_start, size_t hdrLen)
cb69b4c7 367{
52d3f198 368 const char *field_ptr = header_start;
784619e6 369 const char *header_end = header_start + hdrLen; // XXX: remove
56cd29e6 370 int warnOnError = (Config.onoff.relaxed_header_parser <= 0 ? DBG_IMPORTANT : 2);
9bea1d5b 371
3a9cf2d5 372 PROF_start(HttpHeaderParse);
373
9bea1d5b 374 assert(header_start && header_end);
81858ebc 375 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl << getStringPrefix(header_start, hdrLen));
95dc7ff4 376 ++ HttpHeaderStats[owner].parsedCount;
62e76326 377
1d8a896b 378 char *nulpos;
784619e6 379 if ((nulpos = (char*)memchr(header_start, '\0', hdrLen))) {
e0236918 380 debugs(55, DBG_IMPORTANT, "WARNING: HTTP header contains NULL characters {" <<
81858ebc 381 getStringPrefix(header_start, nulpos-header_start) << "}\nNULL\n{" << getStringPrefix(nulpos+1, hdrLen-(nulpos-header_start)-1));
6d7bc97b 382 PROF_stop(HttpHeaderParse);
81ab22b6
FC
383 clean();
384 return 0;
52d3f198 385 }
386
a1b9ec20 387 Http::ContentLengthInterpreter clen(warnOnError);
52d3f198 388 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
389 * continuation lines start with a (single) space or tab */
390 while (field_ptr < header_end) {
391 const char *field_start = field_ptr;
62e76326 392 const char *field_end;
62e76326 393
394 do {
52d3f198 395 const char *this_line = field_ptr;
396 field_ptr = (const char *)memchr(field_ptr, '\n', header_end - field_ptr);
397
6d7bc97b
AJ
398 if (!field_ptr) {
399 // missing <LF>
400 PROF_stop(HttpHeaderParse);
81ab22b6
FC
401 clean();
402 return 0;
6d7bc97b 403 }
52d3f198 404
405 field_end = field_ptr;
406
f53969cc 407 ++field_ptr; /* Move to next line */
52d3f198 408
409 if (field_end > this_line && field_end[-1] == '\r') {
f53969cc 410 --field_end; /* Ignore CR LF */
52d3f198 411
e10272fc
AR
412 if (owner == hoRequest && field_end > this_line) {
413 bool cr_only = true;
414 for (const char *p = this_line; p < field_end && cr_only; ++p) {
415 if (*p != '\r')
416 cr_only = false;
417 }
418 if (cr_only) {
6e09ed7b 419 debugs(55, DBG_IMPORTANT, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
e10272fc 420 "header field to prevent request smuggling attacks: {" <<
81858ebc 421 getStringPrefix(header_start, hdrLen) << "}");
6d7bc97b 422 PROF_stop(HttpHeaderParse);
81ab22b6
FC
423 clean();
424 return 0;
e10272fc 425 }
52d3f198 426 }
427 }
428
429 /* Barf on stray CR characters */
430 if (memchr(this_line, '\r', field_end - this_line)) {
6e09ed7b 431 debugs(55, warnOnError, "WARNING: suspicious CR characters in HTTP header {" <<
81858ebc 432 getStringPrefix(field_start, field_end-field_start) << "}");
52d3f198 433
434 if (Config.onoff.relaxed_header_parser) {
f53969cc 435 char *p = (char *) this_line; /* XXX Warning! This destroys original header content and violates specifications somewhat */
52d3f198 436
a38ec4b1
FC
437 while ((p = (char *)memchr(p, '\r', field_end - p)) != NULL) {
438 *p = ' ';
439 ++p;
440 }
6d7bc97b
AJ
441 } else {
442 PROF_stop(HttpHeaderParse);
81ab22b6
FC
443 clean();
444 return 0;
6d7bc97b 445 }
52d3f198 446 }
447
448 if (this_line + 1 == field_end && this_line > field_start) {
6e09ed7b 449 debugs(55, warnOnError, "WARNING: Blank continuation line in HTTP header {" <<
81858ebc 450 getStringPrefix(header_start, hdrLen) << "}");
6d7bc97b 451 PROF_stop(HttpHeaderParse);
81ab22b6
FC
452 clean();
453 return 0;
52d3f198 454 }
455 } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t'));
456
457 if (field_start == field_end) {
458 if (field_ptr < header_end) {
6e09ed7b 459 debugs(55, warnOnError, "WARNING: unparseable HTTP header field near {" <<
81858ebc 460 getStringPrefix(field_start, hdrLen-(field_start-header_start)) << "}");
6d7bc97b 461 PROF_stop(HttpHeaderParse);
81ab22b6
FC
462 clean();
463 return 0;
52d3f198 464 }
465
f53969cc 466 break; /* terminating blank line */
52d3f198 467 }
62e76326 468
a1b9ec20 469 HttpHeaderEntry *e;
cdce6c61 470 if ((e = HttpHeaderEntry::parse(field_start, field_end)) == NULL) {
6e09ed7b 471 debugs(55, warnOnError, "WARNING: unparseable HTTP header field {" <<
81858ebc
AJ
472 getStringPrefix(field_start, field_end-field_start) << "}");
473 debugs(55, warnOnError, " in {" << getStringPrefix(header_start, hdrLen) << "}");
52d3f198 474
475 if (Config.onoff.relaxed_header_parser)
476 continue;
6e09ed7b 477
6d7bc97b 478 PROF_stop(HttpHeaderParse);
81ab22b6
FC
479 clean();
480 return 0;
47ac2ebe 481 }
62e76326 482
a1b9ec20
AR
483 if (e->id == Http::HdrType::CONTENT_LENGTH && !clen.checkField(e->value)) {
484 delete e;
b3123159 485
a1b9ec20
AR
486 if (Config.onoff.relaxed_header_parser)
487 continue; // clen has printed any necessary warnings
6e09ed7b 488
a1b9ec20
AR
489 PROF_stop(HttpHeaderParse);
490 clean();
491 return 0;
52d3f198 492 }
62e76326 493
789217a2 494 if (e->id == Http::HdrType::OTHER && stringHasWhitespace(e->name.termedBuf())) {
6e09ed7b 495 debugs(55, warnOnError, "WARNING: found whitespace in HTTP header name {" <<
81858ebc 496 getStringPrefix(field_start, field_end-field_start) << "}");
47ac2ebe 497
52d3f198 498 if (!Config.onoff.relaxed_header_parser) {
eede25e7 499 delete e;
6d7bc97b 500 PROF_stop(HttpHeaderParse);
81ab22b6
FC
501 clean();
502 return 0;
52d3f198 503 }
504 }
62e76326 505
a9925b40 506 addEntry(e);
cb69b4c7 507 }
62e76326 508
a1b9ec20
AR
509 if (clen.headerWideProblem) {
510 debugs(55, warnOnError, "WARNING: " << clen.headerWideProblem <<
511 " Content-Length field values in" <<
512 Raw("header", header_start, hdrLen));
513 }
514
c3d0ba0c
AR
515 if (chunked()) {
516 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
a1b9ec20 517 // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length
789217a2 518 delById(Http::HdrType::CONTENT_LENGTH);
a1b9ec20
AR
519 // and clen state becomes irrelevant
520 } else if (clen.sawBad) {
521 // ensure our callers do not accidentally see bad Content-Length values
3e42b356 522 delById(Http::HdrType::CONTENT_LENGTH);
a1b9ec20
AR
523 conflictingContentLength_ = true; // TODO: Rename to badContentLength_.
524 } else if (clen.needsSanitizing) {
525 // RFC 7230 section 3.3.2: MUST either reject or ... [sanitize];
526 // ensure our callers see a clean Content-Length value or none at all
527 delById(Http::HdrType::CONTENT_LENGTH);
528 if (clen.sawGood) {
529 putInt64(Http::HdrType::CONTENT_LENGTH, clen.value);
530 debugs(55, 5, "sanitized Content-Length to be " << clen.value);
531 }
c3d0ba0c
AR
532 }
533
3a9cf2d5 534 PROF_stop(HttpHeaderParse);
f53969cc 535 return 1; /* even if no fields where found, it is a valid header */
cb69b4c7 536}
537
99edd1c3 538/* packs all the entries using supplied packer */
cb69b4c7 539void
17802cf1 540HttpHeader::packInto(Packable * p, bool mask_sensitive_info) const
cb69b4c7 541{
9bea1d5b 542 HttpHeaderPos pos = HttpHeaderInitPos;
543 const HttpHeaderEntry *e;
a9925b40 544 assert(p);
3cc0f4e7
AR
545 debugs(55, 7, this << " into " << p <<
546 (mask_sensitive_info ? " while masking" : ""));
9bea1d5b 547 /* pack all entries one by one */
d8f6c79c
FC
548 while ((e = getEntry(&pos))) {
549 if (!mask_sensitive_info) {
550 e->packInto(p);
551 continue;
552 }
3cc0f4e7
AR
553
554 bool maskThisEntry = false;
d8f6c79c 555 switch (e->id) {
789217a2
FC
556 case Http::HdrType::AUTHORIZATION:
557 case Http::HdrType::PROXY_AUTHORIZATION:
3cc0f4e7 558 maskThisEntry = true;
d3823ea7 559 break;
3cc0f4e7 560
789217a2
FC
561 case Http::HdrType::FTP_ARGUMENTS:
562 if (const HttpHeaderEntry *cmd = findEntry(Http::HdrType::FTP_COMMAND))
3cc0f4e7
AR
563 maskThisEntry = (cmd->value == "PASS");
564 break;
565
d3823ea7 566 default:
d3823ea7 567 break;
d8f6c79c 568 }
3cc0f4e7 569 if (maskThisEntry) {
785b508d
AJ
570 p->append(e->name.rawBuf(), e->name.size());
571 p->append(": ** NOT DISPLAYED **\r\n", 23);
3cc0f4e7
AR
572 } else {
573 e->packInto(p);
574 }
575
d8f6c79c 576 }
35b88ed2 577 /* Pack in the "special" entries */
578
579 /* Cache-Control */
cb69b4c7 580}
581
582/* returns next valid entry */
99edd1c3 583HttpHeaderEntry *
a9925b40 584HttpHeader::getEntry(HttpHeaderPos * pos) const
cb69b4c7 585{
a9925b40 586 assert(pos);
c0abe2ec 587 assert(*pos >= HttpHeaderInitPos && *pos < static_cast<ssize_t>(entries.size()));
62e76326 588
c0abe2ec 589 for (++(*pos); *pos < static_cast<ssize_t>(entries.size()); ++(*pos)) {
4c9eadc2 590 if (entries[*pos])
c0abe2ec 591 return static_cast<HttpHeaderEntry*>(entries[*pos]);
cb69b4c7 592 }
62e76326 593
9bea1d5b 594 return NULL;
cb69b4c7 595}
596
597/*
26ac0430 598 * returns a pointer to a specified entry if any
d8b249ef 599 * note that we return one entry so it does not make much sense to ask for
600 * "list" headers
cb69b4c7 601 */
de336bbe 602HttpHeaderEntry *
789217a2 603HttpHeader::findEntry(Http::HdrType id) const
cb69b4c7 604{
8cccf1f8 605 assert(any_registered_header(id));
81ab22b6 606 assert(!Http::HeaderLookupTable.lookup(id).list);
9bea1d5b 607
608 /* check mask first */
62e76326 609
a9925b40 610 if (!CBIT_TEST(mask, id))
62e76326 611 return NULL;
612
9bea1d5b 613 /* looks like we must have it, do linear search */
81ab22b6
FC
614 for (auto e : entries) {
615 if (e && e->id == id)
62e76326 616 return e;
cb69b4c7 617 }
62e76326 618
9bea1d5b 619 /* hm.. we thought it was there, but it was not found */
81ab22b6
FC
620 assert(false);
621 return nullptr; /* not reached */
cb69b4c7 622}
623
a622fff0 624/*
625 * same as httpHeaderFindEntry
626 */
a9925b40 627HttpHeaderEntry *
789217a2 628HttpHeader::findLastEntry(Http::HdrType id) const
a622fff0 629{
8cccf1f8 630 assert(any_registered_header(id));
81ab22b6 631 assert(!Http::HeaderLookupTable.lookup(id).list);
9bea1d5b 632
633 /* check mask first */
a9925b40 634 if (!CBIT_TEST(mask, id))
62e76326 635 return NULL;
636
81ab22b6
FC
637 for (auto e = entries.rbegin(); e != entries.rend(); ++e) {
638 if (*e && (*e)->id == id)
639 return *e;
a622fff0 640 }
62e76326 641
81ab22b6
FC
642 /* hm.. we thought it was there, but it was not found */
643 assert(false);
644 return nullptr; /* not reached */
a622fff0 645}
646
cb69b4c7 647/*
26ac0430 648 * deletes all fields with a given name if any, returns #fields deleted;
cb69b4c7 649 */
2ac76861 650int
a9925b40 651HttpHeader::delByName(const char *name)
cb69b4c7 652{
9bea1d5b 653 int count = 0;
654 HttpHeaderPos pos = HttpHeaderInitPos;
655 HttpHeaderEntry *e;
f53969cc 656 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
efc26e8e 657 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
62e76326 658
a9925b40 659 while ((e = getEntry(&pos))) {
30abd221 660 if (!e->name.caseCmp(name))
ba9fb01d 661 delAt(pos, count);
662 else
a9925b40 663 CBIT_SET(mask, e->id);
cb69b4c7 664 }
62e76326 665
9bea1d5b 666 return count;
cb69b4c7 667}
668
2246b732 669/* deletes all entries with a given id, returns the #entries deleted */
670int
789217a2 671HttpHeader::delById(Http::HdrType id)
d8b249ef 672{
efc26e8e 673 debugs(55, 8, this << " del-by-id " << id);
8cccf1f8 674 assert(any_registered_header(id));
62e76326 675
a9925b40 676 if (!CBIT_TEST(mask, id))
62e76326 677 return 0;
678
5134d3a4
AR
679 int count = 0;
680
681 HttpHeaderPos pos = HttpHeaderInitPos;
682 while (HttpHeaderEntry *e = getEntry(&pos)) {
683 if (e->id == id)
684 delAt(pos, count); // deletes e
685 }
62e76326 686
a9925b40 687 CBIT_CLR(mask, id);
9bea1d5b 688 assert(count);
689 return count;
d8b249ef 690}
d8b249ef 691
cb69b4c7 692/*
693 * deletes an entry at pos and leaves a gap; leaving a gap makes it
694 * possible to iterate(search) and delete fields at the same time
ba9fb01d 695 * NOTE: Does not update the header mask. Caller must follow up with
696 * a call to refreshMask() if headers_deleted was incremented.
cb69b4c7 697 */
2246b732 698void
ba9fb01d 699HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
cb69b4c7 700{
9bea1d5b 701 HttpHeaderEntry *e;
c0abe2ec
FC
702 assert(pos >= HttpHeaderInitPos && pos < static_cast<ssize_t>(entries.size()));
703 e = static_cast<HttpHeaderEntry*>(entries[pos]);
4c9eadc2 704 entries[pos] = NULL;
9bea1d5b 705 /* decrement header length, allow for ": " and crlf */
a9925b40 706 len -= e->name.size() + 2 + e->value.size() + 2;
707 assert(len >= 0);
eede25e7 708 delete e;
ba9fb01d 709 ++headers_deleted;
cb69b4c7 710}
711
394499bd 712/*
713 * Compacts the header storage
714 */
715void
716HttpHeader::compact()
717{
6e1410f9 718 // TODO: optimize removal, or possibly make it so that's not needed.
81ab22b6
FC
719 entries.erase( std::remove(entries.begin(), entries.end(), nullptr),
720 entries.end());
394499bd 721}
722
ba9fb01d 723/*
724 * Refreshes the header mask. Required after delAt() calls.
725 */
726void
727HttpHeader::refreshMask()
728{
729 httpHeaderMaskInit(&mask, 0);
730 debugs(55, 7, "refreshing the mask in hdr " << this);
81ab22b6
FC
731 for (auto e : entries) {
732 if (e)
733 CBIT_SET(mask, e->id);
ba9fb01d 734 }
735}
99edd1c3 736
62e76326 737/* appends an entry;
eede25e7 738 * does not call e->clone() so one should not reuse "*e"
cb69b4c7 739 */
99edd1c3 740void
a9925b40 741HttpHeader::addEntry(HttpHeaderEntry * e)
cb69b4c7 742{
a9925b40 743 assert(e);
1da82544 744 assert(any_HdrType_enum_value(e->id));
59c50530 745 assert(e->name.size());
9bea1d5b 746
4c9eadc2 747 debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size());
62e76326 748
789217a2 749 if (e->id != Http::HdrType::BAD_HDR) {
73b2aa87
FC
750 if (CBIT_TEST(mask, e->id)) {
751 ++ headerStatsTable[e->id].repCount;
752 } else {
753 CBIT_SET(mask, e->id);
754 }
b88a45ed 755 }
62e76326 756
a9925b40 757 entries.push_back(e);
62e76326 758
9bea1d5b 759 /* increment header length, allow for ": " and crlf */
a9925b40 760 len += e->name.size() + 2 + e->value.size() + 2;
cb69b4c7 761}
762
bbe58ab5 763/* inserts an entry;
eede25e7 764 * does not call e->clone() so one should not reuse "*e"
bbe58ab5 765 */
766void
a9925b40 767HttpHeader::insertEntry(HttpHeaderEntry * e)
bbe58ab5 768{
a9925b40 769 assert(e);
229eb96e 770 assert(any_valid_header(e->id));
bbe58ab5 771
f9514ae8 772 debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size());
bbe58ab5 773
789217a2 774 // Http::HdrType::BAD_HDR is filtered out by assert_any_valid_header
b88a45ed 775 if (CBIT_TEST(mask, e->id)) {
73b2aa87 776 ++ headerStatsTable[e->id].repCount;
b88a45ed 777 } else {
a9925b40 778 CBIT_SET(mask, e->id);
b88a45ed 779 }
bbe58ab5 780
42865d25 781 entries.insert(entries.begin(),e);
bbe58ab5 782
783 /* increment header length, allow for ": " and crlf */
a9925b40 784 len += e->name.size() + 2 + e->value.size() + 2;
bbe58ab5 785}
786
35b88ed2 787bool
789217a2 788HttpHeader::getList(Http::HdrType id, String *s) const
35b88ed2 789{
efc26e8e 790 debugs(55, 9, this << " joining for id " << id);
35b88ed2 791 /* only fields from ListHeaders array can be "listed" */
81ab22b6 792 assert(Http::HeaderLookupTable.lookup(id).list);
35b88ed2 793
794 if (!CBIT_TEST(mask, id))
795 return false;
796
81ab22b6
FC
797 for (auto e: entries) {
798 if (e && e->id == id)
9d7a89a5 799 strListAdd(s, e->value.termedBuf(), ',');
35b88ed2 800 }
801
802 /*
803 * note: we might get an empty (size==0) string if there was an "empty"
804 * header. This results in an empty length String, which may have a NULL
805 * buffer.
806 */
807 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
808 if (!s->size())
81ab22b6 809 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")");
35b88ed2 810 else
811 debugs(55, 6, this << ": joined for id " << id << ": " << s);
812
813 return true;
814}
815
99edd1c3 816/* return a list of entries with the same id separated by ',' and ws */
30abd221 817String
789217a2 818HttpHeader::getList(Http::HdrType id) const
cb69b4c7 819{
9bea1d5b 820 HttpHeaderEntry *e;
821 HttpHeaderPos pos = HttpHeaderInitPos;
efc26e8e 822 debugs(55, 9, this << "joining for id " << id);
9bea1d5b 823 /* only fields from ListHeaders array can be "listed" */
81ab22b6 824 assert(Http::HeaderLookupTable.lookup(id).list);
62e76326 825
a9925b40 826 if (!CBIT_TEST(mask, id))
30abd221 827 return String();
650c4b88 828
30abd221 829 String s;
62e76326 830
a9925b40 831 while ((e = getEntry(&pos))) {
62e76326 832 if (e->id == id)
9d7a89a5 833 strListAdd(&s, e->value.termedBuf(), ',');
d35b9a94 834 }
62e76326 835
9bea1d5b 836 /*
948078e3 837 * note: we might get an empty (size==0) string if there was an "empty"
838 * header. This results in an empty length String, which may have a NULL
839 * buffer.
9bea1d5b 840 */
948078e3 841 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
528b2c61 842 if (!s.size())
81ab22b6 843 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")");
948078e3 844 else
845 debugs(55, 6, this << ": joined for id " << id << ": " << s);
62e76326 846
9bea1d5b 847 return s;
cb69b4c7 848}
849
f66a9ef4 850/* return a string or list of entries with the same id separated by ',' and ws */
30abd221 851String
789217a2 852HttpHeader::getStrOrList(Http::HdrType id) const
f66a9ef4 853{
c7327fa0 854 HttpHeaderEntry *e;
9bea1d5b 855
81ab22b6 856 if (Http::HeaderLookupTable.lookup(id).list)
a9925b40 857 return getList(id);
62e76326 858
a9925b40 859 if ((e = findEntry(id)))
62e76326 860 return e->value;
861
30abd221 862 return String();
f66a9ef4 863}
864
865/*
b2c44718 866 * Returns the value of the specified header and/or an undefined String.
f66a9ef4 867 */
30abd221 868String
a9925b40 869HttpHeader::getByName(const char *name) const
81ab22b6
FC
870{
871 String result;
872 // ignore presence: return undefined string if an empty header is present
873 (void)getByNameIfPresent(name, strlen(name), result);
874 return result;
875}
876
877String
878HttpHeader::getByName(const SBuf &name) const
b2c44718
AR
879{
880 String result;
881 // ignore presence: return undefined string if an empty header is present
882 (void)getByNameIfPresent(name, result);
883 return result;
884}
885
81ab22b6
FC
886String
887HttpHeader::getById(Http::HdrType id) const
888{
889 String result;
890 (void)getByIdIfPresent(id,result);
891 return result;
892}
893
894bool
895HttpHeader::getByNameIfPresent(const SBuf &s, String &result) const
896{
897 return getByNameIfPresent(s.rawContent(), s.length(), result);
898}
899
900bool
901HttpHeader::getByIdIfPresent(Http::HdrType id, String &result) const
902{
903 if (id == Http::HdrType::BAD_HDR)
904 return false;
905 if (!has(id))
906 return false;
907 result = getStrOrList(id);
908 return true;
909}
910
b2c44718 911bool
81ab22b6 912HttpHeader::getByNameIfPresent(const char *name, int namelen, String &result) const
f66a9ef4 913{
789217a2 914 Http::HdrType id;
9bea1d5b 915 HttpHeaderPos pos = HttpHeaderInitPos;
916 HttpHeaderEntry *e;
9bea1d5b 917
9bea1d5b 918 assert(name);
919
920 /* First try the quick path */
81ab22b6 921 id = Http::HeaderLookupTable.lookup(name,namelen).id;
62e76326 922
789217a2 923 if (id != Http::HdrType::BAD_HDR) {
81ab22b6
FC
924 if (getByIdIfPresent(id, result))
925 return true;
b2c44718 926 }
650c4b88 927
9bea1d5b 928 /* Sorry, an unknown header name. Do linear search */
b2c44718 929 bool found = false;
a9925b40 930 while ((e = getEntry(&pos))) {
1997a487 931 if (e->id == Http::HdrType::OTHER && e->name.size() == static_cast<String::size_type>(namelen) && e->name.caseCmp(name, namelen) == 0) {
b2c44718 932 found = true;
9d7a89a5 933 strListAdd(&result, e->value.termedBuf(), ',');
62e76326 934 }
f66a9ef4 935 }
62e76326 936
b2c44718 937 return found;
f66a9ef4 938}
cb69b4c7 939
14b463aa 940/*
372fdfbf 941 * Returns a the value of the specified list member, if any.
14b463aa 942 */
30abd221 943String
a9925b40 944HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
14b463aa 945{
30abd221 946 String header;
14b463aa 947 const char *pos = NULL;
948 const char *item;
949 int ilen;
950 int mlen = strlen(member);
951
14b463aa 952 assert(name);
953
a9925b40 954 header = getByName(name);
14b463aa 955
30abd221 956 String result;
650c4b88 957
14b463aa 958 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 959 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
960 result.append(item + mlen + 1, ilen - mlen - 1);
961 break;
962 }
14b463aa 963 }
62e76326 964
14b463aa 965 return result;
966}
967
968/*
969 * returns a the value of the specified list member, if any.
970 */
30abd221 971String
789217a2 972HttpHeader::getListMember(Http::HdrType id, const char *member, const char separator) const
14b463aa 973{
30abd221 974 String header;
14b463aa 975 const char *pos = NULL;
976 const char *item;
977 int ilen;
978 int mlen = strlen(member);
979
8cccf1f8 980 assert(any_registered_header(id));
14b463aa 981
a9925b40 982 header = getStrOrList(id);
30abd221 983 String result;
14b463aa 984
985 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
62e76326 986 if (strncmp(item, member, mlen) == 0 && item[mlen] == '=') {
987 result.append(item + mlen + 1, ilen - mlen - 1);
988 break;
989 }
14b463aa 990 }
62e76326 991
30abd221 992 header.clean();
14b463aa 993 return result;
994}
995
cb69b4c7 996/* test if a field is present */
2ac76861 997int
789217a2 998HttpHeader::has(Http::HdrType id) const
cb69b4c7 999{
8cccf1f8 1000 assert(any_registered_header(id));
efc26e8e 1001 debugs(55, 9, this << " lookup for " << id);
a9925b40 1002 return CBIT_TEST(mask, id);
cb69b4c7 1003}
1004
1005void
789217a2 1006HttpHeader::putInt(Http::HdrType id, int number)
cb69b4c7 1007{
8cccf1f8 1008 assert(any_registered_header(id));
81ab22b6 1009 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */
9bea1d5b 1010 assert(number >= 0);
eede25e7 1011 addEntry(new HttpHeaderEntry(id, NULL, xitoa(number)));
cb69b4c7 1012}
1013
47f6e231 1014void
789217a2 1015HttpHeader::putInt64(Http::HdrType id, int64_t number)
47f6e231 1016{
8cccf1f8 1017 assert(any_registered_header(id));
81ab22b6 1018 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */
47f6e231 1019 assert(number >= 0);
1020 addEntry(new HttpHeaderEntry(id, NULL, xint64toa(number)));
1021}
1022
cb69b4c7 1023void
789217a2 1024HttpHeader::putTime(Http::HdrType id, time_t htime)
cb69b4c7 1025{
8cccf1f8 1026 assert(any_registered_header(id));
81ab22b6 1027 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */
a1d6870f 1028 assert(htime >= 0);
eede25e7 1029 addEntry(new HttpHeaderEntry(id, NULL, mkrfc1123(htime)));
cb69b4c7 1030}
2ac76861 1031
cb69b4c7 1032void
789217a2 1033HttpHeader::putStr(Http::HdrType id, const char *str)
cb69b4c7 1034{
8cccf1f8 1035 assert(any_registered_header(id));
81ab22b6 1036 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
9bea1d5b 1037 assert(str);
eede25e7 1038 addEntry(new HttpHeaderEntry(id, NULL, str));
cb69b4c7 1039}
1040
63259c34 1041void
a9925b40 1042HttpHeader::putAuth(const char *auth_scheme, const char *realm)
63259c34 1043{
a9925b40 1044 assert(auth_scheme && realm);
789217a2 1045 httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
63259c34 1046}
1047
99edd1c3 1048void
a9925b40 1049HttpHeader::putCc(const HttpHdrCc * cc)
99edd1c3 1050{
a9925b40 1051 assert(cc);
9bea1d5b 1052 /* remove old directives if any */
789217a2 1053 delById(Http::HdrType::CACHE_CONTROL);
9bea1d5b 1054 /* pack into mb */
10201568 1055 MemBuf mb;
2fe7eff9 1056 mb.init();
10201568 1057 cc->packInto(&mb);
9bea1d5b 1058 /* put */
789217a2 1059 addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL, NULL, mb.buf));
9bea1d5b 1060 /* cleanup */
2fe7eff9 1061 mb.clean();
99edd1c3 1062}
1063
d192d11f 1064void
a9925b40 1065HttpHeader::putContRange(const HttpHdrContRange * cr)
d192d11f 1066{
a9925b40 1067 assert(cr);
9bea1d5b 1068 /* remove old directives if any */
789217a2 1069 delById(Http::HdrType::CONTENT_RANGE);
9bea1d5b 1070 /* pack into mb */
10201568 1071 MemBuf mb;
2fe7eff9 1072 mb.init();
10201568 1073 httpHdrContRangePackInto(cr, &mb);
9bea1d5b 1074 /* put */
789217a2 1075 addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE, NULL, mb.buf));
9bea1d5b 1076 /* cleanup */
2fe7eff9 1077 mb.clean();
d192d11f 1078}
1079
1080void
a9925b40 1081HttpHeader::putRange(const HttpHdrRange * range)
d192d11f 1082{
a9925b40 1083 assert(range);
9bea1d5b 1084 /* remove old directives if any */
789217a2 1085 delById(Http::HdrType::RANGE);
9bea1d5b 1086 /* pack into mb */
10201568 1087 MemBuf mb;
2fe7eff9 1088 mb.init();
10201568 1089 range->packInto(&mb);
9bea1d5b 1090 /* put */
789217a2 1091 addEntry(new HttpHeaderEntry(Http::HdrType::RANGE, NULL, mb.buf));
9bea1d5b 1092 /* cleanup */
2fe7eff9 1093 mb.clean();
d192d11f 1094}
1095
43ae1d95 1096void
a9925b40 1097HttpHeader::putSc(HttpHdrSc *sc)
43ae1d95 1098{
a9925b40 1099 assert(sc);
43ae1d95 1100 /* remove old directives if any */
789217a2 1101 delById(Http::HdrType::SURROGATE_CONTROL);
43ae1d95 1102 /* pack into mb */
10201568 1103 MemBuf mb;
2fe7eff9 1104 mb.init();
10201568 1105 sc->packInto(&mb);
43ae1d95 1106 /* put */
789217a2 1107 addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL, NULL, mb.buf));
43ae1d95 1108 /* cleanup */
2fe7eff9 1109 mb.clean();
43ae1d95 1110}
1111
bcfba8bd
AR
1112void
1113HttpHeader::putWarning(const int code, const char *const text)
1114{
1115 char buf[512];
1116 snprintf(buf, sizeof(buf), "%i %s \"%s\"", code, visible_appname_string, text);
789217a2 1117 putStr(Http::HdrType::WARNING, buf);
bcfba8bd
AR
1118}
1119
cb69b4c7 1120/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1121void
a9925b40 1122HttpHeader::putExt(const char *name, const char *value)
cb69b4c7 1123{
9bea1d5b 1124 assert(name && value);
efc26e8e 1125 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
789217a2 1126 addEntry(new HttpHeaderEntry(Http::HdrType::OTHER, name, value));
528b2c61 1127}
1128
1129int
789217a2 1130HttpHeader::getInt(Http::HdrType id) const
528b2c61 1131{
8cccf1f8 1132 assert(any_registered_header(id));
81ab22b6 1133 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */
528b2c61 1134 HttpHeaderEntry *e;
62e76326 1135
a9925b40 1136 if ((e = findEntry(id)))
eede25e7 1137 return e->getInt();
62e76326 1138
528b2c61 1139 return -1;
7c525cc2 1140}
1141
47f6e231 1142int64_t
789217a2 1143HttpHeader::getInt64(Http::HdrType id) const
47f6e231 1144{
8cccf1f8 1145 assert(any_registered_header(id));
81ab22b6 1146 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */
47f6e231 1147 HttpHeaderEntry *e;
1148
1149 if ((e = findEntry(id)))
1150 return e->getInt64();
1151
1152 return -1;
1153}
1154
de336bbe 1155time_t
789217a2 1156HttpHeader::getTime(Http::HdrType id) const
cb69b4c7 1157{
9bea1d5b 1158 HttpHeaderEntry *e;
1159 time_t value = -1;
8cccf1f8 1160 assert(any_registered_header(id));
81ab22b6 1161 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */
62e76326 1162
a9925b40 1163 if ((e = findEntry(id))) {
9d7a89a5 1164 value = parse_rfc1123(e->value.termedBuf());
62e76326 1165 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
d8b249ef 1166 }
62e76326 1167
9bea1d5b 1168 return value;
cb69b4c7 1169}
1170
99edd1c3 1171/* sync with httpHeaderGetLastStr */
de336bbe 1172const char *
789217a2 1173HttpHeader::getStr(Http::HdrType id) const
cb69b4c7 1174{
9bea1d5b 1175 HttpHeaderEntry *e;
8cccf1f8 1176 assert(any_registered_header(id));
81ab22b6 1177 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
62e76326 1178
a9925b40 1179 if ((e = findEntry(id))) {
81ab22b6 1180 httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */
9d7a89a5 1181 return e->value.termedBuf();
d8b249ef 1182 }
62e76326 1183
9bea1d5b 1184 return NULL;
cb69b4c7 1185}
1186
a622fff0 1187/* unusual */
1188const char *
789217a2 1189HttpHeader::getLastStr(Http::HdrType id) const
a622fff0 1190{
9bea1d5b 1191 HttpHeaderEntry *e;
8cccf1f8 1192 assert(any_registered_header(id));
81ab22b6 1193 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
62e76326 1194
a9925b40 1195 if ((e = findLastEntry(id))) {
81ab22b6 1196 httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */
9d7a89a5 1197 return e->value.termedBuf();
a622fff0 1198 }
62e76326 1199
9bea1d5b 1200 return NULL;
a622fff0 1201}
1202
7faf2bdb 1203HttpHdrCc *
a9925b40 1204HttpHeader::getCc() const
cb69b4c7 1205{
789217a2 1206 if (!CBIT_TEST(mask, Http::HdrType::CACHE_CONTROL))
62e76326 1207 return NULL;
35b88ed2 1208 PROF_start(HttpHeader_getCc);
62e76326 1209
c57b8f05 1210 String s;
789217a2 1211 getList(Http::HdrType::CACHE_CONTROL, &s);
62e76326 1212
c57b8f05 1213 HttpHdrCc *cc=new HttpHdrCc();
7ebe76de
FC
1214
1215 if (!cc->parse(s)) {
cf66f10a
FC
1216 delete cc;
1217 cc = NULL;
1218 }
62e76326 1219
95dc7ff4 1220 ++ HttpHeaderStats[owner].ccParsedCount;
62e76326 1221
9bea1d5b 1222 if (cc)
a9925b40 1223 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
62e76326 1224
789217a2 1225 httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL, s, !cc);
62e76326 1226
35b88ed2 1227 PROF_stop(HttpHeader_getCc);
62e76326 1228
9bea1d5b 1229 return cc;
cb69b4c7 1230}
1231
02922e76 1232HttpHdrRange *
a9925b40 1233HttpHeader::getRange() const
02922e76 1234{
9bea1d5b 1235 HttpHdrRange *r = NULL;
1236 HttpHeaderEntry *e;
1237 /* some clients will send "Request-Range" _and_ *matching* "Range"
1238 * who knows, some clients might send Request-Range only;
1239 * this "if" should work correctly in both cases;
1240 * hopefully no clients send mismatched headers! */
62e76326 1241
789217a2
FC
1242 if ((e = findEntry(Http::HdrType::RANGE)) ||
1243 (e = findEntry(Http::HdrType::REQUEST_RANGE))) {
62e76326 1244 r = HttpHdrRange::ParseCreate(&e->value);
1245 httpHeaderNoteParsedEntry(e->id, e->value, !r);
d192d11f 1246 }
62e76326 1247
9bea1d5b 1248 return r;
02922e76 1249}
1250
43ae1d95 1251HttpHdrSc *
a9925b40 1252HttpHeader::getSc() const
43ae1d95 1253{
789217a2 1254 if (!CBIT_TEST(mask, Http::HdrType::SURROGATE_CONTROL))
43ae1d95 1255 return NULL;
1256
30abd221 1257 String s;
26ac0430 1258
789217a2 1259 (void) getList(Http::HdrType::SURROGATE_CONTROL, &s);
43ae1d95 1260
45a58345 1261 HttpHdrSc *sc = httpHdrScParseCreate(s);
43ae1d95 1262
95dc7ff4 1263 ++ HttpHeaderStats[owner].ccParsedCount;
43ae1d95 1264
1265 if (sc)
45a58345 1266 sc->updateStats(&HttpHeaderStats[owner].scTypeDistr);
43ae1d95 1267
789217a2 1268 httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL, s, !sc);
43ae1d95 1269
1270 return sc;
1271}
1272
d76fcfa7 1273HttpHdrContRange *
a9925b40 1274HttpHeader::getContRange() const
d76fcfa7 1275{
9bea1d5b 1276 HttpHdrContRange *cr = NULL;
1277 HttpHeaderEntry *e;
62e76326 1278
789217a2 1279 if ((e = findEntry(Http::HdrType::CONTENT_RANGE))) {
9d7a89a5 1280 cr = httpHdrContRangeParseCreate(e->value.termedBuf());
62e76326 1281 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
d8b249ef 1282 }
62e76326 1283
9bea1d5b 1284 return cr;
cb69b4c7 1285}
1286
99edd1c3 1287const char *
789217a2 1288HttpHeader::getAuth(Http::HdrType id, const char *auth_scheme) const
cb69b4c7 1289{
9bea1d5b 1290 const char *field;
1291 int l;
a9925b40 1292 assert(auth_scheme);
1293 field = getStr(id);
62e76326 1294
f53969cc 1295 if (!field) /* no authorization field */
62e76326 1296 return NULL;
1297
9bea1d5b 1298 l = strlen(auth_scheme);
62e76326 1299
f53969cc 1300 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
62e76326 1301 return NULL;
1302
9bea1d5b 1303 field += l;
62e76326 1304
f53969cc 1305 if (!xisspace(*field)) /* wrong scheme */
62e76326 1306 return NULL;
1307
9bea1d5b 1308 /* skip white space */
95dc7ff4 1309 for (; field && xisspace(*field); ++field);
62e76326 1310
f53969cc 1311 if (!*field) /* no authorization cookie */
62e76326 1312 return NULL;
1313
8bdd0cec 1314 static char decodedAuthToken[8192];
aadbbd7d
AJ
1315 struct base64_decode_ctx ctx;
1316 base64_decode_init(&ctx);
1317 size_t decodedLen = 0;
1318 if (!base64_decode_update(&ctx, &decodedLen, reinterpret_cast<uint8_t*>(decodedAuthToken), strlen(field), reinterpret_cast<const uint8_t*>(field)) ||
1319 !base64_decode_final(&ctx)) {
1320 return NULL;
1321 }
8bdd0cec
AJ
1322 decodedAuthToken[decodedLen] = '\0';
1323 return decodedAuthToken;
cb69b4c7 1324}
1325
a9771e51 1326ETag
789217a2 1327HttpHeader::getETag(Http::HdrType id) const
a9771e51 1328{
26ac0430 1329 ETag etag = {NULL, -1};
9bea1d5b 1330 HttpHeaderEntry *e;
81ab22b6 1331 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftETag); /* must be of an appropriate type */
62e76326 1332
a9925b40 1333 if ((e = findEntry(id)))
9d7a89a5 1334 etagParseInit(&etag, e->value.termedBuf());
62e76326 1335
9bea1d5b 1336 return etag;
a9771e51 1337}
1338
1339TimeOrTag
789217a2 1340HttpHeader::getTimeOrTag(Http::HdrType id) const
a9771e51 1341{
9bea1d5b 1342 TimeOrTag tot;
1343 HttpHeaderEntry *e;
81ab22b6 1344 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123_or_ETag); /* must be of an appropriate type */
9bea1d5b 1345 memset(&tot, 0, sizeof(tot));
62e76326 1346
a9925b40 1347 if ((e = findEntry(id))) {
9d7a89a5 1348 const char *str = e->value.termedBuf();
62e76326 1349 /* try as an ETag */
1350
1351 if (etagParseInit(&tot.tag, str)) {
1352 tot.valid = tot.tag.str != NULL;
1353 tot.time = -1;
1354 } else {
1355 /* or maybe it is time? */
1356 tot.time = parse_rfc1123(str);
1357 tot.valid = tot.time >= 0;
1358 tot.tag.str = NULL;
1359 }
a9771e51 1360 }
62e76326 1361
f53969cc 1362 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
9bea1d5b 1363 return tot;
a9771e51 1364}
1365
cb69b4c7 1366/*
1367 * HttpHeaderEntry
1368 */
1369
789217a2 1370HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId, const char *aName, const char *aValue)
cb69b4c7 1371{
1da82544 1372 assert(any_HdrType_enum_value(anId));
eede25e7 1373 id = anId;
62e76326 1374
789217a2 1375 if (id != Http::HdrType::OTHER)
81ab22b6 1376 name = Http::HeaderLookupTable.lookup(id).name;
9bea1d5b 1377 else
eede25e7 1378 name = aName;
62e76326 1379
eede25e7 1380 value = aValue;
62e76326 1381
1da82544 1382 if (id != Http::HdrType::BAD_HDR)
73b2aa87 1383 ++ headerStatsTable[id].aliveCount;
62e76326 1384
9d7a89a5 1385 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name << " : " << value );
eede25e7 1386}
62e76326 1387
eede25e7 1388HttpHeaderEntry::~HttpHeaderEntry()
2ac76861 1389{
9d7a89a5 1390 debugs(55, 9, "destroying entry " << this << ": '" << name << ": " << value << "'");
62e76326 1391
1da82544
FC
1392 if (id != Http::HdrType::BAD_HDR) {
1393 assert(headerStatsTable[id].aliveCount);
1394 -- headerStatsTable[id].aliveCount;
1395 id = Http::HdrType::BAD_HDR; // it already is BAD_HDR, no sense in resetting it
1396 }
62e76326 1397
cb69b4c7 1398}
1399
eede25e7 1400/* parses and inits header entry, returns true/false */
cdce6c61 1401HttpHeaderEntry *
eede25e7 1402HttpHeaderEntry::parse(const char *field_start, const char *field_end)
cb69b4c7 1403{
9bea1d5b 1404 /* note: name_start == field_start */
52d3f198 1405 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
26ac0430 1406 int name_len = name_end ? name_end - field_start :0;
f53969cc 1407 const char *value_start = field_start + name_len + 1; /* skip ':' */
9bea1d5b 1408 /* note: value_end == field_end */
1409
95dc7ff4 1410 ++ HeaderEntryParsedCount;
9bea1d5b 1411
1412 /* do we have a valid field name within this field? */
62e76326 1413
9bea1d5b 1414 if (!name_len || name_end > field_end)
cdce6c61 1415 return NULL;
62e76326 1416
67746eea 1417 if (name_len > 65534) {
1418 /* String must be LESS THAN 64K and it adds a terminating NULL */
e0236918 1419 debugs(55, DBG_IMPORTANT, "WARNING: ignoring header name of " << name_len << " bytes");
cdce6c61 1420 return NULL;
25acfb53 1421 }
62e76326 1422
52d3f198 1423 if (Config.onoff.relaxed_header_parser && xisspace(field_start[name_len - 1])) {
bf8fe701 1424 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
81858ebc 1425 "NOTICE: Whitespace after header name in '" << getStringPrefix(field_start, field_end-field_start) << "'");
52d3f198 1426
1427 while (name_len > 0 && xisspace(field_start[name_len - 1]))
5e263176 1428 --name_len;
52d3f198 1429
1430 if (!name_len)
cdce6c61 1431 return NULL;
52d3f198 1432 }
1433
9bea1d5b 1434 /* now we know we can parse it */
62e76326 1435
81858ebc 1436 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end-field_start) << "'");
62e76326 1437
9bea1d5b 1438 /* is it a "known" field? */
81ab22b6 1439 Http::HdrType id = Http::HeaderLookupTable.lookup(field_start,name_len).id;
92991271 1440 debugs(55, 9, "got hdr-id=" << id);
cdce6c61 1441
30abd221 1442 String name;
cdce6c61 1443
30abd221 1444 String value;
62e76326 1445
789217a2
FC
1446 if (id == Http::HdrType::BAD_HDR)
1447 id = Http::HdrType::OTHER;
62e76326 1448
9bea1d5b 1449 /* set field name */
789217a2 1450 if (id == Http::HdrType::OTHER)
eede25e7 1451 name.limitInit(field_start, name_len);
9bea1d5b 1452 else
81ab22b6 1453 name = Http::HeaderLookupTable.lookup(id).name;
62e76326 1454
9bea1d5b 1455 /* trim field value */
1456 while (value_start < field_end && xisspace(*value_start))
95dc7ff4 1457 ++value_start;
62e76326 1458
52d3f198 1459 while (value_start < field_end && xisspace(field_end[-1]))
5e263176 1460 --field_end;
52d3f198 1461
67746eea 1462 if (field_end - value_start > 65534) {
1463 /* String must be LESS THAN 64K and it adds a terminating NULL */
e0236918 1464 debugs(55, DBG_IMPORTANT, "WARNING: ignoring '" << name << "' header of " << (field_end - value_start) << " bytes");
62e76326 1465
789217a2 1466 if (id == Http::HdrType::OTHER)
30abd221 1467 name.clean();
62e76326 1468
cdce6c61 1469 return NULL;
25acfb53 1470 }
62e76326 1471
9bea1d5b 1472 /* set field value */
eede25e7 1473 value.limitInit(value_start, field_end - value_start);
62e76326 1474
789217a2 1475 if (id != Http::HdrType::BAD_HDR)
73b2aa87 1476 ++ headerStatsTable[id].seenCount;
62e76326 1477
9d7a89a5 1478 debugs(55, 9, "parsed HttpHeaderEntry: '" << name << ": " << value << "'");
62e76326 1479
9d7a89a5 1480 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1481}
1482
99edd1c3 1483HttpHeaderEntry *
eede25e7 1484HttpHeaderEntry::clone() const
cb69b4c7 1485{
9d7a89a5 1486 return new HttpHeaderEntry(id, name.termedBuf(), value.termedBuf());
cb69b4c7 1487}
1488
de336bbe 1489void
17802cf1 1490HttpHeaderEntry::packInto(Packable * p) const
cb69b4c7 1491{
eede25e7 1492 assert(p);
785b508d
AJ
1493 p->append(name.rawBuf(), name.size());
1494 p->append(": ", 2);
1495 p->append(value.rawBuf(), value.size());
1496 p->append("\r\n", 2);
cb69b4c7 1497}
1498
eede25e7 1499int
1500HttpHeaderEntry::getInt() const
1501{
eede25e7 1502 int val = -1;
9d7a89a5 1503 int ok = httpHeaderParseInt(value.termedBuf(), &val);
81ab22b6 1504 httpHeaderNoteParsedEntry(id, value, ok == 0);
eede25e7 1505 /* XXX: Should we check ok - ie
1506 * return ok ? -1 : value;
1507 */
1508 return val;
1509}
1510
47f6e231 1511int64_t
1512HttpHeaderEntry::getInt64() const
1513{
47f6e231 1514 int64_t val = -1;
a1b9ec20
AR
1515 const bool ok = httpHeaderParseOffset(value.termedBuf(), &val);
1516 httpHeaderNoteParsedEntry(id, value, ok);
1517 return val; // remains -1 if !ok (XXX: bad method API)
47f6e231 1518}
1519
cb69b4c7 1520static void
81ab22b6 1521httpHeaderNoteParsedEntry(Http::HdrType id, String const &context, bool error)
cb69b4c7 1522{
789217a2 1523 if (id != Http::HdrType::BAD_HDR)
73b2aa87 1524 ++ headerStatsTable[id].parsCount;
62e76326 1525
9bea1d5b 1526 if (error) {
789217a2 1527 if (id != Http::HdrType::BAD_HDR)
73b2aa87 1528 ++ headerStatsTable[id].errCount;
81ab22b6 1529 debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderLookupTable.lookup(id).name << ": " << context << "'");
cb69b4c7 1530 }
cb69b4c7 1531}
1532
cb69b4c7 1533/*
12cf1be2 1534 * Reports
cb69b4c7 1535 */
cb69b4c7 1536
fcd2d3ef 1537/* tmp variable used to pass stat info to dumpers */
f53969cc 1538extern const HttpHeaderStat *dump_stat; /* argh! */
fcd2d3ef 1539const HttpHeaderStat *dump_stat = NULL;
1540
59a09b98 1541void
ced8def3 1542httpHeaderFieldStatDumper(StoreEntry * sentry, int, double val, double, int count)
cb69b4c7 1543{
92991271 1544 const int id = static_cast<int>(val);
1da82544 1545 const bool valid_id = Http::any_valid_header(static_cast<Http::HdrType>(id));
81ab22b6 1546 const char *name = valid_id ? Http::HeaderLookupTable.lookup(static_cast<Http::HdrType>(id)).name : "INVALID";
9bea1d5b 1547 int visible = count > 0;
1548 /* for entries with zero count, list only those that belong to current type of message */
62e76326 1549
9bea1d5b 1550 if (!visible && valid_id && dump_stat->owner_mask)
62e76326 1551 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1552
9bea1d5b 1553 if (visible)
62e76326 1554 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1555 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
cb69b4c7 1556}
1557
12cf1be2 1558static void
ced8def3 1559httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double, int count)
cb69b4c7 1560{
9bea1d5b 1561 if (count)
62e76326 1562 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1563 idx, (int) val, count,
1564 xpercent(count, dump_stat->destroyedCount));
cb69b4c7 1565}
1566
cb69b4c7 1567static void
9bea1d5b 1568httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
12cf1be2 1569{
73b2aa87
FC
1570 assert(hs);
1571 assert(e);
9bea1d5b 1572
1573 dump_stat = hs;
1574 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1575 storeAppendPrintf(e, "\nField type distribution\n");
1576 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1577 "id", "name", "count", "#/header");
96886986 1578 hs->fieldTypeDistr.dump(e, httpHeaderFieldStatDumper);
9bea1d5b 1579 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1580 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
62e76326 1581 "id", "name", "count", "#/cc_field");
96886986 1582 hs->ccTypeDistr.dump(e, httpHdrCcStatDumper);
43ae1d95 1583 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1584 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1585 "id", "name", "count", "#/sc_field");
96886986 1586 hs->scTypeDistr.dump(e, httpHdrScStatDumper);
9bea1d5b 1587 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1588 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
62e76326 1589 "id", "#flds", "count", "%total");
96886986 1590 hs->hdrUCountDistr.dump(e, httpHeaderFldsPerHdrDumper);
41b43e70 1591 storeAppendPrintf(e, "\n");
9bea1d5b 1592 dump_stat = NULL;
cb69b4c7 1593}
1594
12cf1be2 1595void
9bea1d5b 1596httpHeaderStoreReport(StoreEntry * e)
cb69b4c7 1597{
9bea1d5b 1598 int i;
9bea1d5b 1599 assert(e);
1600
1601 HttpHeaderStats[0].parsedCount =
62e76326 1602 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
9bea1d5b 1603 HttpHeaderStats[0].ccParsedCount =
62e76326 1604 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
9bea1d5b 1605 HttpHeaderStats[0].destroyedCount =
62e76326 1606 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
9bea1d5b 1607 HttpHeaderStats[0].busyDestroyedCount =
62e76326 1608 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
9bea1d5b 1609
95dc7ff4 1610 for (i = 1; i < HttpHeaderStatCount; ++i) {
62e76326 1611 httpHeaderStatDump(HttpHeaderStats + i, e);
12cf1be2 1612 }
62e76326 1613
9bea1d5b 1614 /* field stats for all messages */
1615 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
62e76326 1616
077fe581 1617 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
62e76326 1618 "id", "name", "#alive", "%err", "%repeat");
1619
fda6d769 1620 // scan heaaderTable and output
81ab22b6
FC
1621 for (auto h : WholeEnum<Http::HdrType>()) {
1622 auto stats = headerStatsTable[h];
fda6d769 1623 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
81ab22b6
FC
1624 Http::HeaderLookupTable.lookup(h).id,
1625 Http::HeaderLookupTable.lookup(h).name,
92991271 1626 stats.aliveCount,
f7ad4af5
FC
1627 xpercent(stats.errCount, stats.parsCount),
1628 xpercent(stats.repCount, stats.seenCount));
fda6d769 1629 }
62e76326 1630
9bea1d5b 1631 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
62e76326 1632 HttpHeaderStats[hoRequest].parsedCount,
1633 HttpHeaderStats[hoReply].parsedCount,
1634 HttpHeaderStats[0].parsedCount);
9bea1d5b 1635 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
cb69b4c7 1636}
97474590 1637
924f73bc 1638int
789217a2 1639HttpHeader::hasListMember(Http::HdrType id, const char *member, const char separator) const
924f73bc 1640{
1641 int result = 0;
1642 const char *pos = NULL;
1643 const char *item;
1644 int ilen;
1645 int mlen = strlen(member);
1646
8cccf1f8 1647 assert(any_registered_header(id));
924f73bc 1648
30abd221 1649 String header (getStrOrList(id));
924f73bc 1650
9d0cdbb9 1651 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1652 if (strncasecmp(item, member, mlen) == 0
9d0cdbb9 1653 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1654 result = 1;
1655 break;
1656 }
1657 }
1658
1659 return result;
1660}
1661
1662int
a9925b40 1663HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
9d0cdbb9 1664{
1665 int result = 0;
1666 const char *pos = NULL;
1667 const char *item;
1668 int ilen;
1669 int mlen = strlen(member);
1670
9d0cdbb9 1671 assert(name);
1672
30abd221 1673 String header (getByName(name));
9d0cdbb9 1674
924f73bc 1675 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
380e7faf 1676 if (strncasecmp(item, member, mlen) == 0
924f73bc 1677 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1678 result = 1;
1679 break;
1680 }
1681 }
1682
1683 return result;
1684}
1685
2cdeea82 1686void
1687HttpHeader::removeHopByHopEntries()
1688{
1689 removeConnectionHeaderEntries();
26ac0430 1690
dcf3665b
AJ
1691 const HttpHeaderEntry *e;
1692 HttpHeaderPos pos = HttpHeaderInitPos;
1693 int headers_deleted = 0;
1694 while ((e = getEntry(&pos))) {
789217a2 1695 Http::HdrType id = e->id;
81ab22b6 1696 if (Http::HeaderLookupTable.lookup(id).hopbyhop) {
26ac0430
AJ
1697 delAt(pos, headers_deleted);
1698 CBIT_CLR(mask, id);
1699 }
dcf3665b 1700 }
2cdeea82 1701}
1702
924f73bc 1703void
1704HttpHeader::removeConnectionHeaderEntries()
1705{
789217a2 1706 if (has(Http::HdrType::CONNECTION)) {
924f73bc 1707 /* anything that matches Connection list member will be deleted */
30abd221 1708 String strConnection;
26ac0430 1709
789217a2 1710 (void) getList(Http::HdrType::CONNECTION, &strConnection);
924f73bc 1711 const HttpHeaderEntry *e;
1712 HttpHeaderPos pos = HttpHeaderInitPos;
1713 /*
1714 * think: on-average-best nesting of the two loops (hdrEntry
1715 * and strListItem) @?@
1716 */
1717 /*
1718 * maybe we should delete standard stuff ("keep-alive","close")
1719 * from strConnection first?
1720 */
1721
ba9fb01d 1722 int headers_deleted = 0;
a9925b40 1723 while ((e = getEntry(&pos))) {
9d7a89a5 1724 if (strListIsMember(&strConnection, e->name.termedBuf(), ','))
ba9fb01d 1725 delAt(pos, headers_deleted);
924f73bc 1726 }
ba9fb01d 1727 if (headers_deleted)
1728 refreshMask();
924f73bc 1729 }
1730}
f53969cc 1731