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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors