Uri.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 23 URL Parsing */
10 
11 #include "squid.h"
12 #include "anyp/Host.h"
13 #include "anyp/Uri.h"
14 #include "base/Raw.h"
15 #include "globals.h"
16 #include "HttpRequest.h"
17 #include "parser/Tokenizer.h"
18 #include "rfc1738.h"
19 #include "SquidConfig.h"
20 #include "SquidMath.h"
21 
22 static const char valid_hostname_chars_u[] =
23  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24  "abcdefghijklmnopqrstuvwxyz"
25  "0123456789-._"
26  "[:]"
27  ;
28 static const char valid_hostname_chars[] =
29  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30  "abcdefghijklmnopqrstuvwxyz"
31  "0123456789-."
32  "[:]"
33  ;
34 
36 static const CharacterSet &
38 {
39  /*
40  * RFC 3986 section 3.2.1
41  *
42  * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
43  * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
44  * pct-encoded = "%" HEXDIG HEXDIG
45  * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
46  */
47  static const auto userInfoValid = CharacterSet("userinfo", ":-._~%!$&'()*+,;=") +
50  return userInfoValid;
51 }
52 
56 SBuf
57 AnyP::Uri::Encode(const SBuf &buf, const CharacterSet &ignore)
58 {
59  if (buf.isEmpty())
60  return buf;
61 
62  Parser::Tokenizer tk(buf);
63  SBuf goodSection;
64  // optimization for the arguably common "no encoding necessary" case
65  if (tk.prefix(goodSection, ignore) && tk.atEnd())
66  return buf;
67 
68  SBuf output;
69  output.reserveSpace(buf.length() * 3); // worst case: encode all chars
70  output.append(goodSection); // may be empty
71 
72  while (!tk.atEnd()) {
73  // TODO: Add Tokenizer::parseOne(void).
74  const auto ch = tk.remaining()[0];
75  output.appendf("%%%02X", static_cast<unsigned int>(static_cast<unsigned char>(ch))); // TODO: Optimize using a table
76  (void)tk.skip(ch);
77 
78  if (tk.prefix(goodSection, ignore))
79  output.append(goodSection);
80  }
81 
82  return output;
83 }
84 
85 SBuf
87 {
88  SBuf output;
90  while (!tok.atEnd()) {
91  SBuf token;
92  static const auto unencodedChars = CharacterSet("percent", "%").complement("unencoded");
93  if (tok.prefix(token, unencodedChars))
94  output.append(token);
95 
96  // we are either at '%' or at end of input
97  if (tok.skip('%')) {
98  int64_t hex1 = 0, hex2 = 0;
99  if (tok.int64(hex1, 16, false, 1) && tok.int64(hex2, 16, false, 1))
100  output.append(static_cast<char>((hex1 << 4) | hex2));
101  else
102  throw TextException("invalid pct-encoded triplet", Here());
103  }
104  }
105  return output;
106 }
107 
108 const SBuf &
110 {
111  static SBuf star("*");
112  return star;
113 }
114 
115 const SBuf &
117 {
118  static SBuf slash("/");
119  return slash;
120 }
121 
122 void
123 AnyP::Uri::host(const char *src)
124 {
125  hostAddr_.fromHost(src);
126  if (hostAddr_.isAnyAddr()) {
127  xstrncpy(host_, src, sizeof(host_));
128  hostIsNumeric_ = false;
129  } else {
130  hostAddr_.toHostStr(host_, sizeof(host_));
131  debugs(23, 3, "given IP: " << hostAddr_);
132  hostIsNumeric_ = 1;
133  }
134  touch();
135 }
136 
137 // TODO: Replace with ToSBuf(parsedHost()) or similar.
138 SBuf
140 {
141  if (hostIsNumeric()) {
142  static char ip[MAX_IPSTRLEN];
143  const auto hostStrLen = hostIP().toHostStr(ip, sizeof(ip));
144  return SBuf(ip, hostStrLen);
145  } else
146  return SBuf(host());
147 }
148 
149 std::optional<AnyP::Host>
151 {
152  if (hostIsNumeric())
153  return Host::ParseIp(hostIP());
154 
155  // XXX: Interpret host subcomponent as reg-name representing a DNS name. It
156  // may actually be, for example, a URN namespace ID (NID; see RFC 8141), but
157  // current Squid APIs do not support adequate representation of those cases.
158  const SBuf regName(host());
159 
160  if (regName.find('%') != SBuf::npos) {
161  debugs(23, 3, "rejecting percent-encoded reg-name: " << regName);
162  return std::nullopt; // TODO: Decode() instead
163  }
164 
165  return Host::ParseSimpleDomainName(regName);
166 }
167 
168 const SBuf &
170 {
171  // RFC 3986 section 3.3 says path can be empty (path-abempty).
172  // RFC 7230 sections 2.7.3, 5.3.1, 5.7.2 - says path cannot be empty, default to "/"
173  // at least when sending and using. We must still accept path-abempty as input.
174  if (path_.isEmpty() && (scheme_ == AnyP::PROTO_HTTP || scheme_ == AnyP::PROTO_HTTPS))
175  return SlashPath();
176 
177  return path_;
178 }
179 
180 void
182 {
183  debugs(23, 5, "urlInitialize: Initializing...");
184  /* this ensures that the number of protocol strings is the same as
185  * the enum slots allocated because the last enum is always 'MAX'.
186  */
187  assert(strcmp(AnyP::ProtocolType_str[AnyP::PROTO_MAX], "MAX") == 0);
188  /*
189  * These test that our matchDomainName() function works the
190  * way we expect it to.
191  */
192  assert(0 == matchDomainName("foo.com", "foo.com"));
193  assert(0 == matchDomainName(".foo.com", "foo.com"));
194  assert(0 == matchDomainName("foo.com", ".foo.com"));
195  assert(0 == matchDomainName(".foo.com", ".foo.com"));
196  assert(0 == matchDomainName("x.foo.com", ".foo.com"));
197  assert(0 == matchDomainName("y.x.foo.com", ".foo.com"));
198  assert(0 != matchDomainName("x.foo.com", "foo.com"));
199  assert(0 != matchDomainName("foo.com", "x.foo.com"));
200  assert(0 != matchDomainName("bar.com", "foo.com"));
201  assert(0 != matchDomainName(".bar.com", "foo.com"));
202  assert(0 != matchDomainName(".bar.com", ".foo.com"));
203  assert(0 != matchDomainName("bar.com", ".foo.com"));
204  assert(0 < matchDomainName("zzz.com", "foo.com"));
205  assert(0 > matchDomainName("aaa.com", "foo.com"));
206  assert(0 == matchDomainName("FOO.com", "foo.COM"));
207  assert(0 < matchDomainName("bfoo.com", "afoo.com"));
208  assert(0 > matchDomainName("afoo.com", "bfoo.com"));
209  assert(0 < matchDomainName("x-foo.com", ".foo.com"));
210 
211  assert(0 == matchDomainName(".foo.com", ".foo.com", mdnRejectSubsubDomains));
212  assert(0 == matchDomainName("x.foo.com", ".foo.com", mdnRejectSubsubDomains));
213  assert(0 != matchDomainName("y.x.foo.com", ".foo.com", mdnRejectSubsubDomains));
214  assert(0 != matchDomainName(".x.foo.com", ".foo.com", mdnRejectSubsubDomains));
215 
216  assert(0 == matchDomainName("*.foo.com", "x.foo.com", mdnHonorWildcards));
217  assert(0 == matchDomainName("*.foo.com", ".x.foo.com", mdnHonorWildcards));
218  assert(0 == matchDomainName("*.foo.com", ".foo.com", mdnHonorWildcards));
219  assert(0 != matchDomainName("*.foo.com", "foo.com", mdnHonorWildcards));
220 
221  assert(0 != matchDomainName("foo.com", ""));
222  assert(0 != matchDomainName("foo.com", "", mdnHonorWildcards));
223  assert(0 != matchDomainName("foo.com", "", mdnRejectSubsubDomains));
224 
225  /* more cases? */
226 }
227 
235 static AnyP::UriScheme
237 {
238  /*
239  * RFC 3986 section 3.1 paragraph 2:
240  *
241  * Scheme names consist of a sequence of characters beginning with a
242  * letter and followed by any combination of letters, digits, plus
243  * ("+"), period ("."), or hyphen ("-").
244  */
245  static const auto schemeChars = CharacterSet("scheme", "+.-") + CharacterSet::ALPHA + CharacterSet::DIGIT;
246 
247  SBuf str;
248  if (tok.prefix(str, schemeChars, 16) && tok.skip(':') && CharacterSet::ALPHA[str.at(0)]) {
249  const auto protocol = AnyP::UriScheme::FindProtocolType(str);
250  if (protocol == AnyP::PROTO_UNKNOWN)
251  return AnyP::UriScheme(protocol, str.c_str());
252  return AnyP::UriScheme(protocol, nullptr);
253  }
254 
255  throw TextException("invalid URI scheme", Here());
256 }
257 
265 bool
266 urlAppendDomain(char *host)
267 {
268  /* For IPv4 addresses check for a dot */
269  /* For IPv6 addresses also check for a colon */
270  if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':')) {
271  const uint64_t dlen = strlen(host);
272  const uint64_t want = dlen + Config.appendDomainLen;
273  if (want > SQUIDHOSTNAMELEN - 1) {
274  debugs(23, 2, "URL domain too large (" << dlen << " bytes)");
275  return false;
276  }
277  strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN - dlen - 1);
278  }
279  return true;
280 }
281 
282 /*
283  * Parse a URI/URL.
284  *
285  * It is assumed that the URL is complete -
286  * ie, the end of the string is the end of the URL. Don't pass a partial
287  * URL here as this routine doesn't have any way of knowing whether
288  * it is partial or not (ie, it handles the case of no trailing slash as
289  * being "end of host with implied path of /".
290  *
291  * method is used to switch parsers. If method is Http::METHOD_CONNECT,
292  * then rather than a URL a hostname:port is looked for.
293  */
294 bool
295 AnyP::Uri::parse(const HttpRequestMethod& method, const SBuf &rawUrl)
296 {
297  try {
298 
299  LOCAL_ARRAY(char, login, MAX_URL);
300  LOCAL_ARRAY(char, foundHost, MAX_URL);
301  LOCAL_ARRAY(char, urlpath, MAX_URL);
302  char *t = nullptr;
303  char *q = nullptr;
304  int foundPort;
305  int l;
306  int i;
307  const char *src;
308  char *dst;
309  foundHost[0] = urlpath[0] = login[0] = '\0';
310 
311  if ((l = rawUrl.length()) + Config.appendDomainLen > (MAX_URL - 1)) {
312  debugs(23, DBG_IMPORTANT, MYNAME << "URL too large (" << l << " bytes)");
313  return false;
314  }
315 
316  if ((method == Http::METHOD_OPTIONS || method == Http::METHOD_TRACE) &&
317  Asterisk().cmp(rawUrl) == 0) {
318  // XXX: these methods might also occur in HTTPS traffic. Handle this better.
319  setScheme(AnyP::PROTO_HTTP, nullptr);
320  port(getScheme().defaultPort());
321  path(Asterisk());
322  return true;
323  }
324 
325  Parser::Tokenizer tok(rawUrl);
326  AnyP::UriScheme scheme;
327 
328  if (method == Http::METHOD_CONNECT) {
329  // For CONNECTs, RFC 9110 Section 9.3.6 requires "only the host and
330  // port number of the tunnel destination, separated by a colon".
331 
332  const auto rawHost = parseHost(tok);
333  Assure(rawHost.length() < sizeof(foundHost));
334  SBufToCstring(foundHost, rawHost);
335 
336  if (!tok.skip(':'))
337  throw TextException("missing required :port in CONNECT target", Here());
338  foundPort = parsePort(tok);
339 
340  if (!tok.remaining().isEmpty())
341  throw TextException("garbage after host:port in CONNECT target", Here());
342  } else {
343 
344  scheme = uriParseScheme(tok);
345 
346  if (scheme == AnyP::PROTO_NONE)
347  return false; // invalid scheme
348 
349  if (scheme == AnyP::PROTO_URN) {
350  parseUrn(tok); // throws on any error
351  return true;
352  }
353 
354  // URLs then have "//"
355  static const SBuf doubleSlash("//");
356  if (!tok.skip(doubleSlash))
357  return false;
358 
359  auto B = tok.remaining();
360  const char *url = B.c_str();
361 
362  /* Parse the URL: */
363  src = url;
364  i = 0;
365 
366  /* Then everything until first /; that's host (and port; which we'll look for here later) */
367  // bug 1881: If we don't get a "/" then we imply it was there
368  // bug 3074: We could just be given a "?" or "#". These also imply "/"
369  // bug 3233: whitespace is also a hostname delimiter.
370  for (dst = foundHost; i < l && *src != '/' && *src != '?' && *src != '#' && *src != '\0' && !xisspace(*src); ++i, ++src, ++dst) {
371  *dst = *src;
372  }
373 
374  /*
375  * We can't check for "i >= l" here because we could be at the end of the line
376  * and have a perfectly valid URL w/ no trailing '/'. In this case we assume we've
377  * been -given- a valid URL and the path is just '/'.
378  */
379  if (i > l)
380  return false;
381  *dst = '\0';
382 
383  // We are looking at path-abempty.
384  if (*src != '/') {
385  // path-empty, including the end of the `src` c-string cases
386  urlpath[0] = '/';
387  dst = &urlpath[1];
388  } else {
389  dst = urlpath;
390  }
391  /* Then everything from / (inclusive) until \r\n or \0 - that's urlpath */
392  for (; i < l && *src != '\r' && *src != '\n' && *src != '\0'; ++i, ++src, ++dst) {
393  *dst = *src;
394  }
395 
396  /* We -could- be at the end of the buffer here */
397  if (i > l)
398  return false;
399  *dst = '\0';
400 
401  // If the parsed scheme has no (known) default port, and there is no
402  // explicit port, then we will reject the zero port during foundPort
403  // validation, often resulting in a misleading 400/ERR_INVALID_URL.
404  // TODO: Remove this hack when switching to Tokenizer-based parsing.
405  foundPort = scheme.defaultPort().value_or(0); // may be reset later
406 
407  /* Is there any login information? (we should eventually parse it above) */
408  t = strrchr(foundHost, '@');
409  if (t != nullptr) {
410  strncpy((char *) login, (char *) foundHost, sizeof(login)-1);
411  login[sizeof(login)-1] = '\0';
412  t = strrchr(login, '@');
413  *t = 0;
414  strncpy((char *) foundHost, t + 1, sizeof(foundHost)-1);
415  foundHost[sizeof(foundHost)-1] = '\0';
416  // Bug 4498: URL-unescape the login info after extraction
417  rfc1738_unescape(login);
418  }
419 
420  /* Is there any host information? (we should eventually parse it above) */
421  if (*foundHost == '[') {
422  /* strip any IPA brackets. valid under IPv6. */
423  dst = foundHost;
424  /* only for IPv6 sadly, pre-IPv6/URL code can't handle the clean result properly anyway. */
425  src = foundHost;
426  ++src;
427  l = strlen(foundHost);
428  i = 1;
429  for (; i < l && *src != ']' && *src != '\0'; ++i, ++src, ++dst) {
430  *dst = *src;
431  }
432 
433  /* we moved in-place, so truncate the actual hostname found */
434  *dst = '\0';
435  ++dst;
436 
437  /* skip ahead to either start of port, or original EOS */
438  while (*dst != '\0' && *dst != ':')
439  ++dst;
440  t = dst;
441  } else {
442  t = strrchr(foundHost, ':');
443 
444  if (t != strchr(foundHost,':') ) {
445  /* RFC 2732 states IPv6 "SHOULD" be bracketed. allowing for times when its not. */
446  /* RFC 3986 'update' simply modifies this to an "is" with no emphasis at all! */
447  /* therefore we MUST accept the case where they are not bracketed at all. */
448  t = nullptr;
449  }
450  }
451 
452  // Bug 3183 sanity check: If scheme is present, host must be too.
453  if (scheme != AnyP::PROTO_NONE && foundHost[0] == '\0') {
454  debugs(23, DBG_IMPORTANT, "SECURITY ALERT: Missing hostname in URL '" << url << "'. see access.log for details.");
455  return false;
456  }
457 
458  if (t && *t == ':') {
459  *t = '\0';
460  ++t;
461  foundPort = atoi(t);
462  }
463  }
464 
465  for (t = foundHost; *t; ++t)
466  *t = xtolower(*t);
467 
468  if (stringHasWhitespace(foundHost)) {
470  t = q = foundHost;
471  while (*t) {
472  if (!xisspace(*t)) {
473  *q = *t;
474  ++q;
475  }
476  ++t;
477  }
478  *q = '\0';
479  }
480  }
481 
482  debugs(23, 3, "Split URL '" << rawUrl << "' into proto='" << scheme.image() << "', host='" << foundHost << "', port='" << foundPort << "', path='" << urlpath << "'");
483 
485  strspn(foundHost, Config.onoff.allow_underscore ? valid_hostname_chars_u : valid_hostname_chars) != strlen(foundHost)) {
486  debugs(23, DBG_IMPORTANT, MYNAME << "Illegal character in hostname '" << foundHost << "'");
487  return false;
488  }
489 
490  if (!urlAppendDomain(foundHost))
491  return false;
492 
493  /* remove trailing dots from hostnames */
494  while ((l = strlen(foundHost)) > 0 && foundHost[--l] == '.')
495  foundHost[l] = '\0';
496 
497  /* reject duplicate or leading dots */
498  if (strstr(foundHost, "..") || *foundHost == '.') {
499  debugs(23, DBG_IMPORTANT, MYNAME << "Illegal hostname '" << foundHost << "'");
500  return false;
501  }
502 
503  if (foundPort < 1 || foundPort > 65535) {
504  debugs(23, 3, "Invalid port '" << foundPort << "'");
505  return false;
506  }
507 
508  if (stringHasWhitespace(urlpath)) {
509  debugs(23, 2, "URI has whitespace: {" << rawUrl << "}");
510 
511  switch (Config.uri_whitespace) {
512 
513  case URI_WHITESPACE_DENY:
514  return false;
515 
517  break;
518 
520  t = rfc1738_escape_unescaped(urlpath);
521  xstrncpy(urlpath, t, MAX_URL);
522  break;
523 
524  case URI_WHITESPACE_CHOP:
525  *(urlpath + strcspn(urlpath, w_space)) = '\0';
526  break;
527 
529  default:
530  t = q = urlpath;
531  while (*t) {
532  if (!xisspace(*t)) {
533  *q = *t;
534  ++q;
535  }
536  ++t;
537  }
538  *q = '\0';
539  }
540  }
541 
542  setScheme(scheme);
543  path(urlpath);
544  host(foundHost);
545  userInfo(SBuf(login));
546  port(foundPort);
547  return true;
548 
549  } catch (...) {
550  debugs(23, 2, "error: " << CurrentException << " " << Raw("rawUrl", rawUrl.rawContent(), rawUrl.length()));
551  return false;
552  }
553 }
554 
569 void
571 {
572  static const auto nidChars = CharacterSet("NID","-") + CharacterSet::ALPHA + CharacterSet::DIGIT;
573  static const auto alphanum = (CharacterSet::ALPHA + CharacterSet::DIGIT).rename("alphanum");
574  SBuf nid;
575  if (!tok.prefix(nid, nidChars, 32))
576  throw TextException("NID not found", Here());
577 
578  if (!tok.skip(':'))
579  throw TextException("NID too long or missing ':' delimiter", Here());
580 
581  if (nid.length() < 2)
582  throw TextException("NID too short", Here());
583 
584  if (!alphanum[*nid.begin()])
585  throw TextException("NID prefix is not alphanumeric", Here());
586 
587  if (!alphanum[*nid.rbegin()])
588  throw TextException("NID suffix is not alphanumeric", Here());
589 
590  setScheme(AnyP::PROTO_URN, nullptr);
591  host(nid.c_str());
592  // TODO validate path characters
593  path(tok.remaining());
594  debugs(23, 3, "Split URI into proto=urn, nid=" << nid << ", " << Raw("path",path().rawContent(),path().length()));
595 }
596 
600 SBuf
602 {
603  // host = IP-literal / IPv4address / reg-name
604 
605  // XXX: CharacterSets below reject uri-host values containing whitespace
606  // (e.g., "10.0.0. 1"). That is not a bug, but the uri_whitespace directive
607  // can be interpreted as if it applies to uri-host and this code. TODO: Fix
608  // uri_whitespace and the code using it to exclude uri-host (and URI scheme,
609  // port, etc.) from that directive scope.
610 
611  // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
612  if (tok.skip('[')) {
613  // Add "." because IPv6address in RFC 3986 includes ls32, which includes
614  // IPv4address: ls32 = ( h16 ":" h16 ) / IPv4address
615  // This set rejects IPvFuture that needs a "v" character.
616  static const CharacterSet IPv6chars = (
617  CharacterSet::HEXDIG + CharacterSet("colon", ":") + CharacterSet("period", ".")).rename("IPv6");
618  SBuf ipv6ish;
619  if (!tok.prefix(ipv6ish, IPv6chars))
620  throw TextException("malformed or unsupported bracketed IP address in uri-host", Here());
621 
622  if (!tok.skip(']'))
623  throw TextException("IPv6 address is missing a closing bracket in uri-host", Here());
624 
625  // This rejects bracketed IPv4address and domain names because they lack ":".
626  if (ipv6ish.find(':') == SBuf::npos)
627  throw TextException("bracketed IPv6 address is missing a colon in uri-host", Here());
628 
629  // This rejects bracketed non-IP addresses that our caller would have
630  // otherwise mistaken for a domain name (e.g., '[127.0.0:1]').
631  Ip::Address ipv6check;
632  if (!ipv6check.fromHost(ipv6ish.c_str()))
633  throw TextException("malformed bracketed IPv6 address in uri-host", Here());
634 
635  return ipv6ish;
636  }
637 
638  // no brackets implies we are looking at IPv4address or reg-name
639 
640  // XXX: This code does not detect/reject some bad host values (e.g. "!#$%&"
641  // and "1.2.3.4.5"). TODO: Add more checks here, after migrating the
642  // non-CONNECT uri-host parsing code to use us.
643 
644  SBuf otherHost; // IPv4address-ish or reg-name-ish;
645  // ":" is not in TCHAR so we will stop before any port specification
646  if (tok.prefix(otherHost, CharacterSet::TCHAR))
647  return otherHost;
648 
649  throw TextException("malformed IPv4 address or host name in uri-host", Here());
650 }
651 
658 int
660 {
661  if (tok.skip('0'))
662  throw TextException("zero or zero-prefixed port", Here());
663 
664  int64_t rawPort = 0;
665  if (!tok.int64(rawPort, 10, false)) // port = *DIGIT
666  throw TextException("malformed or missing port", Here());
667 
668  Assure(rawPort > 0);
669  constexpr KnownPort portMax = 65535; // TODO: Make this a class-scope constant and REuse it.
670  constexpr auto portStorageMax = std::numeric_limits<Port::value_type>::max();
671  static_assert(!Less(portStorageMax, portMax), "Port type can represent the maximum valid port number");
672  if (Less(portMax, rawPort))
673  throw TextException("huge port", Here());
674 
675  // TODO: Return KnownPort after migrating the non-CONNECT uri-host parsing
676  // code to use us (so that foundPort "int" disappears or starts using Port).
677  return NaturalCast<int>(rawPort);
678 }
679 
680 void
682 {
683  absolute_.clear();
684  authorityHttp_.clear();
685  authorityWithPort_.clear();
686 }
687 
688 SBuf &
689 AnyP::Uri::authority(bool requirePort) const
690 {
691  if (authorityHttp_.isEmpty()) {
692 
693  // both formats contain Host/IP
694  authorityWithPort_.append(host());
695  authorityHttp_ = authorityWithPort_;
696 
697  if (port().has_value()) {
698  authorityWithPort_.appendf(":%hu", *port());
699  // authorityHttp_ only has :port for known non-default ports
700  if (port() != getScheme().defaultPort())
701  authorityHttp_ = authorityWithPort_;
702  }
703  // else XXX: We made authorityWithPort_ that does not have a port.
704  // TODO: Audit callers and refuse to give out broken authorityWithPort_.
705  }
706 
707  return requirePort ? authorityWithPort_ : authorityHttp_;
708 }
709 
710 SBuf &
712 {
713  if (absolute_.isEmpty()) {
714  // TODO: most URL will be much shorter, avoid allocating this much
715  absolute_.reserveCapacity(MAX_URL);
716 
717  absolute_.append(getScheme().image());
718  absolute_.append(":",1);
719  if (getScheme() != AnyP::PROTO_URN) {
720  absolute_.append("//", 2);
721  const bool allowUserInfo = getScheme() == AnyP::PROTO_FTP ||
722  getScheme() == AnyP::PROTO_UNKNOWN;
723 
724  if (allowUserInfo && !userInfo().isEmpty()) {
725  static const CharacterSet uiChars = CharacterSet(UserInfoChars())
726  .remove('%')
727  .rename("userinfo-reserved");
728  absolute_.append(Encode(userInfo(), uiChars));
729  absolute_.append("@", 1);
730  }
731  absolute_.append(authority());
732  } else {
733  absolute_.append(host());
734  absolute_.append(":", 1);
735  }
736  absolute_.append(path()); // TODO: Encode each URI subcomponent in path_ as needed.
737  }
738 
739  return absolute_;
740 }
741 
742 /* XXX: Performance: This is an *almost* duplicate of HttpRequest::effectiveRequestUri(). But elides the query-string.
743  * After copying it on in the first place! Would be less code to merge the two with a flag parameter.
744  * and never copy the query-string part in the first place
745  */
746 char *
748 {
749  LOCAL_ARRAY(char, buf, MAX_URL);
750 
751  snprintf(buf, sizeof(buf), SQUIDSBUFPH, SQUIDSBUFPRINT(url));
752  buf[sizeof(buf)-1] = '\0';
753 
754  // URN, CONNECT method, and non-stripped URIs can go straight out
755  if (Config.onoff.strip_query_terms && !(method == Http::METHOD_CONNECT || scheme == AnyP::PROTO_URN)) {
756  // strip anything AFTER a question-mark
757  // leaving the '?' in place
758  if (auto t = strchr(buf, '?')) {
759  *(++t) = '\0';
760  }
761  }
762 
763  if (stringHasCntl(buf))
765 
766  return buf;
767 }
768 
775 const char *
777 {
778  LOCAL_ARRAY(char, buf, MAX_URL);
779 
780  // method CONNECT and port HTTPS
781  if (request->method == Http::METHOD_CONNECT && request->url.port() == 443) {
782  snprintf(buf, MAX_URL, "https://%s/*", request->url.host());
783  return buf;
784  }
785 
786  // else do the normal complete canonical thing.
787  return request->canonicalCleanUrl();
788 }
789 
802 bool
803 urlIsRelative(const char *url)
804 {
805  if (!url)
806  return false; // no URL
807 
808  /*
809  * RFC 3986 section 5.2.3
810  *
811  * path = path-abempty ; begins with "/" or is empty
812  * / path-absolute ; begins with "/" but not "//"
813  * / path-noscheme ; begins with a non-colon segment
814  * / path-rootless ; begins with a segment
815  * / path-empty ; zero characters
816  */
817 
818  if (*url == '\0')
819  return true; // path-empty
820 
821  if (*url == '/') {
822  // network-path reference (a.k.a. 'scheme-relative URI') or
823  // path-absolute (a.k.a. 'absolute-path reference')
824  return true;
825  }
826 
827  for (const auto *p = url; *p != '\0' && *p != '/' && *p != '?' && *p != '#'; ++p) {
828  if (*p == ':')
829  return false; // colon is forbidden in first segment
830  }
831 
832  return true; // path-noscheme, path-abempty, path-rootless
833 }
834 
835 void
836 AnyP::Uri::addRelativePath(const char *relUrl)
837 {
838  // URN cannot be merged
839  if (getScheme() == AnyP::PROTO_URN)
840  return;
841 
842  // TODO: Handle . and .. segment normalization
843 
844  const auto lastSlashPos = path_.rfind('/');
845  // TODO: To optimize and simplify, add and use SBuf::replace().
846  const auto relUrlLength = strlen(relUrl);
847  if (lastSlashPos == SBuf::npos) {
848  // start replacing the whole path
849  path_.reserveCapacity(1 + relUrlLength);
850  path_.assign("/", 1);
851  } else {
852  // start replacing just the last segment
853  path_.reserveCapacity(lastSlashPos + 1 + relUrlLength);
854  path_.chop(0, lastSlashPos+1);
855  }
856  path_.append(relUrl, relUrlLength);
857 }
858 
859 int
860 matchDomainName(const char *h, const char *d, MatchDomainNameFlags flags)
861 {
862  int dl;
863  int hl;
864 
865  const bool hostIncludesSubdomains = (*h == '.');
866  while ('.' == *h)
867  ++h;
868 
869  hl = strlen(h);
870 
871  if (hl == 0)
872  return -1;
873 
874  dl = strlen(d);
875  if (dl == 0)
876  return 1;
877 
878  /*
879  * Start at the ends of the two strings and work towards the
880  * beginning.
881  */
882  while (xtolower(h[--hl]) == xtolower(d[--dl])) {
883  if (hl == 0 && dl == 0) {
884  /*
885  * We made it all the way to the beginning of both
886  * strings without finding any difference.
887  */
888  return 0;
889  }
890 
891  if (0 == hl) {
892  /*
893  * The host string is shorter than the domain string.
894  * There is only one case when this can be a match.
895  * If the domain is just one character longer, and if
896  * that character is a leading '.' then we call it a
897  * match.
898  */
899 
900  if (1 == dl && '.' == d[0])
901  return 0;
902  else
903  return -1;
904  }
905 
906  if (0 == dl) {
907  /*
908  * The domain string is shorter than the host string.
909  * This is a match only if the first domain character
910  * is a leading '.'.
911  */
912 
913  if ('.' == d[0]) {
914  if (flags & mdnRejectSubsubDomains) {
915  // Check for sub-sub domain and reject
916  while(--hl >= 0 && h[hl] != '.');
917  if (hl < 0) {
918  // No sub-sub domain found, but reject if there is a
919  // leading dot in given host string (which is removed
920  // before the check is started).
921  return hostIncludesSubdomains ? 1 : 0;
922  } else
923  return 1; // sub-sub domain, reject
924  } else
925  return 0;
926  } else
927  return 1;
928  }
929  }
930 
931  /*
932  * We found different characters in the same position (from the end).
933  */
934 
935  // If the h has a form of "*.foo.com" and d has a form of "x.foo.com"
936  // then the h[hl] points to '*', h[hl+1] to '.' and d[dl] to 'x'
937  // The following checks are safe, the "h[hl + 1]" in the worst case is '\0'.
938  if ((flags & mdnHonorWildcards) && h[hl] == '*' && h[hl + 1] == '.')
939  return 0;
940 
941  /*
942  * If one of those character is '.' then its special. In order
943  * for splay tree sorting to work properly, "x-foo.com" must
944  * be greater than ".foo.com" even though '-' is less than '.'.
945  */
946  if ('.' == d[dl])
947  return 1;
948 
949  if ('.' == h[hl])
950  return -1;
951 
952  return (xtolower(h[hl]) - xtolower(d[dl]));
953 }
954 
955 /*
956  * return true if we can serve requests for this method.
957  */
958 bool
960 {
961  /* protocol "independent" methods
962  *
963  * actually these methods are specific to HTTP:
964  * they are methods we receive on our HTTP port,
965  * and if we had a FTP listener would not be relevant
966  * there.
967  *
968  * So, we should delegate them to HTTP. The problem is that we
969  * do not have a default protocol from the client side of HTTP.
970  */
971 
972  if (r->method == Http::METHOD_CONNECT)
973  return true;
974 
975  // we support OPTIONS and TRACE directed at us (with a 501 reply, for now)
976  // we also support forwarding OPTIONS and TRACE, except for the *-URI ones
979 
980  if (r->method == Http::METHOD_PURGE)
981  return true;
982 
983  /* does method match the protocol? */
984  switch (r->url.getScheme()) {
985 
986  case AnyP::PROTO_URN:
987  case AnyP::PROTO_HTTP:
988  return true;
989 
990  case AnyP::PROTO_FTP:
991  if (r->method == Http::METHOD_PUT ||
992  r->method == Http::METHOD_GET ||
993  r->method == Http::METHOD_HEAD )
994  return true;
995  return false;
996 
997  case AnyP::PROTO_WAIS:
998  case AnyP::PROTO_WHOIS:
999  if (r->method == Http::METHOD_GET ||
1000  r->method == Http::METHOD_HEAD)
1001  return true;
1002  return false;
1003 
1004  case AnyP::PROTO_HTTPS:
1005 #if USE_OPENSSL || HAVE_LIBGNUTLS
1006  return true;
1007 #else
1008  /*
1009  * Squid can't originate an SSL connection, so it should
1010  * never receive an "https:" URL. It should always be
1011  * CONNECT instead.
1012  */
1013  return false;
1014 #endif
1015 
1016  default:
1017  return false;
1018  }
1019 
1020  /* notreached */
1021  return false;
1022 }
1023 
1025  scheme_(aScheme),
1026  hostIsNumeric_(false)
1027 {
1028  *host_=0;
1029 }
1030 
1031 // TODO: fix code duplication with AnyP::Uri::parse()
1032 char *
1033 AnyP::Uri::cleanup(const char *uri)
1034 {
1035  char *cleanedUri = nullptr;
1036  switch (Config.uri_whitespace) {
1037  case URI_WHITESPACE_ALLOW: {
1038  const auto flags = RFC1738_ESCAPE_NOSPACE | RFC1738_ESCAPE_UNESCAPED;
1039  cleanedUri = xstrndup(rfc1738_do_escape(uri, flags), MAX_URL);
1040  break;
1041  }
1042 
1043  case URI_WHITESPACE_ENCODE:
1045  break;
1046 
1047  case URI_WHITESPACE_CHOP: {
1048  const auto pos = strcspn(uri, w_space);
1049  char *choppedUri = nullptr;
1050  if (pos < strlen(uri))
1051  choppedUri = xstrndup(uri, pos + 1);
1052  cleanedUri = xstrndup(rfc1738_do_escape(choppedUri ? choppedUri : uri,
1054  cleanedUri[pos] = '\0';
1055  xfree(choppedUri);
1056  break;
1057  }
1058 
1059  case URI_WHITESPACE_DENY:
1060  case URI_WHITESPACE_STRIP:
1061  default: {
1062  // TODO: avoid duplication with urlParse()
1063  const char *t;
1064  char *tmp_uri = static_cast<char*>(xmalloc(strlen(uri) + 1));
1065  char *q = tmp_uri;
1066  t = uri;
1067  while (*t) {
1068  if (!xisspace(*t)) {
1069  *q = *t;
1070  ++q;
1071  }
1072  ++t;
1073  }
1074  *q = '\0';
1075  cleanedUri = xstrndup(rfc1738_escape_unescaped(tmp_uri), MAX_URL);
1076  xfree(tmp_uri);
1077  break;
1078  }
1079  }
1080 
1081  assert(cleanedUri);
1082  return cleanedUri;
1083 }
1084 
static char * cleanup(const char *uri)
Definition: Uri.cc:1033
#define URI_WHITESPACE_ENCODE
Definition: defines.h:126
bool prefix(SBuf &returnedToken, const CharacterSet &tokenChars, SBuf::size_type limit=SBuf::npos)
Definition: Tokenizer.cc:79
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
#define Here()
source code location of the caller
Definition: Here.h:15
static const char valid_hostname_chars_u[]
Definition: Uri.cc:22
@ METHOD_HEAD
Definition: MethodType.h:28
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
#define xmalloc
char * canonicalCleanUrl() const
Definition: HttpRequest.cc:813
int stringHasCntl(const char *)
Definition: String.cc:294
#define URI_WHITESPACE_STRIP
Definition: defines.h:124
const_reverse_iterator rbegin() const
Definition: SBuf.h:595
#define URI_WHITESPACE_CHOP
Definition: defines.h:127
static AnyP::ProtocolType FindProtocolType(const SBuf &)
Definition: UriScheme.cc:52
char host_[SQUIDHOSTNAMELEN]
string representation of the URI authority name or IP
Definition: Uri.h:177
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:62
HttpHeader header
Definition: Message.h:74
int check_hostnames
Definition: SquidConfig.h:316
@ mdnHonorWildcards
Definition: Uri.h:228
bool urlCheckRequest(const HttpRequest *r)
Definition: Uri.cc:959
bool isEmpty() const
Definition: SBuf.h:435
void reserveSpace(size_type minSpace)
Definition: SBuf.h:444
static std::optional< Host > ParseSimpleDomainName(const SBuf &)
Definition: Host.cc:49
@ PROTO_NONE
Definition: ProtocolType.h:24
SBuf hostOrIp() const
Definition: Uri.cc:139
bool parse(const HttpRequestMethod &, const SBuf &url)
Definition: Uri.cc:295
const char * ProtocolType_str[]
Definition: SBuf.h:93
bool atEnd() const
whether the end of the buffer has been reached
Definition: Tokenizer.h:41
void SBufToCstring(char *d, const SBuf &s)
Definition: SBuf.h:756
CharacterSet complement(const char *complementLabel=nullptr) const
Definition: CharacterSet.cc:74
bool fromHost(const char *hostWithoutPort)
Definition: Address.cc:918
const A & max(A const &lhs, A const &rhs)
struct SquidConfig::@97 onoff
bool skip(const SBuf &tokenToSkip)
Definition: Tokenizer.cc:189
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
int matchDomainName(const char *h, const char *d, MatchDomainNameFlags flags)
Definition: Uri.cc:860
#define xtolower(x)
Definition: xis.h:17
#define w_space
static int port
Definition: ldap_backend.cc:70
@ PROTO_UNKNOWN
Definition: ProtocolType.h:41
static const CharacterSet ALPHA
Definition: CharacterSet.h:76
@ METHOD_OPTIONS
Definition: MethodType.h:31
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
@ PROTO_URN
Definition: ProtocolType.h:35
Definition: Raw.h:20
#define MAX_URL
Definition: defines.h:76
const SBuf & remaining() const
the remaining unprocessed section of buffer
Definition: Tokenizer.h:44
static const CharacterSet & UserInfoChars()
Characters which are valid within a URI userinfo section.
Definition: Uri.cc:37
#define URI_WHITESPACE_DENY
Definition: defines.h:128
int strip_query_terms
Definition: SquidConfig.h:300
const char * rawContent() const
Definition: SBuf.cc:509
@ PROTO_MAX
Definition: ProtocolType.h:42
void rfc1738_unescape(char *url)
Definition: rfc1738.c:146
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
int parsePort(Parser::Tokenizer &) const
Definition: Uri.cc:659
uint16_t KnownPort
validated/supported port number; these values are never zero
Definition: UriScheme.h:23
char at(size_type pos) const
Definition: SBuf.h:253
static const CharacterSet TCHAR
Definition: CharacterSet.h:105
static const CharacterSet HEXDIG
Definition: CharacterSet.h:88
MatchDomainNameFlags
Definition: Uri.h:226
@ METHOD_CONNECT
Definition: MethodType.h:29
const_iterator begin() const
Definition: SBuf.h:587
int64_t getInt64(Http::HdrType id) const
Definition: HttpHeader.cc:1133
std::optional< Host > parsedHost() const
Definition: Uri.cc:150
static std::optional< Host > ParseIp(const Ip::Address &)
converts an already parsed IP address to a Host object
Definition: Host.cc:15
void parseUrn(Parser::Tokenizer &)
Definition: Uri.cc:570
#define assert(EX)
Definition: assert.h:17
SBuf image() const
Definition: UriScheme.h:57
@ METHOD_PUT
Definition: MethodType.h:27
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
const AnyP::UriScheme & getScheme() const
Definition: Uri.h:58
@ METHOD_TRACE
Definition: MethodType.h:30
Port defaultPort() const
Definition: UriScheme.cc:71
#define Assure(condition)
Definition: Assure.h:35
static const CharacterSet DIGIT
Definition: CharacterSet.h:84
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:90
const char * c_str()
Definition: SBuf.cc:516
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:689
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:419
char * xstrndup(const char *s, size_t n)
Definition: xstring.cc:56
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
#define xfree
void addRelativePath(const char *relUrl)
Definition: Uri.cc:836
CharacterSet & rename(const char *label)
change name; handy in const declarations that use operators
Definition: CharacterSet.h:61
int allow_underscore
Definition: SquidConfig.h:317
static const size_type npos
Definition: SBuf.h:100
static const char valid_hostname_chars[]
Definition: Uri.cc:28
@ METHOD_PURGE
Definition: MethodType.h:92
const char * urlCanonicalFakeHttps(const HttpRequest *request)
Definition: Uri.cc:776
CharacterSet & remove(const unsigned char c)
remove a given character from the character set
Definition: CharacterSet.cc:54
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition: Uri.cc:266
#define rfc1738_escape_unescaped(x)
Definition: rfc1738.h:59
static const SBuf & SlashPath()
the static '/' default URL-path
Definition: Uri.cc:116
void urlInitialize(void)
Definition: Uri.cc:181
SBuf & absolute() const
Definition: Uri.cc:711
@ PROTO_WHOIS
Definition: ProtocolType.h:36
@ PROTO_HTTPS
Definition: ProtocolType.h:27
HttpRequestMethod method
Definition: HttpRequest.h:114
void path(const char *p)
Definition: Uri.h:96
@ PROTO_FTP
Definition: ProtocolType.h:26
@ PROTO_HTTP
Definition: ProtocolType.h:25
void reserveCapacity(size_type minCapacity)
Definition: SBuf.cc:105
Definition: parse.c:160
static SBuf Decode(const SBuf &)
%-decode the given buffer
Definition: Uri.cc:86
@ mdnRejectSubsubDomains
Definition: Uri.h:229
an std::runtime_error with thrower location info
Definition: TextException.h:20
@ PROTO_WAIS
Definition: ProtocolType.h:30
Uri()
Definition: Uri.h:36
#define DBG_IMPORTANT
Definition: Stream.h:38
#define RFC1738_ESCAPE_UNESCAPED
Definition: rfc1738.h:25
#define MYNAME
Definition: Stream.h:219
bool urlIsRelative(const char *url)
Definition: Uri.cc:803
char * rfc1738_do_escape(const char *url, int flags)
Definition: rfc1738.c:56
char * urlCanonicalCleanWithoutRequest(const SBuf &url, const HttpRequestMethod &method, const AnyP::UriScheme &scheme)
Definition: Uri.cc:747
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:17
#define xisspace(x)
Definition: xis.h:15
#define RFC1738_ESCAPE_NOSPACE
Definition: rfc1738.h:22
char * appendDomain
Definition: SquidConfig.h:222
static uint32 B
Definition: md4.c:43
size_t appendDomainLen
Definition: SquidConfig.h:223
const SBuf & path() const
Definition: Uri.cc:169
SBuf & appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:229
@ METHOD_GET
Definition: MethodType.h:25
constexpr bool Less(const A a, const B b)
whether integer a is less than integer b, with correct overflow handling
Definition: SquidMath.h:48
int stringHasWhitespace(const char *)
Definition: String.cc:287
const char * host(void) const
Definition: Uri.h:76
static const SBuf & Asterisk()
the static '*' pseudo-URI
Definition: Uri.cc:109
void host(const char *src)
Definition: Uri.cc:123
int uri_whitespace
Definition: SquidConfig.h:457
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
static SBuf Encode(const SBuf &, const CharacterSet &expected)
Definition: Uri.cc:57
SBuf parseHost(Parser::Tokenizer &) const
Definition: Uri.cc:601
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
void touch()
clear the cached URI display forms
Definition: Uri.cc:681
#define SQUIDSBUFPH
Definition: SBuf.h:31
class SquidConfig Config
Definition: SquidConfig.cc:12
static AnyP::UriScheme uriParseScheme(Parser::Tokenizer &tok)
Definition: Uri.cc:236
#define URI_WHITESPACE_ALLOW
Definition: defines.h:125

 

Introduction

Documentation

Support

Miscellaneous