Go to the documentation of this file.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
30 #define a_zA_Z "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
38 #define RX_VALID_SCHEME "^[" a_zA_Z "][" a_zA_Z "0-9\\.+-]*$"
40 #define RX_VALID_PORT "^[0-9]{1,5}$"
42 #define RX_VALID_HOSTNAME "^[[:alnum:]${_}]+([\\.-][[:alnum:]${_}]+)*$"
44 #define RX_VALID_HOSTIPV4 \
45 "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"
47 #define RX_VALID_HOSTIPV6 \
48 "^\\[[:a-fA-F0-9]+(:[0-9]{1,3}(\\.[0-9]{1,3}){3})?\\]$"
131 {
_assign( std::move(rhs) );
return *
this; }
134 operator const std::string &()
const
137 const std::string &
str()
const
154 static constexpr std::string_view tag {
"proxypass=" };
159 if ( val.substr( 0, tag.size() ) != tag ) {
160 if ( ! safe.empty() )
212 checkUrlData(
const std::string &data,
213 const std::string &name,
214 const std::string ®x,
217 if( regx.empty() || regx ==
"^$")
220 str::form(
_(
"Url scheme does not allow a %s"), name.c_str())
240 name.c_str(), data.c_str())
246 str::form(
_(
"Invalid %s component"), name.c_str())
281 const std::string &authority,
282 const std::string &pathdata,
283 const std::string &querystr,
284 const std::string &fragment)
288 init(scheme, authority, pathdata, querystr, fragment);
295 const std::string &authority,
296 const std::string &pathdata,
297 const std::string &querystr,
298 const std::string &fragment)
300 if ( scheme.empty() && *pathdata.c_str() ==
'/' )
316 config(
"sep_pathparams",
";");
317 config(
"psep_pathparam",
",");
318 config(
"vsep_pathparam",
"=");
320 config(
"psep_querystr",
"&");
321 config(
"vsep_querystr",
"=");
323 config(
"safe_username",
"~!$&'()*+=,;");
324 config(
"safe_password",
"~!$&'()*+=,:;");
325 config(
"safe_hostname",
"[:]${_}");
326 config(
"safe_pathname",
"~!$&'()*+=,:@/");
327 config(
"safe_pathparams",
"~!$&'()*+=,:;@/");
328 config(
"safe_querystr",
"~!$&'()*+=,:;@/?");
329 config(
"safe_fragment",
"~!$&'()*+=,:;@/?");
333 config(
"with_authority",
"y");
339 config(
"require_host",
"n");
340 config(
"require_pathname",
"n");
344 config(
"path_encode_slash2",
"n");
346 config(
"rx_username",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,;~\\._-]|%[a-fA-F0-9]{2})+$");
347 config(
"rx_password",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:;~\\._-]|%[a-fA-F0-9]{2})+$");
349 config(
"rx_pathname",
"^([" a_zA_Z "0-9!$&'\\(\\){}*+=,:@/~\\._-]|%[a-fA-F0-9]{2})+$");
350 config(
"rx_pathparams",
"^([" a_zA_Z "0-9!$&'\\(\\){}*+=,:;@/~\\._-]|%[a-fA-F0-9]{2})+$");
352 config(
"rx_querystr",
"^([" a_zA_Z "0-9!$&'\\(\\){}*+=,:;@/?~\\._-]|%[a-fA-F0-9]{2})+$");
353 config(
"rx_fragment",
"^([" a_zA_Z "0-9!$&'\\(\\){}*+=,:;@/?~\\._-]|%[a-fA-F0-9]{2})+$");
373 return std::string();
427 UrlSchemes::const_iterator s;
429 for(s=schemes.begin(); s!=schemes.end(); ++s)
459 UrlSchemes::const_iterator s;
460 for(s=schemes.begin(); s!=schemes.end(); ++s)
484 if( host.empty() &&
config(
"require_host") !=
"n")
488 if( path.empty() &&
config(
"require_pathname") !=
"n")
495 if( !host.empty() && !path.empty() && path.at(0) !=
'/')
533 if( !tmp.
host.empty())
540 if( !tmp.
user.empty())
547 if( !tmp.
pass.empty())
561 if( !tmp.
port.empty())
584 if(
url.find(
"/") != std::string::npos)
610 &&
url.find(
"/") != std::string::npos)
623 if( !querystr.empty() )
625 url +=
"?" + querystr;
690 config(
"sep_pathparams") +
784 if(
config(
"psep_pathparam").empty())
804 if(
config(
"psep_pathparam").empty() ||
805 config(
"vsep_pathparam").empty())
808 "Path parameter parsing not supported for this URL"
828 zypp::url::ParamMap::const_iterator i( pmap.find(param));
830 return i != pmap.end() ? i->second : std::string();
839 if(
config(
"psep_querystr").empty())
859 if(
config(
"psep_querystr").empty() ||
860 config(
"vsep_querystr").empty())
863 _(
"Query string parsing not supported for this URL")
883 zypp::url::ParamMap::const_iterator i( pmap.find(param));
885 return i != pmap.end() ? i->second : std::string();
901 _(
"Url scheme is a required component")
907 str::form(
_(
"Invalid Url scheme '%s'"), scheme.c_str())
917 std::string s = authority;
920 std::string username, password, host, port;
922 if ((p=s.find(
'@')) != std::string::npos)
925 if (q != std::string::npos && q < p)
934 if ((p = s.rfind(
':')) != std::string::npos && ( (q = s.rfind(
']')) == std::string::npos || q < p) )
947 size_t pos = std::string::npos;
948 std::string sep(
config(
"sep_pathparams"));
951 pos = pathdata.find(sep);
953 if( pos != std::string::npos)
972 if( querystr.empty())
978 checkUrlData(querystr,
"query string",
config(
"rx_querystr"));
990 if( fragment.empty())
998 checkUrlData(fragment,
"fragment",
config(
"rx_fragment"));
1005 fragment,
config(
"safe_fragment")
1023 if(
config(
"with_authority") !=
"y")
1026 _(
"Url scheme does not allow a username")
1032 checkUrlData(user,
"username",
config(
"rx_username"));
1039 user,
config(
"safe_username")
1057 if(
config(
"with_authority") !=
"y")
1060 _(
"Url scheme does not allow a password")
1066 checkUrlData(pass,
"password",
config(
"rx_password"),
false);
1073 pass,
config(
"safe_password")
1086 if(
config(
"require_host") ==
"m")
1089 _(
"Url scheme requires a host component")
1096 if(
config(
"with_authority") !=
"y")
1099 _(
"Url scheme does not allow a host component")
1110 if( host.at(0) ==
'[')
1120 temp,
config(
"safe_hostname")
1126 str::form(
_(
"Invalid host component '%s'"), host.c_str())
1143 if(
config(
"with_authority") !=
"y" ||
1144 config(
"with_port") !=
"y")
1147 _(
"Url scheme does not allow a port")
1158 str::form(
_(
"Invalid port component '%s'"), port.c_str())
1172 if(
config(
"require_pathname") ==
"m")
1175 _(
"Url scheme requires path name")
1184 checkUrlData(path,
"path name",
config(
"rx_pathname"));
1192 if(!(path.at(0) ==
'/' || (path.size() >= 3 &&
1196 _(
"Relative path not allowed if authority exists")
1207 if(path.at(0) !=
'/')
1210 _(
"Relative path not allowed if authority exists")
1217 path,
config(
"safe_pathname")
1235 checkUrlData(params,
"path parameters",
config(
"rx_pathparams"));
1259 if(
config(
"psep_pathparam").empty() ||
1260 config(
"vsep_pathparam").empty())
1263 "Path Parameter parsing not supported for this URL"
1269 config(
"psep_pathparam"),
1270 config(
"vsep_pathparam"),
1271 config(
"safe_pathparams")
1282 pmap[param] = value;
1304 if(
config(
"psep_querystr").empty() ||
1305 config(
"vsep_querystr").empty())
1308 _(
"Query string parsing not supported for this URL")
1326 pmap[param] = value;
1352 std::string
copy( path);
1355 if(
copy.size() >= 3 &&
copy.at(0) !=
'/' &&
1358 copy.replace(0, 3,
"/");
1370 if(
config(
"path_encode_slash2") ==
"y")
1373 if(
copy.size() >= 2 &&
copy.at(0) ==
'/' &&
copy.at(1) ==
'/')
1375 copy.replace(1, 1,
"%2F");
1381 if(
copy.size() >= 4 &&
copy.at(0) ==
'/' &&
1384 copy.replace(1, 4,
"/");
1391 if(
copy.size() >= 2 &&
copy.at(0) ==
'/' &&
copy.at(1) ==
'/')
1393 copy.replace(1, 1,
"%2F");
1410 std::string temp( host.substr(1, host.size()-2));
1412 return inet_pton(AF_INET6, temp.c_str(), &ip) > 0;
1438 long pnum = str::strtonum<long>(port);
1439 return ( pnum >= 1 && pnum <= USHRT_MAX);
virtual std::string getPassword(EEncoding eflag) const
Returns the password from the URL authority.
std::string config(const std::string &opt) const
Get the value of a UrlBase configuration variable.
virtual void setQueryString(const std::string &querystr)
Set the query string in the URL.
virtual void setPathName(const std::string &path, EEncoding eflag)
Set the path name.
const std::string & str() const
virtual void setPathParam(const std::string ¶m, const std::string &value)
Set or add value for the specified path parameter.
virtual UrlBase * clone() const
Returns pointer to a copy of the current object.
std::map< std::string, std::string > UrlConfig
virtual bool isValid() const
Verifies the Url.
virtual std::string getPathParam(const std::string ¶m, EEncoding eflag) const
Return the value for the specified path parameter.
static const ViewOption hotfix1050625
virtual std::string getQueryParam(const std::string ¶m, EEncoding eflag) const
Return the value for the specified query parameter.
virtual void setPathParamsVec(const zypp::url::ParamVec &pvec)
Set the path parameters.
virtual std::string getPathParams() const
Returns the encoded path parameters from the URL.
virtual void setQueryStringMap(const zypp::url::ParamMap &qmap)
Set the query parameters.
std::string _fullQuerytsr
Internal data used by UrlBase.
@ E_DECODED
Flag to request decoded string(s).
std::vector< std::string > UrlSchemes
Vector of URL scheme names.
Thrown if scheme does not allow a component.
std::string decode(const std::string &str, bool allowNUL)
Decodes a URL percent encoded string.
virtual void setAuthority(const std::string &authority)
Set the authority component in the URL.
static const ViewOption EMPTY_PATH_NAME
Explicitely include the "/" path character.
virtual std::string getPort() const
Returns the port number from the URL authority.
virtual void clear()
Clears all data in the object.
static const ViewOption WITH_USERNAME
Option to include username in the URL string.
virtual UrlSchemes getKnownSchemes() const
Returns scheme names known by this object.
std::string asString1050625() const
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static const ViewOption WITH_FRAGMENT
Option to include fragment string in the URL string.
virtual std::string getPathData() const
Returns the encoded path component of the URL.
static const ViewOption WITH_PATH_NAME
Option to include path name in the URL string.
virtual bool isKnownScheme(const std::string &scheme) const
Returns if scheme name is known to this object.
static const ViewOption EMPTY_PATH_PARAMS
Explicitely include the path parameters separator ";".
ViewOption()
Create instance with default combination of view options.
#define RX_VALID_HOSTIPV6
virtual std::string asString() const
Returns a default string representation of the Url object.
std::optional< std::string > _safeQuerytsr
static const ViewOption EMPTY_QUERY_STR
Explicitely include the query string separator "?".
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
static const ViewOption EMPTY_FRAGMENT
Explicitely include the fragment string separator "#".
static const ViewOption WITH_PASSWORD
Option to include password in the URL string.
virtual zypp::url::ParamMap getQueryStringMap(EEncoding eflag) const
Returns a string map with query parameter and their values.
virtual std::string getPathName(EEncoding eflag) const
Returns the path name from the URL.
virtual void configure()
Configures behaviour of the instance.
static const ViewOption DEFAULTS
Default combination of view options.
bool has(const ViewOption &o) const
Check if specified option o is set in the current object.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
static const ViewOption EMPTY_AUTHORITY
Explicitely include the URL authority separator "//".
virtual void setUsername(const std::string &user, EEncoding eflag)
Set the username in the URL authority.
virtual void setPathParamsMap(const zypp::url::ParamMap &pmap)
Set the path parameters.
Easy-to use interface to the ZYPP dependency resolver.
SafeQuerystr & operator=(std::string rhs)
virtual std::string getAuthority() const
Returns the encoded authority component of the URL.
virtual void setPathData(const std::string &pathdata)
Set the path data component in the URL.
std::string toLower(const std::string &s)
Return lowercase version of s.
virtual bool isValidHost(const std::string &host) const
Verifies specified host or IP.
std::map< std::string, std::string > ParamMap
A parameter map container.
virtual std::string getHost(EEncoding eflag) const
Returns the hostname or IP from the URL authority.
virtual zypp::url::ParamVec getQueryStringVec() const
Returns a vector with query string parameter substrings.
void split(ParamVec &pvec, const std::string &pstr, const std::string &psep)
Split into a parameter vector.
static const ViewOption WITH_QUERY_STR
Option to include query string in the URL string.
#define RX_VALID_HOSTNAME
virtual void init(const std::string &scheme, const std::string &authority, const std::string &pathdata, const std::string &querystr, const std::string &fragment)
Initializes current object with new URL components.
virtual void setQueryStringVec(const zypp::url::ParamVec &qvec)
Set the query parameters.
virtual bool isValidScheme(const std::string &scheme) const
Verifies specified scheme name.
std::string join(const ParamVec &pvec, const std::string &psep)
Join parameter vector to a string.
virtual zypp::url::ParamMap getPathParamsMap(EEncoding eflag) const
Returns a string map with path parameter keys and values.
virtual std::string getScheme() const
Returns the scheme name of the URL.
@ E_ENCODED
Flag to request encoded string(s).
std::vector< std::string > ParamVec
A parameter vector container.
virtual void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
virtual void setFragment(const std::string &fragment, EEncoding eflag)
Set the fragment string in the URL.
const std::string & fullStr() const
virtual void delQueryParam(const std::string ¶m)
remove the specified query parameter.
virtual zypp::url::ParamVec getPathParamsVec() const
Returns a vector with encoded path parameter substrings.
virtual void setScheme(const std::string &scheme)
Set the scheme name in the URL.
const std::string & str(const ViewOptions &viewopts_r) const
virtual std::string getUsername(EEncoding eflag) const
Returns the username from the URL authority.
const std::string & safeStr() const
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
SafeQuerystr(std::string rhs)
virtual void setPathParams(const std::string ¶ms)
Set the path parameters.
virtual void setQueryParam(const std::string ¶m, const std::string &value)
Set or add value for the specified query parameter.
virtual void setPort(const std::string &port)
Set the port number in the URL authority.
bool regex_match(const std::string &s, smatch &matches, const regex ®ex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
String related utilities and Regular expression matching.
virtual std::string cleanupPathName(const std::string &path, bool authority) const
Utility method to cleanup an encoded path name.
std::string toUpper(const std::string &s)
Return uppercase version of s.
std::string encode(const std::string &str, const std::string &safe, EEncoding eflag)
Encodes a string using URL percent encoding.
virtual bool isValidPort(const std::string &port) const
Verifies specified port number.
virtual std::string getQueryString() const
Returns the encoded query string component of the URL.
Url::asString() view options.
virtual std::string getFragment(EEncoding eflag) const
Returns the encoded fragment component of the URL.
static const ViewOption WITH_SCHEME
Option to include scheme name in the URL string.
static const ViewOption WITH_HOST
Option to include hostname in the URL string.
UrlBaseData(const UrlConfig &conf)
c++17: std::string_view tools
void _assign(std::string &&rhs)
Hide passwords embedded in a querystr,.
Thrown if a url component is invalid.
void setViewOptions(const ViewOptions &vopts)
Change the view options of the current object.
ViewOptions getViewOptions() const
Return the view options of the current object.
virtual void setPassword(const std::string &pass, EEncoding eflag)
Set the password in the URL authority.
static const ViewOption WITH_PORT
Option to include port number in the URL string.
static const ViewOption WITH_PATH_PARAMS
Option to include path parameters in the URL string.