Squid Web Cache master
Loading...
Searching...
No Matches
HttpHeader.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/* DEBUG: section 55 HTTP Header */
10
11#include "squid.h"
12#include "base/Assure.h"
13#include "base/CharacterSet.h"
14#include "base/EnumIterator.h"
15#include "base/Raw.h"
16#include "base64.h"
17#include "globals.h"
19#include "HttpHdrCc.h"
20#include "HttpHdrContRange.h"
21#include "HttpHdrScTarget.h" // also includes HttpHdrSc.h
22#include "HttpHeader.h"
23#include "HttpHeaderFieldStat.h"
24#include "HttpHeaderStat.h"
25#include "HttpHeaderTools.h"
26#include "MemBuf.h"
27#include "mgr/Registration.h"
28#include "mime_header.h"
29#include "sbuf/Stream.h"
30#include "sbuf/StringConvert.h"
31#include "SquidConfig.h"
32#include "StatHist.h"
33#include "Store.h"
34#include "StrList.h"
35#include "time/gadgets.h"
36#include "TimeOrTag.h"
37#include "util.h"
38
39#include <algorithm>
40#include <array>
41
42/* XXX: the whole set of API managing the entries vector should be rethought
43 * after the parse4r-ng effort is complete.
44 */
45
46/*
47 * On naming conventions:
48 *
49 * HTTP/1.1 defines message-header as
50 *
51 * message-header = field-name ":" [ field-value ] CRLF
52 * field-name = token
53 * field-value = *( field-content | LWS )
54 *
55 * HTTP/1.1 does not give a name name a group of all message-headers in a message.
56 * Squid 1.1 seems to refer to that group _plus_ start-line as "headers".
57 *
58 * HttpHeader is an object that represents all message-headers in a message.
59 * HttpHeader does not manage start-line.
60 *
61 * HttpHeader is implemented as a collection of header "entries".
62 * An entry is a (field_id, field_name, field_value) triplet.
63 */
64
65/*
66 * local constants and vars
67 */
68
69// statistics counters for headers. clients must not allow Http::HdrType::BAD_HDR to be counted
70std::vector<HttpHeaderFieldStat> headerStatsTable(Http::HdrType::enumEnd_);
71
72/* request-only headers. Used for cachemgr */
73static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */
74
75/* reply-only headers. Used for cachemgr */
76static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */
77
78/* header accounting */
79// NP: keep in sync with enum http_hdr_owner_type
80static std::array<HttpHeaderStat, hoEnd> HttpHeaderStats = {{
81 HttpHeaderStat(/*hoNone*/ "all", nullptr),
82#if USE_HTCP
83 HttpHeaderStat(/*hoHtcpReply*/ "HTCP reply", &ReplyHeadersMask),
84#endif
85 HttpHeaderStat(/*hoRequest*/ "request", &RequestHeadersMask),
86 HttpHeaderStat(/*hoReply*/ "reply", &ReplyHeadersMask)
87#if USE_OPENSSL
88 , HttpHeaderStat(/*hoErrorDetail*/ "error detail templates", nullptr)
89#endif
90 /* hoEnd */
91 }
92};
93
95
96/*
97 * forward declarations and local routines
98 */
99
100class StoreEntry;
101
102// update parse statistics for header id; if error is true also account
103// for errors and write to debug log what happened
104static void httpHeaderNoteParsedEntry(Http::HdrType id, String const &value, bool error);
105static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e);
107static void httpHeaderStoreReport(StoreEntry * e);
108
109/*
110 * Module initialization routines
111 */
112
113static void
115{
116 Mgr::RegisterAction("http_headers",
117 "HTTP Header Statistics",
119}
120
121static void
123{
124 memset(mask, value, sizeof(*mask));
125}
126
128static const char *
129getStringPrefix(const char *str, size_t sz)
130{
131#define SHORT_PREFIX_SIZE 512
132 LOCAL_ARRAY(char, buf, SHORT_PREFIX_SIZE);
133 xstrncpy(buf, str, (sz+1 > SHORT_PREFIX_SIZE) ? SHORT_PREFIX_SIZE : sz);
134 return buf;
135}
136
137void
139{
140 /* check that we have enough space for masks */
142
143 // masks are needed for stats page still
144 for (auto h : WholeEnum<Http::HdrType>()) {
145 if (Http::HeaderLookupTable.lookup(h).request)
147 if (Http::HeaderLookupTable.lookup(h).reply)
149 }
150
151 assert(HttpHeaderStats[0].label && "httpHeaderInitModule() called via main()");
152 assert(HttpHeaderStats[hoEnd-1].label && "HttpHeaderStats created with all elements");
153
154 /* init dependent modules */
156
158}
159
166int
167httpHeaderParseQuotedString(const char *start, const int len, String *val)
168{
169 const char *end, *pos;
170 val->clean();
171 if (*start != '"') {
172 debugs(66, 2, "failed to parse a quoted-string header field near '" << start << "'");
173 return 0;
174 }
175 pos = start + 1;
176
177 while (*pos != '"' && len > (pos-start)) {
178
179 if (*pos =='\r') {
180 ++pos;
181 if ((pos-start) > len || *pos != '\n') {
182 debugs(66, 2, "failed to parse a quoted-string header field with '\\r' octet " << (start-pos)
183 << " bytes into '" << start << "'");
184 val->clean();
185 return 0;
186 }
187 }
188
189 if (*pos == '\n') {
190 ++pos;
191 if ( (pos-start) > len || (*pos != ' ' && *pos != '\t')) {
192 debugs(66, 2, "failed to parse multiline quoted-string header field '" << start << "'");
193 val->clean();
194 return 0;
195 }
196 // TODO: replace the entire LWS with a space
197 val->append(" ");
198 ++pos;
199 debugs(66, 2, "len < pos-start => " << len << " < " << (pos-start));
200 continue;
201 }
202
203 bool quoted = (*pos == '\\');
204 if (quoted) {
205 ++pos;
206 if (!*pos || (pos-start) > len) {
207 debugs(66, 2, "failed to parse a quoted-string header field near '" << start << "'");
208 val->clean();
209 return 0;
210 }
211 }
212 end = pos;
213 while (end < (start+len) && *end != '\\' && *end != '\"' && (unsigned char)*end > 0x1F && *end != 0x7F)
214 ++end;
215 if (((unsigned char)*end <= 0x1F && *end != '\r' && *end != '\n') || *end == 0x7F) {
216 debugs(66, 2, "failed to parse a quoted-string header field with CTL octet " << (start-pos)
217 << " bytes into '" << start << "'");
218 val->clean();
219 return 0;
220 }
221 val->append(pos, end-pos);
222 pos = end;
223 }
224
225 if (*pos != '\"') {
226 debugs(66, 2, "failed to parse a quoted-string header field which did not end with \" ");
227 val->clean();
228 return 0;
229 }
230 /* Make sure it's defined even if empty "" */
231 if (!val->termedBuf())
232 val->assign("", 0);
233 return 1;
234}
235
236SBuf
237httpHeaderQuoteString(const char *raw)
238{
239 assert(raw);
240
241 // TODO: Optimize by appending a sequence of characters instead of a char.
242 // This optimization may be easier with Tokenizer after raw becomes SBuf.
243
244 // RFC 7230 says a "sender SHOULD NOT generate a quoted-pair in a
245 // quoted-string except where necessary" (i.e., DQUOTE and backslash)
246 bool needInnerQuote = false;
247 for (const char *s = raw; !needInnerQuote && *s; ++s)
248 needInnerQuote = *s == '"' || *s == '\\';
249
250 SBuf quotedStr;
251 quotedStr.append('"');
252
253 if (needInnerQuote) {
254 for (const char *s = raw; *s; ++s) {
255 if (*s == '"' || *s == '\\')
256 quotedStr.append('\\');
257 quotedStr.append(*s);
258 }
259 } else {
260 quotedStr.append(raw);
261 }
262
263 quotedStr.append('"');
264 return quotedStr;
265}
266
267SBuf
268Http::SlowlyParseQuotedString(const char * const description, const char * const start, const size_t length)
269{
270 String s;
271 if (!httpHeaderParseQuotedString(start, length, &s))
272 throw TextException(ToSBuf("Cannot parse ", description, " as a quoted string"), Here());
273 return StringToSBuf(s);
274}
275
276/*
277 * HttpHeader Implementation
278 */
279
280HttpHeader::HttpHeader(const http_hdr_owner_type anOwner): owner(anOwner), len(0), conflictingContentLength_(false)
281{
282 assert(anOwner > hoNone && anOwner < hoEnd);
283 debugs(55, 7, "init-ing hdr: " << this << " owner: " << owner);
284 entries.reserve(32);
286}
287
288// XXX: Delete as unused, expensive, and violating copy semantics by skipping Warnings
289HttpHeader::HttpHeader(const HttpHeader &other): owner(other.owner), len(other.len), conflictingContentLength_(false)
290{
291 entries.reserve(other.entries.capacity());
293 update(&other); // will update the mask as well
294}
295
300
301// XXX: Delete as unused, expensive, and violating assignment semantics by skipping Warnings
304{
305 if (this != &other) {
306 // we do not really care, but the caller probably does
307 assert(owner == other.owner);
308 clean();
309 update(&other); // will update the mask as well
310 len = other.len;
313 }
314 return *this;
315}
316
317void
319{
320
321 assert(owner > hoNone && owner < hoEnd);
322 debugs(55, 7, "cleaning hdr: " << this << " owner: " << owner);
323
324 if (owner <= hoReply) {
325 /*
326 * An unfortunate bug. The entries array is initialized
327 * such that count is set to zero. httpHeaderClean() seems to
328 * be called both when 'hdr' is created, and destroyed. Thus,
329 * we accumulate a large number of zero counts for 'hdr' before
330 * it is ever used. Can't think of a good way to fix it, except
331 * adding a state variable that indicates whether or not 'hdr'
332 * has been used. As a hack, just never count zero-sized header
333 * arrays.
334 */
335 if (!entries.empty())
336 HttpHeaderStats[owner].hdrUCountDistr.count(entries.size());
337
338 ++ HttpHeaderStats[owner].destroyedCount;
339
340 HttpHeaderStats[owner].busyDestroyedCount += entries.size() > 0;
341 } // if (owner <= hoReply)
342
343 for (HttpHeaderEntry *e : entries) {
344 if (e == nullptr)
345 continue;
346 if (!Http::any_valid_header(e->id)) {
347 debugs(55, DBG_CRITICAL, "ERROR: Squid BUG: invalid entry (" << e->id << "). Ignored.");
348 } else {
349 if (owner <= hoReply)
350 HttpHeaderStats[owner].fieldTypeDistr.count(e->id);
351 delete e;
352 }
353 }
354
355 entries.clear();
357 len = 0;
359 teUnsupported_ = false;
360}
361
362/* append entries (also see httpHeaderUpdate) */
363void
365{
366 assert(src);
367 assert(src != this);
368 debugs(55, 7, "appending hdr: " << this << " += " << src);
369
370 for (auto e : src->entries) {
371 if (e)
372 addEntry(e->clone());
373 }
374}
375
376bool
378{
379 for (const auto e: fresh->entries) {
380 if (!e || skipUpdateHeader(e->id))
381 continue;
382 String value;
383 if (!hasNamed(e->name, &value) ||
384 (value != fresh->getByName(e->name)))
385 return true;
386 }
387 return false;
388}
389
390bool
392{
393 return
394 // TODO: Consider updating Vary headers after comparing the magnitude of
395 // the required changes (and/or cache losses) with compliance gains.
396 (id == Http::HdrType::VARY);
397}
398
399void
401{
402 assert(fresh);
403 assert(this != fresh);
404
405 const HttpHeaderEntry *e;
407
408 while ((e = fresh->getEntry(&pos))) {
409 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
410
411 if (skipUpdateHeader(e->id))
412 continue;
413
414 if (e->id != Http::HdrType::OTHER)
415 delById(e->id);
416 else
417 delByName(e->name);
418 }
419
420 pos = HttpHeaderInitPos;
421 while ((e = fresh->getEntry(&pos))) {
422 /* deny bad guys (ok to check for Http::HdrType::OTHER) here */
423
424 if (skipUpdateHeader(e->id))
425 continue;
426
427 debugs(55, 7, "Updating header '" << Http::HeaderLookupTable.lookup(e->id).name << "' in cached entry");
428
429 addEntry(e->clone());
430 }
431}
432
433bool
434HttpHeader::Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end)
435{
436 /*
437 * parse_start points to the first line of HTTP message *headers*,
438 * not including the request or status lines
439 */
440 const size_t end = headersEnd(*parse_start, l);
441
442 if (end) {
443 *blk_start = *parse_start;
444 *blk_end = *parse_start + end - 1;
445 assert(**blk_end == '\n');
446 // Point blk_end to the first character after the last header field.
447 // In other words, blk_end should point to the CR?LF header terminator.
448 if (end > 1 && *(*blk_end - 1) == '\r')
449 --(*blk_end);
450 *parse_start += end;
451 }
452 return end;
453}
454
455int
456HttpHeader::parse(const char *buf, size_t buf_len, bool atEnd, size_t &hdr_sz, Http::ContentLengthInterpreter &clen)
457{
458 const char *parse_start = buf;
459 const char *blk_start, *blk_end;
460 hdr_sz = 0;
461
462 if (!Isolate(&parse_start, buf_len, &blk_start, &blk_end)) {
463 // XXX: do not parse non-isolated headers even if the connection is closed.
464 // Treat unterminated headers as "partial headers" framing errors.
465 if (!atEnd)
466 return 0;
467 blk_start = parse_start;
468 blk_end = blk_start + strlen(blk_start);
469 }
470
471 if (parse(blk_start, blk_end - blk_start, clen)) {
472 hdr_sz = parse_start - buf;
473 return 1;
474 }
475 return -1;
476}
477
478// XXX: callers treat this return as boolean.
479// XXX: A better mechanism is needed to signal different types of error.
480// lexicon, syntax, semantics, validation, access policy - are all (ab)using 'return 0'
481int
482HttpHeader::parse(const char *header_start, size_t hdrLen, Http::ContentLengthInterpreter &clen)
483{
484 const char *field_ptr = header_start;
485 const char *header_end = header_start + hdrLen; // XXX: remove
486 int warnOnError = (Config.onoff.relaxed_header_parser <= 0 ? DBG_IMPORTANT : 2);
487
488 assert(header_start && header_end);
489 debugs(55, 7, "parsing hdr: (" << this << ")" << std::endl << getStringPrefix(header_start, hdrLen));
490 ++ HttpHeaderStats[owner].parsedCount;
491
492 char *nulpos;
493 if ((nulpos = (char*)memchr(header_start, '\0', hdrLen))) {
494 debugs(55, DBG_IMPORTANT, "WARNING: HTTP header contains NULL characters {" <<
495 getStringPrefix(header_start, nulpos-header_start) << "}\nNULL\n{" << getStringPrefix(nulpos+1, hdrLen-(nulpos-header_start)-1));
496 clean();
497 return 0;
498 }
499
500 /* common format headers are "<name>:[ws]<value>" lines delimited by <CRLF>.
501 * continuation lines start with a (single) space or tab */
502 while (field_ptr < header_end) {
503 const char *field_start = field_ptr;
504 const char *field_end;
505
506 const char *hasBareCr = nullptr;
507 size_t lines = 0;
508 do {
509 const char *this_line = field_ptr;
510 field_ptr = (const char *)memchr(field_ptr, '\n', header_end - field_ptr);
511 ++lines;
512
513 if (!field_ptr) {
514 // missing <LF>
515 clean();
516 return 0;
517 }
518
519 field_end = field_ptr;
520
521 ++field_ptr; /* Move to next line */
522
523 if (field_end > this_line && field_end[-1] == '\r') {
524 --field_end; /* Ignore CR LF */
525
526 if (owner == hoRequest && field_end > this_line) {
527 bool cr_only = true;
528 for (const char *p = this_line; p < field_end && cr_only; ++p) {
529 if (*p != '\r')
530 cr_only = false;
531 }
532 if (cr_only) {
533 debugs(55, DBG_IMPORTANT, "SECURITY WARNING: Rejecting HTTP request with a CR+ "
534 "header field to prevent request smuggling attacks: {" <<
535 getStringPrefix(header_start, hdrLen) << "}");
536 clean();
537 return 0;
538 }
539 }
540 }
541
542 /* Barf on stray CR characters */
543 if (memchr(this_line, '\r', field_end - this_line)) {
544 hasBareCr = "bare CR";
545 debugs(55, warnOnError, "WARNING: suspicious CR characters in HTTP header {" <<
546 getStringPrefix(field_start, field_end-field_start) << "}");
547
549 char *p = (char *) this_line; /* XXX Warning! This destroys original header content and violates specifications somewhat */
550
551 while ((p = (char *)memchr(p, '\r', field_end - p)) != nullptr) {
552 *p = ' ';
553 ++p;
554 }
555 } else {
556 clean();
557 return 0;
558 }
559 }
560
561 if (this_line + 1 == field_end && this_line > field_start) {
562 debugs(55, warnOnError, "WARNING: Blank continuation line in HTTP header {" <<
563 getStringPrefix(header_start, hdrLen) << "}");
564 clean();
565 return 0;
566 }
567 } while (field_ptr < header_end && (*field_ptr == ' ' || *field_ptr == '\t'));
568
569 if (field_start == field_end) {
570 if (field_ptr < header_end) {
571 debugs(55, warnOnError, "WARNING: unparsable HTTP header field near {" <<
572 getStringPrefix(field_start, hdrLen-(field_start-header_start)) << "}");
573 clean();
574 return 0;
575 }
576
577 break; /* terminating blank line */
578 }
579
580 const auto e = HttpHeaderEntry::parse(field_start, field_end, owner);
581 if (!e) {
582 debugs(55, warnOnError, "WARNING: unparsable HTTP header field {" <<
583 getStringPrefix(field_start, field_end-field_start) << "}");
584 debugs(55, warnOnError, " in {" << getStringPrefix(header_start, hdrLen) << "}");
585
586 clean();
587 return 0;
588 }
589
590 if (lines > 1 || hasBareCr) {
591 const auto framingHeader = (e->id == Http::HdrType::CONTENT_LENGTH || e->id == Http::HdrType::TRANSFER_ENCODING);
592 if (framingHeader) {
593 if (!hasBareCr) // already warned about bare CRs
594 debugs(55, warnOnError, "WARNING: obs-fold in framing-sensitive " << e->name << ": " << e->value);
595 delete e;
596 clean();
597 return 0;
598 }
599 }
600
601 if (e->id == Http::HdrType::CONTENT_LENGTH && !clen.checkField(e->value)) {
602 delete e;
603
605 continue; // clen has printed any necessary warnings
606
607 clean();
608 return 0;
609 }
610
611 addEntry(e);
612 }
613
614 if (clen.headerWideProblem) {
615 debugs(55, warnOnError, "WARNING: " << clen.headerWideProblem <<
616 " Content-Length field values in" <<
617 Raw("header", header_start, hdrLen));
618 }
619
620 String rawTe;
621 if (clen.prohibitedAndIgnored()) {
622 // prohibitedAndIgnored() includes trailer header blocks
623 // being parsed as a case to forbid/ignore these headers.
624
625 // RFC 7230 section 3.3.2: A server MUST NOT send a Content-Length
626 // header field in any response with a status code of 1xx (Informational)
627 // or 204 (No Content). And RFC 7230 3.3.3#1 tells recipients to ignore
628 // such Content-Lengths.
630 debugs(55, 3, "Content-Length is " << clen.prohibitedAndIgnored());
631
632 // The same RFC 7230 3.3.3#1-based logic applies to Transfer-Encoding
633 // banned by RFC 7230 section 3.3.1.
635 debugs(55, 3, "Transfer-Encoding is " << clen.prohibitedAndIgnored());
636
638 // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
639 // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length
641 // and clen state becomes irrelevant
642
643 if (rawTe.caseCmp("chunked") == 0) {
644 ; // leave header present for chunked() method
645 } else if (rawTe.caseCmp("identity") == 0) { // deprecated. no coding
647 } else {
648 // This also rejects multiple encodings until we support them properly.
649 debugs(55, warnOnError, "WARNING: unsupported Transfer-Encoding used by client: " << rawTe);
650 teUnsupported_ = true;
651 }
652
653 } else if (clen.sawBad) {
654 // ensure our callers do not accidentally see bad Content-Length values
656 conflictingContentLength_ = true; // TODO: Rename to badContentLength_.
657 } else if (clen.needsSanitizing) {
658 // RFC 7230 section 3.3.2: MUST either reject or ... [sanitize];
659 // ensure our callers see a clean Content-Length value or none at all
661 if (clen.sawGood) {
663 debugs(55, 5, "sanitized Content-Length to be " << clen.value);
664 }
665 }
666
667 return 1; /* even if no fields where found, it is a valid header */
668}
669
670/* packs all the entries using supplied packer */
671void
672HttpHeader::packInto(Packable * p, bool mask_sensitive_info) const
673{
675 const HttpHeaderEntry *e;
676 assert(p);
677 debugs(55, 7, this << " into " << p <<
678 (mask_sensitive_info ? " while masking" : ""));
679 /* pack all entries one by one */
680 while ((e = getEntry(&pos))) {
681 if (!mask_sensitive_info) {
682 e->packInto(p);
683 continue;
684 }
685
686 bool maskThisEntry = false;
687 switch (e->id) {
690 maskThisEntry = true;
691 break;
692
695 maskThisEntry = (cmd->value == "PASS");
696 break;
697
698 default:
699 break;
700 }
701 if (maskThisEntry) {
702 p->append(e->name.rawContent(), e->name.length());
703 p->append(": ** NOT DISPLAYED **\r\n", 23);
704 } else {
705 e->packInto(p);
706 }
707
708 }
709 /* Pack in the "special" entries */
710
711 /* Cache-Control */
712}
713
714/* returns next valid entry */
717{
718 assert(pos);
719 assert(*pos >= HttpHeaderInitPos && *pos < static_cast<ssize_t>(entries.size()));
720
721 for (++(*pos); *pos < static_cast<ssize_t>(entries.size()); ++(*pos)) {
722 if (entries[*pos])
723 return static_cast<HttpHeaderEntry*>(entries[*pos]);
724 }
725
726 return nullptr;
727}
728
729/*
730 * returns a pointer to a specified entry if any
731 * note that we return one entry so it does not make much sense to ask for
732 * "list" headers
733 */
736{
737 assert(any_registered_header(id));
738 assert(!Http::HeaderLookupTable.lookup(id).list);
739
740 /* check mask first */
741
742 if (!CBIT_TEST(mask, id))
743 return nullptr;
744
745 /* looks like we must have it, do linear search */
746 for (auto e : entries) {
747 if (e && e->id == id)
748 return e;
749 }
750
751 /* hm.. we thought it was there, but it was not found */
752 assert(false);
753 return nullptr; /* not reached */
754}
755
756/*
757 * same as httpHeaderFindEntry
758 */
761{
762 assert(any_registered_header(id));
763 assert(!Http::HeaderLookupTable.lookup(id).list);
764
765 /* check mask first */
766 if (!CBIT_TEST(mask, id))
767 return nullptr;
768
769 for (auto e = entries.rbegin(); e != entries.rend(); ++e) {
770 if (*e && (*e)->id == id)
771 return *e;
772 }
773
774 /* hm.. we thought it was there, but it was not found */
775 assert(false);
776 return nullptr; /* not reached */
777}
778
779int
781{
782 int count = 0;
784 httpHeaderMaskInit(&mask, 0); /* temporal inconsistency */
785 debugs(55, 9, "deleting '" << name << "' fields in hdr " << this);
786
787 while (const HttpHeaderEntry *e = getEntry(&pos)) {
788 if (!e->name.caseCmp(name))
789 delAt(pos, count);
790 else
791 CBIT_SET(mask, e->id);
792 }
793
794 return count;
795}
796
797/* deletes all entries with a given id, returns the #entries deleted */
798int
800{
801 debugs(55, 8, this << " del-by-id " << id);
802 assert(any_registered_header(id));
803
804 if (!CBIT_TEST(mask, id))
805 return 0;
806
807 int count = 0;
808
810 while (HttpHeaderEntry *e = getEntry(&pos)) {
811 if (e->id == id)
812 delAt(pos, count); // deletes e
813 }
814
815 CBIT_CLR(mask, id);
816 assert(count);
817 return count;
818}
819
820/*
821 * deletes an entry at pos and leaves a gap; leaving a gap makes it
822 * possible to iterate(search) and delete fields at the same time
823 * NOTE: Does not update the header mask. Caller must follow up with
824 * a call to refreshMask() if headers_deleted was incremented.
825 */
826void
827HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
828{
830 assert(pos >= HttpHeaderInitPos && pos < static_cast<ssize_t>(entries.size()));
831 e = static_cast<HttpHeaderEntry*>(entries[pos]);
832 entries[pos] = nullptr;
833 /* decrement header length, allow for ": " and crlf */
834 len -= e->name.length() + 2 + e->value.size() + 2;
835 assert(len >= 0);
836 delete e;
837 ++headers_deleted;
838}
839
840/*
841 * Compacts the header storage
842 */
843void
845{
846 // TODO: optimize removal, or possibly make it so that's not needed.
847 entries.erase( std::remove(entries.begin(), entries.end(), nullptr),
848 entries.end());
849}
850
851/*
852 * Refreshes the header mask. Required after delAt() calls.
853 */
854void
856{
858 debugs(55, 7, "refreshing the mask in hdr " << this);
859 for (auto e : entries) {
860 if (e)
861 CBIT_SET(mask, e->id);
862 }
863}
864
865/* appends an entry;
866 * does not call e->clone() so one should not reuse "*e"
867 */
868void
870{
871 assert(e);
872 assert(any_HdrType_enum_value(e->id));
873 assert(e->name.length());
874
875 debugs(55, 7, this << " adding entry: " << e->id << " at " << entries.size());
876
877 if (e->id != Http::HdrType::BAD_HDR) {
878 if (CBIT_TEST(mask, e->id)) {
879 ++ headerStatsTable[e->id].repCount;
880 } else {
881 CBIT_SET(mask, e->id);
882 }
883 }
884
885 entries.push_back(e);
886
887 len += e->length();
888}
889
890bool
892{
893 debugs(55, 9, this << " joining for id " << id);
894 /* only fields from ListHeaders array can be "listed" */
895 assert(Http::HeaderLookupTable.lookup(id).list);
896
897 if (!CBIT_TEST(mask, id))
898 return false;
899
900 for (auto e: entries) {
901 if (e && e->id == id)
902 strListAdd(s, e->value.termedBuf(), ',');
903 }
904
905 /*
906 * note: we might get an empty (size==0) string if there was an "empty"
907 * header. This results in an empty length String, which may have a NULL
908 * buffer.
909 */
910 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
911 if (!s->size())
912 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")");
913 else
914 debugs(55, 6, this << ": joined for id " << id << ": " << s);
915
916 return true;
917}
918
919/* return a list of entries with the same id separated by ',' and ws */
920String
922{
925 debugs(55, 9, this << "joining for id " << id);
926 /* only fields from ListHeaders array can be "listed" */
927 assert(Http::HeaderLookupTable.lookup(id).list);
928
929 if (!CBIT_TEST(mask, id))
930 return String();
931
932 String s;
933
934 while ((e = getEntry(&pos))) {
935 if (e->id == id)
936 strListAdd(&s, e->value.termedBuf(), ',');
937 }
938
939 /*
940 * note: we might get an empty (size==0) string if there was an "empty"
941 * header. This results in an empty length String, which may have a NULL
942 * buffer.
943 */
944 /* temporary warning: remove it? (Is it useful for diagnostics ?) */
945 if (!s.size())
946 debugs(55, 3, "empty list header: " << Http::HeaderLookupTable.lookup(id).name << "(" << id << ")");
947 else
948 debugs(55, 6, this << ": joined for id " << id << ": " << s);
949
950 return s;
951}
952
953/* return a string or list of entries with the same id separated by ',' and ws */
954String
956{
958
959 if (Http::HeaderLookupTable.lookup(id).list)
960 return getList(id);
961
962 if ((e = findEntry(id)))
963 return e->value;
964
965 return String();
966}
967
968/*
969 * Returns the value of the specified header and/or an undefined String.
970 */
971String
972HttpHeader::getByName(const char *name) const
973{
974 String result;
975 // ignore presence: return undefined string if an empty header is present
976 (void)hasNamed(name, strlen(name), &result);
977 return result;
978}
979
980String
981HttpHeader::getByName(const SBuf &name) const
982{
983 String result;
984 // ignore presence: return undefined string if an empty header is present
985 (void)hasNamed(name, &result);
986 return result;
987}
988
989String
991{
992 String result;
993 (void)getByIdIfPresent(id, &result);
994 return result;
995}
996
997bool
998HttpHeader::hasNamed(const SBuf &s, String *result) const
999{
1000 return hasNamed(s.rawContent(), s.length(), result);
1001}
1002
1003bool
1005{
1006 if (id == Http::HdrType::BAD_HDR)
1007 return false;
1008 if (!has(id))
1009 return false;
1010 if (result)
1011 *result = getStrOrList(id);
1012 return true;
1013}
1014
1015bool
1016HttpHeader::hasNamed(const char *name, unsigned int namelen, String *result) const
1017{
1018 Http::HdrType id;
1020 HttpHeaderEntry *e;
1021
1022 assert(name);
1023
1024 /* First try the quick path */
1025 id = Http::HeaderLookupTable.lookup(name,namelen).id;
1026
1027 if (id != Http::HdrType::BAD_HDR) {
1028 if (getByIdIfPresent(id, result))
1029 return true;
1030 }
1031
1032 /* Sorry, an unknown header name. Do linear search */
1033 bool found = false;
1034 while ((e = getEntry(&pos))) {
1035 if (e->id == Http::HdrType::OTHER && e->name.length() == namelen && e->name.caseCmp(name, namelen) == 0) {
1036 found = true;
1037 if (!result)
1038 break;
1039 strListAdd(result, e->value.termedBuf(), ',');
1040 }
1041 }
1042
1043 return found;
1044}
1045
1046/*
1047 * Returns a the value of the specified list member, if any.
1048 */
1049SBuf
1050HttpHeader::getByNameListMember(const char *name, const char *member, const char separator) const
1051{
1052 assert(name);
1053 const auto header = getByName(name);
1054 return ::getListMember(header, member, separator);
1055}
1056
1057/*
1058 * returns a the value of the specified list member, if any.
1059 */
1060SBuf
1061HttpHeader::getListMember(Http::HdrType id, const char *member, const char separator) const
1062{
1063 assert(any_registered_header(id));
1064 const auto header = getStrOrList(id);
1065 return ::getListMember(header, member, separator);
1066}
1067
1068/* test if a field is present */
1069int
1071{
1072 assert(any_registered_header(id));
1073 debugs(55, 9, this << " lookup for " << id);
1074 return CBIT_TEST(mask, id);
1075}
1076
1077void
1079{
1080 // TODO: do not add Via header for messages where Squid itself
1081 // generated the message (i.e., Downloader) there should be no Via header added at all.
1082
1083 if (Config.onoff.via) {
1084 SBuf buf;
1085 // RFC 7230 section 5.7.1.: protocol-name is omitted when
1086 // the received protocol is HTTP.
1089 buf.appendf("%s/", AnyP::ProtocolType_str[ver.protocol]);
1090 buf.appendf("%d.%d %s", ver.major, ver.minor, ThisCache);
1091 const HttpHeader *hdr = from ? from : this;
1093 if (!strVia.isEmpty())
1094 strVia.append(", ", 2);
1095 strVia.append(buf);
1097 }
1098}
1099
1100void
1102{
1103 assert(any_registered_header(id));
1104 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */
1105 assert(number >= 0);
1107}
1108
1109void
1111{
1112 assert(any_registered_header(id));
1113 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */
1114 assert(number >= 0);
1116}
1117
1118void
1120{
1121 assert(any_registered_header(id));
1122 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */
1123 assert(htime >= 0);
1125}
1126
1127void
1129{
1130 assert(any_registered_header(id));
1131 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
1132 assert(str);
1133 addEntry(new HttpHeaderEntry(id, SBuf(), str));
1134}
1135
1136void
1137HttpHeader::putAuth(const char *auth_scheme, const char *realm)
1138{
1139 assert(auth_scheme && realm);
1140 httpHeaderPutStrf(this, Http::HdrType::WWW_AUTHENTICATE, "%s realm=\"%s\"", auth_scheme, realm);
1141}
1142
1143void
1145{
1146 /* remove old directives if any */
1148 /* pack into mb */
1149 MemBuf mb;
1150 mb.init();
1151 cc.packInto(&mb);
1152 /* put */
1154 /* cleanup */
1155 mb.clean();
1156}
1157
1158void
1160{
1161 assert(cr);
1162 /* remove old directives if any */
1164 /* pack into mb */
1165 MemBuf mb;
1166 mb.init();
1167 httpHdrContRangePackInto(cr, &mb);
1168 /* put */
1170 /* cleanup */
1171 mb.clean();
1172}
1173
1174void
1176{
1177 assert(range);
1178 /* remove old directives if any */
1180 /* pack into mb */
1181 MemBuf mb;
1182 mb.init();
1183 range->packInto(&mb);
1184 /* put */
1186 /* cleanup */
1187 mb.clean();
1188}
1189
1190void
1192{
1193 assert(sc);
1194 /* remove old directives if any */
1196 /* pack into mb */
1197 MemBuf mb;
1198 mb.init();
1199 sc->packInto(&mb);
1200 /* put */
1202 /* cleanup */
1203 mb.clean();
1204}
1205
1206/* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1207void
1208HttpHeader::putExt(const char *name, const char *value)
1209{
1210 assert(name && value);
1211 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
1213}
1214
1215void
1217{
1218 assert(any_registered_header(id));
1220
1221 // XXX: HttpHeaderEntry::value suffers from String size limits
1222 Assure(newValue.length() < String::SizeMaxXXX());
1223
1224 if (!CBIT_TEST(mask, id)) {
1225 auto newValueCopy = newValue; // until HttpHeaderEntry::value becomes SBuf
1226 addEntry(new HttpHeaderEntry(id, SBuf(), newValueCopy.c_str()));
1227 return;
1228 }
1229
1230 auto foundSameName = false;
1231 for (auto &e: entries) {
1232 if (!e || e->id != id)
1233 continue;
1234
1235 if (foundSameName) {
1236 // get rid of this repeated same-name entry
1237 delete e;
1238 e = nullptr;
1239 continue;
1240 }
1241
1242 if (newValue.cmp(e->value.termedBuf()) != 0)
1243 e->value.assign(newValue.rawContent(), newValue.plength());
1244
1245 foundSameName = true;
1246 // continue to delete any repeated same-name entries
1247 }
1248 assert(foundSameName);
1249 debugs(55, 5, "synced: " << Http::HeaderLookupTable.lookup(id).name << ": " << newValue);
1250}
1251
1252int
1254{
1255 assert(any_registered_header(id));
1256 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */
1257 HttpHeaderEntry *e;
1258
1259 if ((e = findEntry(id)))
1260 return e->getInt();
1261
1262 return -1;
1263}
1264
1265int64_t
1267{
1268 assert(any_registered_header(id));
1269 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */
1270 HttpHeaderEntry *e;
1271
1272 if ((e = findEntry(id)))
1273 return e->getInt64();
1274
1275 return -1;
1276}
1277
1278time_t
1280{
1281 HttpHeaderEntry *e;
1282 time_t value = -1;
1283 assert(any_registered_header(id));
1284 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */
1285
1286 if ((e = findEntry(id))) {
1287 value = Time::ParseRfc1123(e->value.termedBuf());
1288 httpHeaderNoteParsedEntry(e->id, e->value, value < 0);
1289 }
1290
1291 return value;
1292}
1293
1294/* sync with httpHeaderGetLastStr */
1295const char *
1297{
1298 HttpHeaderEntry *e;
1299 assert(any_registered_header(id));
1300 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
1301
1302 if ((e = findEntry(id))) {
1303 httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */
1304 return e->value.termedBuf();
1305 }
1306
1307 return nullptr;
1308}
1309
1310/* unusual */
1311const char *
1313{
1314 HttpHeaderEntry *e;
1315 assert(any_registered_header(id));
1316 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftStr); /* must be of an appropriate type */
1317
1318 if ((e = findLastEntry(id))) {
1319 httpHeaderNoteParsedEntry(e->id, e->value, false); /* no errors are possible */
1320 return e->value.termedBuf();
1321 }
1322
1323 return nullptr;
1324}
1325
1326HttpHdrCc *
1328{
1330 return nullptr;
1331
1332 String s;
1334
1335 HttpHdrCc *cc=new HttpHdrCc();
1336
1337 if (!cc->parse(s)) {
1338 delete cc;
1339 cc = nullptr;
1340 }
1341
1342 ++ HttpHeaderStats[owner].ccParsedCount;
1343
1344 if (cc)
1345 httpHdrCcUpdateStats(cc, &HttpHeaderStats[owner].ccTypeDistr);
1346
1348
1349 return cc;
1350}
1351
1354{
1355 HttpHdrRange *r = nullptr;
1356 HttpHeaderEntry *e;
1357 /* some clients will send "Request-Range" _and_ *matching* "Range"
1358 * who knows, some clients might send Request-Range only;
1359 * this "if" should work correctly in both cases;
1360 * hopefully no clients send mismatched headers! */
1361
1362 if ((e = findEntry(Http::HdrType::RANGE)) ||
1366 }
1367
1368 return r;
1369}
1370
1371HttpHdrSc *
1373{
1375 return nullptr;
1376
1377 String s;
1378
1380
1382
1383 ++ HttpHeaderStats[owner].ccParsedCount;
1384
1385 if (sc)
1386 sc->updateStats(&HttpHeaderStats[owner].scTypeDistr);
1387
1389
1390 return sc;
1391}
1392
1395{
1396 HttpHdrContRange *cr = nullptr;
1397 HttpHeaderEntry *e;
1398
1401 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
1402 }
1403
1404 return cr;
1405}
1406
1407SBuf
1408HttpHeader::getAuthToken(Http::HdrType id, const char *auth_scheme) const
1409{
1410 const char *field;
1411 int l;
1412 assert(auth_scheme);
1413 field = getStr(id);
1414
1415 static const SBuf nil;
1416 if (!field) /* no authorization field */
1417 return nil;
1418
1419 l = strlen(auth_scheme);
1420
1421 if (!l || strncasecmp(field, auth_scheme, l)) /* wrong scheme */
1422 return nil;
1423
1424 field += l;
1425
1426 if (!xisspace(*field)) /* wrong scheme */
1427 return nil;
1428
1429 /* skip white space */
1430 for (; field && xisspace(*field); ++field);
1431
1432 if (!*field) /* no authorization cookie */
1433 return nil;
1434
1435 const auto fieldLen = strlen(field);
1436 SBuf result;
1437 char *decodedAuthToken = result.rawAppendStart(BASE64_DECODE_LENGTH(fieldLen));
1438 struct base64_decode_ctx ctx;
1439 base64_decode_init(&ctx);
1440 size_t decodedLen = 0;
1441 if (!base64_decode_update(&ctx, &decodedLen, reinterpret_cast<uint8_t*>(decodedAuthToken), fieldLen, field) ||
1442 !base64_decode_final(&ctx)) {
1443 return nil;
1444 }
1445 result.rawAppendFinish(decodedAuthToken, decodedLen);
1446 return result;
1447}
1448
1449ETag
1451{
1452 ETag etag = {nullptr, -1};
1453 HttpHeaderEntry *e;
1454 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftETag); /* must be of an appropriate type */
1455
1456 if ((e = findEntry(id)))
1457 etagParseInit(&etag, e->value.termedBuf());
1458
1459 return etag;
1460}
1461
1464{
1465 TimeOrTag tot;
1466 HttpHeaderEntry *e;
1467 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123_or_ETag); /* must be of an appropriate type */
1468 memset(&tot, 0, sizeof(tot));
1469
1470 if ((e = findEntry(id))) {
1471 const char *str = e->value.termedBuf();
1472 /* try as an ETag */
1473
1474 if (etagParseInit(&tot.tag, str)) {
1475 tot.valid = tot.tag.str != nullptr;
1476 tot.time = -1;
1477 } else {
1478 /* or maybe it is time? */
1479 tot.time = Time::ParseRfc1123(str);
1480 tot.valid = tot.time >= 0;
1481 tot.tag.str = nullptr;
1482 }
1483 }
1484
1485 assert(tot.time < 0 || !tot.tag.str); /* paranoid */
1486 return tot;
1487}
1488
1489/*
1490 * HttpHeaderEntry
1491 */
1492
1493HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId, const SBuf &aName, const char *aValue)
1494{
1495 assert(any_HdrType_enum_value(anId));
1496 id = anId;
1497
1498 if (id != Http::HdrType::OTHER)
1500 else
1501 name = aName;
1502
1503 value = aValue;
1504
1505 if (id != Http::HdrType::BAD_HDR)
1506 ++ headerStatsTable[id].aliveCount;
1507
1508 debugs(55, 9, "created HttpHeaderEntry " << this << ": '" << name << " : " << value );
1509}
1510
1512{
1513 debugs(55, 9, "destroying entry " << this << ": '" << name << ": " << value << "'");
1514
1515 if (id != Http::HdrType::BAD_HDR) {
1516 assert(headerStatsTable[id].aliveCount);
1517 -- headerStatsTable[id].aliveCount;
1518 id = Http::HdrType::BAD_HDR; // it already is BAD_HDR, no sense in resetting it
1519 }
1520
1521}
1522
1523/* parses and inits header entry, returns true/false */
1525HttpHeaderEntry::parse(const char *field_start, const char *field_end, const http_hdr_owner_type msgType)
1526{
1527 /* note: name_start == field_start */
1528 const char *name_end = (const char *)memchr(field_start, ':', field_end - field_start);
1529 int name_len = name_end ? name_end - field_start :0;
1530 const char *value_start = field_start + name_len + 1; /* skip ':' */
1531 /* note: value_end == field_end */
1532
1534
1535 /* do we have a valid field name within this field? */
1536
1537 if (!name_len || name_end > field_end)
1538 return nullptr;
1539
1540 if (name_len > 65534) {
1541 /* String must be LESS THAN 64K and it adds a terminating NULL */
1542 // TODO: update this to show proper name_len in Raw markup, but not print all that
1543 debugs(55, 2, "ignoring huge header field (" << Raw("field_start", field_start, 100) << "...)");
1544 return nullptr;
1545 }
1546
1547 /*
1548 * RFC 7230 section 3.2.4:
1549 * "No whitespace is allowed between the header field-name and colon.
1550 * ...
1551 * A server MUST reject any received request message that contains
1552 * whitespace between a header field-name and colon with a response code
1553 * of 400 (Bad Request). A proxy MUST remove any such whitespace from a
1554 * response message before forwarding the message downstream."
1555 */
1556 if (xisspace(field_start[name_len - 1])) {
1557
1558 if (msgType == hoRequest)
1559 return nullptr;
1560
1561 // for now, also let relaxed parser remove this BWS from any non-HTTP messages
1562 const bool stripWhitespace = (msgType == hoReply) ||
1564 if (!stripWhitespace)
1565 return nullptr; // reject if we cannot strip
1566
1567 debugs(55, Config.onoff.relaxed_header_parser <= 0 ? 1 : 2,
1568 "WARNING: Whitespace after header name in '" << getStringPrefix(field_start, field_end-field_start) << "'");
1569
1570 while (name_len > 0 && xisspace(field_start[name_len - 1]))
1571 --name_len;
1572
1573 if (!name_len) {
1574 debugs(55, 2, "found header with only whitespace for name");
1575 return nullptr;
1576 }
1577 }
1578
1579 /* RFC 7230 section 3.2:
1580 *
1581 * header-field = field-name ":" OWS field-value OWS
1582 * field-name = token
1583 * token = 1*TCHAR
1584 */
1585 for (const char *pos = field_start; pos < (field_start+name_len); ++pos) {
1586 if (!CharacterSet::TCHAR[*pos]) {
1587 debugs(55, 2, "found header with invalid characters in " <<
1588 Raw("field-name", field_start, min(name_len,100)) << "...");
1589 return nullptr;
1590 }
1591 }
1592
1593 /* now we know we can parse it */
1594
1595 debugs(55, 9, "parsing HttpHeaderEntry: near '" << getStringPrefix(field_start, field_end-field_start) << "'");
1596
1597 /* is it a "known" field? */
1598 Http::HdrType id = Http::HeaderLookupTable.lookup(field_start,name_len).id;
1599 debugs(55, 9, "got hdr-id=" << id);
1600
1601 SBuf theName;
1602
1603 String value;
1604
1605 if (id == Http::HdrType::BAD_HDR)
1607
1608 /* set field name */
1609 if (id == Http::HdrType::OTHER)
1610 theName.append(field_start, name_len);
1611 else
1612 theName = Http::HeaderLookupTable.lookup(id).name;
1613
1614 /* trim field value */
1615 while (value_start < field_end && xisspace(*value_start))
1616 ++value_start;
1617
1618 while (value_start < field_end && xisspace(field_end[-1]))
1619 --field_end;
1620
1621 if (field_end - value_start > 65534) {
1622 /* String must be LESS THAN 64K and it adds a terminating NULL */
1623 debugs(55, 2, "WARNING: found '" << theName << "' header of " << (field_end - value_start) << " bytes");
1624 return nullptr;
1625 }
1626
1627 /* set field value */
1628 value.assign(value_start, field_end - value_start);
1629
1630 if (id != Http::HdrType::BAD_HDR)
1631 ++ headerStatsTable[id].seenCount;
1632
1633 debugs(55, 9, "parsed HttpHeaderEntry: '" << theName << ": " << value << "'");
1634
1635 return new HttpHeaderEntry(id, theName, value.termedBuf());
1636}
1637
1640{
1641 return new HttpHeaderEntry(id, name, value.termedBuf());
1642}
1643
1644void
1646{
1647 assert(p);
1648 p->append(name.rawContent(), name.length());
1649 p->append(": ", 2);
1650 p->append(value.rawBuf(), value.size());
1651 p->append("\r\n", 2);
1652}
1653
1654int
1656{
1657 int val = -1;
1658 int ok = httpHeaderParseInt(value.termedBuf(), &val);
1659 httpHeaderNoteParsedEntry(id, value, ok == 0);
1660 /* XXX: Should we check ok - ie
1661 * return ok ? -1 : value;
1662 */
1663 return val;
1664}
1665
1666int64_t
1668{
1669 int64_t val = -1;
1670 const bool ok = httpHeaderParseOffset(value.termedBuf(), &val);
1672 return val; // remains -1 if !ok (XXX: bad method API)
1673}
1674
1675static void
1677{
1678 if (id != Http::HdrType::BAD_HDR)
1679 ++ headerStatsTable[id].parsCount;
1680
1681 if (error) {
1682 if (id != Http::HdrType::BAD_HDR)
1683 ++ headerStatsTable[id].errCount;
1684 debugs(55, 2, "cannot parse hdr field: '" << Http::HeaderLookupTable.lookup(id).name << ": " << context << "'");
1685 }
1686}
1687
1688/*
1689 * Reports
1690 */
1691
1692/* tmp variable used to pass stat info to dumpers */
1693extern const HttpHeaderStat *dump_stat; /* argh! */
1694const HttpHeaderStat *dump_stat = nullptr;
1695
1696static void
1697httpHeaderFieldStatDumper(StoreEntry * sentry, int, double val, double, int count)
1698{
1699 const int id = static_cast<int>(val);
1700 const bool valid_id = Http::any_valid_header(static_cast<Http::HdrType>(id));
1701 const char *name = valid_id ? Http::HeaderLookupTable.lookup(static_cast<Http::HdrType>(id)).name : "INVALID";
1702 int visible = count > 0;
1703 /* for entries with zero count, list only those that belong to current type of message */
1704
1705 if (!visible && valid_id && dump_stat->owner_mask)
1706 visible = CBIT_TEST(*dump_stat->owner_mask, id);
1707
1708 if (visible)
1709 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
1710 id, name, count, xdiv(count, dump_stat->busyDestroyedCount));
1711}
1712
1713static void
1714httpHeaderFldsPerHdrDumper(StoreEntry * sentry, int idx, double val, double, int count)
1715{
1716 if (count)
1717 storeAppendPrintf(sentry, "%2d\t %5d\t %5d\t %6.2f\n",
1718 idx, (int) val, count,
1720}
1721
1722static void
1724{
1725 assert(hs);
1726 assert(e);
1727
1728 if (!hs->owner_mask)
1729 return; // these HttpHeaderStat objects were not meant to be dumped here
1730
1731 dump_stat = hs;
1732 storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label);
1733 storeAppendPrintf(e, "\nField type distribution\n");
1734 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1735 "id", "name", "count", "#/header");
1737 storeAppendPrintf(e, "\nCache-control directives distribution\n");
1738 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1739 "id", "name", "count", "#/cc_field");
1741 storeAppendPrintf(e, "\nSurrogate-control directives distribution\n");
1742 storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n",
1743 "id", "name", "count", "#/sc_field");
1745 storeAppendPrintf(e, "\nNumber of fields per header distribution\n");
1746 storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n",
1747 "id", "#flds", "count", "%total");
1749 storeAppendPrintf(e, "\n");
1750 dump_stat = nullptr;
1751}
1752
1753void
1755{
1756 assert(e);
1757
1758 HttpHeaderStats[0].parsedCount =
1759 HttpHeaderStats[hoRequest].parsedCount + HttpHeaderStats[hoReply].parsedCount;
1760 HttpHeaderStats[0].ccParsedCount =
1761 HttpHeaderStats[hoRequest].ccParsedCount + HttpHeaderStats[hoReply].ccParsedCount;
1762 HttpHeaderStats[0].destroyedCount =
1763 HttpHeaderStats[hoRequest].destroyedCount + HttpHeaderStats[hoReply].destroyedCount;
1764 HttpHeaderStats[0].busyDestroyedCount =
1765 HttpHeaderStats[hoRequest].busyDestroyedCount + HttpHeaderStats[hoReply].busyDestroyedCount;
1766
1767 for (const auto &stats: HttpHeaderStats)
1768 httpHeaderStatDump(&stats, e);
1769
1770 /* field stats for all messages */
1771 storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n");
1772
1773 storeAppendPrintf(e, "%2s\t %-25s\t %5s\t %6s\t %6s\n",
1774 "id", "name", "#alive", "%err", "%repeat");
1775
1776 // scan heaaderTable and output
1777 for (auto h : WholeEnum<Http::HdrType>()) {
1778 auto stats = headerStatsTable[h];
1779 storeAppendPrintf(e, "%2d\t %-25s\t %5d\t %6.3f\t %6.3f\n",
1780 Http::HeaderLookupTable.lookup(h).id,
1781 Http::HeaderLookupTable.lookup(h).name,
1782 stats.aliveCount,
1783 xpercent(stats.errCount, stats.parsCount),
1784 xpercent(stats.repCount, stats.seenCount));
1785 }
1786
1787 storeAppendPrintf(e, "Headers Parsed: %d + %d = %d\n",
1788 HttpHeaderStats[hoRequest].parsedCount,
1789 HttpHeaderStats[hoReply].parsedCount,
1790 HttpHeaderStats[0].parsedCount);
1791 storeAppendPrintf(e, "Hdr Fields Parsed: %d\n", HeaderEntryParsedCount);
1792}
1793
1794int
1795HttpHeader::hasListMember(Http::HdrType id, const char *member, const char separator) const
1796{
1797 int result = 0;
1798 const char *pos = nullptr;
1799 const char *item;
1800 int ilen;
1801 int mlen = strlen(member);
1802
1803 assert(any_registered_header(id));
1804
1805 String header (getStrOrList(id));
1806
1807 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1808 if (strncasecmp(item, member, mlen) == 0
1809 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1810 result = 1;
1811 break;
1812 }
1813 }
1814
1815 return result;
1816}
1817
1818int
1819HttpHeader::hasByNameListMember(const char *name, const char *member, const char separator) const
1820{
1821 int result = 0;
1822 const char *pos = nullptr;
1823 const char *item;
1824 int ilen;
1825 int mlen = strlen(member);
1826
1827 assert(name);
1828
1829 String header (getByName(name));
1830
1831 while (strListGetItem(&header, separator, &item, &ilen, &pos)) {
1832 if (strncasecmp(item, member, mlen) == 0
1833 && (item[mlen] == '=' || item[mlen] == separator || item[mlen] == ';' || item[mlen] == '\0')) {
1834 result = 1;
1835 break;
1836 }
1837 }
1838
1839 return result;
1840}
1841
1842void
1844{
1846
1847 const HttpHeaderEntry *e;
1849 int headers_deleted = 0;
1850 while ((e = getEntry(&pos))) {
1851 Http::HdrType id = e->id;
1852 if (Http::HeaderLookupTable.lookup(id).hopbyhop) {
1853 delAt(pos, headers_deleted);
1854 CBIT_CLR(mask, id);
1855 }
1856 }
1857}
1858
1859void
1861{
1863 /* anything that matches Connection list member will be deleted */
1864 String strConnection;
1865
1866 (void) getList(Http::HdrType::CONNECTION, &strConnection);
1867 const HttpHeaderEntry *e;
1869 /*
1870 * think: on-average-best nesting of the two loops (hdrEntry
1871 * and strListItem) @?@
1872 */
1873 /*
1874 * maybe we should delete standard stuff ("keep-alive","close")
1875 * from strConnection first?
1876 */
1877
1878 int headers_deleted = 0;
1879 while ((e = getEntry(&pos))) {
1880 if (strListIsMember(&strConnection, e->name, ','))
1881 delAt(pos, headers_deleted);
1882 }
1883 if (headers_deleted)
1884 refreshMask();
1885 }
1886}
1887
#define Assure(condition)
Definition Assure.h:35
int etagParseInit(ETag *etag, const char *str)
Definition ETag.cc:29
#define Here()
source code location of the caller
Definition Here.h:15
void httpHdrCcUpdateStats(const HttpHdrCc *cc, StatHist *hist)
Definition HttpHdrCc.cc:342
void httpHdrCcStatDumper(StoreEntry *sentry, int, double val, double, int count)
Definition HttpHdrCc.cc:352
void httpHdrContRangePackInto(const HttpHdrContRange *range, Packable *p)
HttpHdrContRange * httpHdrContRangeParseCreate(const char *str)
void httpHdrScStatDumper(StoreEntry *sentry, int, double val, double, int count)
Definition HttpHdrSc.cc:266
HttpHdrSc * httpHdrScParseCreate(const String &str)
Definition HttpHdrSc.cc:59
void httpHdrScInitModule(void)
Definition HttpHdrSc.cc:48
char HttpHeaderMask[12]
bool httpHeaderParseOffset(const char *start, int64_t *value, char **endPtr)
int httpHeaderParseInt(const char *start, int *value)
void httpHeaderPutStrf(HttpHeader *hdr, Http::HdrType id, const char *fmt,...)
static std::array< HttpHeaderStat, hoEnd > HttpHeaderStats
Definition HttpHeader.cc:80
static HttpHeaderMask RequestHeadersMask
Definition HttpHeader.cc:73
SBuf httpHeaderQuoteString(const char *raw)
quotes string using RFC 7230 quoted-string rules
static int HeaderEntryParsedCount
Definition HttpHeader.cc:94
static void httpHeaderStoreReport(StoreEntry *e)
static const char * getStringPrefix(const char *str, size_t sz)
#define SHORT_PREFIX_SIZE
const HttpHeaderStat * dump_stat
static void httpHeaderFieldStatDumper(StoreEntry *sentry, int, double val, double, int count)
static void httpHeaderFldsPerHdrDumper(StoreEntry *sentry, int idx, double val, double, int count)
static void httpHeaderRegisterWithCacheManager(void)
static void httpHeaderStatDump(const HttpHeaderStat *hs, StoreEntry *e)
std::vector< HttpHeaderFieldStat > headerStatsTable(Http::HdrType::enumEnd_)
int httpHeaderParseQuotedString(const char *start, const int len, String *val)
static void httpHeaderNoteParsedEntry(Http::HdrType id, String const &value, bool error)
void httpHeaderInitModule(void)
static void httpHeaderMaskInit(HttpHeaderMask *mask, int value)
static HttpHeaderMask ReplyHeadersMask
Definition HttpHeader.cc:76
http_hdr_owner_type
Definition HttpHeader.h:31
@ hoRequest
Definition HttpHeader.h:36
@ hoNone
Definition HttpHeader.h:32
@ hoReply
Definition HttpHeader.h:37
@ hoEnd
Definition HttpHeader.h:41
ssize_t HttpHeaderPos
Definition HttpHeader.h:45
#define HttpHeaderInitPos
Definition HttpHeader.h:48
class SquidConfig Config
int strListGetItem(const String *str, char del, const char **item, int *ilen, const char **pos)
Definition StrList.cc:78
void strListAdd(String &str, const char *item, const size_t itemSize, const char delimiter)
Appends the given item of a given size to a delimiter-separated list in str.
Definition StrList.cc:18
int strListIsMember(const String *list, const SBuf &m, char del)
Definition StrList.cc:46
SBuf StringToSBuf(const String &s)
create a new SBuf from a String by copying contents
void error(char *format,...)
#define assert(EX)
Definition assert.h:17
void base64_decode_init(struct base64_decode_ctx *ctx)
Definition base64.c:54
int base64_decode_update(struct base64_decode_ctx *ctx, size_t *dst_length, uint8_t *dst, size_t src_length, const char *src)
Definition base64.c:129
int base64_decode_final(struct base64_decode_ctx *ctx)
Definition base64.c:159
#define BASE64_DECODE_LENGTH(length)
Definition base64.h:120
unsigned int major
major version number
ProtocolType protocol
which protocol this version is for
unsigned int minor
minor version number
static const CharacterSet TCHAR
Definition ETag.h:18
const char * str
quoted-string
Definition ETag.h:20
bool parse(const String &s)
parse a header-string and fill in appropriate values.
Definition HttpHdrCc.cc:117
void packInto(Packable *p) const
Definition HttpHdrCc.cc:268
void packInto(Packable *p) const
static HttpHdrRange * ParseCreate(const String *range_spec)
void packInto(Packable *p) const
Definition HttpHdrSc.cc:223
void updateStats(StatHist *) const
Definition HttpHdrSc.cc:245
void packInto(Packable *p) const
static HttpHeaderEntry * parse(const char *field_start, const char *field_end, const http_hdr_owner_type msgType)
int getInt() const
HttpHeaderEntry * clone() const
size_t length() const
expected number of bytes written by packInto(), including ": " and CRLF
Definition HttpHeader.h:64
int64_t getInt64() const
HttpHeaderEntry(Http::HdrType id, const SBuf &name, const char *value)
Http::HdrType id
Definition HttpHeader.h:66
HTTP per header statistics.
StatHist scTypeDistr
HttpHeaderMask * owner_mask
const char * label
StatHist fieldTypeDistr
StatHist hdrUCountDistr
StatHist ccTypeDistr
SBuf getByNameListMember(const char *name, const char *member, const char separator) const
void removeHopByHopEntries()
void putStr(Http::HdrType id, const char *str)
TimeOrTag getTimeOrTag(Http::HdrType id) const
HttpHdrCc * getCc() const
bool getByIdIfPresent(Http::HdrType id, String *result) const
int hasByNameListMember(const char *name, const char *member, const char separator) const
void delAt(HttpHeaderPos pos, int &headers_deleted)
HttpHeader(const http_hdr_owner_type owner)
int parse(const char *header_start, size_t len, Http::ContentLengthInterpreter &interpreter)
SBuf getListMember(Http::HdrType id, const char *member, const char separator) const
void putCc(const HttpHdrCc &cc)
String getStrOrList(Http::HdrType id) const
ETag getETag(Http::HdrType id) const
void putInt(Http::HdrType id, int number)
void compact()
http_hdr_owner_type owner
Definition HttpHeader.h:177
int delById(Http::HdrType id)
String getList(Http::HdrType id) const
bool conflictingContentLength_
Definition HttpHeader.h:194
void putContRange(const HttpHdrContRange *cr)
void refreshMask()
void update(const HttpHeader *fresh)
SBuf getAuthToken(Http::HdrType id, const char *auth_scheme) const
HttpHeaderEntry * getEntry(HttpHeaderPos *pos) const
static bool Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end)
const char * getStr(Http::HdrType id) const
std::vector< HttpHeaderEntry *, PoolingAllocator< HttpHeaderEntry * > > entries
Definition HttpHeader.h:175
HttpHeader & operator=(const HttpHeader &other)
void putSc(HttpHdrSc *sc)
bool teUnsupported_
Definition HttpHeader.h:197
bool needUpdate(const HttpHeader *fresh) const
void putRange(const HttpHdrRange *range)
void addEntry(HttpHeaderEntry *e)
HttpHdrContRange * getContRange() const
void putInt64(Http::HdrType id, int64_t number)
void removeConnectionHeaderEntries()
String getByName(const SBuf &name) const
time_t getTime(Http::HdrType id) const
HttpHdrRange * getRange() const
void addVia(const AnyP::ProtocolVersion &ver, const HttpHeader *from=nullptr)
int has(Http::HdrType id) const
int64_t getInt64(Http::HdrType id) const
String getById(Http::HdrType id) const
void clean()
bool hasNamed(const SBuf &s, String *value=nullptr) const
int getInt(Http::HdrType id) const
HttpHeaderEntry * findEntry(Http::HdrType id) const
void putAuth(const char *auth_scheme, const char *realm)
const char * getLastStr(Http::HdrType id) const
void putExt(const char *name, const char *value)
HttpHeaderMask mask
Definition HttpHeader.h:176
void putTime(Http::HdrType id, time_t htime)
void updateOrAddStr(Http::HdrType, const SBuf &)
HttpHdrSc * getSc() const
void packInto(Packable *p, bool mask_sensitive_info=false) const
HttpHeaderEntry * findLastEntry(Http::HdrType id) const
void append(const HttpHeader *src)
bool skipUpdateHeader(const Http::HdrType id) const
int hasListMember(Http::HdrType id, const char *member, const char separator) const
int delByName(const SBuf &name)
bool sawBad
whether a malformed Content-Length value was present
const char * headerWideProblem
worst header-wide problem found (or nil)
const HeaderTableRecord & lookup(const char *buf, const std::size_t len) const
look record type up by name (C-string and length)
void clean()
Definition MemBuf.cc:110
void init(mb_size_t szInit, mb_size_t szMax)
Definition MemBuf.cc:93
char * buf
Definition MemBuf.h:134
virtual void append(const char *buf, int size)=0
Appends a c-string to existing packed data.
Definition Raw.h:21
Definition SBuf.h:94
char * rawAppendStart(size_type anticipatedSize)
Definition SBuf.cc:136
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition SBuf.h:287
const char * rawContent() const
Definition SBuf.cc:509
const char * c_str()
Definition SBuf.cc:516
size_type length() const
Returns the number of bytes stored in SBuf.
Definition SBuf.h:419
SBuf & appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition SBuf.cc:229
int cmp(const SBuf &S, const size_type n) const
shorthand version for compare()
Definition SBuf.h:279
int plength() const
Definition SBuf.h:426
bool isEmpty() const
Definition SBuf.h:435
SBuf & append(const SBuf &S)
Definition SBuf.cc:185
void rawAppendFinish(const char *start, size_type actualSize)
Definition SBuf.cc:144
struct SquidConfig::@90 onoff
int relaxed_header_parser
void dump(StoreEntry *sentry, StatHistBinDumper *bd) const
Definition StatHist.cc:171
static size_type SizeMaxXXX()
Definition SquidString.h:72
void clean()
Definition String.cc:104
void assign(const char *str, int len)
Definition String.cc:79
char const * rawBuf() const
Definition SquidString.h:87
char const * termedBuf() const
Definition SquidString.h:93
void append(char const *buf, int len)
Definition String.cc:131
int caseCmp(char const *) const
Definition String.cc:273
size_type size() const
Definition SquidString.h:74
an std::runtime_error with thrower location info
ETag tag
Definition TimeOrTag.h:20
time_t time
Definition TimeOrTag.h:21
int valid
Definition TimeOrTag.h:22
A const & min(A const &lhs, A const &rhs)
#define DBG_IMPORTANT
Definition Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define DBG_CRITICAL
Definition Stream.h:37
#define CBIT_SET(mask, bit)
Definition defines.h:72
#define CBIT_CLR(mask, bit)
Definition defines.h:73
#define CBIT_TEST(mask, bit)
Definition defines.h:74
char ThisCache[RFC2181_MAXHOSTNAMELEN<< 1]
size_t headersEnd(const char *mime, size_t l, bool &containsObsFold)
const char * ProtocolType_str[]
@ PROTO_NONE
@ PROTO_HTTPS
@ PROTO_UNKNOWN
@ PROTO_HTTP
SBuf SlowlyParseQuotedString(const char *description, const char *start, size_t length)
@ PROXY_AUTHORIZATION
@ TRANSFER_ENCODING
const HeaderLookupTable_t HeaderLookupTable
bool any_valid_header(const Http::HdrType id)
match any valid header type, including OTHER but not BAD
void RegisterAction(char const *action, char const *desc, OBJH *handler, Protected, Atomic, Format)
time_t ParseRfc1123(const char *)
Convert from RFC 1123 style time: "www, DD MMM YYYY hh:mm:ss ZZZ".
Definition rfc1123.cc:159
const char * FormatRfc1123(time_t)
Definition rfc1123.cc:202
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition Stream.h:63
#define LOCAL_ARRAY(type, name, size)
Definition squid.h:62
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition store.cc:855
number
double xpercent(double part, double whole)
Definition util.cc:40
double xdiv(double nom, double denom)
Definition util.cc:53
const char * xitoa(int num)
Definition util.cc:60
const char * xint64toa(int64_t num)
Definition util.cc:69
#define xisspace(x)
Definition xis.h:15
char * xstrncpy(char *dst, const char *src, size_t n)
Definition xstring.cc:37