28 #ifndef WEBSOCKETPP_PROCESSOR_HYBI13_HPP 29 #define WEBSOCKETPP_PROCESSOR_HYBI13_HPP 53 template <
typename config>
72 explicit hybi13(
bool secure,
bool p_is_server, msg_manager_ptr manager, rng_type& rng)
101 template <
typename header_type>
114 bool error = header.get_header_as_plist(
"Sec-WebSocket-Extensions",p);
126 http::parameter_list::const_iterator it;
131 err_str_pair neg_ret;
132 for (it = p.begin(); it != p.end(); ++it) {
134 if (it->first !=
"permessage-deflate") {
171 ret.second += neg_ret.second;
186 if (r.get_method() !=
"GET") {
190 if (r.get_version() !=
"HTTP/1.1") {
198 if (r.get_header(
"Sec-WebSocket-Key").empty()) {
202 return lib::error_code();
210 std::string
const & subprotocol, response_type & response)
const 212 std::string server_key = request.get_header(
"Sec-WebSocket-Key");
220 response.replace_header(
"Sec-WebSocket-Accept",server_key);
224 if (!subprotocol.empty()) {
225 response.replace_header(
"Sec-WebSocket-Protocol",subprotocol);
228 return lib::error_code();
238 uri, std::vector<std::string>
const & subprotocols)
const 240 req.set_method(
"GET");
241 req.set_uri(uri->get_resource());
242 req.set_version(
"HTTP/1.1");
244 req.append_header(
"Upgrade",
"websocket");
245 req.append_header(
"Connection",
"Upgrade");
246 req.replace_header(
"Sec-WebSocket-Version",
"13");
247 req.replace_header(
"Host",uri->get_host_port());
249 if (!subprotocols.empty()) {
250 std::ostringstream result;
251 std::vector<std::string>::const_iterator it = subprotocols.begin();
253 while (it != subprotocols.end()) {
254 result <<
", " << *it++;
257 req.replace_header(
"Sec-WebSocket-Protocol",result.str());
262 unsigned char raw_key[16];
264 for (
int i = 0; i < 4; i++) {
266 std::copy(conv.
c,conv.
c+4,&raw_key[i*4]);
269 req.replace_header(
"Sec-WebSocket-Key",
base64_encode(raw_key, 16));
273 if (!offer.empty()) {
274 req.replace_header(
"Sec-WebSocket-Extensions",offer);
278 return lib::error_code();
288 response_type& res)
const 296 std::string
const & upgrade_header = res.get_header(
"Upgrade");
304 std::string
const & con_header = res.get_header(
"Connection");
312 std::string key = req.get_header(
"Sec-WebSocket-Key");
315 if (ec || key != res.get_header(
"Sec-WebSocket-Accept")) {
321 return lib::error_code();
324 std::string
get_raw(response_type
const & res)
const {
328 std::string
const &
get_origin(request_type
const & r)
const {
329 return r.get_header(
"Origin");
333 std::vector<std::string> & subprotocol_list)
335 if (!req.get_header(
"Sec-WebSocket-Protocol").empty()) {
338 if (!req.get_header_as_plist(
"Sec-WebSocket-Protocol",p)) {
339 http::parameter_list::const_iterator it;
341 for (it = p.begin(); it != p.end(); ++it) {
342 subprotocol_list.push_back(it->first);
348 return lib::error_code();
382 size_t consume(uint8_t * buf,
size_t len, lib::error_code & ec) {
385 ec = lib::error_code();
480 if (bytes_to_process > 0) {
526 uint8_t trailer[4] = {0x00, 0x00, 0xff, 0xff};
545 return lib::error_code();
618 std::string& i = in->get_raw_payload();
619 std::string& o = out->get_raw_payload();
629 && in->get_compressed();
630 bool fin = in->get_fin();
650 o.resize(o.size()-4);
666 std::copy(i.begin(),i.end(),o.begin());
681 out->set_prepared(
true);
684 return lib::error_code();
688 lib::error_code
prepare_ping(std::string
const & in, message_ptr out)
const {
692 lib::error_code
prepare_pong(std::string
const & in, message_ptr out)
const {
697 std::string
const & reason, message_ptr out)
const 721 payload.resize(reason.size()+2);
723 payload[0] = val.
c[0];
724 payload[1] = val.
c[1];
726 std::copy(reason.begin(),reason.end(),payload.begin()+2);
736 unsigned char message_digest[20];
737 sha1::calc(key.c_str(),key.length(),message_digest);
740 return lib::error_code();
783 return bytes_to_read;
809 size_t offset = out.size();
822 out.append(reinterpret_cast<char *>(buf),len);
849 bool is_server,
bool new_msg)
const 912 return lib::error_code();
946 if (
sizeof(
size_t) == 4 && (payload_size >> 32)) {
950 return lib::error_code();
978 std::string
const & payload, message_ptr out)
const 997 std::string & o = out->get_raw_payload();
998 o.resize(payload.size());
1010 std::copy(payload.begin(),payload.end(),o.begin());
1013 out->set_opcode(op);
1014 out->set_prepared(
true);
1016 return lib::error_code();
1078 #endif //WEBSOCKETPP_PROCESSOR_HYBI13_HPP bool is_control(value v)
Check if an opcode is for a control frame.
config::con_msg_manager_type msg_manager_type
bool invalid(value v)
Check if an opcode is invalid.
virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
Prepare a user data message for writing.
static uint8_t const payload_size_code_16bit
static char const connection_token[]
bool get_rsv1(basic_header const &h)
check whether the frame's RSV1 bit is set
uint16_t value
The type of a close code value.
config::message_type message_type
lib::error_code make_error_code(error::value e)
lib::error_code validate_server_handshake_response(request_type const &req, response_type &res) const
Validate the server's response to an outgoing handshake request.
uri_ptr get_uri_from_host(request_type &request, std::string scheme)
Extract a URI ptr from the host header of the request.
bool get_rsv3(basic_header const &h)
check whether the frame's RSV3 bit is set
msg_metadata m_control_msg
bool decode(iterator_type begin, iterator_type end)
Advance validator state with input from an iterator pair.
Clients may not send unmasked frames.
bool get_masked(basic_header const &h)
check whether the frame is masked
std::pair< lib::error_code, std::string > err_str_pair
std::string const & get_origin(request_type const &r) const
Return the value of the header containing the CORS origin.
WebSocket protocol processor abstract base class.
size_t copy_extended_header_bytes(uint8_t const *buf, size_t len)
Reads bytes from buf into m_extended_header.
bool get_rsv2(basic_header const &h)
check whether the frame's RSV2 bit is set
int get_version() const
Get the protocol version of this processor.
Using a reason requires a close code.
Illegal use of reserved bit.
Provides streaming UTF8 validation functionality.
opcode::value get_opcode(basic_header const &h)
Extract opcode from basic header.
bool reserved(value code)
Test whether a close code is in a reserved range.
size_t process_payload_bytes(uint8_t *buf, size_t len, lib::error_code &ec)
Reads bytes from buf into message payload.
static uint8_t const payload_size_basic
Maximum size of a basic WebSocket payload.
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
lib::error_code finalize_message()
Perform any finalization actions on an incoming message.
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
masking_key_type get_masking_key(basic_header const &, extended_header const &)
Extract the masking key from a frame header.
static char const upgrade_token[]
void masked_copy(std::string const &i, std::string &o, frame::masking_key_type key) const
Copy and mask/unmask in one operation.
bool invalid(value code)
Test whether a close code is invalid on the wire.
Four byte conversion union.
Processor encountered invalid payload data.
void byte_mask(input_iter b, input_iter e, output_iter o, masking_key_type const &key, size_t key_offset=0)
Byte by byte mask/unmask.
std::string base64_encode(unsigned char const *input, size_t len)
Encode a char buffer into a base64 string.
size_t get_header_len(basic_header const &)
Calculates the full length of the header based on the first bytes.
message_type::ptr message_ptr
Continuation without message.
std::string get_raw(response_type const &res) const
Given a completed response, get the raw bytes to put on the wire.
Stores, parses, and manipulates HTTP responses.
err_str_pair negotiate_extensions_helper(header_type const &header)
Extension negotiation helper function.
Stub class for use when disabling permessage_deflate extension.
lib::error_code extract_subprotocols(request_type const &req, std::vector< std::string > &subprotocol_list)
Extracts requested subprotocols from a handshake request.
config::response_type response_type
lib::shared_ptr< message > ptr
lib::error_code validate_incoming_extended_header(frame::basic_header h, frame::extended_header e) const
Validate an incoming extended header.
Not supported on 32 bit systems.
Extension related operation was ignored because extensions are disabled.
bool get_error() const
Test whether or not the processor is in a fatal error state.
message_ptr get_message()
Retrieves the most recently processed message.
err_str_pair negotiate_extensions(request_type const &request)
Initializes extensions based on the Sec-WebSocket-Extensions header.
frame::basic_header m_basic_header
bool validate(std::string const &s)
Validate a UTF8 string.
msg_metadata * m_current_msg
config::rng_type rng_type
size_t prepare_masking_key(masking_key_type const &key)
Extract a masking key into a value the size of a machine word.
config::request_type request_type
msg_manager_type::ptr msg_manager_ptr
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
bool ready() const
Test whether or not the processor has a message ready.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
virtual lib::error_code prepare_close(close::status::value code, std::string const &reason, message_ptr out) const
Prepare a close frame.
Catch-all error for processor policy errors that don't fit in other categories.
msg_manager_ptr m_msg_manager
Thread safe stub "random" integer generator.
Reserved close code used.
uint8_t get_basic_size(basic_header const &)
Extracts the raw payload length specified in the basic header.
bool has_permessage_deflate() const
Namespace for the WebSocket++ project.
lib::error_code process_handshake_key(std::string &key) const
Convert a client handshake key into a server response key in place.
uint64_t get_payload_size(basic_header const &, extended_header const &)
Extract the full payload size field from a WebSocket header.
static unsigned int const MAX_EXTENDED_HEADER_LENGTH
Maximum length of the variable portion of the WebSocket header.
Error parsing subprotocols.
Represents a buffer for a single WebSocket message.
lib::error_code prepare_control(frame::opcode::value op, std::string const &payload, message_ptr out) const
Generic prepare control frame with opcode and payload.
size_t m_max_message_size
static char const handshake_guid[]
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
permessage_deflate_type m_permessage_deflate
static uint8_t const payload_size_code_64bit
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
static value const no_status
A dummy value to indicate that no status code was received.
Servers may not send masked frames.
config::permessage_deflate_type permessage_deflate_type
lib::error_code prepare_ping(std::string const &in, message_ptr out) const
Get URI.
Fragmented control message.
Processor encountered a message that was too large.
Error parsing extensions.
size_t copy_basic_header_bytes(uint8_t const *buf, size_t len)
Reads bytes from buf into m_basic_header.
size_t consume(uint8_t *buf, size_t len, lib::error_code &ec)
Process new websocket connection bytes.
Stores, parses, and manipulates HTTP requests.
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.
lib::error_code validate_incoming_basic_header(frame::basic_header const &h, bool is_server, bool new_msg) const
Validate an incoming basic header.
std::string prepare_header(const basic_header &h, const extended_header &e)
Generate a properly sized contiguous string that encodes a full frame header.
lib::shared_ptr< con_msg_manager > ptr
static uint16_t const payload_size_extended
Maximum size of an extended WebSocket payload (basic payload = 126)
size_t byte_mask_circ(uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
Circular byte aligned mask/unmask.
Opcode was invalid for requested operation.
bool reserved(value v)
Check if an opcode is reserved.
bool get_fin(basic_header const &h)
Check whether the frame's FIN bit is set.
lib::error_code prepare_pong(std::string const &in, message_ptr out) const
Prepare a pong frame.
size_t get_bytes_needed() const
Retrieves the number of bytes presently needed by the processor This value may be used as a hint to t...
A connection message manager that allocates a new message for each request.
hybi13(bool secure, bool p_is_server, msg_manager_ptr manager, rng_type &rng)
Processor for Hybi version 13 (RFC6455)
lib::error_code client_handshake_request(request_type &req, uri_ptr uri, std::vector< std::string > const &subprotocols) const
Fill in a set of request headers for a client connection request.
Type used to convert close statuses between integer and wire representations.
static unsigned int const BASIC_HEADER_LENGTH
Minimum length of a WebSocket frame header.
T::const_iterator ci_find_substr(T const &haystack, T const &needle, std::locale const &loc=std::locale())
Find substring (case insensitive)
The processor method was called with invalid arguments.
void calc(void const *src, size_t bytelength, unsigned char *hash)
Calculate a SHA1 hash.
Payload length not minimally encoded.
err_str_pair negotiate_extensions(response_type const &response)
Initializes extensions based on the Sec-WebSocket-Extensions header.
bool complete()
Return whether the input sequence ended on a valid utf8 codepoint.
frame::extended_header m_extended_header