@@ -89,7 +89,7 @@ static utility::string_t parse_reason_phrase(HINTERNET request_handle)
89
89
}
90
90
91
91
// / <summary>
92
- // / Parses a string containing Http headers.
92
+ // / Parses a string containing HTTP headers.
93
93
// / </summary>
94
94
static void parse_winhttp_headers (HINTERNET request_handle, _In_z_ utf16char *headersStr, http_response &response)
95
95
{
@@ -103,34 +103,35 @@ static void parse_winhttp_headers(HINTERNET request_handle, _In_z_ utf16char *he
103
103
parse_headers_string (headersStr, response.headers ());
104
104
}
105
105
106
+ // Helper function to build error messages.
107
+ static std::string build_error_msg (unsigned long code, const std::string &location)
108
+ {
109
+ std::string msg (location);
110
+ msg.append (" : " );
111
+ msg.append (std::to_string (code));
112
+ msg.append (" : " );
113
+ msg.append (utility::details::windows_category ().message (code));
114
+ return msg;
115
+ }
116
+
106
117
// Helper function to build an error message from a WinHTTP async result.
107
- static std::string build_callback_error_msg (_In_ WINHTTP_ASYNC_RESULT *error_result)
118
+ static std::string build_error_msg (_In_ WINHTTP_ASYNC_RESULT *error_result)
108
119
{
109
- std::stringstream error_msg;
110
120
switch (error_result->dwResult )
111
121
{
112
122
case API_RECEIVE_RESPONSE:
113
- error_msg << " WinHttpReceiveResponse" ;
114
- break ;
123
+ return build_error_msg (error_result->dwError , " WinHttpReceiveResponse" );
115
124
case API_QUERY_DATA_AVAILABLE:
116
- error_msg << " WinHttpQueryDataAvaliable" ;
117
- break ;
125
+ return build_error_msg (error_result->dwError , " WinHttpQueryDataAvaliable" );
118
126
case API_READ_DATA:
119
- error_msg << " WinHttpReadData" ;
120
- break ;
127
+ return build_error_msg (error_result->dwError , " WinHttpReadData" );
121
128
case API_WRITE_DATA:
122
- error_msg << " WinHttpWriteData" ;
123
- break ;
129
+ return build_error_msg (error_result->dwError , " WinHttpWriteData" );
124
130
case API_SEND_REQUEST:
125
- error_msg << " WinHttpSendRequest" ;
126
- break ;
131
+ return build_error_msg (error_result->dwError , " WinHttpSendRequest" );
127
132
default :
128
- error_msg << " Unknown WinHTTP Function" ;
129
- break ;
133
+ return build_error_msg (error_result->dwError , " Unknown WinHTTP Function" );
130
134
}
131
- error_msg << " : " << error_result->dwError << " : "
132
- << utility::details::windows_category ().message (error_result->dwError );
133
- return error_msg.str ();
134
135
}
135
136
136
137
class memory_holder
@@ -416,7 +417,7 @@ class winhttp_client : public _http_client_communicator
416
417
}
417
418
}
418
419
419
- #if 0 // Work in progress. Enable this to support server certrificate revocation check
420
+ #if 0 // Work in progress. Enable this to support server certificate revocation check
420
421
if( m_secure )
421
422
{
422
423
DWORD dwEnableSSLRevocOpt = WINHTTP_ENABLE_SSL_REVOCATION;
@@ -502,11 +503,12 @@ class winhttp_client : public _http_client_communicator
502
503
WINHTTP_FLAG_ESCAPE_DISABLE | (m_secure ? WINHTTP_FLAG_SECURE : 0 ));
503
504
if (winhttp_context->m_request_handle == nullptr )
504
505
{
505
- request->report_error (GetLastError (), _XPLATSTR (" Error opening request" ));
506
+ auto errorCode = GetLastError ();
507
+ request->report_error (errorCode, build_error_msg (errorCode, " WinHttpOpenRequest" ));
506
508
return ;
507
509
}
508
510
509
- if ( proxy_info_required )
511
+ if (proxy_info_required)
510
512
{
511
513
auto result = WinHttpSetOption (
512
514
winhttp_context->m_request_handle ,
@@ -515,7 +517,8 @@ class winhttp_client : public _http_client_communicator
515
517
sizeof (WINHTTP_PROXY_INFO) );
516
518
if (!result)
517
519
{
518
- request->report_error (GetLastError (), _XPLATSTR (" Error setting http proxy option" ));
520
+ auto errorCode = GetLastError ();
521
+ request->report_error (errorCode, build_error_msg (errorCode, " Setting proxy options" ));
519
522
return ;
520
523
}
521
524
}
@@ -534,7 +537,8 @@ class winhttp_client : public _http_client_communicator
534
537
sizeof (data));
535
538
if (!result)
536
539
{
537
- request->report_error (GetLastError (), _XPLATSTR (" Error setting autologon policy to WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH." ));
540
+ auto errorCode = GetLastError ();
541
+ request->report_error (errorCode, build_error_msg (errorCode, " Setting autologon policy to WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH" ));
538
542
return ;
539
543
}
540
544
}
@@ -554,7 +558,8 @@ class winhttp_client : public _http_client_communicator
554
558
sizeof (data));
555
559
if (!result)
556
560
{
557
- request->report_error (GetLastError (), U (" Error setting WinHttp to ignore server certification validation errors." ));
561
+ auto errorCode = GetLastError ();
562
+ request->report_error (errorCode, build_error_msg (errorCode, " Setting ignore server certificate verification" ));
558
563
return ;
559
564
}
560
565
}
@@ -572,7 +577,7 @@ class winhttp_client : public _http_client_communicator
572
577
if (content_length == std::numeric_limits<size_t >::max ())
573
578
{
574
579
// The content length is unknown and the application set a stream. This is an
575
- // indication that we will use tranfer encoding chunked.
580
+ // indication that we will use transfer encoding chunked.
576
581
winhttp_context->m_bodyType = transfer_encoding_chunked;
577
582
}
578
583
else
@@ -593,7 +598,8 @@ class winhttp_client : public _http_client_communicator
593
598
static_cast <DWORD>(flattened_headers.length ()),
594
599
WINHTTP_ADDREQ_FLAG_ADD))
595
600
{
596
- request->report_error (GetLastError (), _XPLATSTR (" Error adding request headers" ));
601
+ auto errorCode = GetLastError ();
602
+ request->report_error (errorCode, build_error_msg (errorCode, " WinHttpAddRequestHeaders" ));
597
603
return ;
598
604
}
599
605
}
@@ -647,7 +653,8 @@ class winhttp_client : public _http_client_communicator
647
653
0 ,
648
654
(DWORD_PTR)winhttp_context))
649
655
{
650
- winhttp_context->report_error (GetLastError (), _XPLATSTR (" Error starting to send request" ));
656
+ auto errorCode = GetLastError ();
657
+ winhttp_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpSendRequest" ));
651
658
}
652
659
653
660
return ;
@@ -671,7 +678,8 @@ class winhttp_client : public _http_client_communicator
671
678
winhttp_context->m_bodyType == content_length_chunked ? (DWORD)content_length : WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH,
672
679
(DWORD_PTR)winhttp_context))
673
680
{
674
- winhttp_context->report_error (GetLastError (), _XPLATSTR (" Error starting to send chunked request" ));
681
+ auto errorCode = GetLastError ();
682
+ winhttp_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpSendRequest chunked" ));
675
683
}
676
684
}
677
685
@@ -685,12 +693,13 @@ class winhttp_client : public _http_client_communicator
685
693
{
686
694
if (!WinHttpQueryDataAvailable (pContext->m_request_handle , nullptr ))
687
695
{
688
- pContext->report_error (GetLastError (), _XPLATSTR (" Error querying for http body data" ));
696
+ auto errorCode = GetLastError ();
697
+ pContext->report_error (errorCode, build_error_msg (errorCode, " WinHttpQueryDataAvaliable" ));
689
698
}
690
699
}
691
700
else
692
701
{
693
- // If bytes read is less than the chunksize this request is done.
702
+ // If bytes read is less than the chunk size this request is done.
694
703
const size_t chunkSize = pContext->m_http_client ->client_config ().chunksize ();
695
704
if (bytesRead < chunkSize && !firstRead)
696
705
{
@@ -707,7 +716,8 @@ class winhttp_client : public _http_client_communicator
707
716
static_cast <DWORD>(chunkSize),
708
717
nullptr ))
709
718
{
710
- pContext->report_error (GetLastError (), _XPLATSTR (" Error receiving http response body chunk" ));
719
+ auto errorCode = GetLastError ();
720
+ pContext->report_error (errorCode, build_error_msg (errorCode, " WinHttpReadData" ));
711
721
}
712
722
}
713
723
}
@@ -763,7 +773,8 @@ class winhttp_client : public _http_client_communicator
763
773
static_cast <DWORD>(length),
764
774
nullptr ))
765
775
{
766
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error writing data" ));
776
+ auto errorCode = GetLastError ();
777
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpWriteData" ));
767
778
}
768
779
};
769
780
@@ -776,17 +787,21 @@ class winhttp_client : public _http_client_communicator
776
787
SafeInt<size64_t > safeCount = p_request_context->m_remaining_to_write ;
777
788
safeCount = safeCount.Min (p_request_context->m_http_client ->client_config ().chunksize ());
778
789
779
- uint8_t * block = nullptr ;
790
+ uint8_t * block = nullptr ;
780
791
size_t length = 0 ;
781
- if ( rbuf.acquire (block, length) )
792
+ if (rbuf.acquire (block, length))
782
793
{
783
- if ( length == 0 )
794
+ if (length == 0 )
784
795
{
785
796
// Unexpected end-of-stream.
786
- if (!(rbuf.exception () == nullptr ))
787
- p_request_context->report_exception (rbuf.exception ());
788
- else
797
+ if (rbuf.exception () == nullptr )
798
+ {
789
799
p_request_context->report_error (GetLastError (), _XPLATSTR (" Error reading outgoing HTTP body from its stream." ));
800
+ }
801
+ else
802
+ {
803
+ p_request_context->report_exception (rbuf.exception ());
804
+ }
790
805
return ;
791
806
}
792
807
@@ -807,7 +822,8 @@ class winhttp_client : public _http_client_communicator
807
822
static_cast <DWORD>(to_write),
808
823
nullptr ))
809
824
{
810
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error writing data" ));
825
+ auto errorCode = GetLastError ();
826
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpWriteData" ));
811
827
}
812
828
}
813
829
else
@@ -839,13 +855,14 @@ class winhttp_client : public _http_client_communicator
839
855
p_request_context->m_bodyType = no_body;
840
856
}
841
857
842
- if ( !WinHttpWriteData (
858
+ if (!WinHttpWriteData (
843
859
p_request_context->m_request_handle ,
844
860
p_request_context->m_body_data .get (),
845
861
static_cast <DWORD>(read),
846
862
nullptr ))
847
863
{
848
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error writing data" ));
864
+ auto errorCode = GetLastError ();
865
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpWriteData" ));
849
866
}
850
867
});
851
868
}
@@ -1003,7 +1020,7 @@ class winhttp_client : public _http_client_communicator
1003
1020
}
1004
1021
}
1005
1022
1006
- p_request_context->report_error (errorCode, utility::conversions::to_string_t ( build_callback_error_msg ( error_result) ));
1023
+ p_request_context->report_error (errorCode, build_error_msg ( error_result));
1007
1024
break ;
1008
1025
}
1009
1026
case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE :
@@ -1034,7 +1051,8 @@ class winhttp_client : public _http_client_communicator
1034
1051
{
1035
1052
if (!WinHttpReceiveResponse (hRequestHandle, nullptr ))
1036
1053
{
1037
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error receiving response" ));
1054
+ auto errorCode = GetLastError ();
1055
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpReceiveResponse" ));
1038
1056
}
1039
1057
}
1040
1058
break ;
@@ -1075,7 +1093,8 @@ class winhttp_client : public _http_client_communicator
1075
1093
{
1076
1094
if (!WinHttpReceiveResponse (hRequestHandle, nullptr ))
1077
1095
{
1078
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error receiving response" ));
1096
+ auto errorCode = GetLastError ();
1097
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpReceiveResponse" ));
1079
1098
}
1080
1099
}
1081
1100
break ;
@@ -1098,7 +1117,8 @@ class winhttp_client : public _http_client_communicator
1098
1117
&headerBufferLength,
1099
1118
WINHTTP_NO_HEADER_INDEX))
1100
1119
{
1101
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error receiving http headers" ));
1120
+ auto errorCode = GetLastError ();
1121
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpQueryHeaders" ));;
1102
1122
return ;
1103
1123
}
1104
1124
@@ -1154,7 +1174,8 @@ class winhttp_client : public _http_client_communicator
1154
1174
num_bytes,
1155
1175
nullptr ))
1156
1176
{
1157
- p_request_context->report_error (GetLastError (), _XPLATSTR (" Error receiving http body chunk" ));
1177
+ auto errorCode = GetLastError ();
1178
+ p_request_context->report_error (errorCode, build_error_msg (errorCode, " WinHttpReadData" ));
1158
1179
}
1159
1180
}
1160
1181
else
@@ -1265,4 +1286,4 @@ pplx::task<http_response> http_network_handler::propagate(http_request request)
1265
1286
return result_task;
1266
1287
}
1267
1288
1268
- }}}} // namespaces
1289
+ }}}}
0 commit comments