@@ -158,7 +158,7 @@ void connection::handle_http_line(const boost::system::error_code& ec)
158
158
std::string http_path_and_version;
159
159
std::getline (request_stream, http_path_and_version);
160
160
const size_t VersionPortionSize = sizeof (" HTTP/1.1\r " ) - 1 ;
161
-
161
+
162
162
// Make sure path and version is long enough to contain the HTTP version
163
163
if (http_path_and_version.size () < VersionPortionSize + 2 )
164
164
{
@@ -176,8 +176,18 @@ void connection::handle_http_line(const boost::system::error_code& ec)
176
176
177
177
// Get the path - remove the version portion and prefix space
178
178
builder.append_path (http_path_and_version.substr (1 , http_path_and_version.size () - VersionPortionSize - 1 ));
179
- m_request.set_request_uri (builder.to_uri ());
180
-
179
+ try
180
+ {
181
+ m_request.set_request_uri (builder.to_uri ());
182
+ }
183
+ catch (const uri_exception &e)
184
+ {
185
+ m_request.reply (status_codes::BadRequest, e.what ());
186
+ m_close = true ;
187
+ do_response (true );
188
+ return ;
189
+ }
190
+
181
191
// Get the version
182
192
std::string http_version = http_path_and_version.substr (http_path_and_version.size () - VersionPortionSize + 1 , VersionPortionSize - 2 );
183
193
// if HTTP version is 1.0 then disable pipelining
@@ -197,10 +207,10 @@ void connection::handle_headers()
197
207
while (std::getline (request_stream, header) && header != " \r " )
198
208
{
199
209
auto colon = header.find (' :' );
200
- if (colon != std::string::npos)
210
+ if (colon != std::string::npos && colon != 0 )
201
211
{
202
212
auto name = header.substr (0 , colon);
203
- auto value = header.substr (colon+ 1 , header.length () - (colon+ 1 )); // also exclude '\r'
213
+ auto value = header.substr (colon + 1 , header.length () - (colon + 1 )); // also exclude '\r'
204
214
http::details::trim_whitespace (name);
205
215
http::details::trim_whitespace (value);
206
216
@@ -217,7 +227,8 @@ void connection::handle_headers()
217
227
else
218
228
{
219
229
m_request.reply (status_codes::BadRequest);
220
- do_response ();
230
+ m_close = true ;
231
+ do_response (true );
221
232
return ;
222
233
}
223
234
}
@@ -290,7 +301,8 @@ void connection::handle_chunked_body(const boost::system::error_code& ec, int to
290
301
auto writebuf = m_request._get_impl ()->outstream ().streambuf ();
291
302
writebuf.putn (buffer_cast<const uint8_t *>(m_request_buf.data ()), toWrite).then ([=](pplx::task<size_t > writeChunkTask)
292
303
{
293
- try {
304
+ try
305
+ {
294
306
writeChunkTask.get ();
295
307
} catch (...) {
296
308
m_request._reply_if_not_already (status_codes::InternalError);
@@ -321,7 +333,8 @@ void connection::handle_body(const boost::system::error_code& ec)
321
333
writebuf.putn (boost::asio::buffer_cast<const uint8_t *>(m_request_buf.data ()), std::min (m_request_buf.size (), m_read_size - m_read)).then ([=](pplx::task<size_t > writtenSizeTask)
322
334
{
323
335
size_t writtenSize = 0 ;
324
- try {
336
+ try
337
+ {
325
338
writtenSize = writtenSizeTask.get ();
326
339
} catch (...) {
327
340
m_request._reply_if_not_already (status_codes::InternalError);
@@ -409,21 +422,16 @@ void connection::dispatch_request_to_listener()
409
422
catch (const std::exception &e)
410
423
{
411
424
pListenerLock->unlock ();
412
- std::ostringstream str_stream;
413
- str_stream << " Error: a std::exception was thrown out of http_listener handle: " << e.what ();
414
-
415
425
m_request._reply_if_not_already (status_codes::InternalError);
416
426
}
417
427
catch (...)
418
428
{
419
429
pListenerLock->unlock ();
420
-
421
430
m_request._reply_if_not_already (status_codes::InternalError);
422
431
}
423
432
}
424
433
425
- if (--m_refs == 0 )
426
- delete this ;
434
+ if (--m_refs == 0 ) delete this ;
427
435
}
428
436
429
437
void connection::request_data_avail (size_t size)
@@ -446,13 +454,11 @@ void connection::do_response(bool bad_request)
446
454
}
447
455
catch (const std::exception& ex)
448
456
{
449
- std::ostringstream str_stream;
450
- str_stream << " Error in listener: " << ex.what () << std::endl;
451
457
response = http::http_response (status_codes::InternalError);
452
458
}
453
459
catch (...)
454
460
{
455
- response = http::http_response (status_codes::InternalError);
461
+ response = http::http_response (status_codes::InternalError);
456
462
}
457
463
458
464
// before sending response, the full incoming message need to be processed.
@@ -536,7 +542,8 @@ void connection::handle_write_chunked_response(http_response response, const boo
536
542
readbuf.getn (buffer_cast<uint8_t *>(membuf) + http::details::chunked_encoding::data_offset, ChunkSize).then ([=](pplx::task<size_t > actualSizeTask)
537
543
{
538
544
size_t actualSize = 0 ;
539
- try {
545
+ try
546
+ {
540
547
actualSize = actualSizeTask.get ();
541
548
} catch (...) {
542
549
return cancel_sending_response_with_error (response, std::current_exception ());
@@ -553,7 +560,6 @@ void connection::handle_write_chunked_response(http_response response, const boo
553
560
});
554
561
}
555
562
556
-
557
563
void connection::handle_write_large_response (http_response response, const boost::system::error_code& ec)
558
564
{
559
565
if (ec || m_write == m_write_size)
@@ -566,7 +572,8 @@ void connection::handle_write_large_response(http_response response, const boost
566
572
readbuf.getn (buffer_cast<uint8_t *>(m_response_buf.prepare (readBytes)), readBytes).then ([=](pplx::task<size_t > actualSizeTask)
567
573
{
568
574
size_t actualSize = 0 ;
569
- try {
575
+ try
576
+ {
570
577
actualSize = actualSizeTask.get ();
571
578
} catch (...) {
572
579
return cancel_sending_response_with_error (response, std::current_exception ());
0 commit comments