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