You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
9.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 <algorithm>
  30. #include <sstream>
  31. #include <string>
  32. #include <websocketpp/common/memory.hpp>
  33. #include <websocketpp/common/stdint.hpp>
  34. #include <websocketpp/error.hpp>
  35. namespace websocketpp
  36. {
  37. // TODO: figure out why this fixes horrible linking errors.
  38. /// Default port for ws://
  39. static const uint16_t uri_default_port = 80;
  40. /// Default port for wss://
  41. static const uint16_t uri_default_secure_port = 443;
  42. class uri
  43. {
  44. public:
  45. explicit uri(const std::string & uri_string) :
  46. m_valid(false)
  47. {
  48. std::string::const_iterator it;
  49. std::string::const_iterator temp;
  50. int state = 0;
  51. it = uri_string.begin();
  52. size_t uri_len = uri_string.length();
  53. if (uri_len >= 7 && std::equal(it, it + 6, "wss://"))
  54. {
  55. m_secure = true;
  56. m_scheme = "wss";
  57. it += 6;
  58. }
  59. else if (uri_len >= 6 && std::equal(it, it + 5, "ws://"))
  60. {
  61. m_secure = false;
  62. m_scheme = "ws";
  63. it += 5;
  64. }
  65. else if (uri_len >= 8 && std::equal(it, it + 7, "http://"))
  66. {
  67. m_secure = false;
  68. m_scheme = "http";
  69. it += 7;
  70. }
  71. else if (uri_len >= 9 && std::equal(it, it + 8, "https://"))
  72. {
  73. m_secure = true;
  74. m_scheme = "https";
  75. it += 8;
  76. }
  77. else
  78. {
  79. return;
  80. }
  81. // extract host.
  82. // either a host string
  83. // an IPv4 address
  84. // or an IPv6 address
  85. if (*it == '[')
  86. {
  87. ++it;
  88. // IPv6 literal
  89. // extract IPv6 digits until ]
  90. // TODO: this doesn't work on g++... not sure why
  91. // temp = std::find(it,it2,']');
  92. temp = it;
  93. while (temp != uri_string.end())
  94. {
  95. if (*temp == ']')
  96. {
  97. break;
  98. }
  99. ++temp;
  100. }
  101. if (temp == uri_string.end())
  102. {
  103. return;
  104. }
  105. else
  106. {
  107. // validate IPv6 literal parts
  108. // can contain numbers, a-f and A-F
  109. m_host.append(it, temp);
  110. }
  111. it = temp + 1;
  112. if (it == uri_string.end())
  113. {
  114. state = 2;
  115. }
  116. else if (*it == '/')
  117. {
  118. state = 2;
  119. ++it;
  120. }
  121. else if (*it == ':')
  122. {
  123. state = 1;
  124. ++it;
  125. }
  126. else
  127. {
  128. // problem
  129. return;
  130. }
  131. }
  132. else
  133. {
  134. // IPv4 or hostname
  135. // extract until : or /
  136. while (state == 0)
  137. {
  138. if (it == uri_string.end())
  139. {
  140. state = 2;
  141. break;
  142. }
  143. else if (*it == '/')
  144. {
  145. state = 2;
  146. }
  147. else if (*it == ':')
  148. {
  149. // end hostname start port
  150. state = 1;
  151. }
  152. else
  153. {
  154. m_host += *it;
  155. }
  156. ++it;
  157. }
  158. }
  159. // parse port
  160. std::string port;
  161. while (state == 1)
  162. {
  163. if (it == uri_string.end())
  164. {
  165. // state is not used after this point presently.
  166. // this should be re-enabled if it ever is needed in a future
  167. // refactoring
  168. // state = 3;
  169. break;
  170. }
  171. else if (*it == '/')
  172. {
  173. state = 3;
  174. }
  175. else
  176. {
  177. port += *it;
  178. }
  179. ++it;
  180. }
  181. lib::error_code ec;
  182. m_port = get_port_from_string(port, ec);
  183. if (ec)
  184. {
  185. return;
  186. }
  187. m_resource = "/";
  188. m_resource.append(it, uri_string.end());
  189. m_valid = true;
  190. }
  191. uri(bool secure, const std::string & host, uint16_t port, const std::string & resource) :
  192. m_scheme(secure ? "wss" : "ws"),
  193. m_host(host),
  194. m_resource(resource.empty() ? "/" : resource),
  195. m_port(port),
  196. m_secure(secure),
  197. m_valid(true)
  198. {
  199. }
  200. uri(bool secure, const std::string & host, const std::string & resource) :
  201. m_scheme(secure ? "wss" : "ws"),
  202. m_host(host),
  203. m_resource(resource.empty() ? "/" : resource),
  204. m_port(secure ? uri_default_secure_port : uri_default_port),
  205. m_secure(secure),
  206. m_valid(true)
  207. {
  208. }
  209. uri(bool secure, const std::string & host, const std::string & port, const std::string & resource) :
  210. m_scheme(secure ? "wss" : "ws"),
  211. m_host(host),
  212. m_resource(resource.empty() ? "/" : resource),
  213. m_secure(secure)
  214. {
  215. lib::error_code ec;
  216. m_port = get_port_from_string(port, ec);
  217. m_valid = ! ec;
  218. }
  219. uri(const std::string & scheme, const std::string & host, uint16_t port, const std::string & resource) :
  220. m_scheme(scheme),
  221. m_host(host),
  222. m_resource(resource.empty() ? "/" : resource),
  223. m_port(port),
  224. m_secure(scheme == "wss" || scheme == "https"),
  225. m_valid(true)
  226. {
  227. }
  228. uri(std::string scheme, const std::string & host, const std::string & resource) :
  229. m_scheme(scheme),
  230. m_host(host),
  231. m_resource(resource.empty() ? "/" : resource),
  232. m_port((scheme == "wss" || scheme == "https") ? uri_default_secure_port : uri_default_port),
  233. m_secure(scheme == "wss" || scheme == "https"),
  234. m_valid(true)
  235. {
  236. }
  237. uri(const std::string & scheme, const std::string & host, const std::string & port, const std::string & resource) :
  238. m_scheme(scheme),
  239. m_host(host),
  240. m_resource(resource.empty() ? "/" : resource),
  241. m_secure(scheme == "wss" || scheme == "https")
  242. {
  243. lib::error_code ec;
  244. m_port = get_port_from_string(port, ec);
  245. m_valid = ! ec;
  246. }
  247. bool get_valid() const
  248. {
  249. return m_valid;
  250. }
  251. bool get_secure() const
  252. {
  253. return m_secure;
  254. }
  255. const std::string & get_scheme() const
  256. {
  257. return m_scheme;
  258. }
  259. const std::string & get_host() const
  260. {
  261. return m_host;
  262. }
  263. std::string get_host_port() const
  264. {
  265. if (m_port == (m_secure ? uri_default_secure_port : uri_default_port))
  266. {
  267. return m_host;
  268. }
  269. else
  270. {
  271. std::stringstream p;
  272. p << m_host << ":" << m_port;
  273. return p.str();
  274. }
  275. }
  276. std::string get_authority() const
  277. {
  278. std::stringstream p;
  279. p << m_host << ":" << m_port;
  280. return p.str();
  281. }
  282. uint16_t get_port() const
  283. {
  284. return m_port;
  285. }
  286. std::string get_port_str() const
  287. {
  288. std::stringstream p;
  289. p << m_port;
  290. return p.str();
  291. }
  292. const std::string & get_resource() const
  293. {
  294. return m_resource;
  295. }
  296. std::string str() const
  297. {
  298. std::stringstream s;
  299. s << m_scheme << "://" << m_host;
  300. if (m_port != (m_secure ? uri_default_secure_port : uri_default_port))
  301. {
  302. s << ":" << m_port;
  303. }
  304. s << m_resource;
  305. return s.str();
  306. }
  307. /// Return the query portion
  308. /**
  309. * Returns the query portion (after the ?) of the URI or an empty string if
  310. * there is none.
  311. *
  312. * @return query portion of the URI.
  313. */
  314. std::string get_query() const
  315. {
  316. std::size_t found = m_resource.find('?');
  317. if (found != std::string::npos)
  318. {
  319. return m_resource.substr(found + 1);
  320. }
  321. else
  322. {
  323. return "";
  324. }
  325. }
  326. // get fragment
  327. // hi <3
  328. // get the string representation of this URI
  329. // std::string base() const; // is this still needed?
  330. // setter methods set some or all (in the case of parse) based on the input.
  331. // These functions throw a uri_exception on failure.
  332. /*void set_uri(const std::string& uri);
  333. void set_secure(bool secure);
  334. void set_host(const std::string& host);
  335. void set_port(uint16_t port);
  336. void set_port(const std::string& port);
  337. void set_resource(const std::string& resource);*/
  338. private:
  339. uint16_t get_port_from_string(const std::string & port, lib::error_code & ec) const
  340. {
  341. ec = lib::error_code();
  342. if (port.empty())
  343. {
  344. return (m_secure ? uri_default_secure_port : uri_default_port);
  345. }
  346. unsigned int t_port = static_cast<unsigned int>(atoi(port.c_str()));
  347. if (t_port > 65535)
  348. {
  349. ec = error::make_error_code(error::invalid_port);
  350. }
  351. if (t_port == 0)
  352. {
  353. ec = error::make_error_code(error::invalid_port);
  354. }
  355. return static_cast<uint16_t>(t_port);
  356. }
  357. std::string m_scheme;
  358. std::string m_host;
  359. std::string m_resource;
  360. uint16_t m_port;
  361. bool m_secure;
  362. bool m_valid;
  363. };
  364. /// Pointer to a URI
  365. typedef lib::shared_ptr<uri> uri_ptr;
  366. } // namespace websocketpp
  367. #endif // WEBSOCKETPP_URI_HPP