All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uri.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009 zooml.com
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #include "uri.h"
23 #include "urisyn.h"
24 #include <stdlib.h>
25 #include <sstream>
26 #include <algorithm>
27 #include <stdexcept>
28 namespace te
29 {
30  namespace common
31  {
32  namespace uri
33  {
34  uri::uri() {}
35  uri::uri(const std::string& v) {
36  std::string::const_iterator first = v.begin();
37  if (!parse(first, v.end(), *this) || first != v.end())
38  throw std::invalid_argument("invalid URI: \"" + v + "\"");
39  }
40  bool uri::is_null() const {
41  return scheme_.is_null() && authority_.is_null() && path_.empty() && query_.empty() && fragment_.empty();
42  }
43  std::string uri::encoding() const {
44  std::ostringstream oss;
45  operator <<(oss);
46  return oss.str();
47  }
48  std::ostream& uri::operator <<(std::ostream& os) const {
49  if (!scheme_.is_null())
50  os << scheme_ << urisyn::SCHEME_TRAITS.end_char;
51  if (!authority_.is_null())
52  os << urisyn::AUTHORITY_TRAITS.begin_cstring << authority_;
53  os << path_;
54  if (!query_.empty())
55  os << urisyn::QUERY_TRAITS.begin_char << query_;
56  if (!fragment_.empty())
57  os << urisyn::FRAGMENT_TRAITS.begin_char << fragment_;
58  return os;
59  }
60  bool uri::parse_literal(std::string::const_iterator& first, std::string::const_iterator last, const char* v) {
61  std::string::const_iterator f = first;
62  if (!v)
63  return false;
64  for (; *v;) {
65  if (f == last)
66  return false;
67  if (*f++ != *v++)
68  return false;
69  }
70  first = f;
71  return true;
72  }
73  bool parse(std::string::const_iterator& first, std::string::const_iterator last, uri& v, std::string* errs) {
74  std::string::const_iterator f = first;
75  char endc;
76  uri tmp;
77  std::string e;
78  // Parse scheme, if any.
79  if (parse(f, last, tmp.scheme_, &endc) && !endc) {
80  // Scheme name found but no ":": relative, so undo scheme parse.
81  f = first;
82  tmp.scheme_ = scheme();
83  }
84  // Parse authority, if any.
85  std::string::const_iterator anchor = f;
86  if (uri::parse_literal(f, last, urisyn::AUTHORITY_TRAITS.begin_cstring)) {
87 
88  if (!parse(f, last, tmp.authority_))
89  f = anchor; // Not authority (must be empty path segment instead).
90  } else
91  f = anchor;
92  // Added by Lauro
93  if(tmp.authority_.port() == 0) {
94  std::string protocol = tmp.scheme().string();
95  tmp.authority_.setDefaultPort(protocol);
96  }
97  // Parse path, if any.
98  parse(f, last, tmp.path_, errs ? &e : 0);
99  // Parse query, if any.
100  if (f != last && *f == urisyn::QUERY_TRAITS.begin_char) {
101  ++f;
102  parse(f, last, tmp.query_, errs ? &e : 0);
103  }
104  // Parse fragment, if any.
105  if (f != last && *f == urisyn::FRAGMENT_TRAITS.begin_char) {
106  ++f;
107  parse(f, last, tmp.fragment_, errs ? &e : 0);
108  }
109  if (tmp.is_null())
110  return false;
111  if (errs && !e.empty()) {
112  if (!errs->empty())
113  *errs += "; ";
114  *errs += "URI \"" + std::string(first, f) + "\": " + e;
115  }
116  v = tmp;
117  first = f;
118  return true;
119  }
120  }
121  }
122 }
friend bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, uri &v, std::string *errs)
Parse URI, returning whether found or not and advancing first and setting URI if found.
Definition: uri.cpp:73
const traits AUTHORITY_TRAITS
authority traits
Definition: urisyn.cpp:52
bool empty() const
Test if null/empty.
Definition: fragment.h:49
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
bool is_null() const
Test if null/empty.
Definition: authority.h:64
Uniform Resource Identifier (URI) reference.
Definition: uri.h:110
bool 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...
Definition: authority.cpp:156
path_type path_
Definition: uri.h:141
bool empty() const
Test if empty and not absolute.
Definition: path.h:62
URI scheme component.
Definition: scheme.h:40
scheme_type scheme_
Definition: uri.h:139
fragment_type fragment_
Definition: uri.h:143
const traits QUERY_TRAITS
query traits
Definition: urisyn.cpp:94
const traits SCHEME_TRAITS
scheme traits
Definition: urisyn.cpp:31
bool is_null() const
Test if null/empty.
Definition: scheme.h:50
query_type query_
Definition: uri.h:142
const traits FRAGMENT_TRAITS
fragment traits
Definition: urisyn.cpp:115
std::ostream & operator<<(std::ostream &os) const
Stream out encoding.
Definition: uri.cpp:48
authority_type authority_
Definition: uri.h:140