uri.hpp 10 KB

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