Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::processor::hybi00< config > Class Template Reference

Processor for Hybi Draft version 00. More...

#include <hybi00.hpp>

Inheritance diagram for websocketpp::processor::hybi00< config >:
Collaboration diagram for websocketpp::processor::hybi00< config >:

Public Types

typedef processor< configbase
 
typedef config::request_type request_type
 
typedef config::response_type response_type
 
typedef config::message_type message_type
 
typedef message_type::ptr message_ptr
 
typedef config::con_msg_manager_type::ptr msg_manager_ptr
 
- Public Types inherited from websocketpp::processor::processor< config >
typedef processor< configtype
 
typedef config::request_type request_type
 
typedef config::response_type response_type
 
typedef config::message_type::ptr message_ptr
 
typedef std::pair< lib::error_code, std::string > err_str_pair
 

Public Member Functions

 hybi00 (bool secure, bool p_is_server, msg_manager_ptr manager)
 
int get_version () const
 Get the protocol version of this processor.
 
lib::error_code validate_handshake (request_type const &r) const
 validate a WebSocket handshake request for this version
 
lib::error_code process_handshake (request_type const &req, std::string const &subprotocol, response_type &res) const
 Calculate the appropriate response for this websocket request.
 
lib::error_code client_handshake_request (request_type &, uri_ptr, std::vector< std::string > const &) const
 Fill in a set of request headers for a client connection request.
 
lib::error_code validate_server_handshake_response (request_type const &, response_type &) const
 Validate the server's response to an outgoing handshake request.
 
std::string get_raw (response_type const &res) const
 Given a completed response, get the raw bytes to put on the wire.
 
std::string const & get_origin (request_type const &r) const
 Return the value of the header containing the CORS origin.
 
lib::error_code extract_subprotocols (request_type const &req, std::vector< std::string > &subprotocol_list)
 Extracts requested subprotocols from a handshake request.
 
uri_ptr get_uri (request_type const &request) const
 Extracts client uri from a handshake request.
 
std::string get_key3 () const
 Get hybi00 handshake key3.
 
size_t consume (uint8_t *buf, size_t len, lib::error_code &ec)
 Process new websocket connection bytes.
 
bool ready () const
 Checks if there is a message ready.
 
bool get_error () const
 Tests whether the processor is in a fatal error state.
 
message_ptr get_message ()
 Retrieves the most recently processed message.
 
virtual lib::error_code prepare_data_frame (message_ptr in, message_ptr out)
 Prepare a message for writing.
 
lib::error_code prepare_ping (std::string const &, message_ptr) const
 Prepare a ping frame.
 
lib::error_code prepare_pong (std::string const &, message_ptr) const
 Prepare a pong frame.
 
lib::error_code prepare_close (close::status::value, std::string const &, message_ptr out) const
 Prepare a close frame.
 
- Public Member Functions inherited from websocketpp::processor::processor< config >
 processor (bool secure, bool p_is_server)
 
virtual ~processor ()
 
size_t get_max_message_size () const
 Get maximum message size.
 
void set_max_message_size (size_t new_value)
 Set maximum message size.
 
virtual bool has_permessage_compress () const
 Returns whether or not the permessage_compress extension is implemented.
 
virtual err_str_pair negotiate_extensions (request_type const &)
 Initializes extensions based on the Sec-WebSocket-Extensions header.
 
virtual err_str_pair negotiate_extensions (response_type const &)
 Initializes extensions based on the Sec-WebSocket-Extensions header.
 
virtual size_t get_bytes_needed () const
 
virtual lib::error_code prepare_data_frame (message_ptr in, message_ptr out)=0
 Prepare a data message for writing.
 
virtual lib::error_code prepare_ping (std::string const &in, message_ptr out) const =0
 Prepare a ping frame.
 
virtual lib::error_code prepare_pong (std::string const &in, message_ptr out) const =0
 Prepare a pong frame.
 
virtual lib::error_code prepare_close (close::status::value code, std::string const &reason, message_ptr out) const =0
 Prepare a close frame.
 

Additional Inherited Members

- Protected Attributes inherited from websocketpp::processor::processor< config >
bool const m_secure
 
bool const m_server
 
size_t m_max_message_size
 

