28 #ifndef WEBSOCKETPP_PROCESSOR_HYBI00_HPP
29 #define WEBSOCKETPP_PROCESSOR_HYBI00_HPP
33 #include <websocketpp/frame.hpp>
34 #include <websocketpp/utf8_validator.hpp>
35 #include <websocketpp/common/network.hpp>
36 #include <websocketpp/common/md5.hpp>
37 #include <websocketpp/common/platforms.hpp>
39 #include <websocketpp/processors/processor.hpp>
48 template <
typename config>
53 typedef typename config::request_type request_type;
54 typedef typename config::response_type response_type;
56 typedef typename config::message_type message_type;
57 typedef typename message_type::ptr message_ptr;
59 typedef typename config::con_msg_manager_type::ptr msg_manager_ptr;
61 explicit hybi00(
bool secure,
bool p_is_server, msg_manager_ptr manager)
66 , m_msg_manager(manager) {}
73 if (r.get_method() !=
"GET") {
77 if (r.get_version() !=
"HTTP/1.1") {
85 if (r.get_header(
"Sec-WebSocket-Key1") ==
"" ||
86 r.get_header(
"Sec-WebSocket-Key2") ==
"" ||
87 r.get_header(
"Sec-WebSocket-Key3") ==
"")
92 return lib::error_code();
96 std::string
const & subprotocol, response_type & res)
const
101 decode_client_key(req.get_header(
"Sec-WebSocket-Key1"), &key_final[0]);
104 decode_client_key(req.get_header(
"Sec-WebSocket-Key2"), &key_final[4]);
111 const std::string& key3 = req.get_header(
"Sec-WebSocket-Key3");
112 std::copy(key3.c_str(),
113 key3.c_str()+(std::min)(static_cast<size_t>(8), key3.size()),
117 "Sec-WebSocket-Key3",
118 md5::md5_hash_string(std::string(key_final,16))
121 res.append_header(
"Upgrade",
"WebSocket");
122 res.append_header(
"Connection",
"Upgrade");
126 if (res.get_header(
"Sec-WebSocket-Origin") ==
"") {
127 res.append_header(
"Sec-WebSocket-Origin",req.get_header(
"Origin"));
132 if (res.get_header(
"Sec-WebSocket-Location") ==
"") {
134 res.append_header(
"Sec-WebSocket-Location",uri->str());
137 if (subprotocol !=
"") {
138 res.replace_header(
"Sec-WebSocket-Protocol",subprotocol);
141 return lib::error_code();
154 std::vector<std::string>
const &)
const
169 response_type &)
const
174 std::string
get_raw(response_type
const & res)
const {
175 response_type temp = res;
176 temp.remove_header(
"Sec-WebSocket-Key3");
177 return temp.raw() + res.get_header(
"Sec-WebSocket-Key3");
180 std::string
const &
get_origin(request_type
const & r)
const {
181 return r.get_header(
"Origin");
193 std::vector<std::string> &)
195 return lib::error_code();
199 std::string h = request.get_header(
"Host");
201 size_t last_colon = h.rfind(
":");
202 size_t last_sbrace = h.rfind(
"]");
209 if (last_colon == std::string::npos ||
210 (last_sbrace != std::string::npos && last_sbrace > last_colon))
212 return lib::make_shared<uri>(base::m_secure, h, request.get_uri());
214 return lib::make_shared<uri>(base::m_secure,
215 h.substr(0,last_colon),
216 h.substr(last_colon+1),
233 size_t consume(uint8_t * buf,
size_t len, lib::error_code & ec) {
239 ec = lib::error_code();
242 if (m_state == HEADER) {
243 if (buf[p] == msg_hdr) {
245 m_msg_ptr = m_msg_manager->get_message(frame::opcode::text,1);
249 m_state = FATAL_ERROR;
255 m_state = FATAL_ERROR;
257 }
else if (m_state == PAYLOAD) {
258 uint8_t *it = std::find(buf+p,buf+len,msg_ftr);
264 l =
static_cast<size_t>(it-(buf+p));
265 m_msg_ptr->append_payload(buf+p,l);
291 return (m_state == READY);
299 message_ptr ret = m_msg_ptr;
300 m_msg_ptr = message_ptr();
319 if (in->get_opcode() != frame::opcode::text) {
323 std::string& i = in->get_raw_payload();
327 if (!utf8_validator::validate(i)) {
332 out->set_header(std::string(reinterpret_cast<char const *>(&msg_hdr),1));
336 out->append_payload(std::string(reinterpret_cast<char const *>(&msg_ftr),1));
341 out->set_prepared(
true);
343 return lib::error_code();
383 message_ptr out)
const
390 val.append(1,
'\xff');
391 val.append(1,
'\x00');
392 out->set_payload(val);
393 out->set_prepared(
true);
395 return lib::error_code();
398 void decode_client_key(std::string
const & key,
char * result)
const {
399 unsigned int spaces = 0;
400 std::string digits =
"";
404 for (
size_t i = 0; i < key.size(); i++) {
407 }
else if (key[i] >=
'0' && key[i] <=
'9') {
412 num =
static_cast<uint32_t
>(strtoul(digits.c_str(), NULL, 10));
413 if (spaces > 0 && num > 0) {
414 num = htonl(num/spaces);
415 std::copy(reinterpret_cast<char*>(&num),
416 reinterpret_cast<char*>(&num)+4,
419 std::fill(result,result+4,0);
430 uint8_t
const msg_hdr;
431 uint8_t
const msg_ftr;
435 msg_manager_ptr m_msg_manager;
436 message_ptr m_msg_ptr;
437 utf8_validator::validator m_validator;
443 #endif //WEBSOCKETPP_PROCESSOR_HYBI00_HPP
bool ready() const
Checks if there is a message ready.
int get_version() const
Get the protocol version of this processor.
uint16_t value
The type of a close code value.
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
lib::error_code extract_subprotocols(request_type const &, std::vector< std::string > &)
Extracts requested subprotocols from a handshake request.
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.
WebSocket protocol processor abstract base class.
No support for this feature in this protocol version.
Processor encountered invalid payload data.
std::string const & get_origin(request_type const &r) const
Return the value of the header containing the CORS origin.
Processor encountered a protocol violation in an incoming message.
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
lib::error_code prepare_close(close::status::value, std::string const &, message_ptr out) const
Prepare a close frame.
message_ptr get_message()
Retrieves the most recently processed message.
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
size_t consume(uint8_t *buf, size_t len, lib::error_code &ec)
Process new websocket connection bytes.
std::string get_key3() const
Get hybi00 handshake key3.
Namespace for the WebSocket++ project.
bool get_error() const
Tests whether the processor is in a fatal error state.
The endpoint is out of incoming message buffers.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
std::string get_raw(response_type const &res) const
Given a completed response, get the raw bytes to put on the wire.
virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
Prepare a message for writing.
Opcode was invalid for requested operation.
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 prepare_pong(const std::string &, message_ptr) const
Prepare a pong frame.
lib::error_code validate_server_handshake_response(request_type const &, response_type &) const
Validate the server's response to an outgoing handshake request.
lib::error_code prepare_ping(std::string const &, message_ptr) const
Prepare a ping frame.
The processor method was called with invalid arguments.
Processor for Hybi Draft version 00.