@@ -216,8 +216,6 @@ namespace web { namespace http
216
216
auto instream = m_request.body ();
217
217
218
218
_ASSERTE ((bool )instream);
219
- _ASSERTE (instream.is_open ());
220
-
221
219
return instream.streambuf ();
222
220
}
223
221
@@ -226,7 +224,6 @@ namespace web { namespace http
226
224
auto outstream = m_response._get_impl ()->outstream ();
227
225
228
226
_ASSERTE ((bool )outstream);
229
- _ASSERTE (outstream.is_open ());
230
227
231
228
return outstream.streambuf ();
232
229
}
@@ -515,13 +512,10 @@ namespace web { namespace http
515
512
516
513
return S_OK;
517
514
}
518
- catch (const http_exception &exc)
519
- {
520
- return (HRESULT)exc.error_code ().value ();
521
- }
522
- catch (const std::system_error &exc)
515
+ catch (...)
523
516
{
524
- return (HRESULT)exc.code ().value ();
517
+ m_request->m_exceptionPtr = std::current_exception ();
518
+ return (HRESULT)STG_E_CANTSAVE;
525
519
}
526
520
}
527
521
@@ -628,7 +622,11 @@ namespace web { namespace http
628
622
{
629
623
IXMLHTTPRequest2 * req = m_request->m_hRequest ;
630
624
631
- m_request->complete_request (m_request->m_stream_bridge ->total_bytes ());
625
+ if (m_request->m_exceptionPtr != nullptr )
626
+ m_request->report_exception (m_request->m_exceptionPtr );
627
+ else
628
+ m_request->complete_request (m_request->m_stream_bridge ->total_bytes ());
629
+
632
630
m_request = nullptr ;
633
631
634
632
if ( req != nullptr ) req->Release ();
@@ -1428,6 +1426,19 @@ namespace web { namespace http
1428
1426
1429
1427
private:
1430
1428
1429
+ static bool _check_streambuf (_In_ winhttp_request_context * winhttp_context, concurrency::streams::streambuf<uint8_t > rdbuf, const utility::char_t * msg)
1430
+ {
1431
+ if ( !rdbuf.is_open () )
1432
+ {
1433
+ auto eptr = rdbuf.exception ();
1434
+ if ( !(eptr == nullptr ) )
1435
+ winhttp_context->report_exception (eptr);
1436
+ else
1437
+ winhttp_context->report_error (msg);
1438
+ }
1439
+ return rdbuf.is_open ();
1440
+ }
1441
+
1431
1442
void _start_request_send (_In_ winhttp_request_context * winhttp_context, size_t content_length)
1432
1443
{
1433
1444
if ( !winhttp_context->m_need_to_chunk && !winhttp_context->m_too_large )
@@ -1446,13 +1457,18 @@ namespace web { namespace http
1446
1457
winhttp_context->report_error (U (" Error starting to send request" ));
1447
1458
}
1448
1459
1449
- winhttp_context->m_readbuf_pos = 0 ;
1450
1460
return ;
1451
1461
}
1452
1462
1453
1463
// Capure the current read position of the stream.
1454
1464
1455
- winhttp_context->m_readbuf_pos = winhttp_context->_get_readbuffer ().seekoff (0 , std::ios_base::cur, std::ios_base::in);
1465
+ auto rbuf = winhttp_context->_get_readbuffer ();
1466
+ if ( !_check_streambuf (winhttp_context, rbuf, U (" Input stream is not open" )) )
1467
+ {
1468
+ return ;
1469
+ }
1470
+
1471
+ winhttp_context->m_readbuf_pos = rbuf.getpos (std::ios_base::in);
1456
1472
1457
1473
// If we find ourselves here, we either don't know how large the message
1458
1474
// body is, or it is larger than our threshold.
@@ -1479,6 +1495,10 @@ namespace web { namespace http
1479
1495
p_request_context->allocate_request_space (nullptr , chunk_size+http::details::chunked_encoding::additional_encoding_space);
1480
1496
1481
1497
auto rbuf = p_request_context->_get_readbuffer ();
1498
+ if ( !_check_streambuf (p_request_context, rbuf, U (" Input stream is not open" )) )
1499
+ {
1500
+ return ;
1501
+ }
1482
1502
1483
1503
rbuf.getn (&p_request_context->request_data .get ()[http::details::chunked_encoding::data_offset], chunk_size).then (
1484
1504
[p_request_context, chunk_size](pplx::task<size_t > op)
@@ -1514,6 +1534,10 @@ namespace web { namespace http
1514
1534
static void _multiple_segment_write_data (_In_ winhttp_request_context * p_request_context)
1515
1535
{
1516
1536
auto rbuf = p_request_context->_get_readbuffer ();
1537
+ if ( !_check_streambuf (p_request_context, rbuf, U (" Input stream is not open" )) )
1538
+ {
1539
+ return ;
1540
+ }
1517
1541
1518
1542
msl::utilities::SafeInt<size_t > safeCount = p_request_context->m_remaining_to_write ;
1519
1543
@@ -1567,8 +1591,7 @@ namespace web { namespace http
1567
1591
try { read = op.get (); } catch (...)
1568
1592
{
1569
1593
p_request_context->report_exception (std::current_exception ());
1570
- read = 0 ;
1571
- p_request_context->m_remaining_to_write = 0 ;
1594
+ return ;
1572
1595
}
1573
1596
1574
1597
_ASSERTE (read != static_cast <size_t >(-1 ));
@@ -1626,6 +1649,10 @@ namespace web { namespace http
1626
1649
if (rdpos != (std::char_traits<uint8_t >::pos_type)-1 )
1627
1650
{
1628
1651
auto rbuf = p_request_context->_get_readbuffer ();
1652
+ if ( !rbuf.is_open () )
1653
+ {
1654
+ can_resend = false ;
1655
+ }
1629
1656
1630
1657
// Try to seek back to the saved read position
1631
1658
if ( rbuf.seekpos (rdpos, std::ios::ios_base::in) == rdpos )
@@ -1892,6 +1919,8 @@ namespace web { namespace http
1892
1919
_ASSERTE (content_length > 0 );
1893
1920
1894
1921
concurrency::streams::streambuf<uint8_t > writebuf = p_request_context->_get_writebuffer ();
1922
+ if ( !_check_streambuf (p_request_context, writebuf, U (" Output stream is not open" )) )
1923
+ break ;
1895
1924
1896
1925
p_request_context->allocate_reply_space (writebuf.alloc (content_length), content_length);
1897
1926
@@ -1915,6 +1944,9 @@ namespace web { namespace http
1915
1944
if (num_bytes > 0 )
1916
1945
{
1917
1946
auto writebuf = p_request_context->_get_writebuffer ();
1947
+ if ( !_check_streambuf (p_request_context, writebuf, U (" Output stream is not open" )) )
1948
+ break ;
1949
+
1918
1950
p_request_context->allocate_reply_space (writebuf.alloc (num_bytes), num_bytes);
1919
1951
1920
1952
// Read in body all at once.
@@ -1941,6 +1973,8 @@ namespace web { namespace http
1941
1973
if (statusInfoLength > 0 )
1942
1974
{
1943
1975
auto writebuf = p_request_context->_get_writebuffer ();
1976
+ if ( !_check_streambuf (p_request_context, writebuf, U (" Output stream is not open" )) )
1977
+ break ;
1944
1978
1945
1979
auto after_sync =
1946
1980
[hRequestHandle, p_request_context]
@@ -1956,6 +1990,7 @@ namespace web { namespace http
1956
1990
if ( !WinHttpQueryDataAvailable (hRequestHandle, nullptr ))
1957
1991
{
1958
1992
p_request_context->report_error (U (" Error querying for http body chunk" ));
1993
+ return ;
1959
1994
}
1960
1995
};
1961
1996
@@ -1989,6 +2024,8 @@ namespace web { namespace http
1989
2024
}
1990
2025
1991
2026
auto wbuf = p_request_context->_get_writebuffer ();
2027
+ if ( !_check_streambuf (p_request_context, wbuf, U (" Output stream is not open" )) )
2028
+ return ;
1992
2029
1993
2030
wbuf.sync ().then (after_sync);
1994
2031
});
0 commit comments