Detailed Description

template<typename config>
class websocketpp::processor::hybi00< config >

There are many differences between Hybi 00 and Hybi 13

Definition at line 54 of file hybi00.hpp.

Member Typedef Documentation

◆ base

Definition at line 56 of file hybi00.hpp.

◆ message_ptr

template<typename config >
message_type::ptr websocketpp::processor::hybi00< config >::message_ptr

Definition at line 62 of file hybi00.hpp.

◆ message_type

template<typename config >
config::message_type websocketpp::processor::hybi00< config >::message_type

Definition at line 61 of file hybi00.hpp.

◆ msg_manager_ptr

template<typename config >
config::con_msg_manager_type::ptr websocketpp::processor::hybi00< config >::msg_manager_ptr

Definition at line 64 of file hybi00.hpp.

◆ request_type

template<typename config >
config::request_type websocketpp::processor::hybi00< config >::request_type

Definition at line 58 of file hybi00.hpp.

◆ response_type

template<typename config >
config::response_type websocketpp::processor::hybi00< config >::response_type

Definition at line 59 of file hybi00.hpp.

Constructor & Destructor Documentation

◆ hybi00()

template<typename config >
websocketpp::processor::hybi00< config >::hybi00 ( bool secure,
bool p_is_server,
msg_manager_ptr manager )
inlineexplicit

Definition at line 66 of file hybi00.hpp.

67 : processor<config>(secure, p_is_server)
68 , msg_hdr(0x00)
69 , msg_ftr(0xff)
70 , m_state(HEADER)
71 , m_msg_manager(manager) {}

Member Function Documentation

◆ client_handshake_request()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::client_handshake_request ( request_type & ,
uri_ptr ,
std::vector< std::string > const &  ) const
inlinevirtual

The Hybi 00 processor only implements incoming connections so this will always return an error.

Parameters
[out]reqSet of headers to fill in
[in]uriThe uri being connected to
[in]subprotocolsThe list of subprotocols to request

Implements websocketpp::processor::processor< config >.

Definition at line 158 of file hybi00.hpp.

160 {
162 }
@ no_protocol_support
No support for this feature in this protocol version.
Definition base.hpp:135
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
Definition base.hpp:244
Here is the call graph for this function:
Here is the caller graph for this function:

◆ consume()

template<typename config >
size_t websocketpp::processor::hybi00< config >::consume ( uint8_t * buf,
size_t len,
lib::error_code & ec )
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 252 of file hybi00.hpp.

252 {
253 // if in state header we are expecting a 0x00 byte, if we don't get one
254 // it is a fatal error
255 size_t p = 0; // bytes processed
256 size_t l = 0;
257
258 ec = lib::error_code();
259
260 while (p < len) {
261 if (m_state == HEADER) {
262 if (buf[p] == msg_hdr) {
263 p++;
264 m_msg_ptr = m_msg_manager->get_message(frame::opcode::text,1);
265
266 if (!m_msg_ptr) {
267 ec = make_error_code(websocketpp::error::no_incoming_buffers);
268 m_state = FATAL_ERROR;
269 } else {
270 m_state = PAYLOAD;
271 }
272 } else {
273 ec = make_error_code(error::protocol_violation);
274 m_state = FATAL_ERROR;
275 }
276 } else if (m_state == PAYLOAD) {
277 uint8_t *it = std::find(buf+p,buf+len,msg_ftr);
278
279 // 0 1 2 3 4 5
280 // 0x00 0x23 0x23 0x23 0xff 0xXX
281
282 // Copy payload bytes into message
283 l = static_cast<size_t>(it-(buf+p));
284 m_msg_ptr->append_payload(buf+p,l);
285 p += l;
286
287 if (it != buf+len) {
288 // message is done, copy it and the trailing
289 p++;
290 // TODO: validation
291 m_state = READY;
292 }
293 } else {
294 // TODO
295 break;
296 }
297 }
298 // If we get one, we create a new message and move to application state
299
300 // if in state application we are copying bytes into the output message
301 // and validating them for UTF8 until we hit a 0xff byte. Once we hit
302 // 0x00, the message is complete and is dispatched. Then we go back to
303 // header state.
304
305 //ec = make_error_code(error::not_implemented);
306 return p;
307 }
const mie::Vuint & p
Definition bn.cpp:27
@ no_incoming_buffers
The endpoint is out of incoming message buffers.
Definition error.hpp:71
@ protocol_violation
Processor encountered a protocol violation in an incoming message.
Definition base.hpp:75
unsigned char uint8_t
Definition stdint.h:124
size_t len
uint8_t buf[2048]
int l
Here is the caller graph for this function:

◆ extract_subprotocols()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::extract_subprotocols ( request_type const & req,
std::vector< std::string > & subprotocol_list )
inlinevirtual

hybi00 does support subprotocols https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.9

Parameters
[in]reqThe request to extract from
[out]subprotocol_listA reference to a vector of strings to store the results in.

Implements websocketpp::processor::processor< config >.

Definition at line 198 of file hybi00.hpp.

200 {
201 if (!req.get_header("Sec-WebSocket-Protocol").empty()) {
202 http::parameter_list p;
203
204 if (!req.get_header_as_plist("Sec-WebSocket-Protocol",p)) {
205 http::parameter_list::const_iterator it;
206
207 for (it = p.begin(); it != p.end(); ++it) {
208 subprotocol_list.push_back(it->first);
209 }
210 } else {
212 }
213 }
214 return lib::error_code();
215 }
@ subprotocol_parse_error
Error parsing subprotocols.
Definition base.hpp:147
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_error()

template<typename config >
bool websocketpp::processor::hybi00< config >::get_error ( ) const
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 313 of file hybi00.hpp.

313 {
314 return false;
315 }

◆ get_key3()

template<typename config >
std::string websocketpp::processor::hybi00< config >::get_key3 ( ) const
inline
Todo
This doesn't appear to be used anymore. It might be able to be removed

Definition at line 247 of file hybi00.hpp.

247 {
248 return "";
249 }

◆ get_message()

template<typename config >
message_ptr websocketpp::processor::hybi00< config >::get_message ( )
inlinevirtual

Retrieves a shared pointer to the recently completed message if there is one. If ready() returns true then there is a message available. Retrieving the message with get_message will reset the state of ready. As such, each new message may be retrieved only once. Calling get_message when there is no message available will result in a null pointer being returned.

Returns
A pointer to the most recently processed message or a null shared pointer.

Implements websocketpp::processor::processor< config >.

Definition at line 317 of file hybi00.hpp.

317 {
318 message_ptr ret = m_msg_ptr;
319 m_msg_ptr = message_ptr();
320 m_state = HEADER;
321 return ret;
322 }
message_type::ptr message_ptr
Definition hybi00.hpp:62
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
CK_RV ret
Here is the caller graph for this function:

◆ get_origin()

template<typename config >
std::string const & websocketpp::processor::hybi00< config >::get_origin ( request_type const & request) const
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 185 of file hybi00.hpp.

185 {
186 return r.get_header("Origin");
187 }
const mie::Vuint & r
Definition bn.cpp:28

◆ get_raw()

template<typename config >
std::string websocketpp::processor::hybi00< config >::get_raw ( response_type const & request) const
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 179 of file hybi00.hpp.

179 {
180 response_type temp = res;
181 temp.remove_header("Sec-WebSocket-Key3");
182 return temp.raw() + res.get_header("Sec-WebSocket-Key3");
183 }
config::response_type response_type
Definition hybi00.hpp:59

◆ get_uri()

template<typename config >
uri_ptr websocketpp::processor::hybi00< config >::get_uri ( request_type const & request) const
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 217 of file hybi00.hpp.

217 {
218 std::string h = request.get_header("Host");
219
220 size_t last_colon = h.rfind(":");
221 size_t last_sbrace = h.rfind("]");
222
223 // no : = hostname with no port
224 // last : before ] = ipv6 literal with no port
225 // : with no ] = hostname with port
226 // : after ] = ipv6 literal with port
227
228 if (last_colon == std::string::npos ||
229 (last_sbrace != std::string::npos && last_sbrace > last_colon))
230 {
231 return lib::make_shared<uri>(base::m_secure, h, request.get_uri());
232 } else {
233 return lib::make_shared<uri>(base::m_secure,
234 h.substr(0,last_colon),
235 h.substr(last_colon+1),
236 request.get_uri());
237 }
238
239 // TODO: check if get_uri is a full uri
240 }
Here is the caller graph for this function:

