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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors