Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / url / UrlBase.h
index 75bb19d..b790784 100644 (file)
@@ -12,8 +12,8 @@
 #ifndef   ZYPP_URL_URLBASE_H
 #define   ZYPP_URL_URLBASE_H
 
-#include <zypp/url/UrlUtils.h>
-#include <zypp/base/PtrTypes.h>
+#include "zypp/url/UrlUtils.h"
+#include "zypp/base/PtrTypes.h"
 
 
 //////////////////////////////////////////////////////////////////////
@@ -27,7 +27,7 @@ namespace zypp
 
     // ---------------------------------------------------------------
     /**
-     * Url::toString() view options.
+     * Url::asString() view options.
      *
      * A instance of this class represents a bit-wise combination
      * of view option constants.
@@ -181,8 +181,7 @@ namespace zypp
       /**
        * Create instance with default combination of view options.
        */
-      ViewOption(): opt(DEFAULTS.opt)
-      {}
+      ViewOption();
 
 
       /**
@@ -232,7 +231,7 @@ namespace zypp
       }
 
     private:
-      ViewOption(int o): opt(o) {}
+      ViewOption(int option);
       int opt;
     };
 
@@ -264,7 +263,7 @@ namespace zypp
      *
      * The UrlBase class implements default behaviour for URL
      * manipulations and a base for implementation of scheme-
-     * specialized URL's for the Url class.
+     * specialized URLs for the Url class.
      *
      */
     class UrlBase
@@ -286,11 +285,15 @@ namespace zypp
        * \brief Construct new object and initializes it with
        *        specified URL components.
        *
-       * \param scheme    A scheme name.
-       * \param authority A authority component data (encoded).
-       * \param pathdata  A path component data (encoded).
-       * \param querystr  A query string (encoded).
-       * \param fragment  A fragment component (encoded),
+       * \param scheme    The scheme name.
+       * \param authority The encoded authority component data.
+       * \param pathdata  The encoded path component data.
+       * \param querystr  The encoded query string component.
+       * \param fragment  The encoded fragment string component.
+       * \throws UrlNotAllowedException if one of the components
+       *         is not allowed for the scheme.
+       * \throws UrlBadComponentException if one of the components
+       *         contains an invalid character.
        */
       UrlBase(const std::string &scheme,
               const std::string &authority,
@@ -323,11 +326,15 @@ namespace zypp
       /**
        * \brief Initializes current object with new URL components.
        *
-       * \param scheme    A scheme name.
-       * \param authority A authority component data (encoded).
-       * \param pathdata  A path component data (encoded).
-       * \param querystr  A query string (encoded).
-       * \param fragment  A fragment component (encoded),
+       * \param scheme    The scheme name.
+       * \param authority The encoded authority component data.
+       * \param pathdata  The encoded path component data.
+       * \param querystr  The encoded query string component.
+       * \param fragment  The encoded fragment string component.
+       * \throws UrlNotAllowedException if one of the components
+       *         is not allowed in the scheme.
+       * \throws UrlBadComponentException if one of the components
+       *         contains an invalid character.
        */
       virtual void
       init(const std::string &scheme,
@@ -347,11 +354,11 @@ namespace zypp
        * or compatible to).
        *
        * For example, if your derived class implements special
-       * features of LDAP URL's, this method may return "ldap"
+       * features of LDAP URLs, this method may return "ldap"
        * and "ldaps" scheme names.
        *
        * The UrlBase class returns an empty vector, that signals
-       * that it is useable with all URL's.
+       * that it is useable with all URLs.
        *
        * \return A vector with scheme names known by this object.
        */
@@ -403,21 +410,14 @@ namespace zypp
        * \return A default string representation of the Url object.
        */
       virtual std::string
-      toString() const;
+      asString() const;
 
       /**
        * Returns a string representation of the Url object.
        *
        * To include a password in the resulting Url string, use:
        * \code
-       *    url.toString(url::ViewOptions() +
-       *                 url::ViewOptions::WITH_PASSWORD);
-       * \endcode
-       *
-       * or its equivalent:
-       *
-       * \code
-       *    url.toString(url::ViewOptions::DEFAULTS +
+       *    url.asString(url.getViewOptions() +
        *                 url::ViewOptions::WITH_PASSWORD);
        * \endcode
        *
@@ -425,7 +425,7 @@ namespace zypp
        * \return A string representation of the Url object. 
        */
       virtual std::string
-      toString(const zypp::url::ViewOptions &opts) const;
+      asString(const zypp::url::ViewOptions &opts) const;
 
 
       // -----------------
@@ -454,6 +454,8 @@ namespace zypp
        * Returns the username from the URL authority.
        * \param eflag Flag if the usename should be percent-decoded or not.
        * \return The username sub-component from the URL authority.
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getUsername(EEncoding eflag) const;
@@ -462,6 +464,8 @@ namespace zypp
        * Returns the password from the URL authority.
        * \param eflag Flag if the password should be percent-decoded or not.
        * \return The password sub-component from the URL authority.
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getPassword(EEncoding eflag) const;
@@ -469,18 +473,20 @@ namespace zypp
       /**
        * Returns the hostname or IP from the URL authority.
        *
-       * In case the Url contains an IP number, it may be surrounded
+       * In case the Url contains an IPv6 number, it is be surrounded
        * by "[" and "]" characters, for example "[::1]" for an IPv6
        * localhost address.
        *
        * \param eflag Flag if the host should be percent-decoded or not.
        * \return The host sub-component from the URL authority.
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getHost(EEncoding eflag) const;
 
       /**
-       * Returns the port from the URL authority.
+       * Returns the port number from the URL authority.
        * \return The port sub-component from the URL authority.
        */
       virtual std::string
@@ -504,20 +510,22 @@ namespace zypp
        * Returns the path name from the URL.
        * \param eflag Flag if the path should be decoded or not.
        * \return The path name sub-component without path parameters
-       *         from Path-Data component of the URL.
+       *         from path data component of the URL.
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getPathName(EEncoding eflag) const;
 
       /**
-       * Returns the path parameters from the URL.
+       * Returns the encoded path parameters from the URL.
        * \return The encoded path parameters from the URL.
        */
       virtual std::string
       getPathParams() const;
 
       /**
-       * Returns a vector with path parameter substrings.
+       * Returns a vector with encoded path parameter substrings.
        *
        * The default path parameter separator is the \c ',' character.
        * A schema specific object may overide the default separators.
@@ -526,7 +534,7 @@ namespace zypp
        * by default into a vector containing the substrings "foo=1" and
        * "bar=2".
        *
-       * \return The path parameters splited into a vector of substrings.
+       * \return The encoded path parameters vector.
        */
       virtual zypp::url::ParamVec
       getPathParamsVec() const;
@@ -545,6 +553,10 @@ namespace zypp
        * \param eflag Flag if the path parameter keys and values should
        *               be decoded or not.
        * \return The path parameters key and values as a string map.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual zypp::url::ParamMap
       getPathParamsMap(EEncoding eflag) const;
@@ -560,6 +572,10 @@ namespace zypp
        * \param eflag Flag if the path parameter keys and values should
        *              be decoded or not.
        * \return The value for the path parameter key or empty string.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getPathParam(const std::string &param, EEncoding eflag) const;
@@ -570,7 +586,7 @@ namespace zypp
        * Returns the encoded query string component of the URL.
        *
        * The query string is returned without first "?" (separator)
-       * character. Further "?" characters as in e.g. LDAP URL's
+       * character. Further "?" characters as in e.g. LDAP URLs
        * remains in the returned string.
        *
        * \return The encoded query string component of the URL.
@@ -607,6 +623,10 @@ namespace zypp
        * \param eflag Flag if the query string keys and values should
        *               be decoded or not.
        * \return The query string as a key/value string map.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual zypp::url::ParamMap
       getQueryStringMap(EEncoding eflag) const;
@@ -622,6 +642,10 @@ namespace zypp
        * \param eflag Flag if the query parameter keys and values should
        *              be decoded or not.
        * \return The value for the query parameter key or empty string.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getQueryParam(const std::string &param, EEncoding eflag) const;
@@ -632,6 +656,8 @@ namespace zypp
        * Returns the encoded fragment component of the URL.
        * \param eflag Flag if the fragment should be percent-decoded or not.
        * \return The encoded fragment component of the URL.
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual std::string
       getFragment(EEncoding eflag) const;
@@ -641,6 +667,8 @@ namespace zypp
       /**
        * \brief Set the scheme name in the URL.
        * \param scheme The new scheme name.
+       * \throws UrlBadComponentException if the \p scheme
+       *         contains an invalid character or is empty.
        */
       virtual void
       setScheme(const std::string &scheme);
@@ -654,6 +682,11 @@ namespace zypp
        * "//" separator characters (just "user:pass@host:port").
        *
        * \param authority The authority component string.
+       * \throws UrlNotAllowedException if the \p authority
+       *         has to be empty in for the current scheme.
+       * \throws UrlBadComponentException if the \p authority
+       *         contains an invalid character.
+       * \throws UrlParsingException if \p authority parsing fails.
        */
       virtual void
       setAuthority(const std::string &authority);
@@ -662,6 +695,10 @@ namespace zypp
        * \brief Set the username in the URL authority.
        * \param user  The new username.
        * \param eflag If the \p username is encoded or not.
+       * \throws UrlNotAllowedException if the \p user
+       *         has to be empty in for the current scheme.
+       * \throws UrlBadComponentException if the \p user
+       *         contains an invalid character.
        */
       virtual void
       setUsername(const std::string &user,
@@ -671,6 +708,10 @@ namespace zypp
        * \brief Set the password in the URL authority.
        * \param pass  The new password.
        * \param eflag If the \p password is encoded or not.
+       * \throws UrlNotAllowedException if the \p pass
+       *         has to be empty in for the current scheme.
+       * \throws UrlBadComponentException if the \p pass
+       *         contains an invalid character.
        */
       virtual void
       setPassword(const std::string &pass,
@@ -684,14 +725,17 @@ namespace zypp
        * within square brackets (RFC3513, Sect. 2.2).
        *
        * A hostname may contain national alphanumeric UTF8 characters
-       * (letters other than ASCII a-z0-9), that will be encoded.
+       * (letters other than ASCII a-zA-Z), that will be encoded.
        * This function allows to specify both, a encoded or decoded
        * hostname.
        *
        * Other IP literals in "[v ... ]" square bracket format are not
        * supported by the implementation in UrlBase class.
        *
-       * \param host The new hostname or IP.
+       * \param host The new hostname or IP address.
+       * \throws UrlNotAllowedException if the \p host
+       *         has to be empty in for the current scheme.
+       * \throws UrlBadComponentException if the \p host is invalid.
        */
       virtual void
       setHost(const std::string &host);
@@ -699,6 +743,9 @@ namespace zypp
       /**
        * \brief Set the port number in the URL authority.
        * \param port The new port number.
+       * \throws UrlNotAllowedException if the \p port
+       *         has to be empty in for the current scheme.
+       * \throws UrlBadComponentException if the \p port is invalid.
        */
       virtual void
       setPort(const std::string &port);
@@ -712,6 +759,8 @@ namespace zypp
        * parameters separated by the ";" separator character.
        *
        * \param pathdata The encoded path data component string.
+       * \throws UrlBadComponentException if the \p pathdata
+       *         contains an invalid character.
        */
       virtual void
       setPathData(const std::string &pathdata);
@@ -720,6 +769,8 @@ namespace zypp
        * \brief Set the path name.
        * \param path  The new path name.
        * \param eflag If the \p path name is encoded or not.
+       * \throws UrlBadComponentException if the \p path name
+       *         contains an invalid character.
        */
       virtual void
       setPathName(const std::string &path,
@@ -728,6 +779,8 @@ namespace zypp
       /**
        * \brief Set the path parameters.
        * \param params The new encoded path parameter string.
+       * \throws UrlBadComponentException if the path \p params
+       *         contains an invalid character.
        */
       virtual void
       setPathParams(const std::string &params);
@@ -735,6 +788,8 @@ namespace zypp
       /**
        * \brief Set the path parameters.
        * \param pvec The vector with encoded path parameters.
+       * \throws UrlBadComponentException if the \p pvec
+       *         contains an invalid character.
        */
       virtual void
       setPathParamsVec(const zypp::url::ParamVec &pvec);
@@ -742,6 +797,8 @@ namespace zypp
       /**
        * \brief Set the path parameters.
        * \param pmap The map with decoded path parameters.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
        */
       virtual void
       setPathParamsMap(const zypp::url::ParamMap &pmap);
@@ -750,6 +807,10 @@ namespace zypp
        * \brief Set or add value for the specified path parameter.
        * \param param The decoded path parameter name.
        * \param value The decoded path parameter value.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual void
       setPathParam(const std::string &param, const std::string &value);
@@ -759,7 +820,14 @@ namespace zypp
       /**
        * \brief Set the query string in the URL.
        *
+       * The \p querystr string parameter is supposed
+       * to not to contain the "?" URL query separator
+       * character (use just a "foo=bar&x=22" instead
+       * of "?foo=bar&x=22").
+       *
        * \param querystr The new encoded query string.
+       * \throws UrlBadComponentException if the \p querystr
+       *         contains an invalid character.
        */
       virtual void
       setQueryString(const std::string &querystr);
@@ -767,6 +835,8 @@ namespace zypp
       /**
        * \brief Set the query parameters.
        * \param qvec The vector with encoded query parameters.
+       * \throws UrlBadComponentException if the \p qvec
+       *         contains an invalid character.
        */
       virtual void
       setQueryStringVec(const zypp::url::ParamVec &qvec);
@@ -774,6 +844,8 @@ namespace zypp
       /**
        * \brief Set the query parameters.
        * \param qmap The map with decoded query parameters.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
        */
       virtual void
       setQueryStringMap(const zypp::url::ParamMap &qmap);
@@ -782,16 +854,33 @@ namespace zypp
        * \brief Set or add value for the specified query parameter.
        * \param param The decoded query parameter name.
        * \param value The decoded query parameter value.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
        */
       virtual void
       setQueryParam(const std::string &param, const std::string &value);
 
+      /**
+       * \brief remove the specified query parameter.
+       * \param param The decoded query parameter name.
+       * \throws UrlNotSupportedException if parameter parsing
+       *         is not supported for a URL (scheme).
+       * \throws UrlDecodingException if the decoded result string
+       *         would contain a '\\0' character.
+       */
+      virtual void
+      delQueryParam(const std::string &param);
+
 
       // -----------------
       /**
        * \brief Set the fragment string in the URL.
        * \param fragment The new fragment string.
        * \param eflag If the \p fragment is encoded or not.
+       * \throws UrlBadComponentException if the \p querystr
+       *         contains an invalid character.
        */
       virtual void
       setFragment(const std::string &fragment,
@@ -813,6 +902,9 @@ namespace zypp
        * - Common path parameter separators:
        *   - \a \c sep_pathparams   \c ";"
        *     Separator used to split path parameters from path name.
+       *     Setting it to empty string disables splitting of path
+       *     name and path parameters. Set also rx_pathparams to an
+       *     empty string.
        *   - \a \c psep_pathparam   \c ","
        *     Separator between path parameters.
        *   - \a \c vsep_pathparam   \c "="
@@ -868,8 +960,6 @@ namespace zypp
       std::string
       config(const std::string &opt) const;
 
-    //protected:
-      // friend class Url;
       /**
        * Set the value of a UrlBase configuration variable.
        *
@@ -887,7 +977,7 @@ namespace zypp
        * Return the view options of the current object.
        *
        * This method is used to query the view options
-       * used by the Url::toString() method.
+       * used by the asString() method.
        *
        * \return The current view option combination.
        */
@@ -898,7 +988,7 @@ namespace zypp
        * Change the view options of the current object.
        *
        * This method is used to change the view options
-       * used by the Url::toString() method.
+       * used by the asString() method.
        *
        * \param vopts New view options combination.
        */
@@ -908,33 +998,70 @@ namespace zypp
 
     protected:
       /**
-       * Utility method to cleanup a path name.
+       * Utility method to cleanup an encoded path name.
        *
-       * By default, this method replaces multiple occurences
-       * of the "/" characeter from the begining of the path,
-       * so the cleaned path begins with at most one "/".
+       * By default, this method makes sure, that the first slash
+       * in the path is not encoded, and that the second slash
+       * before the first path segment, is encoded (to "%2F").
+       * It modifies the path in the url, for example:
+       *   "ftp://host//aaa//bbb" to "ftp://host/%2Faaa//bbb"
+       * or as encoded path only also "%2f/name" to "/%2fname".
        *
-       * This operation is required in some cases to not to
-       * missinterpret multiple "/" occurences as an empty
-       * URL authority.
+       * This operation is required to fulfill the path-absolute
+       * rule of RFC3986, if there is no authority. It avoids the
+       * missinterpretation of the path as an authority separator.
        *
-       * \param path   A path name to cleanup.
-       * \return A path begining with at most one "/" character.
+       * It is not required if there is an authority ("//" behind
+       * the "scheme:"), that is in the path-abempty rule, but it
+       * is used e.g. in ftp url's defined by RFC1738.
+       * 
+       * We apply this operation in both cases (for all paths),
+       * but if \p authority is true, the encoding of the second
+       * slash depends on the schema configuration (for ftp only).
+       *
+       * \param path      The encoded path name to cleanup.
+       * \param authority Whether the url contains authority or not.
+       * \return A modified encoded path.
+       */
+      virtual std::string
+      cleanupPathName(const std::string &path, bool authority) const;
+
+      /**
+       * Utility method to cleanup an encoded path name.
+       *
+       * This variant of the method checks if the host component
+       * in the url is empty or not to differentiate if there is
+       * an authority.
+       *
+       * \param path      The encoded path name to cleanup.
+       * \return A modified encoded path.
        */
       virtual std::string
-      cleanupPathName(const std::string &path);
+      cleanupPathName(const std::string &path) const;
 
       /**
        * \brief Verifies specified host or IP.
        *
+       * It verifies, if the specified \p host parameter contains
+       * a hostname, an IPv4 address in dotted-decimal form or an
+       * IPv6 address literal encapsulated within square brackets
+       * (RFC3513, Sect. 2.2).
+       *
+       * A hostname in the \p host parameter, may contain national
+       * alphanumeric UTF8 characters (letters other than ASCII
+       * a-zA-Z) and allows to specify both, a encoded or decoded
+       * hostname.
+       *
        * This function does not perform any hostname lookups and
-       * supports only IPv6 addresses in "[ ... ]" notation.
+       * supports only IPv6 addresses in "[ ... ]" notation. The
+       * "[v ... ]" square bracket format is not supported by
+       * this implementation.
        *
        * \param  host  The host name or IP to verify.
        * \return True, if host seems to be valid.
        */
       virtual bool
-      isValidHost(const std::string &host);
+      isValidHost(const std::string &host) const;
 
       /**
        * \brief Verifies specified port number.
@@ -943,7 +1070,7 @@ namespace zypp
        * \return True, if port number is valid.
        */
       virtual bool
-      isValidPort(const std::string &port);
+      isValidPort(const std::string &port) const;
 
     private:
       UrlBaseData *m_data;