uri.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2014, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_URI_HPP
  28. #define WEBSOCKETPP_URI_HPP
  29. #include <websocketpp/error.hpp>
  30. #include <websocketpp/common/memory.hpp>
  31. #include <websocketpp/common/stdint.hpp>
  32. #include <algorithm>
  33. #include <sstream>
  34. #include <string>
  35. namespace websocketpp {
  36. // TODO: figure out why this fixes horrible linking errors.
  37. /// Default port for ws://
  38. static uint16_t const uri_default_port = 80;
  39. /// Default port for wss://
  40. static uint16_t const uri_default_secure_port = 443;
  41. class uri {
  42. public:
  43. explicit uri(std::string const & uri_string) : m_valid(false) {
  44. std::string::const_iterator it;
  45. std::string::const_iterator temp;
  46. int state = 0;
  47. it = uri_string.begin();
  48. size_t uri_len = uri_string.length();
  49. if (uri_len >= 7 && std::equal(it,it+6,"wss://")) {
  50. m_secure = true;
  51. m_scheme = "wss";
  52. it += 6;
  53. } else if (uri_len >= 6 && std::equal(it,it+5,"ws://")) {
  54. m_secure = false;
  55. m_scheme = "ws";
  56. it += 5;
  57. } else if (uri_len >= 8 && std::equal(it,it+7,"http://")) {
  58. m_secure = false;
  59. m_scheme = "http";
  60. it += 7;
  61. } else if (uri_len >= 9 && std::equal(it,it+8,"https://")) {
  62. m_secure = true;
  63. m_scheme = "https";
  64. it += 8;
  65. } else {
  66. return;
  67. }
  68. // extract host.
  69. // either a host string
  70. // an IPv4 address
  71. // or an IPv6 address
  72. if (*it == '[') {
  73. ++it;
  74. // IPv6 literal
  75. // extract IPv6 digits until ]
  76. // TODO: this doesn't work on g++... not sure why
  77. //temp = std::find(it,it2,']');
  78. temp = it;
  79. while (temp != uri_string.end()) {
  80. if (*temp == ']') {
  81. break;
  82. }
  83. ++temp;
  84. }
  85. if (temp == uri_string.end()) {
  86. return;
  87. } else {
  88. // validate IPv6 literal parts
  89. // can contain numbers, a-f and A-F
  90. m_host.append(it,temp);
  91. }
  92. it = temp+1;
  93. if (it == uri_string.end()) {
  94. state = 2;
  95. } else if (*it == '/') {
  96. state = 2;
  97. ++it;
  98. } else if (*it == ':') {
  99. state = 1;
  100. ++it;
  101. } else {
  102. // problem
  103. return;
  104. }
  105. } else {
  106. // IPv4 or hostname
  107. // extract until : or /
  108. while (state == 0) {
  109. if (it == uri_string.end()) {
  110. state = 2;
  111. break;
  112. } else if (*it == '/') {
  113. state = 2;
  114. } else if (*it == ':') {
  115. // end hostname start port
  116. state = 1;
  117. } else {
  118. m_host += *it;
  119. }
  120. ++it;
  121. }
  122. }
  123. // parse port
  124. std::string port;
  125. while (state == 1) {
  126. if (it == uri_string.end()) {
  127. // state is not used after this point presently.
  128. // this should be re-enabled if it ever is needed in a future
  129. // refactoring
  130. //state = 3;
  131. break;
  132. } else if (*it == '/') {
  133. state = 3;
  134. } else {
  135. port += *it;
  136. }
  137. ++it;
  138. }
  139. lib::error_code ec;
  140. m_port = get_port_from_string(port, ec);
  141. if (ec) {
  142. return;
  143. }
  144. m_resource = "/";
  145. m_resource.append(it,uri_string.end());
  146. m_valid = true;
  147. }
  148. uri(bool secure, std::string const & host, uint16_t port,
  149. std::string const & resource)
  150. : m_scheme(secure ? "wss" : "ws")
  151. , m_host(host)
  152. , m_resource(resource.empty() ? "/" : resource)
  153. , m_port(port)
  154. , m_secure(secure)
  155. , m_valid(true) {}
  156. uri(bool secure, std::string const & host, std::string const & resource)
  157. : m_scheme(secure ? "wss" : "ws")
  158. , m_host(host)
  159. , m_resource(resource.empty() ? "/" : resource)
  160. , m_port(secure ? uri_default_secure_port : uri_default_port)
  161. , m_secure(secure)
  162. , m_valid(true) {}
  163. uri(bool secure, std::string const & host, std::string const & port,
  164. std::string const & resource)
  165. : m_scheme(secure ? "wss" : "ws")
  166. , m_host(host)
  167. , m_resource(resource.empty() ? "/" : resource)
  168. , m_secure(secure)
  169. {
  170. lib::error_code ec;
  171. m_port = get_port_from_string(port,ec);
  172. m_valid = !ec;
  173. }
  174. uri(std::string const & scheme, std::string const & host, uint16_t port,
  175. std::string const & resource)
  176. : m_scheme(scheme)
  177. , m_host(host)
  178. , m_resource(resource.empty() ? "/" : resource)
  179. , m_port(port)
  180. , m_secure(scheme == "wss" || scheme == "https")
  181. , m_valid(true) {}
  182. uri(std::string scheme, std::string const & host, std::string const & resource)
  183. : m_scheme(scheme)
  184. , m_host(host)
  185. , m_resource(resource.empty() ? "/" : resource)
  186. , m_port((scheme == "wss" || scheme == "https") ? uri_default_secure_port : uri_default_port)
  187. , m_secure(scheme == "wss" || scheme == "https")
  188. , m_valid(true) {}
  189. uri(std::string const & scheme, std::string const & host,
  190. std::string const & port, std::string const & resource)
  191. : m_scheme(scheme)
  192. , m_host(host)
  193. , m_resource(resource.empty() ? "/" : resource)
  194. , m_secure(scheme == "wss" || scheme == "https")
  195. {
  196. lib::error_code ec;
  197. m_port = get_port_from_string(port,ec);
  198. m_valid = !ec;
  199. }
  200. bool get_valid() const {
  201. return m_valid;
  202. }
  203. bool get_secure() const {
  204. return m_secure;
  205. }
  206. std::string const & get_scheme() const {
  207. return m_scheme;
  208. }
  209. std::string const & get_host() const {
  210. return m_host;
  211. }
  212. std::string get_host_port() const {
  213. if (m_port == (m_secure ? uri_default_secure_port : uri_default_port)) {
  214. return m_host;
  215. } else {
  216. std::stringstream p;
  217. p << m_host << ":" << m_port;
  218. return p.str();
  219. }
  220. }
  221. std::string get_authority() const {
  222. std::stringstream p;
  223. p << m_host << ":" << m_port;
  224. return p.str();
  225. }
  226. uint16_t get_port() const {
  227. return m_port;
  228. }
  229. std::string get_port_str() const {
  230. std::stringstream p;
  231. p << m_port;
  232. return p.str();
  233. }
  234. std::string const & get_resource() const {
  235. return m_resource;
  236. }
  237. std::string str() const {
  238. std::stringstream s;
  239. s << m_scheme << "://" << m_host;
  240. if (m_port != (m_secure ? uri_default_secure_port : uri_default_port)) {
  241. s << ":" << m_port;
  242. }
  243. s << m_resource;
  244. return s.str();
  245. }
  246. /// Return the query portion
  247. /**
  248. * Returns the query portion (after the ?) of the URI or an empty string if
  249. * there is none.
  250. *
  251. * @return query portion of the URI.
  252. */
  253. std::string get_query() const {
  254. std::size_t found = m_resource.find('?');
  255. if (found != std::string::npos) {
  256. return m_resource.substr(found + 1);
  257. } else {
  258. return "";
  259. }
  260. }
  261. // get fragment
  262. // hi <3
  263. // get the string representation of this URI
  264. //std::string base() const; // is this still needed?
  265. // setter methods set some or all (in the case of parse) based on the input.
  266. // These functions throw a uri_exception on failure.
  267. /*void set_uri(const std::string& uri);
  268. void set_secure(bool secure);
  269. void set_host(const std::string& host);
  270. void set_port(uint16_t port);
  271. void set_port(const std::string& port);
  272. void set_resource(const std::string& resource);*/
  273. private:
  274. uint16_t get_port_from_string(std::string const & port, lib::error_code &
  275. ec) const
  276. {
  277. ec = lib::error_code();
  278. if (port.empty()) {
  279. return (m_secure ? uri_default_secure_port : uri_default_port);
  280. }
  281. unsigned int t_port = static_cast<unsigned int>(atoi(port.c_str()));
  282. if (t_port > 65535) {
  283. ec = error::make_error_code(error::invalid_port);
  284. }
  285. if (t_port == 0) {
  286. ec = error::make_error_code(error::invalid_port);
  287. }
  288. return static_cast<uint16_t>(t_port);
  289. }
  290. std::string m_scheme;
  291. std::string m_host;
  292. std::string m_resource;
  293. uint16_t m_port;
  294. bool m_secure;
  295. bool m_valid;
  296. };
  297. /// Pointer to a URI
  298. typedef lib::shared_ptr<uri> uri_ptr;
  299. } // namespace websocketpp
  300. #endif // WEBSOCKETPP_URI_HPP