uri.h
Go to the documentation of this file.
1 /** URI C++ Library
2  *
3  * \section feat_sec Features
4  * <ol>
5  * <li><a href="http://tools.ietf.org/html/rfc3986">RFC-3986</a> compliant (most features supported)</li>
6  * <li>encodes and decodes URI</li>
7  * <li>separate classes for each part of the URI, e.g. URI authority</li>
8  * <li>path class supports easy composition and parsing of segments</li>
9  * <li>query class has \p find method for finding and converting query parameters</li>
10  * <li>conditional compilation for Microsoft DLLs (see apidefs.h)
11  * </ol>
12  *
13  * \section req_sec Requirements
14  * <ol>
15  * <li><a href="http://en.wikipedia.org/wiki/C%2B%2B_standard_library">standard library</a></li>
16  * <li><a href="http://www.boost.org/">boost library</a></li>
17  * </ol>
18  *
19  * \section lic_sec License (MIT)
20 Copyright (c) 2009 zooml.com
21 
22 Permission is hereby granted, free of charge, to any person obtaining a copy
23 of this software and associated documentation files (the "Software"), to deal
24 in the Software without restriction, including without limitation the rights
25 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 copies of the Software, and to permit persons to whom the Software is
27 furnished to do so, subject to the following conditions:
28 
29 The above copyright notice and this permission notice shall be included in
30 all copies or substantial portions of the Software.
31 
32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 THE SOFTWARE.
39  */
40 
41 /// URI library.
42 
43 #ifndef __TERRALIB_COMMON_INTERNAL_URI_H
44 #define __TERRALIB_COMMON_INTERNAL_URI_H
45 #include "../Config.h"
46 #include "scheme.h"
47 #include "authority.h"
48 #include "path.h"
49 #include "query.h"
50 #include "fragment.h"
51 #include "utils.h"
52 #include <string>
53 #include <iostream>
54 
55 namespace te
56 {
57  namespace common
58  {
59  namespace uri
60  {
61  /*!
62  \class uri
63 
64  \brief Uniform Resource Identifier (URI) reference.
65 
66  A "URI-reference" is either a URI or a relative reference.
67 
68  Syntax: Note that it is up to the application to correctly
69  compose a URI reference with a missing scheme or authority, i.e.
70  to make sure the initial path chars (if any) do not look like
71  a scheme or authority (it helps with this library that
72  the \c path class cannot have empty segments so it cannot
73  have a URI authority-like prefix).
74 
75  <pre>
76  URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
77  relative-ref = relative-part [ "?" query ] [ "#" fragment ]
78  relative-part = "//" authority path-abempty
79  | path-absolute
80  | path-noscheme
81  | path-empty
82  hier-part = "//" authority path-abempty
83  | path-absolute
84  | path-rootless
85  | path-empty
86  path-abempty = *( "/" segment )
87  path-absolute = "/" [ segment-nz *( "/" segment ) ]
88  path-noscheme = segment-nz-nc *( "/" segment )
89  path-rootless = segment-nz *( "/" segment )
90  path-empty = 0<pchar>
91  </pre>
92 
93  Example:
94  \code
95  te::common::uri::uri u("http://mydomain.com/a/b/c?id=12345&s=x%20y");
96  assert(u.scheme().string() == "http");
97  assert(u.authority().host() == "mydomain.com");
98  assert(u.path().absolute());
99  te::common::uri::path::const_iterator it = u.path().begin();
100  assert(*it == "a" && *++it == "b" && *++it == "c" && ++it == u.path().end());
101  int id;
102  std::string s;
103  bool is_null;
104  assert(u.query().find("id", id, is_null) && id == 12345 && !is_null);
105  assert(u.query().find("s", s, is_null) && s == "x y" && !is_null);
106  assert(!u.query().find("foo", s, is_null));
107  \endcode
108  @see http://tools.ietf.org/html/rfc3986
109  */
111  public:
112  typedef te::common::uri::scheme scheme_type; ///< scheme type
113  typedef te::common::uri::authority authority_type; ///< authority type
114  typedef te::common::uri::path path_type; ///< path type
115  typedef te::common::uri::query query_type; ///< query type
116  typedef te::common::uri::fragment fragment_type; ///< fragment type
117  uri(); ///< Construct.
118  /// Construct from encoded string.
119  /// @exception std::invalid_argument if invalid encoding
120  uri(const std::string& v);
121  bool empty() const {return is_null();} ///< Test if null/empty.
122  bool is_null() const; ///< Test if null/empty.
123  bool relative() const {return scheme_.is_null();} ///< Test if relative (null scheme).
124  const scheme_type& scheme() const {return scheme_;} ///< Get scheme (null if relative).
125  scheme_type& scheme() {return scheme_;} ///< Get scheme (null if relative).
126  const authority_type& authority() const {return authority_;} ///< Get authority.
127  authority_type& authority() {return authority_;} ///< Get authority.
128  const path_type& path() const {return path_;} ///< Get path.
129  path_type& path() {return path_;} ///< Get path.
130  const query_type& query() const {return query_;} ///< Get query.
131  query_type& query() {return query_;} ///< Get query.
132  const fragment_type& fragment() const {return fragment_;} ///< Get fragment.
133  fragment_type& fragment() {return fragment_;} ///< Get fragment.
134  std::string encoding() const; ///< Calculate encoded string.
135  std::ostream& operator <<(std::ostream& os) const; ///< Stream out encoding.
136  private:
137  friend bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, uri& v, std::string* errs);
138  static bool parse_literal(std::string::const_iterator& first, std::string::const_iterator last, const char* v);
139  scheme_type scheme_;
140  authority_type authority_;
141  path_type path_;
142  query_type query_;
143  fragment_type fragment_;
144  };
145  /// Parse URI, returning whether found or not and advancing first
146  /// and setting URI if found. Does not skip leading space.
147  ///
148  /// If \p errs is specified parsing is more lax allowing decoding
149  /// and other errors and setting \p errs with the error messages.
150  /// See the individual component \c parse functions for details.
151  bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, uri& v, std::string* errs = 0);
152  /// Stream out URI encoding.
153  inline std::ostream& operator <<(std::ostream& os, const uri& v) {return v.operator <<(os);}
154  }
155  }
156 }
157 #endif
authority_type & authority()
Get authority.
Definition: uri.h:127
path_type & path()
Get path.
Definition: uri.h:129
bool relative() const
Test if relative (null scheme).
Definition: uri.h:123
scheme_type & scheme()
Get scheme (null if relative).
Definition: uri.h:125
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
const scheme_type & scheme() const
Get scheme (null if relative).
Definition: uri.h:124
Uniform Resource Identifier (URI) reference.
Definition: uri.h:110
URI path component.
Definition: path.h:51
bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Parse URI authority, returning whether found or not and advancing first and setting authority if foun...
te::common::uri::authority authority_type
authority type
Definition: uri.h:113
path_type path_
Definition: uri.h:141
URI fragment component.
Definition: fragment.h:41
te::common::uri::query query_type
query type
Definition: uri.h:115
bool empty() const
Test if null/empty.
Definition: uri.h:121
URI authority component (without userinfo).
Definition: authority.h:49
URI scheme component.
Definition: scheme.h:40
const fragment_type & fragment() const
Get fragment.
Definition: uri.h:132
scheme_type scheme_
Definition: uri.h:139
URI C++ Library.
fragment_type fragment_
Definition: uri.h:143
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
te::common::uri::path path_type
path type
Definition: uri.h:114
const authority_type & authority() const
Get authority.
Definition: uri.h:126
fragment_type & fragment()
Get fragment.
Definition: uri.h:133
te::common::uri::fragment fragment_type
fragment type
Definition: uri.h:116
URI query component.
Definition: query.h:54
te::common::uri::scheme scheme_type
scheme type
Definition: uri.h:112
const path_type & path() const
Get path.
Definition: uri.h:128
query_type query_
Definition: uri.h:142
const query_type & query() const
Get query.
Definition: uri.h:130
authority_type authority_
Definition: uri.h:140
query_type & query()
Get query.
Definition: uri.h:131