1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
15 #include "zypp/url/UrlBase.h"
16 #include "zypp/url/UrlUtils.h"
19 //////////////////////////////////////////////////////////////////////
21 { ////////////////////////////////////////////////////////////////////
23 namespace filesystem {
26 using filesystem::Pathname;
30 * \brief Url manipulation class.
32 * The generic URL (URI) syntax and its main components are defined in
33 * RFC3986 (http://rfc.net/rfc3986.html) Section 3, "Syntax Components".
34 * The scheme specific URL syntax and semantics is defined in the
35 * specification of the particular scheme. See also RFC1738
36 * (http://rfc.net/rfc1738.html), that defines specific syntax for
37 * several URL schemes.
39 * This class provides methods to access and manipulate generic and
40 * common scheme-specific URL components (or using the more general
41 * term, URI components).
42 * To consider the scheme-specifics of a URL, the Url class contains
43 * a reference object pointing to a UrlBase or derived object, that
44 * implements the scheme specifics.
46 * Using the Url::registerScheme() method, it is possible to register
47 * a preconfigured or derived UrlBase object for a specific scheme
48 * name. The registered object will be cloned to handle all URL's
49 * containing the specified scheme name.
51 * \par RFC3986, Syntax Components:
53 * The generic URI syntax consists of a hierarchical sequence of
54 * components referred to as the scheme, authority, path, query,
58 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
60 * hier-part = "//" authority path-abempty
66 * The scheme and path components are required, though the path may be
67 * empty (no characters).
68 * When authority is present, the path must either be empty or begin
69 * with a slash ("/") character.
70 * When authority is not present, the path cannot begin with two slash
72 * These restrictions result in five different ABNF rules for a path
73 * (Section 3.3), only one of which will match any given URI reference.
75 * The following are two example URIs and their component parts:
77 * foo://example.com:8042/over/there?name=ferret#nose
78 * \_/ \______________/\_________/ \_________/ \__/
80 * scheme authority path query fragment
81 * | _____________________|__
83 * urn:example:animal:ferret:nose
93 typedef zypp::url::EEncoding EEncoding;
98 typedef zypp::url::ViewOptions ViewOptions;
105 * Create a new Url object as shared copy of the given one.
107 * Upon return, both objects will point to the same underlying
108 * object. This state will remain until one of the object is
111 * \param url The Url object to make a copy of.
112 * \throws url::UrlException if copy fails (should not happen).
118 * Create a new Url object as shared copy of the given reference.
120 * Upon return, both objects will point to the same underlying
121 * object. This state will remain until one of the object is
124 * \param url The URL implementation reference to make a copy of.
125 * \throws url::UrlException if reference is empty.
127 Url(const zypp::url::UrlRef &url);
131 * \brief Construct a Url object from percent-encoded URL string.
133 * Parses the \p encodedUrl string using the parseUrl() method
134 * and assings the result to the new created object.
136 * \param encodedUrl A percent-encoded URL string.
137 * \throws url::UrlParsingException if parsing of the url fails.
138 * \throws url::UrlNotAllowedException if one of the components
139 * is not allowed for the scheme.
140 * \throws url::UrlBadComponentException if one of the components
141 * contains an invalid character.
143 Url(const std::string &encodedUrl);
148 * \brief Parse a percent-encoded URL string.
150 * Trys to parses the given string into generic URL components
151 * and created a clone of a scheme-specialized object or a new
154 * \param encodedUrl A percent-encoded URL string.
155 * \return A reference to a (derived) UrlBase object or
156 * empty reference if the \p encodedUrl string
157 * does not match the generic URL syntax.
158 * \throws url::UrlNotAllowedException if one of the components
159 * is not allowed for the scheme.
160 * \throws url::UrlBadComponentException if one of the components
161 * contains an invalid character.
164 parseUrl(const std::string &encodedUrl);
169 * \brief Assigns parsed percent-encoded URL string to the object.
171 * Parses \p encodedUrl string using the parseUrl() method
172 * and assigns the result to the current object.
174 * \param encodedUrl A percent-encoded URL string.
175 * \return A reference to this Url object.
176 * \throws url::UrlParsingException if parsing of the url fails.
177 * \throws url::UrlNotAllowedException if one of the components
178 * is not allowed for the scheme.
179 * \throws url::UrlBadComponentException if one of the components
180 * contains an invalid character.
183 operator = (const std::string &encodedUrl);
187 * \brief Assign shared copy of \p url to the current object.
189 * Upon return, both objects will point to the same underlying
190 * object. This state will remain until one of the object is
193 * \param url The Url object to make a copy of.
194 * \return A reference to this Url object.
197 operator = (const Url &url);
202 * \brief Register a scheme-specific implementation.
204 * \param scheme A name of a scheme.
205 * \param urlImpl A UrlBase object specialized for this scheme.
206 * \return True, if the object claims to implement the scheme.
209 registerScheme(const std::string &scheme,
210 url::UrlRef urlImpl);
213 * \brief Returns all registered scheme names.
214 * \return A vector with registered URL scheme names.
216 static zypp::url::UrlSchemes
217 getRegisteredSchemes();
220 * \brief Returns if scheme name is registered.
221 * \return True, if scheme name is registered.
224 isRegisteredScheme(const std::string &scheme);
229 * \brief Returns scheme names known to this object.
230 * \return A vector with scheme names known by this object.
232 zypp::url::UrlSchemes
233 getKnownSchemes() const;
237 * \brief Verifies specified scheme name.
239 * Verifies the generic syntax of the specified \p scheme name
240 * and if it is contained in the current object's list of known
241 * schemes (see getKnownSchemes()) if the list is not empty.
243 * The default implementation in the UrlBase class returns an
244 * emtpy list of known schemes, causing a check of the generic
247 * \return True, if generic scheme name syntax is valid and
248 * the scheme name is known to the current object.
251 isValidScheme(const std::string &scheme) const;
254 /** hd cd dvd dir file iso */
255 static bool schemeIsLocal( const std::string & scheme_r );
256 /** \overload nonstatic version */
257 bool schemeIsLocal() const { return schemeIsLocal( getScheme() ); }
259 /** nfs nfs4 smb cifs http https ftp sftp tftp */
260 static bool schemeIsRemote( const std::string & scheme_r );
261 /** \overload nonstatic version */
262 bool schemeIsRemote() const { return schemeIsRemote( getScheme() ); }
265 static bool schemeIsVolatile( const std::string & scheme_r );
266 /** \overload nonstatic version */
267 bool schemeIsVolatile() const { return schemeIsVolatile( getScheme() ); }
269 /** http https ftp sftp tftp */
270 static bool schemeIsDownloading( const std::string & scheme_r );
271 /** \overload nonstatic version */
272 bool schemeIsDownloading() const { return schemeIsDownloading( getScheme() ); }
275 * \brief Verifies the Url.
277 * Verifies if the current object contains a non-empty scheme
278 * name. Additional semantical URL checks may be performed by
279 * derived UrlBase objects.
281 * \return True, if the Url seems to be valid.
289 * Returns a default string representation of the Url object.
291 * By default, a password in the URL will be hidden.
293 * \return A default string representation of the Url object.
299 * Returns a string representation of the Url object.
301 * To include a password in the resulting Url string, use:
303 * url.asString(url.getViewOptions() +
304 * url::ViewOptions::WITH_PASSWORD);
307 * \param opts A combination of view options.
308 * \return A string representation of the Url object.
311 asString(const ViewOptions &opts) const;
314 * Returns a complete string representation of the Url object.
316 * This function ignores the configuration of the view options
317 * in the current object (see setViewOption()) and forces to
318 * return an string with all URL components included.
320 * \return A complete string representation of the Url object.
323 asCompleteString() const;
328 * Returns the scheme name of the URL.
329 * \return Scheme name of the current Url object.
337 * Returns the encoded authority component of the URL.
339 * The returned authority string does not contain the leading
340 * "//" separator characters, but just its "user:pass@host:port"
343 * \return The encoded authority component string.
346 getAuthority() const;
349 * Returns the username from the URL authority.
350 * \param eflag Flag if the usename should be percent-decoded or not.
351 * \return The username sub-component from the URL authority.
352 * \throws url::UrlDecodingException if the decoded result string
353 * would contain a '\\0' character.
356 getUsername(EEncoding eflag = zypp::url::E_DECODED) const;
359 * Returns the password from the URL authority.
360 * \param eflag Flag if the password should be percent-decoded or not.
361 * \return The password sub-component from the URL authority.
362 * \throws url::UrlDecodingException if the decoded result string
363 * would contain a '\\0' character.
366 getPassword(EEncoding eflag = zypp::url::E_DECODED) const;
369 * Returns \c true if username \b and password are encoded in the authority component.
371 bool hasCredentialsInAuthority() const
372 { return ! ( getUsername().empty() || getPassword().empty() ); }
375 * Returns the hostname or IP from the URL authority.
377 * In case the Url contains an IP number, it may be surrounded
378 * by "[" and "]" characters, for example "[::1]" for an IPv6
381 * \param eflag Flag if the host should be percent-decoded or not.
382 * \return The host sub-component from the URL authority.
383 * \throws url::UrlDecodingException if the decoded result string
384 * would contain a '\\0' character.
387 getHost(EEncoding eflag = zypp::url::E_DECODED) const;
390 * Returns the port from the URL authority.
391 * \return The port sub-component from the URL authority.
399 * Returns the encoded path component of the URL.
401 * The path data contains the path name, optionally
402 * followed by path parameters separated with a ";"
403 * character, for example "/foo/bar;version=1.1".
405 * \return The encoded path component of the URL.
411 * Returns the path name from the URL.
412 * \param eflag Flag if the path should be decoded or not.
413 * \return The path name sub-component without path parameters
414 * from Path-Data component of the URL.
415 * \throws url::UrlDecodingException if the decoded result string
416 * would contain a '\\0' character.
419 getPathName(EEncoding eflag = zypp::url::E_DECODED) const;
422 * Returns the path parameters from the URL.
423 * \return The encoded path parameters from the URL.
426 getPathParams() const;
429 * Returns a vector with path parameter substrings.
431 * The default path parameter separator is the \c ',' character.
432 * A schema specific object may overide the default separators.
434 * For example, the path parameters string "foo=1,bar=2" is splited
435 * by default into a vector containing the substrings "foo=1" and
438 * \return The path parameters splited into a vector of substrings.
441 getPathParamsVec() const;
444 * Returns a string map with path parameter keys and values.
446 * The default path parameter separator is the \c ',' character,
447 * the default key/value separator for the path parameters is
448 * the \c '=' character.
449 * A schema specific object may overide the default separators.
451 * For example, the path parameters string "foo=1,bar=2" is splited
452 * into a map containing "foo" = "1" and "bar" = "2" by default.
454 * \param eflag Flag if the path parameter keys and values should
456 * \return The path parameters key and values as a string map.
457 * \throws url::UrlNotSupportedException if parameter parsing
458 * is not supported for a URL (scheme).
459 * \throws url::UrlDecodingException if the decoded result string
460 * would contain a '\\0' character.
463 getPathParamsMap(EEncoding eflag = zypp::url::E_DECODED) const;
466 * Return the value for the specified path parameter.
468 * For example, if the path parameters string is "foo=1,bar=2"
469 * the method will return the substring "1" for the param key
470 * "foo" and "2" for the param key "bar".
472 * \param param The path parameter key.
473 * \param eflag Flag if the path parameter keys and values should
475 * \return The value for the path parameter key or empty string.
476 * \throws url::UrlNotSupportedException if parameter parsing
477 * is not supported for a URL (scheme).
478 * \throws url::UrlDecodingException if the decoded result string
479 * would contain a '\\0' character.
482 getPathParam(const std::string ¶m,
483 EEncoding eflag = zypp::url::E_DECODED) const;
488 * Returns the encoded query string component of the URL.
490 * The query string is returned without first "?" (separator)
491 * character. Further "?" characters as in e.g. LDAP URL's
492 * remains in the returned string.
494 * \return The encoded query string component of the URL.
497 getQueryString() const;
500 * Returns a vector with query string parameter substrings.
502 * The default query string parameter separator is the \c '&'
504 * A schema specific object may overide the default separators.
506 * For example, the query string "foo=1&bar=2" is splited by
507 * default into a vector containing the substrings "foo=1" and
510 * \return The query string splited into a vector of substrings.
513 getQueryStringVec() const;
516 * Returns a string map with query parameter and their values.
518 * The default query string parameter separator is the \c ','
519 * character, the default key/value separator the \c '=' character.
520 * A schema specific object may overide the default separators.
522 * For example, the query string "foo=1&bar=2" is splited by
523 * default into a map containing "foo" = "1" and "bar" = "2".
525 * \param eflag Flag if the query string keys and values should
527 * \return The query string as a key/value string map.
528 * \throws url::UrlNotSupportedException if parameter parsing
529 * is not supported for a URL (scheme).
530 * \throws url::UrlDecodingException if the decoded result string
531 * would contain a '\\0' character.
534 getQueryStringMap(EEncoding eflag = zypp::url::E_DECODED) const;
537 * Return the value for the specified query parameter.
539 * For example, if the query string is "foo=1,bar=2" the method
540 * will return the substring "1" for the param key "foo" and
541 * "2" for the param key "bar".
543 * \param param The query parameter key.
544 * \param eflag Flag if the query parameter keys and values should
546 * \return The value for the query parameter key or empty string.
547 * \throws url::UrlNotSupportedException if parameter parsing
548 * is not supported for a URL (scheme).
549 * \throws url::UrlDecodingException if the decoded result string
550 * would contain a '\\0' character.
553 getQueryParam(const std::string ¶m,
554 EEncoding eflag = zypp::url::E_DECODED) const;
559 * Returns the encoded fragment component of the URL.
560 * \param eflag Flag if the fragment should be percent-decoded or not.
561 * \return The encoded fragment component of the URL.
562 * \throws url::UrlDecodingException if the decoded result string
563 * would contain a '\\0' character.
566 getFragment(EEncoding eflag = zypp::url::E_DECODED) const;
571 * \brief Set the scheme name in the URL.
572 * \param scheme The new scheme name.
573 * \throws url::UrlBadComponentException if the \p scheme
574 * contains an invalid character or is empty.
577 setScheme(const std::string &scheme);
582 * \brief Set the authority component in the URL.
584 * The \p authority string shoud contain the "user:pass@host:port"
585 * sub-components without any leading "//" separator characters.
587 * \param authority The encoded authority component string.
588 * \throws url::UrlNotAllowedException if the \p authority
589 * has to be empty in for the current scheme.
590 * \throws url::UrlBadComponentException if the \p authority
591 * contains an invalid character.
592 * \throws url::UrlParsingException if \p authority parsing fails.
595 setAuthority(const std::string &authority);
598 * \brief Set the username in the URL authority.
599 * \param user The new username.
600 * \param eflag If the \p username is encoded or not.
601 * \throws url::UrlNotAllowedException if the \p user
602 * has to be empty in for the current scheme
603 * \throws url::UrlBadComponentException if the \p user
604 * contains an invalid character.
607 setUsername(const std::string &user,
608 EEncoding eflag = zypp::url::E_DECODED);
611 * \brief Set the password in the URL authority.
612 * \param pass The new password.
613 * \param eflag If the \p password is encoded or not.
614 * \throws url::UrlNotAllowedException if the \p pass
615 * has to be empty in for the current scheme.
616 * \throws url::UrlBadComponentException if the \p pass
617 * contains an invalid character.
620 setPassword(const std::string &pass,
621 EEncoding eflag = zypp::url::E_DECODED);
624 * \brief Set the hostname or IP in the URL authority.
626 * The \p host parameter may contain a hostname, an IPv4 address
627 * in dotted-decimal form or an IPv6 address literal encapsulated
628 * within square brackets (RFC3513, Sect. 2.2).
630 * A hostname may contain national alphanumeric UTF8 characters
631 * (letters other than ASCII a-z0-9), that will be encoded.
632 * This function allows to specify both, a encoded or decoded
635 * Other IP literals in "[v ... ]" square bracket format are not
636 * supported by the implementation in UrlBase class.
638 * \param host The new hostname or IP address.
639 * \throws url::UrlNotAllowedException if the \p host (authority)
640 * has to be empty in for the current scheme.
641 * \throws url::UrlBadComponentException if the \p host is invalid.
644 setHost(const std::string &host);
647 * \brief Set the port number in the URL authority.
648 * \param port The new port number.
649 * \throws url::UrlNotAllowedException if the \p port (authority)
650 * has to be empty in for the current scheme.
651 * \throws url::UrlBadComponentException if the \p port is invalid.
654 setPort(const std::string &port);
659 * \brief Set the path data component in the URL.
661 * By default, the \p pathdata string may include path
662 * parameters separated by the ";" separator character.
664 * \param pathdata The encoded path data component string.
665 * \throws url::UrlBadComponentException if the \p pathdata
666 * contains an invalid character.
669 setPathData(const std::string &pathdata);
672 * \brief Set the path name.
673 * \param path The new path name.
674 * \param eflag If the \p path name is encoded or not.
675 * \throws url::UrlBadComponentException if the \p path name
676 * contains an invalid character.
679 setPathName(const std::string &path,
680 EEncoding eflag = zypp::url::E_DECODED);
683 setPathName(const Pathname &path,
684 EEncoding eflag = zypp::url::E_DECODED);
687 setPathName(const char *path,
688 EEncoding eflag = zypp::url::E_DECODED);
691 * \brief Set the path parameters.
692 * \param params The new encoded path parameter string.
693 * \throws url::UrlBadComponentException if the path \p params
694 * contains an invalid character.
697 setPathParams(const std::string ¶ms);
700 * \brief Set the path parameters.
701 * \param pvec The vector with encoded path parameters.
702 * \throws url::UrlBadComponentException if the \p pvec
703 * contains an invalid character.
706 setPathParamsVec(const zypp::url::ParamVec &pvec);
709 * \brief Set the path parameters.
710 * \param pmap The map with decoded path parameters.
711 * \throws url::UrlNotSupportedException if parameter parsing
712 * is not supported for a URL (scheme).
715 setPathParamsMap(const zypp::url::ParamMap &pmap);
718 * \brief Set or add value for the specified path parameter.
719 * \param param The decoded path parameter name.
720 * \param value The decoded path parameter value.
721 * \throws url::UrlNotSupportedException if parameter parsing
722 * is not supported for a URL (scheme).
723 * \throws url::UrlDecodingException if the decoded result string
724 * would contain a '\\0' character.
727 setPathParam(const std::string ¶m, const std::string &value);
732 * \brief Set the query string in the URL.
733 * \param querystr The new encoded query string.
734 * \throws url::UrlBadComponentException if the \p querystr
735 * contains an invalid character.
738 setQueryString(const std::string &querystr);
741 * \brief Set the query parameters.
742 * \param qvec The vector with encoded query parameters.
743 * \throws url::UrlBadComponentException if the \p qvec
744 * contains an invalid character.
747 setQueryStringVec(const zypp::url::ParamVec &qvec);
750 * \brief Set the query parameters.
751 * \param qmap The map with decoded query parameters.
752 * \throws url::UrlNotSupportedException if parameter parsing
753 * is not supported for a URL (scheme).
756 setQueryStringMap(const zypp::url::ParamMap &qmap);
759 * \brief Set or add value for the specified query parameter.
760 * \param param The decoded query parameter name.
761 * \param value The decoded query parameter value.
762 * \throws url::UrlNotSupportedException if parameter parsing
763 * is not supported for a URL (scheme).
764 * \throws url::UrlDecodingException if the decoded result string
765 * would contain a '\\0' character.
768 setQueryParam(const std::string ¶m, const std::string &value);
771 * \brief remove the specified query parameter.
772 * \param param The decoded query parameter name.
773 * \throws url::UrlNotSupportedException if parameter parsing
774 * is not supported for a URL (scheme).
775 * \throws url::UrlDecodingException if the decoded result string
776 * would contain a '\\0' character.
779 delQueryParam(const std::string ¶m);
784 * \brief Set the fragment string in the URL.
785 * \param fragment The new fragment string.
786 * \param eflag If the \p fragment is encoded or not.
787 * \throws url::UrlBadComponentException if the \p fragment
788 * contains an invalid character.
791 setFragment(const std::string &fragment,
792 EEncoding eflag = zypp::url::E_DECODED);
797 * Return the view options of the current object.
799 * This method is used to query the view options
800 * used by the asString() method.
802 * \return The current view option combination.
805 getViewOptions() const;
808 * Change the view options of the current object.
810 * This method is used to change the view options
811 * used by the asString() method.
813 * \param vopts New view options combination.
816 setViewOptions(const ViewOptions &vopts);
822 std::ostream & operator<<( std::ostream & str, const Url & url );
825 * needed for std::set
827 bool operator<( const Url &lhs, const Url &rhs );
832 bool operator==( const Url &lhs, const Url &rhs );
835 bool operator!=( const Url &lhs, const Url &rhs );
837 ////////////////////////////////////////////////////////////////////
839 //////////////////////////////////////////////////////////////////////
841 #endif /* ZYPP_URL_H */
843 ** vim: set ts=2 sts=2 sw=2 ai et: