20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
28 #define a_zA_Z "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 #define RX_VALID_SCHEME "^[" a_zA_Z "][" a_zA_Z "0-9\\.+-]*$"
38 #define RX_VALID_PORT "^[0-9]{1,5}$"
40 #define RX_VALID_HOSTNAME "^[[:alnum:]]+([\\.-][[:alnum:]]+)*$"
42 #define RX_VALID_HOSTIPV4 \
43 "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$"
45 #define RX_VALID_HOSTIPV6 \
46 "^\\[[:a-fA-F0-9]+(:[0-9]{1,3}(\\.[0-9]{1,3}){3})?\\]$"
146 checkUrlData(
const std::string &data,
147 const std::string &name,
148 const std::string ®x,
151 if( regx.empty() || regx ==
"^$")
154 str::form(
_(
"Url scheme does not allow a %s"), name.c_str())
174 name.c_str(), data.c_str())
180 str::form(
_(
"Invalid %s component"), name.c_str())
215 const std::string &authority,
216 const std::string &pathdata,
217 const std::string &querystr,
218 const std::string &fragment)
222 init(scheme, authority, pathdata, querystr, fragment);
229 const std::string &authority,
230 const std::string &pathdata,
231 const std::string &querystr,
232 const std::string &fragment)
246 config(
"sep_pathparams",
";");
247 config(
"psep_pathparam",
",");
248 config(
"vsep_pathparam",
"=");
250 config(
"psep_querystr",
"&");
251 config(
"vsep_querystr",
"=");
253 config(
"safe_username",
"~!$&'()*+=,;");
254 config(
"safe_password",
"~!$&'()*+=,:;");
255 config(
"safe_hostname",
"[:]");
256 config(
"safe_pathname",
"~!$&'()*+=,:@/");
257 config(
"safe_pathparams",
"~!$&'()*+=,:;@/");
258 config(
"safe_querystr",
"~!$&'()*+=,:;@/?");
259 config(
"safe_fragment",
"~!$&'()*+=,:;@/?");
263 config(
"with_authority",
"y");
269 config(
"require_host",
"n");
270 config(
"require_pathname",
"n");
274 config(
"path_encode_slash2",
"n");
276 config(
"rx_username",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,;~\\._-]|%[a-fA-F0-9]{2})+$");
277 config(
"rx_password",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:;~\\._-]|%[a-fA-F0-9]{2})+$");
279 config(
"rx_pathname",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:@/~\\._-]|%[a-fA-F0-9]{2})+$");
280 config(
"rx_pathparams",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:;@/~\\._-]|%[a-fA-F0-9]{2})+$");
282 config(
"rx_querystr",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:;@/?~\\._-]|%[a-fA-F0-9]{2})+$");
283 config(
"rx_fragment",
"^([" a_zA_Z "0-9!$&'\\(\\)*+=,:;@/?~\\._-]|%[a-fA-F0-9]{2})+$");
303 return std::string();
357 UrlSchemes::const_iterator s;
359 for(s=schemes.begin(); s!=schemes.end(); ++s)
389 UrlSchemes::const_iterator s;
390 for(s=schemes.begin(); s!=schemes.end(); ++s)
414 if( host.empty() &&
config(
"require_host") !=
"n")
418 if( path.empty() &&
config(
"require_pathname") !=
"n")
425 if( !host.empty() && !path.empty() && path.at(0) !=
'/')
457 if( !tmp.
host.empty())
464 if( !tmp.
user.empty())
471 if( !tmp.
pass.empty())
473 url +=
":" + tmp.
pass;
485 if( !tmp.
port.empty())
487 url +=
":" + tmp.
port;
508 if(url.find(
"/") != std::string::npos)
534 && url.find(
"/") != std::string::npos)
614 config(
"sep_pathparams") +
703 if(
config(
"psep_pathparam").empty())
723 if(
config(
"psep_pathparam").empty() ||
724 config(
"vsep_pathparam").empty())
727 "Path parameter parsing not supported for this URL"
747 zypp::url::ParamMap::const_iterator i( pmap.find(param));
749 return i != pmap.end() ? i->second : std::string();
758 if(
config(
"psep_querystr").empty())
778 if(
config(
"psep_querystr").empty() ||
779 config(
"vsep_querystr").empty())
782 _(
"Query string parsing not supported for this URL")
802 zypp::url::ParamMap::const_iterator i( pmap.find(param));
804 return i != pmap.end() ? i->second : std::string();
820 _(
"Url scheme is a required component")
826 str::form(
_(
"Invalid Url scheme '%s'"), scheme.c_str())
836 std::string s = authority;
839 std::string username, password, host, port;
841 if ((p=s.find(
'@')) != std::string::npos)
844 if (q != std::string::npos && q < p)
853 if ((p = s.rfind(
':')) != std::string::npos && ( (q = s.rfind(
']')) == std::string::npos || q < p) )
866 size_t pos = std::string::npos;
867 std::string sep(
config(
"sep_pathparams"));
870 pos = pathdata.find(sep);
872 if( pos != std::string::npos)
891 if( querystr.empty())
897 checkUrlData(querystr,
"query string",
config(
"rx_querystr"));
909 if( fragment.empty())
917 checkUrlData(fragment,
"fragment",
config(
"rx_fragment"));
924 fragment,
config(
"safe_fragment")
942 if(
config(
"with_authority") !=
"y")
945 _(
"Url scheme does not allow a username")
951 checkUrlData(user,
"username",
config(
"rx_username"));
958 user,
config(
"safe_username")
976 if(
config(
"with_authority") !=
"y")
979 _(
"Url scheme does not allow a password")
985 checkUrlData(pass,
"password",
config(
"rx_password"),
false);
992 pass,
config(
"safe_password")
1005 if(
config(
"require_host") ==
"m")
1008 _(
"Url scheme requires a host component")
1015 if(
config(
"with_authority") !=
"y")
1018 _(
"Url scheme does not allow a host component")
1029 if( host.at(0) ==
'[')
1039 temp,
config(
"safe_hostname")
1045 str::form(
_(
"Invalid host component '%s'"), host.c_str())
1062 if(
config(
"with_authority") !=
"y" ||
1063 config(
"with_port") !=
"y")
1066 _(
"Url scheme does not allow a port")
1077 str::form(
_(
"Invalid port component '%s'"), port.c_str())
1091 if(
config(
"require_pathname") ==
"m")
1094 _(
"Url scheme requires path name")
1103 checkUrlData(path,
"path name",
config(
"rx_pathname"));
1111 if(!(path.at(0) ==
'/' || (path.size() >= 3 &&
1115 _(
"Relative path not allowed if authority exists")
1126 if(path.at(0) !=
'/')
1129 _(
"Relative path not allowed if authority exists")
1136 path,
config(
"safe_pathname")
1154 checkUrlData(params,
"path parameters",
config(
"rx_pathparams"));
1178 if(
config(
"psep_pathparam").empty() ||
1179 config(
"vsep_pathparam").empty())
1182 "Path Parameter parsing not supported for this URL"
1188 config(
"psep_pathparam"),
1189 config(
"vsep_pathparam"),
1190 config(
"safe_pathparams")
1201 pmap[param] = value;
1223 if(
config(
"psep_querystr").empty() ||
1224 config(
"vsep_querystr").empty())
1227 _(
"Query string parsing not supported for this URL")
1245 pmap[param] = value;
1271 std::string
copy( path);
1274 if(copy.size() >= 3 && copy.at(0) !=
'/' &&
1277 copy.replace(0, 3,
"/");
1289 if(
config(
"path_encode_slash2") ==
"y")
1292 if(copy.size() >= 2 && copy.at(0) ==
'/' && copy.at(1) ==
'/')
1294 copy.replace(1, 1,
"%2F");
1300 if(copy.size() >= 4 && copy.at(0) ==
'/' &&
1303 copy.replace(1, 4,
"/");
1310 if(copy.size() >= 2 && copy.at(0) ==
'/' && copy.at(1) ==
'/')
1312 copy.replace(1, 1,
"%2F");
1329 std::string temp( host.substr(1, host.size()-2));
1331 return inet_pton(AF_INET6, temp.c_str(), &ip) > 0;
1357 long pnum = str::strtonum<long>(port);
1358 return ( pnum >= 1 && pnum <= USHRT_MAX);
bool has(const ViewOption &o) const
Check if specified option o is set in the current object.
virtual std::string getQueryParam(const std::string ¶m, EEncoding eflag) const
Return the value for the specified query parameter.
static const ViewOption WITH_USERNAME
Option to include username in the URL string.
virtual std::string getQueryString() const
Returns the encoded query string component of the URL.
std::vector< std::string > ParamVec
A parameter vector container.
static const ViewOption WITH_FRAGMENT
Option to include fragment string in the URL string.
std::map< std::string, std::string > ParamMap
A parameter map container.
Internal data used by UrlBase.
virtual std::string getPathParam(const std::string ¶m, EEncoding eflag) const
Return the value for the specified path parameter.
ViewOptions getViewOptions() const
Return the view options of the current object.
virtual void setQueryParam(const std::string ¶m, const std::string &value)
Set or add value for the specified query parameter.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
#define RX_VALID_HOSTNAME
std::vector< std::string > UrlSchemes
Vector of URL scheme names.
Flag to request encoded string(s).
ViewOption()
Create instance with default combination of view options.
virtual void setUsername(const std::string &user, EEncoding eflag)
Set the username in the URL authority.
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.
virtual void setPathData(const std::string &pathdata)
Set the path data component in the URL.
virtual std::string getPathData() const
Returns the encoded path component of the URL.
std::string encode(const std::string &str, const std::string &safe, EEncoding eflag)
Encodes a string using URL percent encoding.
virtual std::string asString() const
Returns a default string representation of the Url object.
virtual std::string getUsername(EEncoding eflag) const
Returns the username from the URL authority.
UrlBaseData(const UrlConfig &conf)
static const ViewOption EMPTY_FRAGMENT
Explicitely include the fragment string separator "#".
virtual void setPort(const std::string &port)
Set the port number in the URL authority.
static const ViewOption WITH_PATH_NAME
Option to include path name in the URL string.
Url::asString() view options.
void setViewOptions(const ViewOptions &vopts)
Change the view options of the current object.
virtual void setAuthority(const std::string &authority)
Set the authority component in the URL.
virtual std::string getScheme() const
Returns the scheme name of the URL.
virtual void setHost(const std::string &host)
Set the hostname or IP in the URL authority.
virtual void setPathParams(const std::string ¶ms)
Set the path parameters.
virtual void setPathParamsVec(const zypp::url::ParamVec &pvec)
Set the path parameters.
std::map< std::string, std::string > UrlConfig
virtual void setFragment(const std::string &fragment, EEncoding eflag)
Set the fragment string in the URL.
virtual void setPassword(const std::string &pass, EEncoding eflag)
Set the password in the URL authority.
virtual zypp::url::ParamMap getQueryStringMap(EEncoding eflag) const
Returns a string map with query parameter and their values.
virtual void setQueryStringMap(const zypp::url::ParamMap &qmap)
Set the query parameters.
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.
static const ViewOption EMPTY_QUERY_STR
Explicitely include the query string separator "?".
static const ViewOption EMPTY_AUTHORITY
Explicitely include the URL authority separator "//".
virtual zypp::url::ParamVec getPathParamsVec() const
Returns a vector with encoded path parameter substrings.
void split(ParamVec &pvec, const std::string &pstr, const std::string &psep)
Split into a parameter vector.
static const ViewOption WITH_PATH_PARAMS
Option to include path parameters in the URL string.
virtual void setScheme(const std::string &scheme)
Set the scheme name in the URL.
virtual bool isValid() const
Verifies the Url.
std::string join(const ParamVec &pvec, const std::string &psep)
Join parameter vector to a string.
#define _(MSG)
Return translated text.
virtual void setQueryString(const std::string &querystr)
Set the query string in the URL.
virtual std::string getPathParams() const
Returns the encoded path parameters from the URL.
static const ViewOption EMPTY_PATH_PARAMS
Explicitely include the path parameters separator ";".
std::string toLower(const std::string &s)
Return lowercase version of s.
virtual zypp::url::ParamVec getQueryStringVec() const
Returns a vector with query string parameter substrings.
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
virtual void clear()
Clears all data in the object.
virtual void setPathName(const std::string &path, EEncoding eflag)
Set the path name.
std::string form(const char *format,...)
Printf style construction of std::string.
static const ViewOption WITH_QUERY_STR
Option to include query string in the URL string.
virtual std::string getPort() const
Returns the port number from the URL authority.
static const ViewOption EMPTY_PATH_NAME
Explicitely include the "/" path character.
static const ViewOption WITH_PASSWORD
Option to include password in the URL string.
virtual std::string cleanupPathName(const std::string &path, bool authority) const
Utility method to cleanup an encoded path name.
virtual std::string getAuthority() const
Returns the encoded authority component of the URL.
virtual zypp::url::ParamMap getPathParamsMap(EEncoding eflag) const
Returns a string map with path parameter keys and values.
virtual void configure()
Configures behaviour of the instance.
virtual std::string getHost(EEncoding eflag) const
Returns the hostname or IP from the URL authority.
virtual bool isValidScheme(const std::string &scheme) const
Verifies specified scheme name.
virtual std::string getFragment(EEncoding eflag) const
Returns the encoded fragment component of the URL.
virtual std::string getPassword(EEncoding eflag) const
Returns the password from the URL authority.
virtual void setPathParamsMap(const zypp::url::ParamMap &pmap)
Set the path parameters.
virtual bool isValidHost(const std::string &host) const
Verifies specified host or IP.
bool regex_match(const std::string &s, smatch &matches, const regex ®ex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
static const ViewOption DEFAULTS
Default combination of view options.
virtual std::string getPathName(EEncoding eflag) const
Returns the path name from the URL.
virtual UrlSchemes getKnownSchemes() const
Returns scheme names known by this object.
#define RX_VALID_HOSTIPV6
virtual bool isValidPort(const std::string &port) const
Verifies specified port number.
static const ViewOption WITH_PORT
Option to include port number in the URL string.
virtual void setPathParam(const std::string ¶m, const std::string &value)
Set or add value for the specified path parameter.
virtual void setQueryStringVec(const zypp::url::ParamVec &qvec)
Set the query parameters.
std::string toUpper(const std::string &s)
Return uppercase version of s.
Thrown if a url component is invalid.
virtual bool isKnownScheme(const std::string &scheme) const
Returns if scheme name is known to this object.
virtual UrlBase * clone() const
Returns pointer to a copy of the current object.
std::string config(const std::string &opt) const
Get the value of a UrlBase configuration variable.
Thrown if scheme does not allow a component.
virtual void delQueryParam(const std::string ¶m)
remove the specified query parameter.
std::string decode(const std::string &str, bool allowNUL)
Decodes a URL percent encoded string.
Flag to request decoded string(s).