1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
15 #include <zypp/url/UrlBase.h>
16 #include <zypp/url/UrlUtils.h>
19 //////////////////////////////////////////////////////////////////////
21 { ////////////////////////////////////////////////////////////////////
26 * \brief Url manipulation class.
28 * The generic URL (URI) syntax and its main components are defined in
29 * RFC3986 (http://rfc.net/rfc3986.html) Section 3, "Syntax Components".
30 * The scheme specific URL syntax and semantics is defined in the
31 * specification of the particular scheme. See also RFC1738
32 * (http://rfc.net/rfc1738.html), that defines specific syntax for
33 * several URL schemes.
35 * This class provides methods to access and manipulate generic and
36 * common scheme-specific URL components (or using the more general
37 * term, URI components).
38 * To consider the scheme-specifics of a URL, the Url class contains
39 * a reference object pointing to a UrlBase or derived object, that
40 * implements the scheme specifics.
42 * Using the Url::registerScheme() method, it is possible to register
43 * a preconfigured or derived UrlBase object for a specific scheme
44 * name. The registered object will be cloned to handle all URL's
45 * containing the specified scheme name.
47 * \par RFC3986, Syntax Components:
49 * The generic URI syntax consists of a hierarchical sequence of
50 * components referred to as the scheme, authority, path, query,
54 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
56 * hier-part = "//" authority path-abempty
62 * The scheme and path components are required, though the path may be
63 * empty (no characters).
64 * When authority is present, the path must either be empty or begin
65 * with a slash ("/") character.
66 * When authority is not present, the path cannot begin with two slash
68 * These restrictions result in five different ABNF rules for a path
69 * (Section 3.3), only one of which will match any given URI reference.
71 * The following are two example URIs and their component parts:
73 * foo://example.com:8042/over/there?name=ferret#nose
74 * \_/ \______________/\_________/ \_________/ \__/
76 * scheme authority path query fragment
77 * | _____________________|__
79 * urn:example:animal:ferret:nose
89 typedef zypp::url::EEncoding EEncoding;
94 typedef zypp::url::ViewOptions ViewOptions;
101 * Create a new Url object as shared copy of the given one.
103 * Upon return, both objects will point to the same underlying
104 * object. This state will remain until one of the object is
107 * \param url The Url object to make a copy of.
108 * \throws url::UrlException if copy fails (should not happen).
114 * Create a new Url object as shared copy of the given reference.
116 * Upon return, both objects will point to the same underlying
117 * object. This state will remain until one of the object is
120 * \param url The URL implementation reference to make a copy of.
121 * \throws url::UrlException if reference is empty.
123 Url(const zypp::url::UrlRef &url);
127 * \brief Construct a Url object from percent-encoded URL string.
129 * Parses the \p encodedUrl string using the parseUrl() method
130 * and assings the result to the new created object.
132 * \param encodedUrl A percent-encoded URL string.
133 * \throws url::UrlParsingException if parsing of the url fails.
134 * \throws url::UrlNotAllowedException if one of the components
135 * is not allowed for the scheme.
136 * \throws url::UrlBadComponentException if one of the components
137 * contains an invalid character.
139 Url(const std::string &encodedUrl);
144 * \brief Parse a percent-encoded URL string.
146 * Trys to parses the given string into generic URL components
147 * and created a clone of a scheme-specialized object or a new
150 * \param encodedUrl A percent-encoded URL string.
151 * \return A reference to a (derived) UrlBase object or
152 * empty reference if the \p encodedUrl string
153 * does not match the generic URL syntax.
154 * \throws url::UrlNotAllowedException if one of the components
155 * is not allowed for the scheme.
156 * \throws url::UrlBadComponentException if one of the components
157 * contains an invalid character.
160 parseUrl(const std::string &encodedUrl);
165 * \brief Assigns parsed percent-encoded URL string to the object.
167 * Parses \p encodedUrl string using the parseUrl() method
168 * and assigns the result to the current object.
170 * \param encodedUrl A percent-encoded URL string.
171 * \return A reference to this Url object.
172 * \throws url::UrlParsingException if parsing of the url fails.
173 * \throws url::UrlNotAllowedException if one of the components
174 * is not allowed for the scheme.
175 * \throws url::UrlBadComponentException if one of the components
176 * contains an invalid character.
179 operator = (const std::string &encodedUrl);
183 * \brief Assign shared copy of \p url to the current object.
185 * Upon return, both objects will point to the same underlying
186 * object. This state will remain until one of the object is
189 * \param url The Url object to make a copy of.
190 * \return A reference to this Url object.
193 operator = (const Url &url);
198 * \brief Register a scheme-specific implementation.
200 * \param scheme A name of a scheme.
201 * \param urlImpl A UrlBase object specialized for this scheme.
202 * \return True, if the object claims to implement the scheme.
205 registerScheme(const std::string &scheme,
206 url::UrlRef urlImpl);
209 * \brief Returns all registered scheme names.
210 * \return A vector with registered URL scheme names.
212 static zypp::url::UrlSchemes
213 getRegisteredSchemes();
216 * \brief Returns if scheme name is registered.
217 * \return True, if scheme name is registered.
220 isRegisteredScheme(const std::string &scheme);
225 * \brief Returns scheme names known to this object.
226 * \return A vector with scheme names known by this object.
228 zypp::url::UrlSchemes
229 getKnownSchemes() const;
233 * \brief Verifies specified scheme name.
235 * Verifies the generic syntax of the specified \p scheme name
236 * and if it is contained in the current object's list of known
237 * schemes (see getKnownSchemes()) if the list is not empty.
239 * The default implementation in the UrlBase class returns an
240 * emtpy list of known schemes, causing a check of the generic
243 * \return True, if generic scheme name syntax is valid and
244 * the scheme name is known to the current object.
247 isValidScheme(const std::string &scheme) const;
251 * \brief Verifies the Url.
253 * Verifies if the current object contains a non-empty scheme
254 * name. Additional semantical URL checks may be performed by
255 * derived UrlBase objects.
257 * \return True, if the Url seems to be valid.
265 * Returns a default string representation of the Url object.
267 * By default, a password in the URL will be hidden.
269 * \return A default string representation of the Url object.
275 * Returns a string representation of the Url object.
277 * To include a password in the resulting Url string, use:
279 * url.asString(url.getViewOptions() +
280 * url::ViewOptions::WITH_PASSWORD);
283 * \param opts A combination of view options.
284 * \return A string representation of the Url object.
287 asString(const ViewOptions &opts) const;
290 * Returns a complete string representation of the Url object.
292 * This function ignores the configuration of the view options
293 * in the current object (see setViewOption()) and forces to
294 * return an string with all URL components included.
296 * \return A complete string representation of the Url object.
299 asCompleteString() const;
304 * Returns the scheme name of the URL.
305 * \return Scheme name of the current Url object.
313 * Returns the encoded authority component of the URL.
315 * The returned authority string does not contain the leading
316 * "//" separator characters, but just its "user:pass@host:port"
319 * \return The encoded authority component string.
322 getAuthority() const;
325 * Returns the username from the URL authority.
326 * \param eflag Flag if the usename should be percent-decoded or not.
327 * \return The username sub-component from the URL authority.
328 * \throws url::UrlDecodingException if the decoded result string
329 * would contain a '\\0' character.
332 getUsername(EEncoding eflag = zypp::url::E_DECODED) const;
335 * Returns the password from the URL authority.
336 * \param eflag Flag if the password should be percent-decoded or not.
337 * \return The password sub-component from the URL authority.
338 * \throws url::UrlDecodingException if the decoded result string
339 * would contain a '\\0' character.
342 getPassword(EEncoding eflag = zypp::url::E_DECODED) const;
345 * Returns the hostname or IP from the URL authority.
347 * In case the Url contains an IP number, it may be surrounded
348 * by "[" and "]" characters, for example "[::1]" for an IPv6
351 * \param eflag Flag if the host should be percent-decoded or not.
352 * \return The host sub-component from the URL authority.
353 * \throws url::UrlDecodingException if the decoded result string
354 * would contain a '\\0' character.
357 getHost(EEncoding eflag = zypp::url::E_DECODED) const;
360 * Returns the port from the URL authority.
361 * \return The port sub-component from the URL authority.
369 * Returns the encoded path component of the URL.
371 * The path data contains the path name, optionally
372 * followed by path parameters separated with a ";"
373 * character, for example "/foo/bar;version=1.1".
375 * \return The encoded path component of the URL.
381 * Returns the path name from the URL.
382 * \param eflag Flag if the path should be decoded or not.
383 * \return The path name sub-component without path parameters
384 * from Path-Data component of the URL.
385 * \throws url::UrlDecodingException if the decoded result string
386 * would contain a '\\0' character.
389 getPathName(EEncoding eflag = zypp::url::E_DECODED) const;
392 * Returns the path parameters from the URL.
393 * \return The encoded path parameters from the URL.
396 getPathParams() const;
399 * Returns a vector with path parameter substrings.
401 * The default path parameter separator is the \c ',' character.
402 * A schema specific object may overide the default separators.
404 * For example, the path parameters string "foo=1,bar=2" is splited
405 * by default into a vector containing the substrings "foo=1" and
408 * \return The path parameters splited into a vector of substrings.
411 getPathParamsVec() const;
414 * Returns a string map with path parameter keys and values.
416 * The default path parameter separator is the \c ',' character,
417 * the default key/value separator for the path parameters is
418 * the \c '=' character.
419 * A schema specific object may overide the default separators.
421 * For example, the path parameters string "foo=1,bar=2" is splited
422 * into a map containing "foo" = "1" and "bar" = "2" by default.
424 * \param eflag Flag if the path parameter keys and values should
426 * \return The path parameters key and values as a string map.
427 * \throws url::UrlNotSupportedException if parameter parsing
428 * is not supported for a URL (scheme).
429 * \throws url::UrlDecodingException if the decoded result string
430 * would contain a '\\0' character.
433 getPathParamsMap(EEncoding eflag = zypp::url::E_DECODED) const;
436 * Return the value for the specified path parameter.
438 * For example, if the path parameters string is "foo=1,bar=2"
439 * the method will return the substring "1" for the param key
440 * "foo" and "2" for the param key "bar".
442 * \param param The path parameter key.
443 * \param eflag Flag if the path parameter keys and values should
445 * \return The value for the path parameter key or empty string.
446 * \throws url::UrlNotSupportedException if parameter parsing
447 * is not supported for a URL (scheme).
448 * \throws url::UrlDecodingException if the decoded result string
449 * would contain a '\\0' character.
452 getPathParam(const std::string ¶m,
453 EEncoding eflag = zypp::url::E_DECODED) const;
458 * Returns the encoded query string component of the URL.
460 * The query string is returned without first "?" (separator)
461 * character. Further "?" characters as in e.g. LDAP URL's
462 * remains in the returned string.
464 * \return The encoded query string component of the URL.
467 getQueryString() const;
470 * Returns a vector with query string parameter substrings.
472 * The default query string parameter separator is the \c '&'
474 * A schema specific object may overide the default separators.
476 * For example, the query string "foo=1&bar=2" is splited by
477 * default into a vector containing the substrings "foo=1" and
480 * \return The query string splited into a vector of substrings.
483 getQueryStringVec() const;
486 * Returns a string map with query parameter and their values.
488 * The default query string parameter separator is the \c ','
489 * character, the default key/value separator the \c '=' character.
490 * A schema specific object may overide the default separators.
492 * For example, the query string "foo=1&bar=2" is splited by
493 * default into a map containing "foo" = "1" and "bar" = "2".
495 * \param eflag Flag if the query string keys and values should
497 * \return The query string as a key/value string map.
498 * \throws url::UrlNotSupportedException if parameter parsing
499 * is not supported for a URL (scheme).
500 * \throws url::UrlDecodingException if the decoded result string
501 * would contain a '\\0' character.
504 getQueryStringMap(EEncoding eflag = zypp::url::E_DECODED) const;
507 * Return the value for the specified query parameter.
509 * For example, if the query string is "foo=1,bar=2" the method
510 * will return the substring "1" for the param key "foo" and
511 * "2" for the param key "bar".
513 * \param param The query parameter key.
514 * \param eflag Flag if the query parameter keys and values should
516 * \return The value for the query parameter key or empty string.
517 * \throws url::UrlNotSupportedException if parameter parsing
518 * is not supported for a URL (scheme).
519 * \throws url::UrlDecodingException if the decoded result string
520 * would contain a '\\0' character.
523 getQueryParam(const std::string ¶m,
524 EEncoding eflag = zypp::url::E_DECODED) const;
529 * Returns the encoded fragment component of the URL.
530 * \param eflag Flag if the fragment should be percent-decoded or not.
531 * \return The encoded fragment component of the URL.
532 * \throws url::UrlDecodingException if the decoded result string
533 * would contain a '\\0' character.
536 getFragment(EEncoding eflag = zypp::url::E_DECODED) const;
541 * \brief Set the scheme name in the URL.
542 * \param scheme The new scheme name.
543 * \throws url::UrlBadComponentException if the \p scheme
544 * contains an invalid character or is empty.
547 setScheme(const std::string &scheme);
552 * \brief Set the authority component in the URL.
554 * The \p authority string shoud contain the "user:pass@host:port"
555 * sub-components without any leading "//" separator characters.
557 * \param authority The encoded authority component string.
558 * \throws url::UrlNotAllowedException if the \p authority
559 * has to be empty in for the current scheme.
560 * \throws url::UrlBadComponentException if the \p authority
561 * contains an invalid character.
562 * \throws url::UrlParsingException if \p authority parsing fails.
565 setAuthority(const std::string &authority);
568 * \brief Set the username in the URL authority.
569 * \param user The new username.
570 * \param eflag If the \p username is encoded or not.
571 * \throws url::UrlNotAllowedException if the \p user
572 * has to be empty in for the current scheme
573 * \throws url::UrlBadComponentException if the \p user
574 * contains an invalid character.
577 setUsername(const std::string &user,
578 EEncoding eflag = zypp::url::E_DECODED);
581 * \brief Set the password in the URL authority.
582 * \param pass The new password.
583 * \param eflag If the \p password is encoded or not.
584 * \throws url::UrlNotAllowedException if the \p pass
585 * has to be empty in for the current scheme.
586 * \throws url::UrlBadComponentException if the \p pass
587 * contains an invalid character.
590 setPassword(const std::string &pass,
591 EEncoding eflag = zypp::url::E_DECODED);
594 * \brief Set the hostname or IP in the URL authority.
596 * The \p host parameter may contain a hostname, an IPv4 address
597 * in dotted-decimal form or an IPv6 address literal encapsulated
598 * within square brackets (RFC3513, Sect. 2.2).
600 * A hostname may contain national alphanumeric UTF8 characters
601 * (letters other than ASCII a-z0-9), that will be encoded.
602 * This function allows to specify both, a encoded or decoded
605 * Other IP literals in "[v ... ]" square bracket format are not
606 * supported by the implementation in UrlBase class.
608 * \param host The new hostname or IP address.
609 * \throws url::UrlNotAllowedException if the \p host (authority)
610 * has to be empty in for the current scheme.
611 * \throws url::UrlBadComponentException if the \p host is invalid.
614 setHost(const std::string &host);
617 * \brief Set the port number in the URL authority.
618 * \param port The new port number.
619 * \throws url::UrlNotAllowedException if the \p port (authority)
620 * has to be empty in for the current scheme.
621 * \throws url::UrlBadComponentException if the \p port is invalid.
624 setPort(const std::string &port);
629 * \brief Set the path data component in the URL.
631 * By default, the \p pathdata string may include path
632 * parameters separated by the ";" separator character.
634 * \param pathdata The encoded path data component string.
635 * \throws url::UrlBadComponentException if the \p pathdata
636 * contains an invalid character.
639 setPathData(const std::string &pathdata);
642 * \brief Set the path name.
643 * \param path The new path name.
644 * \param eflag If the \p path name is encoded or not.
645 * \throws url::UrlBadComponentException if the \p path name
646 * contains an invalid character.
649 setPathName(const std::string &path,
650 EEncoding eflag = zypp::url::E_DECODED);
653 * \brief Set the path parameters.
654 * \param params The new encoded path parameter string.
655 * \throws url::UrlBadComponentException if the path \p params
656 * contains an invalid character.
659 setPathParams(const std::string ¶ms);
662 * \brief Set the path parameters.
663 * \param pvec The vector with encoded path parameters.
664 * \throws url::UrlBadComponentException if the \p pvec
665 * contains an invalid character.
668 setPathParamsVec(const zypp::url::ParamVec &pvec);
671 * \brief Set the path parameters.
672 * \param pmap The map with decoded path parameters.
673 * \throws url::UrlNotSupportedException if parameter parsing
674 * is not supported for a URL (scheme).
677 setPathParamsMap(const zypp::url::ParamMap &pmap);
680 * \brief Set or add value for the specified path parameter.
681 * \param param The decoded path parameter name.
682 * \param value The decoded path parameter value.
683 * \throws url::UrlNotSupportedException if parameter parsing
684 * is not supported for a URL (scheme).
685 * \throws url::UrlDecodingException if the decoded result string
686 * would contain a '\\0' character.
689 setPathParam(const std::string ¶m, const std::string &value);
694 * \brief Set the query string in the URL.
695 * \param querystr The new encoded query string.
696 * \throws url::UrlBadComponentException if the \p querystr
697 * contains an invalid character.
700 setQueryString(const std::string &querystr);
703 * \brief Set the query parameters.
704 * \param qvec The vector with encoded query parameters.
705 * \throws url::UrlBadComponentException if the \p qvec
706 * contains an invalid character.
709 setQueryStringVec(const zypp::url::ParamVec &qvec);
712 * \brief Set the query parameters.
713 * \param qmap The map with decoded query parameters.
714 * \throws url::UrlNotSupportedException if parameter parsing
715 * is not supported for a URL (scheme).
718 setQueryStringMap(const zypp::url::ParamMap &qmap);
721 * \brief Set or add value for the specified query parameter.
722 * \param param The decoded query parameter name.
723 * \param value The decoded query parameter value.
724 * \throws url::UrlNotSupportedException if parameter parsing
725 * is not supported for a URL (scheme).
726 * \throws url::UrlDecodingException if the decoded result string
727 * would contain a '\\0' character.
730 setQueryParam(const std::string ¶m, const std::string &value);
735 * \brief Set the fragment string in the URL.
736 * \param fragment The new fragment string.
737 * \param eflag If the \p fragment is encoded or not.
738 * \throws url::UrlBadComponentException if the \p fragment
739 * contains an invalid character.
742 setFragment(const std::string &fragment,
743 EEncoding eflag = zypp::url::E_DECODED);
748 * Return the view options of the current object.
750 * This method is used to query the view options
751 * used by the asString() method.
753 * \return The current view option combination.
756 getViewOptions() const;
759 * Change the view options of the current object.
761 * This method is used to change the view options
762 * used by the asString() method.
764 * \param vopts New view options combination.
767 setViewOptions(const ViewOptions &vopts);
773 std::ostream & operator<<( std::ostream & str, const Url & url );
776 * needed for std::set
778 bool operator<( const Url &lhs, const Url &rhs );
783 bool operator==( const Url &lhs, const Url &rhs );
786 bool operator!=( const Url &lhs, const Url &rhs );
788 ////////////////////////////////////////////////////////////////////
790 //////////////////////////////////////////////////////////////////////
792 #endif /* ZYPP_URL_H */
794 ** vim: set ts=2 sts=2 sw=2 ai et: