2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
28#ifndef HTTP_PARSER_HPP
29#define HTTP_PARSER_HPP
36#include <websocketpp/utilities.hpp>
37#include <websocketpp/http/constants.hpp>
52namespace body_encoding {
60typedef std::map<std::string, std::string, utility::ci_less > header_list;
64
65
66
67
68
69
70
71
72template <
typename InputIterator>
73std::pair<std::string,InputIterator> extract_token(InputIterator begin,
77 return std::make_pair(std::string(begin,it),it);
82
83
84
85
86
87
88
89
90
91template <
typename InputIterator>
92std::pair<std::string,InputIterator> extract_quoted_string(InputIterator begin,
98 return std::make_pair(s,begin);
102 return std::make_pair(s,begin);
105 InputIterator cursor = begin+1;
106 InputIterator marker = cursor;
108 cursor = std::find(cursor,end,
'"');
110 while (cursor != end) {
112 if (*(cursor-1) ==
'\\') {
113 s.append(marker,cursor-1);
118 s.append(marker,cursor);
120 return std::make_pair(s,cursor);
123 cursor = std::find(cursor,end,
'"');
126 return std::make_pair(
"",begin);
131
132
133
134
135
136
137
138template <
typename InputIterator>
139InputIterator extract_lws(InputIterator begin, InputIterator end) {
140 InputIterator it = begin;
143 if (end-begin > 2 && *begin ==
'\r' && *(begin+1) ==
'\n' &&
155
156
157
158
159
160
161
162
163template <
typename InputIterator>
164InputIterator extract_all_lws(InputIterator begin, InputIterator end) {
165 InputIterator old_it;
166 InputIterator new_it = begin;
173 new_it = extract_lws(old_it,end);
174 }
while (new_it != end && old_it != new_it);
181
182
183
184
185
186
187
188
189
190
191
192
193
194template <
typename InputIterator>
195InputIterator extract_attributes(InputIterator begin, InputIterator end,
198 InputIterator cursor;
206 std::pair<std::string,InputIterator> ret;
208 while (cursor != end) {
211 cursor =
http::parser::extract_all_lws(cursor,end);
220 if (*cursor ==
';') {
230 cursor =
http::parser::extract_all_lws(cursor,end);
231 ret =
http::parser::extract_token(cursor,end);
233 if (ret.first.empty()) {
241 cursor =
http::parser::extract_all_lws(cursor,end);
242 if (cursor == end || *cursor !=
'=') {
245 attributes[name].clear();
252 cursor =
http::parser::extract_all_lws(cursor,end);
258 ret =
http::parser::extract_quoted_string(cursor,end);
259 if (ret.second != cursor) {
260 attributes[name] = ret.first;
265 ret =
http::parser::extract_token(cursor,end);
266 if (ret.first.empty()) {
270 attributes[name] = ret.first;
280
281
282
283
284
285
286
287
288
289
290
291
292template <
typename InputIterator>
293InputIterator extract_parameters(InputIterator begin, InputIterator end,
296 InputIterator cursor;
304 std::pair<std::string,InputIterator> ret;
307
308
309
310
311
312
313
314 while (cursor != end) {
315 std::string parameter_name;
319 cursor =
http::parser::extract_all_lws(cursor,end);
320 if (cursor == end) {
break;}
322 ret =
http::parser::extract_token(cursor,end);
324 if (ret.first.empty()) {
328 parameter_name = ret.first;
333 cursor =
http::parser::extract_all_lws(cursor,end);
336 parameters.push_back(std::make_pair(parameter_name,attributes));
341 if (*cursor ==
';') {
342 InputIterator acursor;
345 acursor =
http::parser::extract_attributes(cursor,end,attributes);
347 if (acursor == cursor) {
357 parameters.push_back(std::make_pair(parameter_name,attributes));
359 cursor =
http::parser::extract_all_lws(cursor,end);
360 if (cursor == end) {
break;}
363 if (*cursor !=
',') {
379inline std::string strip_lws(std::string
const & input) {
380 std::string::const_iterator begin = extract_all_lws(input.begin(),input.end());
381 if (begin == input.end()) {
382 return std::string();
385 std::string::const_reverse_iterator rbegin = extract_all_lws(input.rbegin(),input.rend());
386 if (rbegin == input.rend()) {
387 return std::string();
390 return std::string(begin,rbegin.base());
395
396
397
402 , m_body_bytes_needed(0)
403 , m_body_bytes_max(max_body_size)
404 , m_body_encoding(body_encoding::unknown) {}
408
409
416
417
418
419
420
421 void set_version(std::string
const & version);
425
426
427
428
429
430 std::string
const & get_header(std::string
const & key)
const;
434
435
436
437
438
439
440
441 bool get_header_as_plist(std::string
const & key,
parameter_list & out)
446
447
448
449
450
451
456
457
458
459
460
461
462
463
464
465
466
467
468
469 void append_header(std::string
const & key, std::string
const & val);
473
474
475
476
477
478
479
480
481
482
483
484
485
486 void replace_header(std::string
const & key, std::string
const & val);
490
491
492
493
494
495
496
497 void remove_header(std::string
const & key);
501
502
503
504
511
512
513
514
515
516
517
518 void set_body(std::string
const & value);
522
523
524
525
526
527
528
530 return m_body_bytes_max;
535
536
537
538
539
540
541
543 m_body_bytes_max = value;
548
549
550
551
552 bool parse_parameter_list(std::string
const & in,
parameter_list & out)
557
558
559
560
561
562 void process_header(std::string::iterator begin, std::string::iterator end);
566
567
568
569
570
571
572
573
574
575
576
581
582
583
584
585
586
587
588
593
594
595
596
597
598
600 return (m_body_bytes_needed == 0);
605
606
607
608
609
612 std::string m_version;
613 header_list m_headers;
615 size_t m_header_bytes;
618 size_t m_body_bytes_needed;
619 size_t m_body_bytes_max;
620 body_encoding::value m_body_encoding;
627#include <websocketpp/http/impl/parser.hpp>
#define _WEBSOCKETPP_CPP11_FUNCTIONAL_
#define _WEBSOCKETPP_CPP11_THREAD_
#define _WEBSOCKETPP_CPP11_MEMORY_
#define _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
Concurrency policy that uses std::mutex / boost::mutex.
Stub for user supplied base class.
Stub for user supplied base class.
Stub class for use when disabling permessage_deflate extension.
header_list const & get_headers() const
Return a list of all HTTP headers.
size_t process_body(char const *buf, size_t len)
Process body data.
std::string const & get_body() const
Get HTTP body.
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
bool body_ready() const
Check if the parser is done parsing the body.
bool prepare_body()
Prepare the parser to begin parsing body data.
void set_max_body_size(size_t value)
Set body size limit.
std::string raw_headers() const
Generate and return the HTTP headers as a string.
std::string const & get_version() const
Get the HTTP version string.
size_t get_max_body_size() const
Get body size limit.
Stores, parses, and manipulates HTTP requests.
std::string raw() const
Returns the full raw request (including the body)
std::string const & get_uri() const
Return the requested URI.
std::string const & get_method() const
Return the request method.
size_t consume(char const *buf, size_t len)
Process bytes in the input buffer.
bool ready() const
Returns whether or not the request is ready for reading.
std::string raw_head() const
Returns the raw request headers only (similar to an HTTP HEAD request)
Stores, parses, and manipulates HTTP responses.
Basic logger that outputs to an ostream.
Thread safe stub "random" integer generator.
Server endpoint role based on the given config.
Basic ASIO endpoint socket component.
Asio based endpoint transport component.
connection_hdl get_handle() const
Get the connection handle.
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
void set_uri(uri_ptr)
Set uri hook.
config::concurrency_type concurrency_type
transport concurrency policy
void async_shutdown(transport::shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
void set_write_handler(write_handler h)
Sets the write handler.
void set_secure(bool value)
Set whether or not this connection is secure.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
void fatal_error()
Signal transport error.
size_t read_some(char const *buf, size_t len)
Manual input supply (read some)
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
config::elog_type elog_type
Type of this transport's error logging policy.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
config::alog_type alog_type
Type of this transport's access logging policy.
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
size_t readsome(char const *buf, size_t len)
Manual input supply (DEPRECATED)
void init(init_handler handler)
Initialize the connection transport.
timer_ptr set_timer(long, timer_handler)
Call back a function after a period of time.
friend std::istream & operator>>(std::istream &in, type &t)
Overloaded stream input operator.
void set_vector_write_handler(vector_write_handler h)
Sets the vectored write handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
void set_handle(connection_hdl hdl)
Set Connection Handle.
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
Initiate an async_read for at least num_bytes bytes into buf.
void async_write(std::vector< buffer > const &bufs, transport::write_handler handler)
Asyncronous Transport Write (scatter-gather)
ptr get_shared()
Get a shared pointer to this component.
connection< config > type
Type of this connection transport component.
transport_con_type::ptr transport_con_ptr
void set_write_handler(write_handler h)
Sets the write handler.
config::concurrency_type concurrency_type
Type of this endpoint's concurrency policy.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
void async_connect(transport_con_ptr, uri_ptr, connect_handler cb)
Initiate a new connection.
config::alog_type alog_type
Type of this endpoint's access logging policy.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
void init_logging(lib::shared_ptr< alog_type > a, lib::shared_ptr< elog_type > e)
Initialize logging.
endpoint type
Type of this endpoint transport component.
iostream::connection< config > transport_con_type
void register_ostream(std::ostream *o)
Register a default output stream.
void set_secure(bool value)
Set whether or not endpoint can create secure connections.
config::elog_type elog_type
Type of this endpoint's error logging policy.
lib::shared_ptr< type > ptr
Type of a pointer to this endpoint transport component.
iostream transport error category
std::string get_query() const
Return the query portion.
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Concurrency handling support.
Library level error codes.
@ general
Catch-all library error.
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
@ invalid_port
Invalid port in URI.
@ client_only
Attempted to use a client specific feature on a server endpoint.
@ http_connection_ended
HTTP connection ended.
@ async_accept_not_listening
@ operation_canceled
The requested operation was canceled.
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
@ http_parse_error
HTTP parse error.
@ reserved_close_code
Close code is in a reserved range.
@ con_creation_failed
Connection creation attempted failed.
@ no_incoming_buffers
The endpoint is out of incoming message buffers.
@ invalid_state
The connection was in the wrong state for this operation.
@ extension_neg_failed
Extension negotiation failed.
@ rejected
Connection rejected.
@ unsupported_version
Unsupported WebSocket protocol version.
@ invalid_utf8
Invalid UTF-8.
@ invalid_close_code
Close code is invalid.
@ server_only
Attempted to use a server specific feature on a client endpoint.
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
@ close_handshake_timeout
WebSocket close handshake timed out.
@ invalid_subprotocol
Invalid subprotocol.
@ bad_close_code
Unable to parse close code.
@ open_handshake_timeout
WebSocket opening handshake timed out.
@ invalid_version
Invalid WebSocket protocol version.
@ send_queue_full
send attempted when endpoint write queue was full
@ test
Unit testing utility error code.
@ invalid_uri
An invalid uri was supplied.
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
bool is_not_token_char(unsigned char c)
Is the character a non-token.
bool is_whitespace_char(unsigned char c)
Is the character whitespace.
bool is_not_whitespace_char(unsigned char c)
Is the character non-whitespace.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Stub RNG policy that always returns 0.
Random number generation policies.
Transport policy that uses asio.
Generic transport related errors.
@ pass_through
underlying transport pass through
@ operation_not_supported
Operation not supported.
@ operation_aborted
Operation aborted.
@ tls_error
Other TLS error.
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
@ action_after_shutdown
read or write after shutdown
@ tls_short_read
TLS short read.
@ double_read
async_read called while another async_read was in progress
iostream transport errors
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
@ double_read
async_read called while another async_read was in progress
lib::error_code make_error_code(error::value e)
Get an error code with the given value and the iostream transport category.
lib::error_category const & get_category()
Get a reference to a static copy of the iostream transport error category.
Transport policy that uses STL iostream for I/O and does not support timers.
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
lib::function< lib::error_code(connection_hdl)> shutdown_handler
lib::function< lib::error_code(connection_hdl, char const *, size_t)> write_handler
The type and signature of the callback used by iostream transport to write.
Transport policies provide network connectivity and timers.
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
lib::function< void()> interrupt_handler
The type and signature of the callback passed to the interrupt method.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Namespace for the WebSocket++ project.
static uint16_t const uri_default_secure_port
Default port for wss://.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
static uint16_t const uri_default_port
Default port for ws://.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
Server config with asio transport and TLS disabled.
Extension specific settings:
static const uint8_t minimum_outgoing_window_bits
static const bool allow_disabling_context_takeover
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
static const long timeout_connect
Length of time to wait for TCP connect.
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
static const long timeout_socket_pre_init
Default timer values (in ms)
static bool const enable_multithreading
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Server config with iostream transport.
websocketpp::random::none::int_generator< uint32_t > rng_type
RNG policies.
static const websocketpp::log::level elog_level
Default static error logging channels.
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
static const size_t max_http_body_size
Default maximum http body size.
static const long timeout_open_handshake
Default timer values (in ms)
static const size_t max_message_size
Default maximum message size.
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
static const websocketpp::log::level alog_level
Default static access logging channels.
static const long timeout_pong
Length of time to wait for a pong after a ping.
static const bool silent_close
Suppresses the return of detailed connection close information.
static bool const enable_multithreading
static const size_t connection_read_buffer_size
Size of the per-connection read buffer.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
static const int client_version
WebSocket Protocol version to use as a client.
Package of log levels for logging access events.
static char const * channel_name(level channel)
Get the textual name of a channel given a channel id.
static level const fail
One line for each failed WebSocket connection with details.
static level const none
Special aggregate value representing "no levels".
static level const debug_handshake
Extra information about opening handshakes.
static level const devel
Development messages (warning: very chatty)
static level const all
Special aggregate value representing "all levels".
static level const debug_close
Extra information about closing handshakes.
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
static level const connect
Information about new connections.
static level const app
Special channel for application specific logs. Not used by the library.
static level const frame_header
One line per frame, includes the full frame header.
static level const message_payload
Reserved.
static level const endpoint
Reserved.
static level const message_header
Reserved.
static level const control
One line per control frame.
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
static level const access_core
static level const http
Access related to HTTP requests.
Package of values for hinting at the nature of a given logger.
static value const none
No information.
uint32_t value
Type of a channel type hint value.
static value const access
Access log.
static value const error
Error log.
Package of log levels for logging errors.
static level const devel
Low level debugging information (warning: very chatty)
static char const * channel_name(level channel)
Get the textual name of a channel given a channel id.
static level const library
static level const all
Special aggregate value representing "all levels".
static level const none
Special aggregate value representing "no levels".
static level const rerror
A simple utility buffer class.
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_