◆ get_version()

template<typename config >
int websocketpp::processor::hybi00< config >::get_version ( ) const
inlinevirtual

Implements websocketpp::processor::processor< config >.

Definition at line 73 of file hybi00.hpp.

73 {
74 return 0;
75 }
Here is the caller graph for this function:

◆ prepare_close()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::prepare_close ( close::status::value ,
std::string const & ,
message_ptr out ) const
inline

Hybi 00 doesn't support the close code or reason so these parameters are ignored.

Parameters
codeThe close code to send
reasonThe reason string to send
outThe message buffer to prepare the fame in
Returns
Status code, zero on success, non-zero on failure

Definition at line 401 of file hybi00.hpp.

403 {
404 if (!out) {
405 return lib::error_code(error::invalid_arguments);
406 }
407
408 std::string val;
409 val.append(1,'\xff');
410 val.append(1,'\x00');
411 out->set_payload(val);
412 out->set_prepared(true);
413
414 return lib::error_code();
415 }
@ invalid_arguments
The processor method was called with invalid arguments.
Definition base.hpp:84

◆ prepare_data_frame()

template<typename config >
virtual lib::error_code websocketpp::processor::hybi00< config >::prepare_data_frame ( message_ptr in,
message_ptr out )
inlinevirtual

Performs validation, masking, compression, etc. will return an error if there was an error, otherwise msg will be ready to be written

Definition at line 329 of file hybi00.hpp.

330 {
331 if (!in || !out) {
332 return make_error_code(error::invalid_arguments);
333 }
334
335 // TODO: check if the message is prepared already
336
337 // validate opcode
338 if (in->get_opcode() != frame::opcode::text) {
339 return make_error_code(error::invalid_opcode);
340 }
341
342 std::string& i = in->get_raw_payload();
343 //std::string& o = out->get_raw_payload();
344
345 // validate payload utf8
346 if (!utf8_validator::validate(i)) {
347 return make_error_code(error::invalid_payload);
348 }
349
350 // generate header
351 out->set_header(std::string(reinterpret_cast<char const *>(&msg_hdr),1));
352
353 // process payload
354 out->set_payload(i);
355 out->append_payload(std::string(reinterpret_cast<char const *>(&msg_ftr),1));
356
357 // hybi00 doesn't support compression
358 // hybi00 doesn't have masking
359
360 out->set_prepared(true);
361
362 return lib::error_code();
363 }
@ invalid_opcode
Opcode was invalid for requested operation.
Definition base.hpp:87
@ invalid_payload
Processor encountered invalid payload data.
Definition base.hpp:81
bool validate(std::string const &s)
Validate a UTF8 string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_ping()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::prepare_ping ( std::string const & ,
message_ptr  ) const
inline

Hybi 00 doesn't support pings so this will always return an error

Parameters
inThe string to use for the ping payload
outThe message buffer to prepare the ping in.
Returns
Status code, zero on success, non-zero on failure

Definition at line 373 of file hybi00.hpp.

374 {
375 return lib::error_code(error::no_protocol_support);
376 }

◆ prepare_pong()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::prepare_pong ( std::string const & ,
message_ptr  ) const
inline

Hybi 00 doesn't support pongs so this will always return an error

Parameters
inThe string to use for the pong payload
outThe message buffer to prepare the pong in.
Returns
Status code, zero on success, non-zero on failure

Definition at line 386 of file hybi00.hpp.

387 {
388 return lib::error_code(error::no_protocol_support);
389 }

◆ process_handshake()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::process_handshake ( request_type const & req,
std::string const & subprotocol,
response_type & res ) const
inlinevirtual
Parameters
reqThe request to process
subprotocolThe subprotocol in use
resThe response to store the processed response in
Returns
An error code, 0 on success, non-zero for other errors

Implements websocketpp::processor::processor< config >.

Definition at line 100 of file hybi00.hpp.

