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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors