websocket_request_handler.cpp
Go to the documentation of this file.
2 
5 
6 #include <boost/algorithm/string.hpp>
7 #include <boost/regex.hpp>
8 #include <openssl/bio.h>
9 #include <openssl/buffer.h>
10 #include <openssl/evp.h>
11 #include <openssl/hmac.h>
12 #include <openssl/sha.h>
13 
14 namespace async_web_server_cpp
15 {
16 
18  "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
19 
22  : handler_(handler)
23 {
24 }
25 
27  const HttpRequest& request, boost::shared_ptr<HttpConnection> connection,
28  const char* begin, const char* end)
29 {
30  std::string connection_header =
31  request.get_header_value_or_default("Connection", "");
32  std::string upgrade_header =
33  request.get_header_value_or_default("Upgrade", "");
34  std::string websocket_key =
35  request.get_header_value_or_default("Sec-WebSocket-Key", "");
36 
37  if (connection_header.find("Upgrade") != std::string::npos
38  && boost::iequals(upgrade_header, "websocket")
39  && websocket_key.size() > 0)
40  {
41  std::string concat_key = websocket_key + KEY_MAGIC_STRING;
42 
43  // compute the sha1 hash of the concatonated key
44  unsigned char sha1_buf[20];
45  SHA1((const unsigned char*)concat_key.data(), concat_key.size(),
46  sha1_buf);
47 
48  // base64 encode the hash
49  BIO* b64 = BIO_new(BIO_f_base64());
50  BIO* bmem = BIO_new(BIO_s_mem());
51  b64 = BIO_push(b64, bmem);
52  BIO_write(b64, sha1_buf, 20);
53  BIO_flush(b64);
54  BUF_MEM* bptr;
55  BIO_get_mem_ptr(b64, &bptr);
56  std::string base64_key(bptr->data, bptr->length - 1);
57  BIO_free_all(b64);
58 
61  .header("Upgrade", "websocket")
62  .header("Connection", "Upgrade")
63  .header("Sec-WebSocket-Version", "13")
64  .header("Sec-WebSocket-Accept", base64_key)
65  .write(connection);
66 
67  WebsocketConnectionPtr websocket_connection(
68  new WebsocketConnection(connection));
69  WebsocketConnection::MessageHandler message_handler =
70  handler_(request, websocket_connection);
71  websocket_connection->set_message_handler(message_handler);
72  websocket_connection->handle_read(begin, end);
73  }
74  else
75  {
78  begin, end);
79  }
80  return true;
81 }
82 
83 } // namespace async_web_server_cpp
async_web_server_cpp::WebsocketHttpRequestHandler::WebsocketHttpRequestHandler
WebsocketHttpRequestHandler(WebsocketRequestHandler handler)
Definition: websocket_request_handler.cpp:20
async_web_server_cpp::ReplyBuilder::header
ReplyBuilder & header(const std::string &name, const std::string &value)
Definition: http_reply.cpp:457
async_web_server_cpp::WebsocketRequestHandler
boost::function< WebsocketConnection::MessageHandler(const HttpRequest &, boost::shared_ptr< WebsocketConnection >)> WebsocketRequestHandler
Definition: websocket_request_handler.hpp:13
websocket_request_handler.hpp
async_web_server_cpp
Definition: http_connection.hpp:14
async_web_server_cpp::HttpReply::stock_reply
static HttpServerRequestHandler stock_reply(status_type status)
Definition: http_reply.cpp:410
async_web_server_cpp::WebsocketConnectionPtr
boost::shared_ptr< WebsocketConnection > WebsocketConnectionPtr
Definition: websocket_connection.hpp:18
async_web_server_cpp::HttpRequest
Definition: http_request.hpp:18
async_web_server_cpp::HttpRequest::get_header_value_or_default
std::string get_header_value_or_default(const std::string &name, const std::string &default_value) const
Definition: http_request.cpp:62
async_web_server_cpp::HttpReply::switching_protocols
@ switching_protocols
Definition: http_reply.hpp:26
async_web_server_cpp::WebsocketHttpRequestHandler::handler_
WebsocketRequestHandler handler_
Definition: websocket_request_handler.hpp:34
async_web_server_cpp::WebsocketHttpRequestHandler::KEY_MAGIC_STRING
static const std::string KEY_MAGIC_STRING
Definition: websocket_request_handler.hpp:31
async_web_server_cpp::WebsocketHttpRequestHandler::operator()
bool operator()(const HttpRequest &request, boost::shared_ptr< HttpConnection > connection, const char *begin, const char *end)
Definition: websocket_request_handler.cpp:26
async_web_server_cpp::HttpReply::builder
static ReplyBuilder builder(status_type status)
Definition: http_reply.cpp:447
async_web_server_cpp::WebsocketConnection
Definition: websocket_connection.hpp:26
async_web_server_cpp::ReplyBuilder::write
void write(HttpConnectionPtr connection)
Definition: http_reply.cpp:475
http_reply.hpp
websocket_connection.hpp
async_web_server_cpp::HttpReply::bad_request
@ bad_request
Definition: http_reply.hpp:35
async_web_server_cpp::WebsocketConnection::MessageHandler
boost::function< void(const WebsocketMessage &message)> MessageHandler
Definition: websocket_connection.hpp:34


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06