@@ -55,33 +55,41 @@ namespace websocketpp {
55
55
56
56
typedef boost::asio::buffers_iterator<boost::asio::streambuf::const_buffers_type> bufIterator;
57
57
58
- static std::pair<bufIterator, bool > match_header (bufIterator begin, bufIterator end)
58
+ static std::pair<bufIterator, bool > match_header (boost::shared_ptr<std::string> string,
59
+ bufIterator nBegin, bufIterator nEnd)
59
60
{
61
+ if (nBegin == nEnd)
62
+ return std::make_pair (nEnd, false );
63
+
64
+ (*string) += std::string (nBegin, nEnd);
65
+ std::string::const_iterator begin = string->begin ();
66
+ std::string::const_iterator end = string->end ();
67
+
60
68
static const std::string eol_match = " \n " ;
61
- static const std::string header_match = " \n\r\n " ;
62
- static const std::string alt_header_match = " \n\n " ;
69
+ static const std::string header_match = " \r\n\r\n " ;
63
70
static const std::string flash_match = " <policy-file-request/>" ;
64
71
65
72
// Do we have a complete HTTP request
66
- bufIterator it = std::search (begin, end, header_match.begin (), header_match.end ());
67
- if (it != end)
68
- return std::make_pair (it + header_match.size (), true );
69
- it = std::search (begin, end, alt_header_match.begin (), alt_header_match.end ());
73
+ std::string::const_iterator it = std::search (begin, end, header_match.begin (), header_match.end ());
70
74
if (it != end)
71
- return std::make_pair (it + alt_header_match.size (), true );
75
+ {
76
+ int leftOver = (end - (it + header_match.size ()));
77
+ return std::make_pair (nEnd - leftOver, true );
78
+ }
72
79
73
80
// If we don't have a flash policy request, we're done
74
81
it = std::search (begin, end, flash_match.begin (), flash_match.end ());
75
82
if (it == end) // No match
76
- return std::make_pair (end , false );
83
+ return std::make_pair (nEnd , false );
77
84
78
85
// If we have a line ending before the flash policy request, treat as http
79
- bufIterator it2 = std::search (begin, end, eol_match.begin (), eol_match.end ());
86
+ std::string::const_iterator it2 = std::search (begin, end, eol_match.begin (), eol_match.end ());
80
87
if ((it2 != end) && (it2 < it))
81
- return std::make_pair (end , false );
88
+ return std::make_pair (nEnd , false );
82
89
83
90
// Treat as flash policy request
84
- return std::make_pair (it + flash_match.size (), true );
91
+ int leftOver = (end - (it + flash_match.size ()));
92
+ return std::make_pair (nEnd - leftOver, true );
85
93
}
86
94
87
95
// Forward declarations
@@ -170,7 +178,7 @@ class server {
170
178
171
179
// initializes the websocket connection
172
180
void async_init ();
173
- void handle_read_request (const boost::system::error_code& error,
181
+ void handle_read_request (boost::shared_ptr<std::string>, const boost::system::error_code& error,
174
182
std::size_t bytes_transferred);
175
183
void handle_short_key3 (const boost::system::error_code& error,
176
184
std::size_t bytes_transferred);
@@ -543,12 +551,14 @@ void server<endpoint>::connection<connection_type>::async_init() {
543
551
m_connection.register_timeout (5000 ,fail::status::TIMEOUT_WS,
544
552
" Timeout on WebSocket handshake" );
545
553
554
+ boost::shared_ptr<std::string> stringPtr = boost::make_shared<std::string>();
546
555
m_connection.get_socket ().async_read_until (
547
556
m_connection.buffer (),
548
- match_header,
557
+ boost::bind (& match_header, stringPtr, _1, _2) ,
549
558
m_connection.get_strand ().wrap (boost::bind (
550
559
&type::handle_read_request,
551
560
m_connection.shared_from_this (),
561
+ stringPtr,
552
562
boost::asio::placeholders::error,
553
563
boost::asio::placeholders::bytes_transferred
554
564
))
@@ -562,6 +572,7 @@ void server<endpoint>::connection<connection_type>::async_init() {
562
572
template <class endpoint >
563
573
template <class connection_type >
564
574
void server<endpoint>::connection<connection_type>::handle_read_request(
575
+ boost::shared_ptr<std::string> header,
565
576
const boost::system::error_code& error, std::size_t /* bytes_transferred*/ )
566
577
{
567
578
if (error) {
@@ -571,9 +582,11 @@ void server<endpoint>::connection<connection_type>::handle_read_request(
571
582
m_connection.terminate (false );
572
583
return ;
573
584
}
585
+
586
+ m_connection.buffer ().consume (header->size ());
574
587
575
588
try {
576
- std::istream request (&m_connection. buffer () );
589
+ std::istringstream request (*header );
577
590
578
591
if (!m_request.parse_complete (request)) {
579
592
// not a valid HTTP request/response
@@ -607,6 +620,8 @@ void server<endpoint>::connection<connection_type>::handle_read_request(
607
620
// m_endpoint.m_alog.at(log::alevel::DEBUG_HANDSHAKE) << m_request.raw() << log::endl;
608
621
609
622
std::string h = m_request.header (" Upgrade" );
623
+ if (h.empty ())
624
+ h = m_request.header (" upgrade" );
610
625
if (boost::ifind_first (h," websocket" )) {
611
626
// Version is stored in the Sec-WebSocket-Version header for all
612
627
// versions after draft Hybi 00/Hixie 76. The absense of a version
0 commit comments