]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/anyp/Uri.h
2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_SRC_ANYP_URI_H
10 #define SQUID_SRC_ANYP_URI_H
12 #include "anyp/UriScheme.h"
13 #include "ip/Address.h"
15 #include "sbuf/SBuf.h"
19 class HttpRequestMethod
;
25 * Represents a Uniform Resource Identifier.
26 * Can store both URL or URN representations.
28 * Governed by RFC 3986
35 Uri() : hostIsNumeric_(false), port_(0) {*host_
=0;}
36 Uri(AnyP::UriScheme
const &aScheme
);
37 Uri(const Uri
&other
) {
38 this->operator =(other
);
40 Uri
&operator =(const Uri
&o
) {
42 userInfo_
= o
.userInfo_
;
43 memcpy(host_
, o
.host_
, sizeof(host_
));
44 hostIsNumeric_
= o
.hostIsNumeric_
;
45 hostAddr_
= o
.hostAddr_
;
53 scheme_
=AnyP::PROTO_NONE
;
54 hostIsNumeric_
= false;
60 void touch(); ///< clear the cached URI display forms
62 bool parse(const HttpRequestMethod
&, const char *url
);
64 /// \return a new URI that honors uri_whitespace
65 static char *cleanup(const char *uri
);
67 AnyP::UriScheme
const & getScheme() const {return scheme_
;}
69 /// convert the URL scheme to that given
70 void setScheme(const AnyP::ProtocolType
&p
, const char *str
) {
71 scheme_
= AnyP::UriScheme(p
, str
);
75 void userInfo(const SBuf
&s
) {userInfo_
=s
; touch();}
76 const SBuf
&userInfo() const {return userInfo_
;}
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_
;}
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()); }
88 void path(const char *p
) {path_
=p
; touch();}
89 void path(const SBuf
&p
) {path_
=p
; touch();}
90 const SBuf
&path() const;
92 /// the static '/' default URL-path
93 static const SBuf
&SlashPath();
95 /// the static '*' pseudo-URI
96 static const SBuf
&Asterisk();
99 * The authority-form URI for currently stored values.
101 * As defined by RFC 7230 section 5.3.3 this form omits the
102 * userinfo@ field from RFC 3986 defined authority segment.
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.
108 SBuf
&authority(bool requirePort
= false) const;
111 * The absolute-form URI for currently stored values.
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:.
117 SBuf
&absolute() const;
120 void parseFinish(const AnyP::ProtocolType
, const char *const, const char *const, const char *const, const SBuf
&, const int);
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.
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.
137 * Deferring the decision, its a type code for now. RBC 20060507.
139 * In order to make taking any of these routes easy, scheme is private,
140 * only settable at construction time, or with explicit setter
142 AnyP::UriScheme scheme_
;
144 SBuf userInfo_
; // aka 'URL-login'
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
152 unsigned short port_
; ///< URL port
154 // XXX: for now includes query-string.
155 SBuf path_
; ///< URI path segment
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
165 inline std::ostream
&
166 operator <<(std::ostream
&os
, const AnyP::Uri
&url
)
168 // none means explicit empty string for scheme.
169 if (url
.getScheme() != AnyP::PROTO_NONE
)
170 os
<< url
.getScheme().image();
173 // no authority section on URN
174 if (url
.getScheme() != AnyP::PROTO_URN
)
175 os
<< "//" << url
.authority();
177 // path is what it is - including absent
182 /* Deprecated functions for Legacy code handling URLs */
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
);
197 enum MatchDomainNameFlags
{
199 mdnHonorWildcards
= 1 << 0,
200 mdnRejectSubsubDomains
= 1 << 1
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:
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
219 * if mdnHonorWildcards flag is set then the matchDomainName() also accepts
220 * optional wildcards on hostname:
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
229 * The combination of mdnHonorWildcards and mdnRejectSubsubDomains flags is
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
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);
241 #endif /* SQUID_SRC_ANYP_URI_H */