All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
urisyn.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 "urisyn.h"
23 namespace te
24 {
25  namespace common
26  {
27  namespace uri
28  {
29  namespace urisyn {
30  const char ENCODE_BEGIN_CHAR = '%';
32  0, 0, ':',
33  {
50  }
51  };
53  "//", 0, 0,
54  {
71  }
72  };
73  const traits PATH_TRAITS = {
74  0, 0, 0,
75  { // '/' is invalid
92  }
93  };
94  const traits QUERY_TRAITS = {
95  0, '?', 0,
96  { // '=' and '&' are invalid
113  }
114  };
115  const traits FRAGMENT_TRAITS = { // lauro: 124 (bar caracter "|") was changed (CINV to CVAL)
116  0, '#', 0,
117  {
134  }
135  };
136  bool parse(const traits& ts, std::string::const_iterator& first, std::string::const_iterator last, std::string& comp, char* endc) {
137  std::string::const_iterator f = first;
138  char ec = 0;
139  for (; f != last; ++f) {
140  char c = *f;
141  char cls = ts.char_class[(unsigned char)c];
142  if (cls == CEND) { // End char.
143  ec = c;
144  break;
145  }
146  if (cls != CVAL && (f == first || cls != CVA2))
147  break;
148  }
149  if (f == first && !ec)
150  return false;
151  comp.assign(first, f);
152  if (endc)
153  *endc = ec;
154  first = ec ? f + 1 : f;
155  return true;
156  }
157  std::string encode(const traits& ts, const std::string& comp) { // Modified by Lauro
158  std::string::const_iterator f = comp.begin();
159  std::string s;
160  for (; f != comp.end(); f++) {
161  char c = *f;
162  if (ts.char_class[(unsigned char)c] < CVAL || c == ENCODE_BEGIN_CHAR) { // Must encode.
163  s.append(1, ENCODE_BEGIN_CHAR);
164  append_hex(c, s); // Convert.
165  }
166  else
167  s.append(1, c); // Catch up to this char.
168  }
169  return s;
170  }
171  bool decode(std::string& s) { // Modified by Lauro - change plus to space
172  size_t pos = s.find(ENCODE_BEGIN_CHAR);
173  if (pos == std::string::npos) // Handle the "99%" case fast.
174  {
176  return true;
177  }
178  std::string v;
179  for (size_t i = 0;;) {
180  if (pos == std::string::npos) {
181  v.append(s, i, s.size() - i); // Append up to end.
182  break;
183  }
184  v.append(s, i, pos - i); // Append up to char.
185  i = pos + 3; // Skip all 3 chars.
186  char c;
187  if (!parse_hex(s, pos + 1, c)) // Convert hex.
188  return false;
189  v.insert(v.end(), c); // Append converted hex.
190  pos = s.find(ENCODE_BEGIN_CHAR, i); // Find next
191  }
192  s = v;
193  convertPlus2Space(s); // Added by Lauro
194  return true;
195  }
196  void convertPlus2Space(std::string& s) { // Added by Lauro
197  size_t p = s.find("+");
198  while(p != std::string::npos) {
199  s.replace(p, 1, 1, ' ');
200  p = s.find("+");
201  }
202  }
203  }
204  }
205  }
206 }
const traits AUTHORITY_TRAITS
authority traits
Definition: urisyn.cpp:52
const char ENCODE_BEGIN_CHAR
encode begin char ('%')
Definition: urisyn.cpp:30
void append_hex(char v, std::string &s)
Convert the char v to hex and add the 2 chars to the end of s.
Definition: utils.cpp:206
char char_class[256]
map of char to class
Definition: urisyn.h:47
void convertPlus2Space(std::string &s)
Definition: urisyn.cpp:196
bool parse(const traits &ts, std::string::const_iterator &first, std::string::const_iterator last, std::string &comp, char *endc)
Parse the URI componet, returning whether successful and setting the string and end char and advancin...
Definition: urisyn.cpp:136
std::string encode(const traits &ts, const std::string &comp)
Encode the URI (sub) component.
Definition: urisyn.cpp:157
const traits QUERY_TRAITS
query traits
Definition: urisyn.cpp:94
const traits PATH_TRAITS
path traits
Definition: urisyn.cpp:73
bool decode(std::string &s)
Decode the pct-encoded (hex) sequences, if any, return success.
Definition: urisyn.cpp:171
Traits used for parsing and encoding components.
Definition: urisyn.h:43
const traits SCHEME_TRAITS
scheme traits
Definition: urisyn.cpp:31
valid any position
Definition: urisyn.h:39
bool parse_hex(const std::string &s, size_t pos, char &chr)
Parse hex chars at pos, returning success, and set the char and advance first on success.
Definition: utils.cpp:181
const traits FRAGMENT_TRAITS
fragment traits
Definition: urisyn.cpp:115
valid anywhere but 1st position
Definition: urisyn.h:40