]> git.ipfire.org Git - thirdparty/squid.git/blame - src/URL.h
HTTP: MUST always revalidate Cache-Control:no-cache responses.
[thirdparty/squid.git] / src / URL.h
CommitLineData
985c86bc 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
985c86bc 3 *
bbc27441
AJ
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.
985c86bc 7 */
8
9#ifndef SQUID_SRC_URL_H
10#define SQUID_SRC_URL_H
11
1ca54a54 12#include "anyp/UriScheme.h"
5c51bffb
AJ
13#include "ip/Address.h"
14#include "rfc2181.h"
65e41a45 15#include "sbuf/SBuf.h"
985c86bc 16
51b5dcf5
AJ
17#include <iosfwd>
18
63be0a78 19/**
63be0a78 20 * The URL class represents a Uniform Resource Location
5c51bffb
AJ
21 *
22 * Governed by RFC 3986
63be0a78 23 */
985c86bc 24class URL
25{
985c86bc 26 MEMPROXY_CLASS(URL);
741c2986
AJ
27
28public:
d59e4742
FC
29 URL() : hostIsNumeric_(false), port_(0) {*host_=0;}
30 URL(AnyP::UriScheme const &aScheme);
d31d59d8
AJ
31 URL(const URL &other) {
32 this->operator =(other);
33 }
34 URL &operator =(const URL &o) {
35 scheme_ = o.scheme_;
36 userInfo_ = o.userInfo_;
37 memcpy(host_, o.host_, sizeof(host_));
38 hostIsNumeric_ = o.hostIsNumeric_;
39 hostAddr_ = o.hostAddr_;
40 port_ = o.port_;
41 path_ = o.path_;
42 touch();
43 return *this;
44 }
4e3f4dc7
AJ
45
46 void clear() {
47 scheme_=AnyP::PROTO_NONE;
5c51bffb
AJ
48 hostIsNumeric_ = false;
49 *host_ = 0;
50 hostAddr_.setEmpty();
51 port_ = 0;
52 touch();
4e3f4dc7 53 }
5c51bffb 54 void touch(); ///< clear the cached URI display forms
4e3f4dc7 55
1ca54a54 56 AnyP::UriScheme const & getScheme() const {return scheme_;}
985c86bc 57
4e3f4dc7 58 /// convert the URL scheme to that given
d31d59d8
AJ
59 void setScheme(const AnyP::ProtocolType &p, const char *str) {
60 scheme_ = AnyP::UriScheme(p, str);
61 touch();
62 }
4e3f4dc7 63
5c51bffb 64 void userInfo(const SBuf &s) {userInfo_=s; touch();}
92d6986d
AJ
65 const SBuf &userInfo() const {return userInfo_;}
66
5c51bffb
AJ
67 void host(const char *src);
68 const char *host(void) const {return host_;}
69 int hostIsNumeric(void) const {return hostIsNumeric_;}
70 Ip::Address const & hostIP(void) const {return hostAddr_;}
71
72 void port(unsigned short p) {port_=p; touch();}
73 unsigned short port() const {return port_;}
74
51b5dcf5
AJ
75 void path(const char *p) {path_=p; touch();}
76 void path(const SBuf &p) {path_=p; touch();}
77 const SBuf &path() const;
78
79 /// the static '/' default URL-path
80 static const SBuf &SlashPath();
81
2e260208
AJ
82 /// the static '*' pseudo-URL
83 static const SBuf &Asterisk();
84
5c51bffb
AJ
85 /**
86 * The authority-form URI for currently stored values.
87 *
88 * As defined by RFC 7230 section 5.3.3 this form omits the
89 * userinfo@ field from RFC 3986 defined authority segment.
90 *
91 * \param requirePort when true the port will be included, otherwise
92 * port will be elided when it is the default for
93 * the current scheme.
94 */
95 SBuf &authority(bool requirePort = false) const;
96
c823e2da
AJ
97 /**
98 * The absolute-form URI for currently stored values.
99 *
100 * As defined by RFC 7230 section 5.3.3 this form omits the
101 * userinfo@ field from RFC 3986 defined authority segments
102 * when the protocol scheme is http: or https:.
103 */
104 SBuf &absolute() const;
105
985c86bc 106private:
63be0a78 107 /**
108 \par
109 * The scheme of this URL. This has the 'type code' smell about it.
26ac0430
AJ
110 * In future we may want to make the methods that dispatch based on
111 * the scheme virtual and have a class per protocol.
63be0a78 112 \par
113 * On the other hand, having Protocol as an explicit concept is useful,
985c86bc 114 * see for instance the ACLProtocol acl type. One way to represent this
26ac0430 115 * is to have one prototype URL with no host etc for each scheme,
985c86bc 116 * another is to have an explicit scheme class, and then each URL class
26ac0430 117 * could be a subclass of the scheme. Another way is one instance of
1ca54a54 118 * a AnyP::UriScheme class instance for each URL scheme we support, and one URL
985c86bc 119 * class for each manner of treating the scheme : a Hierarchical URL, a
63be0a78 120 * non-hierarchical URL etc.
121 \par
985c86bc 122 * Deferring the decision, its a type code for now. RBC 20060507.
63be0a78 123 \par
26ac0430 124 * In order to make taking any of these routes easy, scheme is private
985c86bc 125 * and immutable, only settable at construction time,
126 */
4e3f4dc7 127 AnyP::UriScheme scheme_;
92d6986d
AJ
128
129 SBuf userInfo_; // aka 'URL-login'
5c51bffb
AJ
130
131 // XXX: uses char[] instead of SBUf to reduce performance regressions
132 // from c_str() since most code using this is not yet using SBuf
133 char host_[SQUIDHOSTNAMELEN]; ///< string representation of the URI authority name or IP
134 bool hostIsNumeric_; ///< whether the authority 'host' is a raw-IP
135 Ip::Address hostAddr_; ///< binary representation of the URI authority if it is a raw-IP
136
137 unsigned short port_; ///< URL port
138
51b5dcf5
AJ
139 // XXX: for now includes query-string.
140 SBuf path_; ///< URL path segment
141
5c51bffb
AJ
142 // pre-assembled URL forms
143 mutable SBuf authorityHttp_; ///< RFC 7230 section 5.3.3 authority, maybe without default-port
144 mutable SBuf authorityWithPort_; ///< RFC 7230 section 5.3.3 authority with explicit port
c823e2da 145 mutable SBuf absolute_; ///< RFC 7230 section 5.3.2 absolute-URI
985c86bc 146};
147
51b5dcf5
AJ
148inline std::ostream &
149operator <<(std::ostream &os, const URL &url)
150{
d31d59d8
AJ
151 // none means explicit empty string for scheme.
152 if (url.getScheme() != AnyP::PROTO_NONE)
153 os << url.getScheme().image();
154 os << ":";
155
156 // no authority section on URN
157 if (url.getScheme() != AnyP::PROTO_URN)
158 os << "//" << url.authority();
159
160 // path is what it is - including absent
161 os << url.path();
51b5dcf5
AJ
162 return os;
163}
164
fc54b8d2
FC
165class HttpRequest;
166class HttpRequestMethod;
167
8a648e8d
FC
168void urlInitialize(void);
169HttpRequest *urlParse(const HttpRequestMethod&, char *, HttpRequest *request = NULL);
8a648e8d
FC
170char *urlCanonicalClean(const HttpRequest *);
171const char *urlCanonicalFakeHttps(const HttpRequest * request);
172bool urlIsRelative(const char *);
173char *urlMakeAbsolute(const HttpRequest *, const char *);
174char *urlRInternal(const char *host, unsigned short port, const char *dir, const char *name);
175char *urlInternal(const char *dir, const char *name);
69f69080
CT
176
177/**
178 * matchDomainName() compares a hostname (usually extracted from traffic)
179 * with a domainname (usually from an ACL) according to the following rules:
180 *
181 * HOST | DOMAIN | MATCH?
182 * -------------|-------------|------
183 * foo.com | foo.com | YES
184 * .foo.com | foo.com | YES
185 * x.foo.com | foo.com | NO
186 * foo.com | .foo.com | YES
187 * .foo.com | .foo.com | YES
188 * x.foo.com | .foo.com | YES
189 *
190 * We strip leading dots on hosts (but not domains!) so that
191 * ".foo.com" is always the same as "foo.com".
192 *
193 * if honorWildcards is true then the matchDomainName() also accepts
194 * optional wildcards on hostname:
195 *
196 * HOST | DOMAIN | MATCH?
197 * -------------|--------------|-------
198 * *.foo.com | x.foo.com | YES
199 * *.foo.com | .x.foo.com | YES
200 * *.foo.com | .foo.com | YES
201 * *.foo.com | foo.com | NO
202 *
203 * \retval 0 means the host matches the domain
204 * \retval 1 means the host is greater than the domain
205 * \retval -1 means the host is less than the domain
206 */
207int matchDomainName(const char *host, const char *domain, bool honorWildcards = false);
8a648e8d 208int urlCheckRequest(const HttpRequest *);
8a648e8d
FC
209char *urlHostname(const char *url);
210void urlExtMethodConfigure(void);
fc54b8d2 211
985c86bc 212#endif /* SQUID_SRC_URL_H_H */
f53969cc 213