1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 * \file zypp/url/UrlBase.h
12 #ifndef ZYPP_URL_URLBASE_H
13 #define ZYPP_URL_URLBASE_H
15 #include <zypp/url/UrlUtils.h>
16 #include <zypp/base/PtrTypes.h>
19 //////////////////////////////////////////////////////////////////////
21 { ////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////
25 { //////////////////////////////////////////////////////////////////
28 // ---------------------------------------------------------------
30 * UrlBase behaviour configuration variables container.
32 typedef std::map< std::string, std::string > UrlConfig;
35 // ---------------------------------------------------------------
37 * Vector of URL scheme names.
39 typedef std::vector<std::string> UrlSchemes;
42 // ---------------------------------------------------------------
44 * Url::toString() view options.
46 * A instance of this class represents a bit-wise combination
47 * of view option constants.
49 * It provides ViewOption::operator+() and ViewOption::operator-()
50 * to modify a view option combination and a ViewOption::has()
51 * method, to check if a specified option is enabled or not.
57 * Option to include scheme name in the URL string.
59 * Disabling this option causes, that the URL string
60 * contains the path, query and fragment components
61 * only, for example just "/foo/bar.txt".
63 * This option is \b enabled by default.
65 static const ViewOption WITH_SCHEME;
67 * Option to include username in the URL string.
69 * This option depends on a enabled WITH_SCHEME and
70 * WITH_HOST options and is \b enabled by default.
72 static const ViewOption WITH_USERNAME;
74 * Option to include password in the URL string.
76 * This option depends on a enabled WITH_SCHEME,
77 * WITH_HOST and WITH_USERNAME options and is
78 * \b disabled by default, causing to hide the
79 * password in the URL authority.
81 static const ViewOption WITH_PASSWORD;
83 * Option to include hostname in the URL string.
85 * This option depends on a enabled WITH_SCHEME
86 * option and is \b enabled by default.
88 static const ViewOption WITH_HOST;
90 * Option to include port number in the URL string.
92 * This option depends on a enabled WITH_SCHEME and
93 * WITH_HOST options and is \b enabled by default.
95 static const ViewOption WITH_PORT;
97 * Option to include path name in the URL string.
99 * This option is \b enabled by default.
101 static const ViewOption WITH_PATH_NAME;
103 * Option to include path parameters in the URL string.
105 * This option depends on a enabled WITH_PATH_NAME
106 * option and is \b disabled by default, causing to
107 * hide the path parameters.
109 static const ViewOption WITH_PATH_PARAMS;
111 * Option to include query string in the URL string.
113 * This option is \b enabled by default.
115 static const ViewOption WITH_QUERY_STR;
117 * Option to include fragment string in the URL string.
119 * This option is \b enabled by default.
121 static const ViewOption WITH_FRAGMENT;
126 * Explicitely include the URL authority separator "//".
128 * It causes, that the URL string includes an empty URL
129 * authority, for example:
130 * "file:///foo.txt" instead of just "file:/foo.txt".
132 * This option depends on a enabled WITH_SCHEME view
133 * option and is enabled by default.
135 static const ViewOption EMPTY_AUTHORITY;
137 * Explicitely include the "/" path character.
139 * It causes, that a "/" is added to the Url if the path
140 * name is empty, for example:
142 * "http://localhost/" instead of just "http://localhost".
144 * This option depends on a enabled WITH_PATH_NAME view
145 * option and is enabled by default.
147 static const ViewOption EMPTY_PATH_NAME;
149 * Explicitely include the path parameters separator ";".
151 * It causes, that the URL allways contains the ";" path
152 * parameters separator.
154 * This option depends on a enabled EMPTY_PATH_NAME view
155 * option and is disabled by default.
157 static const ViewOption EMPTY_PATH_PARAMS;
159 * Explicitely include the query string separator "?".
161 * It causes, that if the query string is requested using
162 * the WITH_QUERY_STR option, the URL allways contains the
163 * "?" query string separator, even if the query string is
165 * This option depends on a enabled WITH_QUERY_STR view
166 * option and is disabled by default.
168 static const ViewOption EMPTY_QUERY_STR;
170 * Explicitely include the fragment string separator "#".
172 * It causes, that if the fragment string is requested using
173 * the WITH_FRAGMENT option, the URL allways contains the "#"
174 * fragment string separator, even if the fragment string is
176 * This option depends on a enabled WITH_FRAGMENT view
177 * option and is disabled by default.
179 static const ViewOption EMPTY_FRAGMENT;
184 * Default combination of view options.
186 * By default, following view options are enabled:
187 * WITH_SCHEME, WITH_USERNAME, WITH_HOST,
188 * WITH_PORT, WITH_PATH_NAME, WITH_QUERY_STR,
189 * WITH_FRAGMENT, EMPTY_AUTHORITY, EMPTY_PATH_NAME.
191 static const ViewOption DEFAULTS;
195 * Create instance with default combination of view options.
197 ViewOption(): opt(DEFAULTS.opt)
201 * Adds \p l and \p r to a new option combination.
203 * @return The new option combination.
205 friend inline ViewOption
206 operator + (const ViewOption &l, const ViewOption &r)
207 { return ViewOption(l.opt | r.opt); }
210 * Substract \p r from \p l to a new option combination.
212 * @return The new option combination.
214 friend inline ViewOption
215 operator - (const ViewOption &l, const ViewOption &r)
216 { return ViewOption(l.opt & ~r.opt); }
219 * Assign specified option combination \p o to the current object.
221 * \param o The option or option combination to make a copy of.
222 * \return A reference to this option combination.
225 operator = (const ViewOption &o)
226 { opt = o.opt; return *this; }
229 * Check if specified option \p o is set in the current object.
230 * \param o A view option constant.
231 * \return True, if specified option \p o is
232 * set/enabled in the instance.
235 has(const ViewOption &o) const
236 { return o.opt & opt; }
239 ViewOption(int o): opt(o) {}
244 // ---------------------------------------------------------------
246 * ViewOptions is just an alias for ViewOption.
248 typedef ViewOption ViewOptions;
251 // ---------------------------------------------------------------
253 * Internal data as used by UrlBase.
261 UrlData(const UrlConfig &conf)
273 std::string pathname;
274 std::string pathparams;
275 std::string querystr;
276 std::string fragment;
280 // ---------------------------------------------------------------
282 * \brief Generic Url base class.
284 * The UrlBase class implements default behaviour for URL
285 * manipulations and a base for implementation of scheme-
286 * specialized URL's for the Url class.
297 * Create a new Url object as copy of the given one.
298 * \param url The Url object to make a copy of.
300 UrlBase(const UrlBase &url);
303 * \brief Construct new object and initializes it with
304 * specified URL components.
306 * \param scheme A scheme name.
307 * \param authority A authority component data (encoded).
308 * \param pathdata A path component data (encoded).
309 * \param querystr A query string (encoded).
310 * \param fragment A fragment component (encoded),
312 UrlBase(const std::string &scheme,
313 const std::string &authority,
314 const std::string &pathdata,
315 const std::string &querystr,
316 const std::string &fragment);
321 * \brief Clears all data in the object.
327 * Returns pointer to a copy of the current object.
329 * Should be reimplemented by all derived object using
330 * the copy constructor of the derived class, e.g.:
332 * return new MyUrlDerivedFromUrlBase(*this);
335 * \return A pointer to a copy of the current object.
341 * \brief Initializes current object with new URL components.
343 * \param scheme A scheme name.
344 * \param authority A authority component data (encoded).
345 * \param pathdata A path component data (encoded).
346 * \param querystr A query string (encoded).
347 * \param fragment A fragment component (encoded),
350 init(const std::string &scheme,
351 const std::string &authority,
352 const std::string &pathdata,
353 const std::string &querystr,
354 const std::string &fragment);
359 * \brief Returns scheme names known by this object.
361 * This method is used in the isValidScheme() method and
362 * is intended to be reimplemented by derived classes to
363 * return the scheme names it implements (is restricted
366 * For example, if your derived class implements special
367 * features of LDAP URL's, this method may return "ldap"
368 * and "ldaps" scheme names.
370 * The UrlBase class returns an empty vector, that signals
371 * that it is useable with all URL's.
373 * \return A vector with scheme names known by this object.
376 getKnownSchemes() const;
379 * \brief Returns if scheme name is known to this object.
380 * \return True, if scheme name is known to this object.
383 isKnownScheme(const std::string &scheme) const;
387 * \brief Verifies specified scheme name.
389 * Verifies the generic syntax of the specified \p scheme name
390 * and if it is contained in the current object's list of known
391 * schemes (see getKnownSchemes()) if the list is not empty (as
392 * in the UrlBase class).
394 * \return True, if generic scheme name syntax is valid and
395 * the scheme name is known to the current object.
398 isValidScheme(const std::string &scheme) const;
402 * \brief Verifies the Url.
404 * Verifies if the current object contains a non-empty scheme
405 * name. Additional semantical URL checks may be performed by
406 * derived UrlBase-objects.
408 * \return True, if the Url seems to be valid.
416 * Returns a default string representation of the Url object.
418 * By default, a password in the URL will be hidden.
420 * \return A default string representation of the Url object.
426 * Returns a string representation of the Url object.
428 * To include a password in the resulting Url string, use:
430 * url.toString(url::ViewOptions() +
431 * url::ViewOptions::WITH_PASSWORD);
437 * url.toString(url::ViewOptions::DEFAULTS +
438 * url::ViewOptions::WITH_PASSWORD);
441 * \param opts A combination of view options.
442 * \return A string representation of the Url object.
445 toString(const zypp::url::ViewOptions &opts) const;
450 * Returns the scheme name of the URL.
451 * \return Scheme name of the current Url object.
459 * Returns the encoded authority component of the URL.
461 * The returned authority string does not contain the leading
462 * "//" separator characters, but just its "user:pass@host:port"
465 * \return The encoded authority component string.
468 getAuthority() const;
471 * Returns the username from the URL authority.
472 * \param eflag Flag if the usename should be percent-decoded or not.
473 * \return The username sub-component from the URL authority.
476 getUsername(EEncoding eflag) const;
479 * Returns the password from the URL authority.
480 * \param eflag Flag if the password should be percent-decoded or not.
481 * \return The password sub-component from the URL authority.
484 getPassword(EEncoding eflag) const;
487 * Returns the hostname or IP from the URL authority.
489 * In case the Url contains an IP number, it may be surrounded
490 * by "[" and "]" characters, for example "[::1]" for an IPv6
493 * \param eflag Flag if the host should be percent-decoded or not.
494 * \return The host sub-component from the URL authority.
497 getHost(EEncoding eflag) const;
500 * Returns the port from the URL authority.
501 * \return The port sub-component from the URL authority.
509 * Returns the encoded path component of the URL.
511 * The path data contains the path name, optionally
512 * followed by path parameters separated with a ";"
513 * character, for example "/foo/bar;version=1.1".
515 * \return The encoded path component of the URL.
521 * Returns the path name from the URL.
522 * \param eflag Flag if the path should be decoded or not.
523 * \return The path name sub-component without path parameters
524 * from Path-Data component of the URL.
527 getPathName(EEncoding eflag) const;
530 * Returns the path parameters from the URL.
531 * \return The encoded path parameters from the URL.
534 getPathParams() const;
537 * Returns a vector with path parameter substrings.
539 * The default path parameter separator is the \c ',' character.
540 * A schema specific object may overide the default separators.
542 * For example, the path parameters string "foo=1,bar=2" is splited
543 * by default into a vector containing the substrings "foo=1" and
546 * \return The path parameters splited into a vector of substrings.
548 virtual zypp::url::ParamVec
549 getPathParamsVec() const;
552 * Returns a string map with path parameter keys and values.
554 * The default path parameter separator is the \c ',' character,
555 * the default key/value separator for the path parameters is
556 * the \c '=' character.
557 * A schema specific object may overide the default separators.
559 * For example, the path parameters string "foo=1,bar=2" is splited
560 * into a map containing "foo" = "1" and "bar" = "2" by default.
562 * \param eflag Flag if the path parameter keys and values should
564 * \return The path parameters key and values as a string map.
566 virtual zypp::url::ParamMap
567 getPathParamsMap(EEncoding eflag) const;
570 * Return the value for the specified path parameter.
572 * For example, if the path parameters string is "foo=1,bar=2"
573 * the method will return the substring "1" for the param key
574 * "foo" and "2" for the param key "bar".
576 * \param param The path parameter key.
577 * \param eflag Flag if the path parameter keys and values should
579 * \return The value for the path parameter key or empty string.
582 getPathParam(const std::string ¶m, EEncoding eflag) const;
587 * Returns the encoded query string component of the URL.
589 * The query string is returned without first "?" (separator)
590 * character. Further "?" characters as in e.g. LDAP URL's
591 * remains in the returned string.
593 * \return The encoded query string component of the URL.
596 getQueryString() const;
599 * Returns a vector with query string parameter substrings.
601 * The default query string parameter separator is the \c '&'
603 * A schema specific object may overide the default separators.
605 * For example, the query string "foo=1&bar=2" is splited by
606 * default into a vector containing the substrings "foo=1" and
609 * \return The query string splited into a vector of substrings.
611 virtual zypp::url::ParamVec
612 getQueryStringVec() const;
615 * Returns a string map with query parameter and their values.
617 * The default query string parameter separator is the \c ','
618 * character, the default key/value separator the \c '=' character.
619 * A schema specific object may overide the default separators.
621 * For example, the query string "foo=1&bar=2" is splited by
622 * default into a map containing "foo" = "1" and "bar" = "2".
624 * \param eflag Flag if the query string keys and values should
626 * \return The query string as a key/value string map.
628 virtual zypp::url::ParamMap
629 getQueryStringMap(EEncoding eflag) const;
632 * Return the value for the specified query parameter.
634 * For example, if the query string is "foo=1,bar=2" the method
635 * will return the substring "1" for the param key "foo" and
636 * "2" for the param key "bar".
638 * \param param The query parameter key.
639 * \param eflag Flag if the query parameter keys and values should
641 * \return The value for the query parameter key or empty string.
644 getQueryParam(const std::string ¶m, EEncoding eflag) const;
649 * Returns the encoded fragment component of the URL.
650 * \param eflag Flag if the fragment should be percent-decoded or not.
651 * \return The encoded fragment component of the URL.
654 getFragment(EEncoding eflag) const;
659 * \brief Set the scheme name in the URL.
660 * \param scheme The new scheme name.
663 setScheme(const std::string &scheme);
668 * \brief Set the authority component in the URL.
670 * The \p authority string shoud not contain any leading
671 * "//" separator characters (just "user:pass@host:port").
673 * \param authority The authority component string.
676 setAuthority(const std::string &authority);
679 * \brief Set the username in the URL authority.
680 * \param user The new username.
683 setUsername(const std::string &user);
686 * \brief Set the password in the URL authority.
687 * \param pass The new password.
690 setPassword(const std::string &pass);
693 * \brief Set the hostname or IP in the URL authority.
694 * \param host The new hostname or IP.
697 setHost(const std::string &host);
700 * \brief Set the port number in the URL authority.
701 * \param port The new port number.
704 setPort(const std::string &port);
709 * \brief Set the path data component in the URL.
711 * The \p pathdata string may include path parameters
712 * separated using the ";" separator character.
714 * \param pathdata The encoded path data component string.
717 setPathData(const std::string &pathdata);
720 * \brief Set the path name.
721 * \param path The new path name.
724 setPathName(const std::string &path);
727 * \brief Set the path parameters.
728 * \param params The new path parameter string.
731 setPathParams(const std::string ¶ms);
734 * \brief Set the path parameters.
735 * \param pvec The vector with path parameters.
738 setPathParamsVec(const zypp::url::ParamVec &pvec);
741 * \brief Set the path parameters.
742 * \param pmap The map with path parameters.
745 setPathParamsMap(const zypp::url::ParamMap &pmap);
748 * \brief Set or add value for the specified path parameter.
749 * \param param The path parameter name.
750 * \param value The path parameter value.
753 setPathParam(const std::string ¶m, const std::string &value);
758 * \brief Set the query string in the URL.
760 * The \p querystr string will be supposed to not to
761 * contain the "?" separator character.
763 * \param querystr The new encoded query string.
766 setQueryString(const std::string &querystr);
769 * \brief Set the query parameters.
770 * \param qvec The vector with query parameters.
773 setQueryStringVec(const zypp::url::ParamVec &qvec);
776 * \brief Set the query parameters.
777 * \param qmap The map with query parameters.
780 setQueryStringMap(const zypp::url::ParamMap &qmap);
783 * \brief Set or add value for the specified query parameter.
784 * \param param The query parameter name.
785 * \param value The query parameter value.
788 setQueryParam(const std::string ¶m, const std::string &value);
793 * \brief Set the fragment string in the URL.
794 * \param fragment The new encoded fragment string.
797 setFragment(const std::string &fragment);
802 * Configures behaviour of the instance.
804 * This method is called in UrlBase constructors before
805 * any URL components are applied.
806 * Derived classes may reimplement this method to change
807 * the behaviour of the object.
808 * Use the config() methods to query and change them.
810 * The UrlBase class uses following config variables:
812 * - Path parameter separators:
813 * - \a \c sep_pathparams \c ";"
814 * Separator used to split path parameters from path name.
815 * - \a \c psep_pathparam \c ","
816 * Separator between path parameters.
817 * - \a \c vsep_pathparam \c "="
818 * Separator between key and value of a path parameter.
822 * - Query string separators:
823 * - \a \c psep_querystr \c "&"
824 * Separator between query string parameters.
825 * - \a \c vsep_querystr \c "="
826 * Separator between key and value of a query parameter.
830 * - Characters in URL components, that are safe without
831 * URL percent-encoding (see zypp::url::encode()).
832 * - \a safe_username ""
833 * - \a safe_password ""
834 * - \a safe_hostname "[:]"
835 * - \a safe_pathname "/"
836 * - \a safe_pathparams ""
837 * - \a safe_querystr ""
838 * - \a safe_fragment ""
842 * - Regular expressions used to verify URL components
843 * and their sub-components:
844 * - \a rx_scheme "^[a-zA-Z][a-zA-Z0-9\\._-]*$"
845 * - \a rx_username ".*"
846 * - \a rx_password ".*"
847 * - \a rx_hostname ".*" FIXME!
848 * - \a rx_port "^[0-9]{1,5}$"
849 * - \a rx_pathname ".*"
850 * - \a rx_pathparams ".*"
851 * - \a rx_querystr ".*"
852 * - \a rx_fragment ".*"
861 * Get the value of a UrlBase configuration variable.
863 * See configure() method for names an purpose of the
864 * configuration variables used in UrlBase class.
866 * \param opt The name of the configuration variable.
867 * \return The value of the specified variable
871 config(const std::string &opt) const;
876 * Set the value of a UrlBase configuration variable.
878 * See configure() method for names an purpose of the
879 * configuration variables used in UrlBase class.
881 * \param opt The name of the configuration variable.
882 * \param val The new value for the configuration variable.
885 config(const std::string &opt, const std::string &val);
889 * Return the view options of the current object.
891 * This method is used to query the view options
892 * used by the Url::toString() method.
894 * \return The current view option combination.
897 getViewOptions() const;
900 * Change the view options of the current object.
902 * This method is used to change the view options
903 * used by the Url::toString() method.
905 * \param vopts New view options combination.
908 setViewOptions(const ViewOptions &vopts);
913 * Utility method to cleanup a path name.
915 * By default, this method replaces multiple occurences
916 * of the "/" characeter from the begining of the path,
917 * so the cleaned path begins with at most one "/".
919 * This operation is required in some cases to not to
920 * missinterpret multiple "/" occurences as an empty
923 * \param path A path name.
924 * \return A path begining with at most one "/" character.
927 cleanupPathName(const std::string &path);
935 // ---------------------------------------------------------------
937 * \brief Copy-On-Write Url reference.
939 typedef RWCOW_pointer<UrlBase> UrlRef;
942 //////////////////////////////////////////////////////////////////
944 ////////////////////////////////////////////////////////////////////
946 ////////////////////////////////////////////////////////////////////
948 //////////////////////////////////////////////////////////////////////
950 #endif /* ZYPP_URL_URLBASE_H */
952 ** vim: set ts=2 sts=2 sw=2 ai et: