NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
hybi07.cpp
Go to the documentation of this file.
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 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE hybi_07_processor
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
41 
42 struct stub_config {
45 
50 
52 
53  static const size_t max_message_size = 16000000;
54 
56  static const bool enable_extensions = false;
57 
59 
63  };
64 
67 };
68 
69 BOOST_AUTO_TEST_CASE( exact_match ) {
72  stub_config::con_msg_manager_type::ptr msg_manager;
74  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
75  websocketpp::lib::error_code ec;
76 
77  std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
78 
79  r.consume(handshake.c_str(),handshake.size());
80 
83  ec = p.validate_handshake(r);
84  BOOST_CHECK(!ec);
85 
87 
88  u = p.get_uri(r);
89 
90  BOOST_CHECK(u->get_valid());
91  BOOST_CHECK(!u->get_secure());
92  BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
93  BOOST_CHECK_EQUAL(u->get_resource(), "/");
94  BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
95 
96  p.process_handshake(r,"",response);
97 
98  BOOST_CHECK_EQUAL(response.get_header("Connection"), "upgrade");
99  BOOST_CHECK_EQUAL(response.get_header("Upgrade"), "websocket");
100  BOOST_CHECK_EQUAL(response.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
101 }
102 
103 BOOST_AUTO_TEST_CASE( non_get_method ) {
106  stub_config::con_msg_manager_type::ptr msg_manager;
108  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
109  websocketpp::lib::error_code ec;
110 
111  std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
112 
113  r.consume(handshake.c_str(),handshake.size());
114 
117  ec = p.validate_handshake(r);
119 }
120 
121 BOOST_AUTO_TEST_CASE( old_http_version ) {
124  stub_config::con_msg_manager_type::ptr msg_manager;
126  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
127  websocketpp::lib::error_code ec;
128 
129  std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
130 
131  r.consume(handshake.c_str(),handshake.size());
132 
135  ec = p.validate_handshake(r);
137 }
138 
139 BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
142  stub_config::con_msg_manager_type::ptr msg_manager;
144  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
145  websocketpp::lib::error_code ec;
146 
147  std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
148 
149  r.consume(handshake.c_str(),handshake.size());
150 
153  ec = p.validate_handshake(r);
155 }
156 
157 BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
160  stub_config::con_msg_manager_type::ptr msg_manager;
162  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
163  websocketpp::lib::error_code ec;
164 
165  std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
166 
167  r.consume(handshake.c_str(),handshake.size());
168 
171  ec = p.validate_handshake(r);
173 }
174 
175 BOOST_AUTO_TEST_CASE( bad_host ) {
178  stub_config::con_msg_manager_type::ptr msg_manager;
180  websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
181  websocketpp::lib::error_code ec;
182 
183  std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
184 
185  r.consume(handshake.c_str(),handshake.size());
186 
189  ec = p.validate_handshake(r);
190  BOOST_CHECK( !ec );
191 
192  BOOST_CHECK( !p.get_uri(r)->get_valid() );
193 }
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition: processor.hpp:68
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
Definition: hybi13.hpp:163
core::rng_type rng_type
Definition: connection.cpp:78
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
Definition: processor.hpp:107
lib::error_code process_handshake(request_type const &request, std::string const &subprotocol, response_type &response) const
Calculate the appropriate response for this websocket request.
Definition: hybi13.hpp:187
websocketpp::extensions::permessage_deflate::disabled< permessage_deflate_config > permessage_deflate_type
Definition: hybi07.cpp:66
websocketpp::random::none::int_generator< uint32_t > rng_type
Definition: hybi07.cpp:51
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
Stub class for use when disabling permessage_deflate extension.
Definition: disabled.hpp:52
websocketpp::http::parser::response response_type
Definition: hybi07.cpp:44
Processor for Hybi Draft version 07.
Definition: hybi07.hpp:44
int get_version() const
Get the protocol version of this processor.
Definition: hybi07.hpp:69
Thread safe stub "random" integer generator.
Definition: none.hpp:46
Represents a buffer for a single WebSocket message.
Definition: message.hpp:84
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:351
stub_config::request_type request_type
Definition: hybi07.cpp:62
websocketpp::http::parser::request request_type
Definition: hybi07.cpp:43
BOOST_AUTO_TEST_CASE(exact_match)
Definition: hybi07.cpp:69
core::request_type request_type
Definition: connection.cpp:68
Extension specific config.
Definition: hybi07.cpp:61
static const size_t max_message_size
Definition: hybi00.cpp:49
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
core::response_type response_type
Definition: connection.cpp:69
websocketpp::message_buffer::alloc::con_msg_manager< message_type > con_msg_manager_type
Definition: hybi07.cpp:49
static const bool enable_extensions
Extension related config.
Definition: hybi07.cpp:56
static uint16_t const uri_default_port
Default port for ws://.
Definition: uri.hpp:44
A connection message manager that allocates a new message for each request.
Definition: alloc.hpp:41
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
Definition: hybi13.hpp:329
websocketpp::message_buffer::message< websocketpp::message_buffer::alloc::con_msg_manager > message_type
Definition: hybi07.cpp:47