2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef WEBSOCKETPP_URI_HPP
29 #define WEBSOCKETPP_URI_HPP
31 #include <websocketpp/error.hpp>
33 #include <websocketpp/common/memory.hpp>
39 namespace websocketpp {
41 // TODO: figure out why this fixes horrible linking errors.
43 /// Default port for ws://
44 static uint16_t const uri_default_port = 80;
45 /// Default port for wss://
46 static uint16_t const uri_default_secure_port = 443;
50 explicit uri(std::string const & uri_string) : m_valid(false) {
51 std::string::const_iterator it;
52 std::string::const_iterator temp;
56 it = uri_string.begin();
57 size_t uri_len = uri_string.length();
59 if (uri_len >= 7 && std::equal(it,it+6,"wss://")) {
63 } else if (uri_len >= 6 && std::equal(it,it+5,"ws://")) {
67 } else if (uri_len >= 8 && std::equal(it,it+7,"http://")) {
71 } else if (uri_len >= 9 && std::equal(it,it+8,"https://")) {
80 // either a host string
86 // extract IPv6 digits until ]
88 // TODO: this doesn't work on g++... not sure why
89 //temp = std::find(it,it2,']');
92 while (temp != uri_string.end()) {
99 if (temp == uri_string.end()) {
102 // validate IPv6 literal parts
103 // can contain numbers, a-f and A-F
104 m_host.append(it,temp);
107 if (it == uri_string.end()) {
109 } else if (*it == '/') {
112 } else if (*it == ':') {
121 // extract until : or /
123 if (it == uri_string.end()) {
126 } else if (*it == '/') {
128 } else if (*it == ':') {
129 // end hostname start port
141 if (it == uri_string.end()) {
142 // state is not used after this point presently.
143 // this should be re-enabled if it ever is needed in a future
147 } else if (*it == '/') {
156 m_port = get_port_from_string(port, ec);
163 m_resource.append(it,uri_string.end());
169 uri(bool secure, std::string const & host, uint16_t port,
170 std::string const & resource)
171 : m_scheme(secure ? "wss" : "ws")
173 , m_resource(resource.empty() ? "/" : resource)
178 uri(bool secure, std::string const & host, std::string const & resource)
179 : m_scheme(secure ? "wss" : "ws")
181 , m_resource(resource.empty() ? "/" : resource)
182 , m_port(secure ? uri_default_secure_port : uri_default_port)
186 uri(bool secure, std::string const & host, std::string const & port,
187 std::string const & resource)
188 : m_scheme(secure ? "wss" : "ws")
190 , m_resource(resource.empty() ? "/" : resource)
194 m_port = get_port_from_string(port,ec);
198 uri(std::string const & scheme, std::string const & host, uint16_t port,
199 std::string const & resource)
202 , m_resource(resource.empty() ? "/" : resource)
204 , m_secure(scheme == "wss" || scheme == "https")
207 uri(std::string scheme, std::string const & host, std::string const & resource)
210 , m_resource(resource.empty() ? "/" : resource)
211 , m_port((scheme == "wss" || scheme == "https") ? uri_default_secure_port : uri_default_port)
212 , m_secure(scheme == "wss" || scheme == "https")
215 uri(std::string const & scheme, std::string const & host,
216 std::string const & port, std::string const & resource)
219 , m_resource(resource.empty() ? "/" : resource)
220 , m_secure(scheme == "wss" || scheme == "https")
223 m_port = get_port_from_string(port,ec);
227 bool get_valid() const {
231 bool get_secure() const {
235 std::string const & get_scheme() const {
239 std::string const & get_host() const {
243 std::string get_host_port() const {
244 if (m_port == (m_secure ? uri_default_secure_port : uri_default_port)) {
248 p << m_host << ":" << m_port;
253 std::string get_authority() const {
255 p << m_host << ":" << m_port;
259 uint16_t get_port() const {
263 std::string get_port_str() const {
269 std::string const & get_resource() const {
273 std::string str() const {
276 s << m_scheme << "://" << m_host;
278 if (m_port != (m_secure ? uri_default_secure_port : uri_default_port)) {
286 /// Return the query portion
288 * Returns the query portion (after the ?) of the URI or an empty string if
291 * @return query portion of the URI.
293 std::string get_query() const {
294 std::size_t found = m_resource.find('?');
295 if (found != std::string::npos) {
296 return m_resource.substr(found + 1);
306 // get the string representation of this URI
308 //std::string base() const; // is this still needed?
310 // setter methods set some or all (in the case of parse) based on the input.
311 // These functions throw a uri_exception on failure.
312 /*void set_uri(const std::string& uri);
314 void set_secure(bool secure);
315 void set_host(const std::string& host);
316 void set_port(uint16_t port);
317 void set_port(const std::string& port);
318 void set_resource(const std::string& resource);*/
320 uint16_t get_port_from_string(std::string const & port, lib::error_code &
323 ec = lib::error_code();
326 return (m_secure ? uri_default_secure_port : uri_default_port);
329 unsigned int t_port = static_cast<unsigned int>(atoi(port.c_str()));
331 if (t_port > 65535) {
332 ec = error::make_error_code(error::invalid_port);
336 ec = error::make_error_code(error::invalid_port);
339 return static_cast<uint16_t>(t_port);
342 std::string m_scheme;
344 std::string m_resource;
351 typedef lib::shared_ptr<uri> uri_ptr;
353 } // namespace websocketpp
355 #endif // WEBSOCKETPP_URI_HPP