]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHeader.cc
RFC 9111: Stop treating Warning specially (#1072)
[thirdparty/squid.git] / src / HttpHeader.cc
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"
17 #include "http/ContentLengthInterpreter.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
68 std::vector<HttpHeaderFieldStat> headerStatsTable(Http::HdrType::enumEnd_);
69
70 /* request-only headers. Used for cachemgr */
71 static HttpHeaderMask RequestHeadersMask; /* set run-time using RequestHeaders */
72
73 /* reply-only headers. Used for cachemgr */
74 static HttpHeaderMask ReplyHeadersMask; /* set run-time using ReplyHeaders */
75
76 /* header accounting */
77 // NP: keep in sync with enum http_hdr_owner_type
78 static 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
91 static int HeaderEntryParsedCount = 0;
92
93 /*
94 * forward declarations and local routines
95 */
96
97 class 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
101 static void httpHeaderNoteParsedEntry(Http::HdrType id, String const &value, bool error);
102 static void httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e);
103 /** store report about current header usage and other stats */
104 static void httpHeaderStoreReport(StoreEntry * e);
105
106 /*
107 * Module initialization routines
108 */
109
110 static void
111 httpHeaderRegisterWithCacheManager(void)
112 {
113 Mgr::RegisterAction("http_headers",
114 "HTTP Header Statistics",
115 httpHeaderStoreReport, 0, 1);
116 }
117
118 void
119 httpHeaderInitModule(void)
120 {
121 /* check that we have enough space for masks */
122 assert(8 * sizeof(HttpHeaderMask) >= Http::HdrType::enumEnd_);
123
124 // masks are needed for stats page still
125 for (auto h : WholeEnum<Http::HdrType>()) {
126 if (Http::HeaderLookupTable.lookup(h).request)
127 CBIT_SET(RequestHeadersMask,h);
128 if (Http::HeaderLookupTable.lookup(h).reply)
129 CBIT_SET(ReplyHeadersMask,h);
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 */
136 httpHdrCcInitModule();
137 httpHdrScInitModule();
138
139 httpHeaderRegisterWithCacheManager();
140 }
141
142 /*
143 * HttpHeader Implementation
144 */
145
146 HttpHeader::HttpHeader() : owner (hoNone), len (0), conflictingContentLength_(false)
147 {
148 entries.reserve(32);
149 httpHeaderMaskInit(&mask, 0);
150 }
151
152 HttpHeader::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);
157 httpHeaderMaskInit(&mask, 0);
158 }
159
160 // XXX: Delete as unused, expensive, and violating copy semantics by skipping Warnings
161 HttpHeader::HttpHeader(const HttpHeader &other): owner(other.owner), len(other.len), conflictingContentLength_(false)
162 {
163 entries.reserve(other.entries.capacity());
164 httpHeaderMaskInit(&mask, 0);
165 update(&other); // will update the mask as well
166 }
167
168 HttpHeader::~HttpHeader()
169 {
170 clean();
171 }
172
173 // XXX: Delete as unused, expensive, and violating assignment semantics by skipping Warnings
174 HttpHeader &
175 HttpHeader::operator =(const HttpHeader &other)
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;
183 conflictingContentLength_ = other.conflictingContentLength_;
184 teUnsupported_ = other.teUnsupported_;
185 }
186 return *this;
187 }
188
189 void
190 HttpHeader::clean()
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();
228 httpHeaderMaskInit(&mask, 0);
229 len = 0;
230 conflictingContentLength_ = false;
231 teUnsupported_ = false;
232 }
233
234 /* append entries (also see httpHeaderUpdate) */
235 void
236 HttpHeader::append(const HttpHeader * src)
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
248 bool
249 HttpHeader::needUpdate(HttpHeader const *fresh) const
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
262 bool
263 HttpHeader::skipUpdateHeader(const Http::HdrType id) const
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
271 void
272 HttpHeader::update(HttpHeader const *fresh)
273 {
274 assert(fresh);
275 assert(this != fresh);
276
277 const HttpHeaderEntry *e;
278 HttpHeaderPos pos = HttpHeaderInitPos;
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
305 bool
306 HttpHeader::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
327 int
328 HttpHeader::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'
353 int
354 HttpHeader::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
420 if (Config.onoff.relaxed_header_parser) {
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
476 if (Config.onoff.relaxed_header_parser)
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.
501 if (delById(Http::HdrType::CONTENT_LENGTH))
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.
506 if (delById(Http::HdrType::TRANSFER_ENCODING))
507 debugs(55, 3, "Transfer-Encoding is " << clen.prohibitedAndIgnored());
508
509 } else if (getByIdIfPresent(Http::HdrType::TRANSFER_ENCODING, &rawTe)) {
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
512 delById(Http::HdrType::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
518 delById(Http::HdrType::TRANSFER_ENCODING);
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
527 delById(Http::HdrType::CONTENT_LENGTH);
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
532 delById(Http::HdrType::CONTENT_LENGTH);
533 if (clen.sawGood) {
534 putInt64(Http::HdrType::CONTENT_LENGTH, clen.value);
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 */
543 void
544 HttpHeader::packInto(Packable * p, bool mask_sensitive_info) const
545 {
546 HttpHeaderPos pos = HttpHeaderInitPos;
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) {
560 case Http::HdrType::AUTHORIZATION:
561 case Http::HdrType::PROXY_AUTHORIZATION:
562 maskThisEntry = true;
563 break;
564
565 case Http::HdrType::FTP_ARGUMENTS:
566 if (const HttpHeaderEntry *cmd = findEntry(Http::HdrType::FTP_COMMAND))
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 */
587 HttpHeaderEntry *
588 HttpHeader::getEntry(HttpHeaderPos * pos) const
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 */
606 HttpHeaderEntry *
607 HttpHeader::findEntry(Http::HdrType id) const
608 {
609 assert(any_registered_header(id));
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 */
631 HttpHeaderEntry *
632 HttpHeader::findLastEntry(Http::HdrType id) const
633 {
634 assert(any_registered_header(id));
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
651 int
652 HttpHeader::delByName(const SBuf &name)
653 {
654 int count = 0;
655 HttpHeaderPos pos = HttpHeaderInitPos;
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 */
670 int
671 HttpHeader::delById(Http::HdrType id)
672 {
673 debugs(55, 8, this << " del-by-id " << id);
674 assert(any_registered_header(id));
675
676 if (!CBIT_TEST(mask, id))
677 return 0;
678
679 int count = 0;
680
681 HttpHeaderPos pos = HttpHeaderInitPos;
682 while (HttpHeaderEntry *e = getEntry(&pos)) {
683 if (e->id == id)
684 delAt(pos, count); // deletes e
685 }
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 */
698 void
699 HttpHeader::delAt(HttpHeaderPos pos, int &headers_deleted)
700 {
701 HttpHeaderEntry *e;
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 */
715 void
716 HttpHeader::compact()
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 */
726 void
727 HttpHeader::refreshMask()
728 {
729 httpHeaderMaskInit(&mask, 0);
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 */
740 void
741 HttpHeader::addEntry(HttpHeaderEntry * e)
742 {
743 assert(e);
744 assert(any_HdrType_enum_value(e->id));
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 */
766 void
767 HttpHeader::insertEntry(HttpHeaderEntry * e)
768 {
769 assert(e);
770 assert(any_valid_header(e->id));
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
787 bool
788 HttpHeader::getList(Http::HdrType id, String *s) const
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 */
817 String
818 HttpHeader::getList(Http::HdrType id) const
819 {
820 HttpHeaderEntry *e;
821 HttpHeaderPos pos = HttpHeaderInitPos;
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 */
851 String
852 HttpHeader::getStrOrList(Http::HdrType id) const
853 {
854 HttpHeaderEntry *e;
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 */
868 String
869 HttpHeader::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
877 String
878 HttpHeader::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
886 String
887 HttpHeader::getById(Http::HdrType id) const
888 {
889 String result;
890 (void)getByIdIfPresent(id, &result);
891 return result;
892 }
893
894 bool
895 HttpHeader::hasNamed(const SBuf &s, String *result) const
896 {
897 return hasNamed(s.rawContent(), s.length(), result);
898 }
899
900 bool
901 HttpHeader::getByIdIfPresent(Http::HdrType id, String *result) const
902 {
903 if (id == Http::HdrType::BAD_HDR)
904 return false;
905 if (!has(id))
906 return false;
907 if (result)
908 *result = getStrOrList(id);
909 return true;
910 }
911
912 bool
913 HttpHeader::hasNamed(const char *name, unsigned int namelen, String *result) const
914 {
915 Http::HdrType id;
916 HttpHeaderPos pos = HttpHeaderInitPos;
917 HttpHeaderEntry *e;
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 */
946 SBuf
947 HttpHeader::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 */
957 SBuf
958 HttpHeader::getListMember(Http::HdrType id, const char *member, const char separator) const
959 {
960 assert(any_registered_header(id));
961 const auto header = getStrOrList(id);
962 return ::getListMember(header, member, separator);
963 }
964
965 /* test if a field is present */
966 int
967 HttpHeader::has(Http::HdrType id) const
968 {
969 assert(any_registered_header(id));
970 debugs(55, 9, this << " lookup for " << id);
971 return CBIT_TEST(mask, id);
972 }
973
974 void
975 HttpHeader::addVia(const AnyP::ProtocolVersion &ver, const HttpHeader *from)
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.
984 if (ver.protocol > AnyP::PROTO_NONE && ver.protocol < AnyP::PROTO_UNKNOWN &&
985 ver.protocol != AnyP::PROTO_HTTP && ver.protocol != AnyP::PROTO_HTTPS)
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;
989 SBuf strVia = StringToSBuf(hdr->getList(Http::HdrType::VIA));
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());
995 delById(Http::HdrType::VIA);
996 putStr(Http::HdrType::VIA, strVia.c_str());
997 }
998 }
999
1000 void
1001 HttpHeader::putInt(Http::HdrType id, int number)
1002 {
1003 assert(any_registered_header(id));
1004 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt); /* must be of an appropriate type */
1005 assert(number >= 0);
1006 addEntry(new HttpHeaderEntry(id, SBuf(), xitoa(number)));
1007 }
1008
1009 void
1010 HttpHeader::putInt64(Http::HdrType id, int64_t number)
1011 {
1012 assert(any_registered_header(id));
1013 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftInt64); /* must be of an appropriate type */
1014 assert(number >= 0);
1015 addEntry(new HttpHeaderEntry(id, SBuf(), xint64toa(number)));
1016 }
1017
1018 void
1019 HttpHeader::putTime(Http::HdrType id, time_t htime)
1020 {
1021 assert(any_registered_header(id));
1022 assert(Http::HeaderLookupTable.lookup(id).type == Http::HdrFieldType::ftDate_1123); /* must be of an appropriate type */
1023 assert(htime >= 0);
1024 addEntry(new HttpHeaderEntry(id, SBuf(), Time::FormatRfc1123(htime)));
1025 }
1026
1027 void
1028 HttpHeader::putStr(Http::HdrType id, const char *str)
1029 {
1030 assert(any_registered_header(id));
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
1036 void
1037 HttpHeader::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
1043 void
1044 HttpHeader::putCc(const HttpHdrCc * cc)
1045 {
1046 assert(cc);
1047 /* remove old directives if any */
1048 delById(Http::HdrType::CACHE_CONTROL);
1049 /* pack into mb */
1050 MemBuf mb;
1051 mb.init();
1052 cc->packInto(&mb);
1053 /* put */
1054 addEntry(new HttpHeaderEntry(Http::HdrType::CACHE_CONTROL, SBuf(), mb.buf));
1055 /* cleanup */
1056 mb.clean();
1057 }
1058
1059 void
1060 HttpHeader::putContRange(const HttpHdrContRange * cr)
1061 {
1062 assert(cr);
1063 /* remove old directives if any */
1064 delById(Http::HdrType::CONTENT_RANGE);
1065 /* pack into mb */
1066 MemBuf mb;
1067 mb.init();
1068 httpHdrContRangePackInto(cr, &mb);
1069 /* put */
1070 addEntry(new HttpHeaderEntry(Http::HdrType::CONTENT_RANGE, SBuf(), mb.buf));
1071 /* cleanup */
1072 mb.clean();
1073 }
1074
1075 void
1076 HttpHeader::putRange(const HttpHdrRange * range)
1077 {
1078 assert(range);
1079 /* remove old directives if any */
1080 delById(Http::HdrType::RANGE);
1081 /* pack into mb */
1082 MemBuf mb;
1083 mb.init();
1084 range->packInto(&mb);
1085 /* put */
1086 addEntry(new HttpHeaderEntry(Http::HdrType::RANGE, SBuf(), mb.buf));
1087 /* cleanup */
1088 mb.clean();
1089 }
1090
1091 void
1092 HttpHeader::putSc(HttpHdrSc *sc)
1093 {
1094 assert(sc);
1095 /* remove old directives if any */
1096 delById(Http::HdrType::SURROGATE_CONTROL);
1097 /* pack into mb */
1098 MemBuf mb;
1099 mb.init();
1100 sc->packInto(&mb);
1101 /* put */
1102 addEntry(new HttpHeaderEntry(Http::HdrType::SURROGATE_CONTROL, SBuf(), mb.buf));
1103 /* cleanup */
1104 mb.clean();
1105 }
1106
1107 /* add extension header (these fields are not parsed/analyzed/joined, etc.) */
1108 void
1109 HttpHeader::putExt(const char *name, const char *value)
1110 {
1111 assert(name && value);
1112 debugs(55, 8, this << " adds ext entry " << name << " : " << value);
1113 addEntry(new HttpHeaderEntry(Http::HdrType::OTHER, SBuf(name), value));
1114 }
1115
1116 int
1117 HttpHeader::getInt(Http::HdrType id) const
1118 {
1119 assert(any_registered_header(id));
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
1129 int64_t
1130 HttpHeader::getInt64(Http::HdrType id) const
1131 {
1132 assert(any_registered_header(id));
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
1142 time_t
1143 HttpHeader::getTime(Http::HdrType id) const
1144 {
1145 HttpHeaderEntry *e;
1146 time_t value = -1;
1147 assert(any_registered_header(id));
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 */
1159 const char *
1160 HttpHeader::getStr(Http::HdrType id) const
1161 {
1162 HttpHeaderEntry *e;
1163 assert(any_registered_header(id));
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 */
1175 const char *
1176 HttpHeader::getLastStr(Http::HdrType id) const
1177 {
1178 HttpHeaderEntry *e;
1179 assert(any_registered_header(id));
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
1190 HttpHdrCc *
1191 HttpHeader::getCc() const
1192 {
1193 if (!CBIT_TEST(mask, Http::HdrType::CACHE_CONTROL))
1194 return nullptr;
1195
1196 String s;
1197 getList(Http::HdrType::CACHE_CONTROL, &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
1211 httpHeaderNoteParsedEntry(Http::HdrType::CACHE_CONTROL, s, !cc);
1212
1213 return cc;
1214 }
1215
1216 HttpHdrRange *
1217 HttpHeader::getRange() const
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)) ||
1227 (e = findEntry(Http::HdrType::REQUEST_RANGE))) {
1228 r = HttpHdrRange::ParseCreate(&e->value);
1229 httpHeaderNoteParsedEntry(e->id, e->value, !r);
1230 }
1231
1232 return r;
1233 }
1234
1235 HttpHdrSc *
1236 HttpHeader::getSc() const
1237 {
1238 if (!CBIT_TEST(mask, Http::HdrType::SURROGATE_CONTROL))
1239 return nullptr;
1240
1241 String s;
1242
1243 (void) getList(Http::HdrType::SURROGATE_CONTROL, &s);
1244
1245 HttpHdrSc *sc = httpHdrScParseCreate(s);
1246
1247 ++ HttpHeaderStats[owner].ccParsedCount;
1248
1249 if (sc)
1250 sc->updateStats(&HttpHeaderStats[owner].scTypeDistr);
1251
1252 httpHeaderNoteParsedEntry(Http::HdrType::SURROGATE_CONTROL, s, !sc);
1253
1254 return sc;
1255 }
1256
1257 HttpHdrContRange *
1258 HttpHeader::getContRange() const
1259 {
1260 HttpHdrContRange *cr = nullptr;
1261 HttpHeaderEntry *e;
1262
1263 if ((e = findEntry(Http::HdrType::CONTENT_RANGE))) {
1264 cr = httpHdrContRangeParseCreate(e->value.termedBuf());
1265 httpHeaderNoteParsedEntry(e->id, e->value, !cr);
1266 }
1267
1268 return cr;
1269 }
1270
1271 SBuf
1272 HttpHeader::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
1313 ETag
1314 HttpHeader::getETag(Http::HdrType id) const
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
1326 TimeOrTag
1327 HttpHeader::getTimeOrTag(Http::HdrType id) const
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
1357 HttpHeaderEntry::HttpHeaderEntry(Http::HdrType anId, const SBuf &aName, const char *aValue)
1358 {
1359 assert(any_HdrType_enum_value(anId));
1360 id = anId;
1361
1362 if (id != Http::HdrType::OTHER)
1363 name = Http::HeaderLookupTable.lookup(id).name;
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
1375 HttpHeaderEntry::~HttpHeaderEntry()
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 */
1388 HttpHeaderEntry *
1389 HttpHeaderEntry::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
1397 ++ HeaderEntryParsedCount;
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) ||
1427 Config.onoff.relaxed_header_parser;
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)
1470 id = Http::HdrType::OTHER;
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
1502 HttpHeaderEntry *
1503 HttpHeaderEntry::clone() const
1504 {
1505 return new HttpHeaderEntry(id, name, value.termedBuf());
1506 }
1507
1508 void
1509 HttpHeaderEntry::packInto(Packable * p) const
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
1518 int
1519 HttpHeaderEntry::getInt() const
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
1530 int64_t
1531 HttpHeaderEntry::getInt64() const
1532 {
1533 int64_t val = -1;
1534 const bool ok = httpHeaderParseOffset(value.termedBuf(), &val);
1535 httpHeaderNoteParsedEntry(id, value, !ok);
1536 return val; // remains -1 if !ok (XXX: bad method API)
1537 }
1538
1539 static void
1540 httpHeaderNoteParsedEntry(Http::HdrType id, String const &context, bool error)
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 */
1557 extern const HttpHeaderStat *dump_stat; /* argh! */
1558 const HttpHeaderStat *dump_stat = nullptr;
1559
1560 static void
1561 httpHeaderFieldStatDumper(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
1577 static void
1578 httpHeaderFldsPerHdrDumper(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,
1583 xpercent(count, dump_stat->destroyedCount));
1584 }
1585
1586 static void
1587 httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e)
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");
1600 hs->fieldTypeDistr.dump(e, httpHeaderFieldStatDumper);
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");
1604 hs->ccTypeDistr.dump(e, httpHdrCcStatDumper);
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");
1608 hs->scTypeDistr.dump(e, httpHdrScStatDumper);
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");
1612 hs->hdrUCountDistr.dump(e, httpHeaderFldsPerHdrDumper);
1613 storeAppendPrintf(e, "\n");
1614 dump_stat = nullptr;
1615 }
1616
1617 void
1618 httpHeaderStoreReport(StoreEntry * e)
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)
1632 httpHeaderStatDump(&stats, e);
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
1658 int
1659 HttpHeader::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
1667 assert(any_registered_header(id));
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
1682 int
1683 HttpHeader::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
1706 void
1707 HttpHeader::removeHopByHopEntries()
1708 {
1709 removeConnectionHeaderEntries();
1710
1711 const HttpHeaderEntry *e;
1712 HttpHeaderPos pos = HttpHeaderInitPos;
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
1723 void
1724 HttpHeader::removeConnectionHeaderEntries()
1725 {
1726 if (has(Http::HdrType::CONNECTION)) {
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;
1732 HttpHeaderPos pos = HttpHeaderInitPos;
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