102 {
103 char key_final[16];
104
105 // copy key1 into final key
106 decode_client_key(req.get_header("Sec-WebSocket-Key1"), &key_final[0]);
107
108 // copy key2 into final key
109 decode_client_key(req.get_header("Sec-WebSocket-Key2"), &key_final[4]);
110
111 // copy key3 into final key
112 // key3 should be exactly 8 bytes. If it is more it will be truncated
113 // if it is less the final key will almost certainly be wrong.
114 // TODO: decide if it is best to silently fail here or produce some sort
115 // of warning or exception.
116 std::string const & key3 = req.get_header("Sec-WebSocket-Key3");
117 std::copy(key3.c_str(),
118 key3.c_str()+(std::min)(static_cast<size_t>(8), key3.size()),
119 &key_final[8]);
120
121 res.append_header(
122 "Sec-WebSocket-Key3",
123 md5::md5_hash_string(std::string(key_final,16))
124 );
125
126 res.append_header("Upgrade","WebSocket");
127 res.append_header("Connection","Upgrade");
128
129 // Echo back client's origin unless our local application set a
130 // more restrictive one.
131 if (res.get_header("Sec-WebSocket-Origin").empty()) {
132 res.append_header("Sec-WebSocket-Origin",req.get_header("Origin"));
133 }
134
135 // Echo back the client's request host unless our local application
136 // set a different one.
137 if (res.get_header("Sec-WebSocket-Location").empty()) {
138 uri_ptr uri = get_uri(req);
139 res.append_header("Sec-WebSocket-Location",uri->str());
140 }
141
142 if (!subprotocol.empty()) {
143 res.replace_header("Sec-WebSocket-Protocol",subprotocol);
144 }
145
146 return lib::error_code();
147 }
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
Definition hybi00.hpp:217
std::string md5_hash_string(std::string const &s)
Definition md5.hpp:415
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:351
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ready()

template<typename config >
bool websocketpp::processor::hybi00< config >::ready ( ) const
inlinevirtual

Checks if the most recent consume operation processed enough bytes to complete a new WebSocket message. The message can be retrieved by calling get_message() which will reset the internal state to not-ready and allow consume to read more bytes.

Returns
Whether or not a message is ready.

Implements websocketpp::processor::processor< config >.

Definition at line 309 of file hybi00.hpp.

309 {
310 return (m_state == READY);
311 }
Here is the caller graph for this function:

◆ validate_handshake()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::validate_handshake ( request_type const & request) const
inlinevirtual
Parameters
requestThe WebSocket handshake request to validate. is_websocket_handshake(request) must be true and get_websocket_version(request) must equal this->get_version().
Returns
A status code, 0 on success, non-zero for specific sorts of failure

Implements websocketpp::processor::processor< config >.

Definition at line 77 of file hybi00.hpp.

77 {
78 if (r.get_method() != "GET") {
79 return make_error_code(error::invalid_http_method);
80 }
81
82 if (r.get_version() != "HTTP/1.1") {
83 return make_error_code(error::invalid_http_version);
84 }
85
86 // required headers
87 // Host is required by HTTP/1.1
88 // Connection is required by is_websocket_handshake
89 // Upgrade is required by is_websocket_handshake
90 if (r.get_header("Sec-WebSocket-Key1").empty() ||
91 r.get_header("Sec-WebSocket-Key2").empty() ||
92 r.get_header("Sec-WebSocket-Key3").empty())
93 {
94 return make_error_code(error::missing_required_header);
95 }
96
97 return lib::error_code();
98 }
@ missing_required_header
Missing Required Header.
Definition base.hpp:129
@ invalid_http_method
Invalid HTTP method.
Definition base.hpp:120
@ invalid_http_version
Invalid HTTP version.
Definition base.hpp:123
Here is the caller graph for this function:

◆ validate_server_handshake_response()

template<typename config >
lib::error_code websocketpp::processor::hybi00< config >::validate_server_handshake_response ( request_type const & ,
response_type &  ) const
inlinevirtual

The Hybi 00 processor only implements incoming connections so this will always return an error.

Parameters
reqThe original request sent
resThe reponse to generate
Returns
An error code, 0 on success, non-zero for other errors

Implements websocketpp::processor::processor< config >.

Definition at line 173 of file hybi00.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: