revert isLocal metod and use downloads instead to decide when cache packages.
[platform/upstream/libzypp.git] / zypp / Url.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /**
10  * \file zypp/Url.h
11  */
12 #ifndef   ZYPP_URL_H
13 #define   ZYPP_URL_H
14
15 #include <zypp/url/UrlBase.h>
16 #include <zypp/url/UrlUtils.h>
17
18
19 //////////////////////////////////////////////////////////////////////
20 namespace zypp
21 { ////////////////////////////////////////////////////////////////////
22
23
24   /**
25    * \class Url
26    * \brief Url manipulation class.
27    *
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.
34    *
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.
41    *
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.
46    *
47    * \par RFC3986, Syntax Components:
48    *
49    * The generic URI syntax consists of a hierarchical sequence of
50    * components referred to as the scheme, authority, path, query,
51    * and fragment.
52    *
53    * \code
54    *    URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
55    *
56    *    hier-part   = "//" authority path-abempty
57    *                / path-absolute
58    *                / path-rootless
59    *                / path-empty
60    * \endcode
61    *
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
67    * characters ("//").
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.
70    *
71    * The following are two example URIs and their component parts:
72    * \code
73    *      foo://example.com:8042/over/there?name=ferret#nose
74    *      \_/   \______________/\_________/ \_________/ \__/
75    *       |           |            |            |        |
76    *    scheme     authority       path        query   fragment
77    *       |   _____________________|__
78    *      / \ /                        \
79    *      urn:example:animal:ferret:nose
80    * \endcode
81    *
82    */
83   class Url
84   {
85   public:
86     /**
87      * Encoding flags.
88      */
89     typedef zypp::url::EEncoding    EEncoding;
90
91     /**
92      * View options.
93      */
94     typedef zypp::url::ViewOptions  ViewOptions;
95
96
97     ~Url();
98     Url();
99
100     /**
101      * Create a new Url object as shared copy of the given one.
102      *
103      * Upon return, both objects will point to the same underlying
104      * object. This state will remain until one of the object is
105      * modified.
106      *
107      * \param url The Url object to make a copy of.
108      * \throws url::UrlException if copy fails (should not happen).
109      */
110     Url(const Url &url);
111
112
113     /**
114      * Create a new Url object as shared copy of the given reference.
115      *
116      * Upon return, both objects will point to the same underlying
117      * object. This state will remain until one of the object is
118      * modified.
119      *
120      * \param url The URL implementation reference to make a copy of.
121      * \throws url::UrlException if reference is empty.
122      */
123     Url(const zypp::url::UrlRef &url);
124
125
126     /**
127      * \brief Construct a Url object from percent-encoded URL string.
128      *
129      * Parses the \p encodedUrl string using the parseUrl() method
130      * and assings the result to the new created object.
131      *
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.
138      */
139     Url(const std::string &encodedUrl);
140
141
142     // -----------------
143     /**
144      * \brief Parse a percent-encoded URL string.
145      *
146      * Trys to parses the given string into generic URL components
147      * and created a clone of a scheme-specialized object or a new
148      * UrlBase object.
149      *
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.
158      */
159     static url::UrlRef
160     parseUrl(const std::string &encodedUrl);
161
162
163     // -----------------
164     /**
165      * \brief Assigns parsed percent-encoded URL string to the object.
166      *
167      * Parses \p encodedUrl string using the parseUrl() method
168      * and assigns the result to the current object.
169      * 
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.
177      */
178     Url&
179     operator = (const std::string &encodedUrl);
180
181
182     /**
183      * \brief Assign shared copy of \p url to the current object.
184      *
185      * Upon return, both objects will point to the same underlying
186      * object. This state will remain until one of the object is
187      * modified.
188      *
189      * \param url The Url object to make a copy of.
190      * \return A reference to this Url object.
191      */
192     Url&
193     operator = (const Url &url);
194
195
196     // -----------------
197     /**
198      * \brief Register a scheme-specific implementation.
199      *
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.
203      */
204     static bool
205     registerScheme(const std::string &scheme,
206                    url::UrlRef       urlImpl);
207
208     /**
209      * \brief Returns all registered scheme names.
210      * \return A vector with registered URL scheme names.
211      */
212     static zypp::url::UrlSchemes
213     getRegisteredSchemes();
214
215     /**
216      * \brief Returns if scheme name is registered.
217      * \return True, if scheme name is registered.
218      */
219     static bool
220     isRegisteredScheme(const std::string &scheme);
221
222
223     // -----------------
224     /**
225      * \brief Returns scheme names known to this object.
226      * \return A vector with scheme names known by this object.
227      */
228     zypp::url::UrlSchemes
229     getKnownSchemes() const;
230
231
232     /**
233      * \brief Verifies specified scheme name.
234      *
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.
238      *
239      * The default implementation in the UrlBase class returns an
240      * emtpy list of known schemes, causing a check of the generic
241      * syntax only.
242      *
243      * \return True, if generic scheme name syntax is valid and
244      *         the scheme name is known to the current object.
245      */
246     bool
247     isValidScheme(const std::string &scheme) const;
248
249
250     /**
251      * \brief Verifies the Url.
252      *
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.
256      *
257      * \return True, if the Url seems to be valid.
258      */
259     bool
260     isValid() const;
261
262
263     // -----------------
264     /**
265      * Returns a default string representation of the Url object.
266      *
267      * By default, a password in the URL will be hidden.
268      *
269      * \return A default string representation of the Url object.
270      */
271     std::string
272     asString() const;
273
274     /**
275      * Returns a string representation of the Url object.
276      *
277      * To include a password in the resulting Url string, use:
278      * \code
279      *    url.asString(url.getViewOptions() +
280      *                 url::ViewOptions::WITH_PASSWORD);
281      * \endcode
282      *
283      * \param opts  A combination of view options.
284      * \return A string representation of the Url object. 
285      */
286     std::string
287     asString(const ViewOptions &opts) const;
288
289     /**
290      * Returns a complete string representation of the Url object.
291      *
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.
295      *
296      * \return A complete string representation of the Url object.
297      */
298     std::string
299     asCompleteString() const;
300
301
302     // -----------------
303     /**
304      * Returns the scheme name of the URL.
305      * \return Scheme name of the current Url object.
306      */
307     std::string
308     getScheme() const;
309
310
311     // -----------------
312     /**
313      * Returns the encoded authority component of the URL.
314      *
315      * The returned authority string does not contain the leading
316      * "//" separator characters, but just its "user:pass@host:port"
317      * content only.
318      *
319      * \return The encoded authority component string.
320      */
321     std::string
322     getAuthority() const;
323
324     /**
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.
330      */
331     std::string
332     getUsername(EEncoding eflag = zypp::url::E_DECODED) const;
333
334     /**
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.
340      */
341     std::string
342     getPassword(EEncoding eflag = zypp::url::E_DECODED) const;
343
344     /**
345      * Returns the hostname or IP from the URL authority.
346      *
347      * In case the Url contains an IP number, it may be surrounded
348      * by "[" and "]" characters, for example "[::1]" for an IPv6
349      * localhost address.
350      *
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.
355      */
356     std::string
357     getHost(EEncoding eflag = zypp::url::E_DECODED) const;
358
359     /**
360      * Returns the port from the URL authority.
361      * \return The port sub-component from the URL authority.
362      */
363     std::string
364     getPort() const;
365
366
367     // -----------------
368     /**
369      * Returns the encoded path component of the URL.
370      *
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".
374      *
375      * \return The encoded path component of the URL.
376      */
377     std::string
378     getPathData() const;
379
380     /**
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.
387      */
388     std::string
389     getPathName(EEncoding eflag = zypp::url::E_DECODED) const;
390
391     /**
392      * Returns the path parameters from the URL.
393      * \return The encoded path parameters from the URL.
394      */
395     std::string
396     getPathParams() const;
397
398     /**
399      * Returns a vector with path parameter substrings.
400      *
401      * The default path parameter separator is the \c ',' character.
402      * A schema specific object may overide the default separators.
403      *
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
406      * "bar=2".
407      *
408      * \return The path parameters splited into a vector of substrings.
409      */
410     zypp::url::ParamVec
411     getPathParamsVec() const;
412
413     /**
414      * Returns a string map with path parameter keys and values.
415      *
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.
420      *
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.
423      *
424      * \param eflag Flag if the path parameter keys and values should
425      *               be decoded or not.
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.
431      */
432     zypp::url::ParamMap
433     getPathParamsMap(EEncoding eflag = zypp::url::E_DECODED) const;
434
435     /**
436      * Return the value for the specified path parameter.
437      *
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".
441      *
442      * \param param The path parameter key.
443      * \param eflag Flag if the path parameter keys and values should
444      *              be decoded or not.
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.
450      */
451     std::string
452     getPathParam(const std::string &param,
453                  EEncoding eflag = zypp::url::E_DECODED) const;
454
455
456     // -----------------
457     /**
458      * Returns the encoded query string component of the URL.
459      *
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.
463      *
464      * \return The encoded query string component of the URL.
465      */
466     std::string
467     getQueryString() const;
468
469     /**
470      * Returns a vector with query string parameter substrings.
471      *
472      * The default query string parameter separator is the \c '&'
473      * character.
474      * A schema specific object may overide the default separators.
475      *
476      * For example, the query string "foo=1&bar=2" is splited by
477      * default into a vector containing the substrings "foo=1" and
478      * "bar=2".
479      *
480      * \return The query string splited into a vector of substrings.
481      */
482     zypp::url::ParamVec
483     getQueryStringVec() const;
484
485     /**
486      * Returns a string map with query parameter and their values.
487      *
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.
491      *
492      * For example, the query string "foo=1&bar=2" is splited by
493      * default into a map containing "foo" = "1" and "bar" = "2".
494      *
495      * \param eflag Flag if the query string keys and values should
496      *               be decoded or not.
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.
502      */
503     zypp::url::ParamMap
504     getQueryStringMap(EEncoding eflag = zypp::url::E_DECODED) const;
505
506     /**
507      * Return the value for the specified query parameter.
508      *
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".
512      *
513      * \param param The query parameter key.
514      * \param eflag Flag if the query parameter keys and values should
515      *              be decoded or not.
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.
521      */
522     std::string
523     getQueryParam(const std::string &param,
524                   EEncoding eflag = zypp::url::E_DECODED) const;
525
526
527     // -----------------
528     /**
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.
534      */
535     std::string
536     getFragment(EEncoding eflag = zypp::url::E_DECODED) const;
537
538
539     // -----------------
540     /**
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.
545      */
546     void
547     setScheme(const std::string &scheme);
548
549
550     // -----------------
551     /**
552      * \brief Set the authority component in the URL.
553      *
554      * The \p authority string shoud contain the "user:pass@host:port"
555      * sub-components without any leading "//" separator characters.
556      *
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.
563      */
564     void
565     setAuthority(const std::string &authority);
566
567     /**
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.
575      */
576     void
577     setUsername(const std::string &user,
578                 EEncoding         eflag = zypp::url::E_DECODED);
579
580     /**
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.
588      */
589     void
590     setPassword(const std::string &pass,
591                 EEncoding         eflag = zypp::url::E_DECODED);
592
593     /**
594      * \brief Set the hostname or IP in the URL authority.
595      *
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).
599      *
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
603      * hostname.
604      *
605      * Other IP literals in "[v ... ]" square bracket format are not
606      * supported by the implementation in UrlBase class.
607      *
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.
612      */
613     void
614     setHost(const std::string &host);
615
616     /**
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.
622      */
623     void
624     setPort(const std::string &port);
625
626
627     // -----------------
628     /**
629      * \brief Set the path data component in the URL.
630      *
631      * By default, the \p pathdata string may include path
632      * parameters separated by the ";" separator character.
633      *
634      * \param pathdata The encoded path data component string.
635      * \throws url::UrlBadComponentException if the \p pathdata
636      *         contains an invalid character.
637      */
638     void
639     setPathData(const std::string &pathdata);
640
641     /**
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.
647      */
648     void
649     setPathName(const std::string &path,
650                 EEncoding         eflag = zypp::url::E_DECODED);
651
652     /**
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.
657      */
658     void
659     setPathParams(const std::string &params);
660
661     /**
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.
666      */
667     void
668     setPathParamsVec(const zypp::url::ParamVec &pvec);
669
670     /**
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).
675      */
676     void
677     setPathParamsMap(const zypp::url::ParamMap &pmap);
678
679     /**
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.
687      */
688     void
689     setPathParam(const std::string &param, const std::string &value);
690
691
692     // -----------------
693     /**
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.
698      */
699     void
700     setQueryString(const std::string &querystr);
701
702     /**
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.
707      */
708     void
709     setQueryStringVec(const zypp::url::ParamVec &qvec);
710
711     /**
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).
716      */
717     void
718     setQueryStringMap(const zypp::url::ParamMap &qmap);
719
720     /**
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.
728      */
729     void
730     setQueryParam(const std::string &param, const std::string &value);
731
732
733     // -----------------
734     /**
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.
740      */
741     void
742     setFragment(const std::string &fragment,
743                 EEncoding         eflag = zypp::url::E_DECODED);
744
745
746     // -----------------
747     /**
748      * Return the view options of the current object.
749      *
750      * This method is used to query the view options
751      * used by the asString() method.
752      *
753      * \return The current view option combination.
754      */
755     ViewOptions
756     getViewOptions() const;
757
758     /**
759      * Change the view options of the current object.
760      *
761      * This method is used to change the view options
762      * used by the asString() method.
763      *
764      * \param vopts New view options combination.
765      */
766     void
767     setViewOptions(const ViewOptions &vopts);
768
769   private:
770     url::UrlRef m_impl;
771   };
772
773   std::ostream & operator<<( std::ostream & str, const Url & url );
774
775   /**
776    * needed for std::set
777    */
778   bool operator<( const Url &lhs, const Url &rhs );
779   
780   /**
781    * needed for find
782    */
783   bool operator==( const Url &lhs, const Url &rhs );
784   
785   
786   bool operator!=( const Url &lhs, const Url &rhs );
787   
788   ////////////////////////////////////////////////////////////////////
789 } // namespace zypp
790 //////////////////////////////////////////////////////////////////////
791
792 #endif /* ZYPP_URL_H */
793 /*
794 ** vim: set ts=2 sts=2 sw=2 ai et:
795 */