Uri.h
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#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
20
21namespace AnyP
22{
23
30class Uri
31{
33
34public:
35 Uri(): hostIsNumeric_(false) { *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_;
43 memcpy(host_, o.host_, sizeof(host_));
46 port_ = o.port_;
47 path_ = o.path_;
48 touch();
49 return *this;
50 }
51
52 void clear() {
54 hostIsNumeric_ = false;
55 *host_ = 0;
57 port_ = std::nullopt;
58 touch();
59 }
60 void touch();
61
62 bool parse(const HttpRequestMethod &, const SBuf &url);
63
65 static char *cleanup(const char *uri);
66
67 AnyP::UriScheme const & getScheme() const {return scheme_;}
68
70 void setScheme(const AnyP::ProtocolType &p, const char *str) {
71 scheme_ = AnyP::UriScheme(p, str);
72 touch();
73 }
74 void setScheme(const AnyP::UriScheme &s) {
75 scheme_ = s;
76 touch();
77 }
78
79 void userInfo(const SBuf &s) {userInfo_=s; touch();}
82 const SBuf &userInfo() const {return userInfo_;}
83
84 void host(const char *src);
85 const char *host(void) const {return host_;}
86 int hostIsNumeric(void) const {return hostIsNumeric_;}
87 Ip::Address const & hostIP(void) const {return hostAddr_;}
88
92 SBuf hostOrIp() const;
93
95 void port(const Port p) { port_ = p; touch(); }
97 Port port() const { return port_; }
100
101 void path(const char *p) {path_=p; touch();}
102 void path(const SBuf &p) {path_=p; touch();}
103 const SBuf &path() const;
104
114 void addRelativePath(const char *relUrl);
115
117 static const SBuf &SlashPath();
118
120 static const SBuf &Asterisk();
121
124 static SBuf Encode(const SBuf &, const CharacterSet &expected);
125
136 SBuf &authority(bool requirePort = false) const;
137
145 SBuf &absolute() const;
146
147private:
149
151 int parsePort(Parser::Tokenizer &) const;
152
174
175 SBuf userInfo_; // aka 'URL-login'
176
177 // XXX: uses char[] instead of SBUf to reduce performance regressions
178 // from c_str() since most code using this is not yet using SBuf
182
184
185 // XXX: for now includes query-string.
187
188 // pre-assembled URI forms
191 mutable SBuf absolute_;
192};
193
194inline std::ostream &
195operator <<(std::ostream &os, const Uri &url)
196{
197 // none means explicit empty string for scheme.
198 if (url.getScheme() != PROTO_NONE)
199 os << url.getScheme().image();
200 os << ":";
201
202 // no authority section on URN
203 if (url.getScheme() != PROTO_URN)
204 os << "//" << url.authority();
205
206 // path is what it is - including absent
207 os << url.path();
208 return os;
209}
210
211} // namespace AnyP
212
213/* Deprecated functions for Legacy code handling URLs */
214
215class HttpRequest;
216
217void urlInitialize(void);
222const char *urlCanonicalFakeHttps(const HttpRequest * request);
223bool urlIsRelative(const char *);
224char *urlRInternal(const char *host, unsigned short port, const char *dir, const char *name);
225char *urlInternal(const char *dir, const char *name);
226bool urlAppendDomain(char *host);
227
233
267int matchDomainName(const char *host, const char *domain, MatchDomainNameFlags flags = mdnNone);
268bool urlCheckRequest(const HttpRequest *);
270
271#endif /* SQUID_SRC_ANYP_URI_H */
272
char * urlRInternal(const char *host, unsigned short port, const char *dir, const char *name)
bool urlIsRelative(const char *)
Definition: Uri.cc:760
MatchDomainNameFlags
Definition: Uri.h:228
@ mdnNone
Definition: Uri.h:229
@ mdnRejectSubsubDomains
Definition: Uri.h:231
@ mdnHonorWildcards
Definition: Uri.h:230
void urlInitialize(void)
Definition: Uri.cc:138
char * urlInternal(const char *dir, const char *name)
int matchDomainName(const char *host, const char *domain, MatchDomainNameFlags flags=mdnNone)
Definition: Uri.cc:820
const char * urlCanonicalFakeHttps(const HttpRequest *request)
Definition: Uri.cc:733
bool urlCheckRequest(const HttpRequest *)
Definition: Uri.cc:919
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition: Uri.cc:223
char * urlCanonicalCleanWithoutRequest(const SBuf &url, const HttpRequestMethod &, const AnyP::UriScheme &)
Definition: Uri.cc:704
void urlExtMethodConfigure(void)
SBuf image() const
Definition: UriScheme.h:57
Definition: Uri.h:31
static const SBuf & SlashPath()
the static '/' default URL-path
Definition: Uri.cc:93
Port port() const
authority port subcomponent
Definition: Uri.h:97
SBuf parseHost(Parser::Tokenizer &) const
Definition: Uri.cc:558
void parseUrn(Parser::Tokenizer &)
Definition: Uri.cc:527
AnyP::UriScheme const & getScheme() const
Definition: Uri.h:67
void touch()
clear the cached URI display forms
Definition: Uri.cc:638
const SBuf & userInfo() const
Definition: Uri.h:82
void clear()
Definition: Uri.h:52
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:646
Uri & operator=(const Uri &o)
Definition: Uri.h:40
SBuf authorityHttp_
RFC 7230 section 5.3.3 authority, maybe without default-port.
Definition: Uri.h:189
void setScheme(const AnyP::ProtocolType &p, const char *str)
convert the URL scheme to that given
Definition: Uri.h:70
void path(const char *p)
Definition: Uri.h:101
Ip::Address hostAddr_
binary representation of the URI authority if it is a raw-IP
Definition: Uri.h:181
char host_[SQUIDHOSTNAMELEN]
string representation of the URI authority name or IP
Definition: Uri.h:179
SBuf userInfo_
Definition: Uri.h:175
const char * host(void) const
Definition: Uri.h:85
Uri()
Definition: Uri.h:35
SBuf path_
URI path segment.
Definition: Uri.h:186
bool hostIsNumeric_
whether the authority 'host' is a raw-IP
Definition: Uri.h:180
void defaultPort()
reset the port to the default port number for the current scheme
Definition: Uri.h:99
MEMPROXY_CLASS(Uri)
SBuf authorityWithPort_
RFC 7230 section 5.3.3 authority with explicit port.
Definition: Uri.h:190
static char * cleanup(const char *uri)
Definition: Uri.cc:993
void addRelativePath(const char *relUrl)
Definition: Uri.cc:796
void path(const SBuf &p)
Definition: Uri.h:102
Port port_
authority port subcomponent
Definition: Uri.h:183
void setScheme(const AnyP::UriScheme &s)
Definition: Uri.h:74
int parsePort(Parser::Tokenizer &) const
Definition: Uri.cc:616
SBuf & absolute() const
Definition: Uri.cc:668
static const SBuf & Asterisk()
the static '*' pseudo-URI
Definition: Uri.cc:86
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:95
const SBuf & path() const
Definition: Uri.cc:126
bool parse(const HttpRequestMethod &, const SBuf &url)
Definition: Uri.cc:252
SBuf absolute_
RFC 7230 section 5.3.2 absolute-URI.
Definition: Uri.h:191
Ip::Address const & hostIP(void) const
Definition: Uri.h:87
AnyP::UriScheme scheme_
Definition: Uri.h:173
int hostIsNumeric(void) const
Definition: Uri.h:86
SBuf hostOrIp() const
Definition: Uri.cc:115
Uri(const Uri &other)
Definition: Uri.h:37
static SBuf Encode(const SBuf &, const CharacterSet &expected)
Definition: Uri.cc:57
void userInfo(const SBuf &s)
Definition: Uri.h:79
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:18
RequestFlags flags
Definition: HttpRequest.h:141
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
void setEmpty()
Fast reset of the stored content to what would be after default constructor.
Definition: Address.cc:184
Definition: SBuf.h:94
static int port
Definition: ldap_backend.cc:70
Definition: forward.h:15
std::optional< KnownPort > Port
validated/supported port number (if any)
Definition: UriScheme.h:26
std::ostream & operator<<(std::ostream &os, ProtocolType const &p)
Definition: ProtocolType.h:52
ProtocolType
Definition: ProtocolType.h:23
@ PROTO_NONE
Definition: ProtocolType.h:24
@ PROTO_URN
Definition: ProtocolType.h:35
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors