]> git.ipfire.org Git - thirdparty/squid.git/blob - src/anyp/Uri.h
Fixed startup assertions when using a src_as or dst_as ACL (#397)
[thirdparty/squid.git] / src / anyp / Uri.h
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 #ifndef SQUID_SRC_ANYP_URI_H
10 #define SQUID_SRC_ANYP_URI_H
11
12 #include "anyp/UriScheme.h"
13 #include "ip/Address.h"
14 #include "rfc2181.h"
15 #include "sbuf/SBuf.h"
16
17 #include <iosfwd>
18
19 class HttpRequestMethod;
20
21 namespace AnyP
22 {
23
24 /**
25 * Represents a Uniform Resource Identifier.
26 * Can store both URL or URN representations.
27 *
28 * Governed by RFC 3986
29 */
30 class Uri
31 {
32 MEMPROXY_CLASS(Uri);
33
34 public:
35 Uri() : hostIsNumeric_(false), port_(0) {*host_=0;}
36 Uri(AnyP::UriScheme const &aScheme);
37 Uri(const Uri &other) {
38 this->operator =(other);
39 }
40 Uri &operator =(const Uri &o) {
41 scheme_ = o.scheme_;
42 userInfo_ = o.userInfo_;
43 memcpy(host_, o.host_, sizeof(host_));
44 hostIsNumeric_ = o.hostIsNumeric_;
45 hostAddr_ = o.hostAddr_;
46 port_ = o.port_;
47 path_ = o.path_;
48 touch();
49 return *this;
50 }
51
52 void clear() {
53 scheme_=AnyP::PROTO_NONE;
54 hostIsNumeric_ = false;
55 *host_ = 0;
56 hostAddr_.setEmpty();
57 port_ = 0;
58 touch();
59 }
60 void touch(); ///< clear the cached URI display forms
61
62 bool parse(const HttpRequestMethod &, const char *url);
63
64 /// \return a new URI that honors uri_whitespace
65 static char *cleanup(const char *uri);
66
67 AnyP::UriScheme const & getScheme() const {return scheme_;}
68
69 /// convert the URL scheme to that given
70 void setScheme(const AnyP::ProtocolType &p, const char *str) {
71 scheme_ = AnyP::UriScheme(p, str);
72 touch();
73 }
74
75 void userInfo(const SBuf &s) {userInfo_=s; touch();}
76 const SBuf &userInfo() const {return userInfo_;}
77
78 void host(const char *src);
79 const char *host(void) const {return host_;}
80 int hostIsNumeric(void) const {return hostIsNumeric_;}
81 Ip::Address const & hostIP(void) const {return hostAddr_;}
82
83 void port(unsigned short p) {port_=p; touch();}
84 unsigned short port() const {return port_;}
85 /// reset the port to the default port number for the current scheme
86 void defaultPort() { port(getScheme().defaultPort()); }
87
88 void path(const char *p) {path_=p; touch();}
89 void path(const SBuf &p) {path_=p; touch();}
90 const SBuf &path() const;
91
92 /// the static '/' default URL-path
93 static const SBuf &SlashPath();
94
95 /// the static '*' pseudo-URI
96 static const SBuf &Asterisk();
97
98 /**
99 * The authority-form URI for currently stored values.
100 *
101 * As defined by RFC 7230 section 5.3.3 this form omits the
102 * userinfo@ field from RFC 3986 defined authority segment.
103 *
104 * \param requirePort when true the port will be included, otherwise
105 * port will be elided when it is the default for
106 * the current scheme.
107 */
108 SBuf &authority(bool requirePort = false) const;
109
110 /**
111 * The absolute-form URI for currently stored values.
112 *
113 * As defined by RFC 7230 section 5.3.3 this form omits the
114 * userinfo@ field from RFC 3986 defined authority segments
115 * when the protocol scheme is http: or https:.
116 */
117 SBuf &absolute() const;
118
119 private:
120 void parseFinish(const AnyP::ProtocolType, const char *const, const char *const, const char *const, const SBuf &, const int);
121
122 /**
123 \par
124 * The scheme of this URL. This has the 'type code' smell about it.
125 * In future we may want to make the methods that dispatch based on
126 * the scheme virtual and have a class per protocol.
127 \par
128 * On the other hand, having Protocol as an explicit concept is useful,
129 * see for instance the ACLProtocol acl type. One way to represent this
130 * is to have one prototype URL with no host etc for each scheme,
131 * another is to have an explicit scheme class, and then each URL class
132 * could be a subclass of the scheme. Another way is one instance of
133 * a AnyP::UriScheme class instance for each URL scheme we support, and one
134 * class for each manner of treating the scheme : a Hierarchical URL, a
135 * non-hierarchical URL etc.
136 \par
137 * Deferring the decision, its a type code for now. RBC 20060507.
138 \par
139 * In order to make taking any of these routes easy, scheme is private,
140 * only settable at construction time, or with explicit setter
141 */
142 AnyP::UriScheme scheme_;
143
144 SBuf userInfo_; // aka 'URL-login'
145
146 // XXX: uses char[] instead of SBUf to reduce performance regressions
147 // from c_str() since most code using this is not yet using SBuf
148 char host_[SQUIDHOSTNAMELEN]; ///< string representation of the URI authority name or IP
149 bool hostIsNumeric_; ///< whether the authority 'host' is a raw-IP
150 Ip::Address hostAddr_; ///< binary representation of the URI authority if it is a raw-IP
151
152 unsigned short port_; ///< URL port
153
154 // XXX: for now includes query-string.
155 SBuf path_; ///< URI path segment
156
157 // pre-assembled URI forms
158 mutable SBuf authorityHttp_; ///< RFC 7230 section 5.3.3 authority, maybe without default-port
159 mutable SBuf authorityWithPort_; ///< RFC 7230 section 5.3.3 authority with explicit port
160 mutable SBuf absolute_; ///< RFC 7230 section 5.3.2 absolute-URI
161 };
162
163 } // namespace AnyP
164
165 inline std::ostream &
166 operator <<(std::ostream &os, const AnyP::Uri &url)
167 {
168 // none means explicit empty string for scheme.
169 if (url.getScheme() != AnyP::PROTO_NONE)
170 os << url.getScheme().image();
171 os << ":";
172
173 // no authority section on URN
174 if (url.getScheme() != AnyP::PROTO_URN)
175 os << "//" << url.authority();
176
177 // path is what it is - including absent
178 os << url.path();
179 return os;
180 }
181
182 /* Deprecated functions for Legacy code handling URLs */
183
184 class HttpRequest;
185
186 void urlInitialize(void);
187 /// call HttpRequest::canonicalCleanUrl() instead if you have HttpRequest
188 /// \returns a pointer to a local static buffer containing request URI
189 /// that honors strip_query_terms and %-encodes unsafe URI characters
190 char *urlCanonicalCleanWithoutRequest(const SBuf &url, const HttpRequestMethod &, const AnyP::UriScheme &);
191 const char *urlCanonicalFakeHttps(const HttpRequest * request);
192 bool urlIsRelative(const char *);
193 char *urlMakeAbsolute(const HttpRequest *, const char *);
194 char *urlRInternal(const char *host, unsigned short port, const char *dir, const char *name);
195 char *urlInternal(const char *dir, const char *name);
196
197 enum MatchDomainNameFlags {
198 mdnNone = 0,
199 mdnHonorWildcards = 1 << 0,
200 mdnRejectSubsubDomains = 1 << 1
201 };
202
203 /**
204 * matchDomainName() matches a hostname (usually extracted from traffic)
205 * with a domainname when mdnNone or mdnRejectSubsubDomains flags are used
206 * according to the following rules:
207 *
208 * HOST | DOMAIN | mdnNone | mdnRejectSubsubDomains
209 * -------------|-------------|-----------|-----------------------
210 * foo.com | foo.com | YES | YES
211 * .foo.com | foo.com | YES | YES
212 * x.foo.com | foo.com | NO | NO
213 * foo.com | .foo.com | YES | YES
214 * .foo.com | .foo.com | YES | YES
215 * x.foo.com | .foo.com | YES | YES
216 * .x.foo.com | .foo.com | YES | NO
217 * y.x.foo.com | .foo.com | YES | NO
218 *
219 * if mdnHonorWildcards flag is set then the matchDomainName() also accepts
220 * optional wildcards on hostname:
221 *
222 * HOST | DOMAIN | MATCH?
223 * -------------|--------------|-------
224 * *.foo.com | x.foo.com | YES
225 * *.foo.com | .x.foo.com | YES
226 * *.foo.com | .foo.com | YES
227 * *.foo.com | foo.com | NO
228 *
229 * The combination of mdnHonorWildcards and mdnRejectSubsubDomains flags is
230 * supported.
231 *
232 * \retval 0 means the host matches the domain
233 * \retval 1 means the host is greater than the domain
234 * \retval -1 means the host is less than the domain
235 */
236 int matchDomainName(const char *host, const char *domain, uint8_t flags = mdnNone);
237 int urlCheckRequest(const HttpRequest *);
238 char *urlHostname(const char *url);
239 void urlExtMethodConfigure(void);
240
241 #endif /* SQUID_SRC_ANYP_URI_H */
242