Gorgon Game Engine
URI.h
Go to the documentation of this file.
1  #pragma once
2 
3 #include <string.h>
4 #include <stdexcept>
5 #include <set>
6 #include <map>
7 #include <vector>
8 
9 namespace Gorgon { namespace Encoding {
11  class URIError : public std::runtime_error {
12  public:
13  using std::runtime_error::runtime_error;
14  };
15 
22  class URI {
23  public:
25  URI() = default;
26 
28  URI(const std::string &scheme, const std::string &host, const std::string &path, const std::string &query = "", const std::string &fragment="");
29 
31  URI(const std::string &scheme, const std::string &host, int port, const std::string &path, const std::string &query = "", const std::string &fragment="");
32 
38  URI(const std::string &str, bool inpagelink = false);
39 
42  URI(const char *str) : URI(std::string(str)) { }
43 
45  operator std::string() const;
46 
48  std::string Convert() const { return *this; }
49 
52  bool IsValid() const;
53 
55  operator bool() const {
56  return IsValid();
57  }
58 
62  bool operator ==(const URI &other) const;
63 
67  bool operator !=(const URI &other) const { return !(*this==other); }
68 
72  void Combine(const std::string &link);
73 
74  URI operator +(const std::string &link) {
75  auto t=*this;
76  t.Combine(link);
77  return t;
78  }
79 
82  std::string scheme;
83 
85  std::string host;
86 
89  std::string userinfo;
90 
92  int port = 0;
93 
95  std::string path;
96 
98  std::string query;
99 
101  std::string fragment;
102  };
103 
105  class HTTPQuery {
106  public:
107 
109  HTTPQuery() = default;
110 
112  HTTPQuery(std::initializer_list<std::pair<const std::string, std::string>> init);
113 
115  HTTPQuery(const std::string &query);
116 
118  operator std::string() const;
119 
121  std::string Convert() const { return *this; }
122 
124  std::string Get(const std::string &key) const {
125  if(data.count(key) == 0) return "";
126 
127  return data.at(key);
128  }
129 
131  std::string &operator[](const std::string &key) {
132  return data[key];
133  }
134 
136  bool Exists(const std::string &key) const {
137  return data.count(key) != 0;
138  }
139 
141  std::map<std::string, std::string> data;
142  };
143 
146  class URIPath {
147  public:
148 
150  URIPath() = default;
151 
153  URIPath(std::initializer_list<std::string> init);
154 
156  URIPath(const std::string &path);
157 
159  URIPath(const char *path) : URIPath(std::string(path)) { }
160 
162  operator std::string() const;
163 
165  bool operator ==(const URIPath &other) const;
166 
168  bool operator !=(const URIPath &other) const { return !(*this==other); }
169 
170  int GetSize() const { return (int)segments.size(); }
171 
173  std::string &operator[] (int ind) { return segments[ind]; }
174 
176  std::string Get(int ind) const {
177  if(ind==0 && segments.size()==0) return "";
178 
179  return segments[ind];
180  }
181 
184  std::string StripFirst() {
185  if(segments.size()==0) return "";
186 
187  auto ret = segments.front();
188  segments.erase(segments.begin());
189 
190  return ret;
191  }
192 
194  const std::string &operator[] (int ind) const { return segments[ind]; }
195 
197  std::string Convert() const { return *this; }
198 
200  void Combine(const URIPath &another);
201 
203  URIPath operator +(const URIPath &another) const {
204  auto newp = *this;
205  newp.Combine(another);
206  return newp;
207  }
208 
210  URIPath &operator +=(const URIPath &another) {
211  Combine(another);
212  return *this;
213  }
214 
216  void Normalize();
217 
219  std::vector<std::string> segments;
220  };
221 
222  std::ostream &operator <<(std::ostream &out, const URI &uri);
223 
224  std::ostream &operator <<(std::ostream &out, const HTTPQuery &query);
225 
226  std::ostream &operator <<(std::ostream &out, const URIPath &path);
227 
229  std::string URIDecode(const std::string &str);
230 
232  std::string URIEncode(const std::string &str);
233 
236  std::string PCTEncode(const std::string &str, const std::set<char> &allowed, bool allowalpha=true, bool allownum=true);
237 
238 } }
Gorgon::Encoding::HTTPQuery
Represents and HTTP query that might be send to page using POST or embedded in URI.
Definition: URI.h:105
Gorgon::Encoding::URIError
This error is thrown while URI decoding and building.
Definition: URI.h:11
Gorgon::Encoding::URI::IsValid
bool IsValid() const
Returns if this URI is valid.
Definition: URI.cpp:385
Gorgon::Encoding::URIPath::URIPath
URIPath(const char *path)
Parses the given path.
Definition: URI.h:159
Gorgon::Encoding::URI::operator+
URI operator+(const std::string &link)
Definition: URI.h:74
Gorgon::Encoding::URIEncode
std::string URIEncode(const std::string &str)
Encodes a given URI string according to RFC 3986.
Definition: URI.cpp:125
Gorgon::Encoding::PCTEncode
std::string PCTEncode(const std::string &str, const std::set< char > &allowed, bool allowalpha=true, bool allownum=true)
Customized percentage encoding.
Definition: URI.cpp:108
Gorgon::Encoding::URI::URI
URI(const char *str)
Builds a new URI from the given string.
Definition: URI.h:42
Gorgon::Encoding::HTTPQuery::Convert
std::string Convert() const
Converts this query system to string.
Definition: URI.h:121
URI.h
Gorgon::WindowManager::init
void init()
Definition: X11.cpp:143
Gorgon::Encoding::URIPath::GetSize
int GetSize() const
Definition: URI.h:170
Gorgon::Encoding::HTTPQuery::Get
std::string Get(const std::string &key) const
Returns the value that is stored within key. If key does not exist, an empty string will be returned.
Definition: URI.h:124
Gorgon::Encoding::URI::operator!=
bool operator!=(const URI &other) const
Compares two URIs in their simplest forms.
Definition: URI.h:67
Gorgon::Encoding::URIPath::operator[]
std::string & operator[](int ind)
Returns the segment at the given index.
Definition: URI.h:173
Gorgon::String::FixLineEndings
std::string FixLineEndings(const std::string &in, LineEnding type=LineEnding::Standard)
Fixes/changes line endings.
Definition: String.cpp:8
Gorgon::Encoding::URI::query
std::string query
Query for the resource, must be properly escaped and encoded, you may use HTTPQuery to build http que...
Definition: URI.h:98
Gorgon::Encoding::URI::Combine
void Combine(const std::string &link)
Combines a relative URI into this URI.
Definition: URI.cpp:398
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Encoding::URI::Convert
std::string Convert() const
Converts this URI to a properly encoded string.
Definition: URI.h:48
Gorgon::Encoding::URIDecode
std::string URIDecode(const std::string &str)
Decodes a given URI string according to RFC 3986. May throw URIError.
Definition: URI.cpp:74
Gorgon::Encoding::URIPath::Normalize
void Normalize()
Normalizes any relative references in the path. May throw URIError.
Definition: URI.cpp:593
Gorgon::Encoding::URIPath::Convert
std::string Convert() const
Converts the path to string properly escaping for URI.
Definition: URI.h:197
Gorgon::Encoding::URIPath::operator!=
bool operator!=(const URIPath &other) const
Compares two paths after normalization.
Definition: URI.h:168
Gorgon::String::ToLower
std::string ToLower(std::string str)
Converts the given string to lowercase.
Definition: String.h:416
Gorgon::Encoding::URI::host
std::string host
Host address, either IP/domain name or empty, ex: darkgaze.org.
Definition: URI.h:85
Gorgon::Encoding::URI::scheme
std::string scheme
Scheme of the URI, ex: http.
Definition: URI.h:82
Gorgon::Encoding::URI::fragment
std::string fragment
Fragment of the resource (anchor in HTML)
Definition: URI.h:101
Gorgon::Encoding::URIPath::StripFirst
std::string StripFirst()
Returns the first segment and removes it from the list of segments.
Definition: URI.h:184
Gorgon::Encoding::URIPath::URIPath
URIPath()=default
Empty constructor.
Gorgon::Encoding::URIPath::Combine
void Combine(const URIPath &another)
Combines another URIPath using this one as the root.
Definition: URI.cpp:622
Gorgon::Encoding::HTTPQuery::Exists
bool Exists(const std::string &key) const
Returns if the given key exists.
Definition: URI.h:136
Gorgon::Encoding::URIPath::operator+
URIPath operator+(const URIPath &another) const
Combines this path with another, using this path as the root.
Definition: URI.h:203
Gorgon::Encoding::URI::URI
URI()=default
Empty constructor. Does not set scheme thus would not be a valid URI.
Gorgon::Encoding::URIPath
Helps to manage URIPaths.
Definition: URI.h:146
Gorgon::Encoding::URI::path
std::string path
Path of the resource. Segments must be separated by /. You may use URIPath to construct a path.
Definition: URI.h:95
Gorgon::Encoding::HTTPQuery::operator[]
std::string & operator[](const std::string &key)
Returns the value that is stored within key. If key does not exist, an empty value with the given key...
Definition: URI.h:131
Gorgon::Encoding::URI::port
int port
The port number, 0 means it is not defined.
Definition: URI.h:92
Gorgon::Encoding::operator<<
std::ostream & operator<<(std::ostream &out, const URI &uri)
Definition: URI.cpp:642
Gorgon::Encoding::URIPath::operator+=
URIPath & operator+=(const URIPath &another)
Combines another URIPath into this one, using this path as the root.
Definition: URI.h:210
Gorgon::String::Concat
std::string Concat(const P_ &... rest)
Streams the given parameters into a stringstream and returns the result, effectively concatinating al...
Definition: String.h:560
Gorgon::Encoding::URIPath::Get
std::string Get(int ind) const
Returns the segment at the given index. Index 0 will never cause an error, if does not exists,...
Definition: URI.h:176
Gorgon::Encoding::URI::userinfo
std::string userinfo
User information.
Definition: URI.h:89
Gorgon::Encoding::URI
Represents an unfolded URI.
Definition: URI.h:22
Gorgon::Encoding::HTTPQuery::HTTPQuery
HTTPQuery()=default
Empty constructor.
Gorgon::Encoding::HTTPQuery::data
std::map< std::string, std::string > data
Key-value pairs for this HTTP query.
Definition: URI.h:141
Gorgon::Encoding::URI::operator==
bool operator==(const URI &other) const
Compares two URIs in their simplest forms.
Definition: URI.cpp:454
Gorgon::Encoding::URIPath::operator==
bool operator==(const URIPath &other) const
Compares two paths after normalization.
Definition: URI.cpp:629
Gorgon::Encoding::URIPath::segments
std::vector< std::string > segments
List of segments.
Definition: URI.h:219