@@ -521,7 +521,7 @@ class winhttp_client : public _http_client_communicator
521
521
// If credentials are specified, use autologon policy: WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH
522
522
// => default credentials are not used.
523
523
// Else, the default autologon policy WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM will be used.
524
- if ( !client_config ().credentials ().username (). empty () )
524
+ if (!client_config ().credentials ().is_set () )
525
525
{
526
526
DWORD data = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH;
527
527
@@ -674,17 +674,6 @@ class winhttp_client : public _http_client_communicator
674
674
}
675
675
}
676
676
677
- static bool has_credentials (winhttp_request_context * p_request_context)
678
- {
679
- auto has_proxy_credentials = !p_request_context->m_http_client ->client_config ().proxy ().credentials ().username ().empty ()
680
- && !p_request_context->m_http_client ->client_config ().proxy ().credentials ().password ().empty ();
681
-
682
- auto has_server_credentials = !p_request_context->m_http_client ->client_config ().credentials ().username ().empty ()
683
- && !p_request_context->m_http_client ->client_config ().credentials ().password ().empty ();
684
-
685
- return has_proxy_credentials || has_server_credentials;
686
- }
687
-
688
677
// Helper function to query/read next part of response data from winhttp.
689
678
static void read_next_response_chunk (winhttp_request_context *pContext, DWORD bytesRead, bool firstRead=false )
690
679
{
@@ -865,7 +854,7 @@ class winhttp_client : public _http_client_communicator
865
854
}
866
855
}
867
856
868
- // Returns true if we handle successfuly and resending the request
857
+ // Returns true if we handle successfully and resending the request
869
858
// or false if we fail to handle.
870
859
static bool handle_authentication_failure (
871
860
HINTERNET hRequestHandle,
@@ -878,22 +867,12 @@ class winhttp_client : public _http_client_communicator
878
867
_ASSERTE (response.status_code () == status_codes::Unauthorized || response.status_code () == status_codes::ProxyAuthRequired
879
868
|| error == ERROR_WINHTTP_RESEND_REQUEST);
880
869
881
- bool got_credentials = false ;
882
- BOOL results;
883
- DWORD dwSupportedSchemes;
884
- DWORD dwFirstScheme;
885
- DWORD dwTarget = 0 ;
886
- DWORD dwSelectedScheme = 0 ;
887
- string_t username;
888
- string_t password;
889
-
890
870
// Check if the saved read position is valid
891
871
auto rdpos = p_request_context->m_startingPosition ;
892
872
if (rdpos != static_cast <std::char_traits<uint8_t >::pos_type>(std::char_traits<uint8_t >::eof ()))
893
873
{
894
- auto rbuf = p_request_context->_get_readbuffer ();
895
-
896
874
// Try to seek back to the saved read position
875
+ auto rbuf = p_request_context->_get_readbuffer ();
897
876
if (rbuf.seekpos (rdpos, std::ios::ios_base::in) != rdpos)
898
877
{
899
878
return false ;
@@ -904,66 +883,54 @@ class winhttp_client : public _http_client_communicator
904
883
// we cannot call WinHttpQueryAuthSchemes and WinHttpSetCredentials.
905
884
if (error != ERROR_WINHTTP_RESEND_REQUEST)
906
885
{
907
- // The proxy requires authentication. Sending credentials...
908
- // Obtain the supported and preferred schemes.
909
- results = WinHttpQueryAuthSchemes ( hRequestHandle,
910
- &dwSupportedSchemes,
911
- &dwFirstScheme,
912
- &dwTarget );
886
+ DWORD dwSupportedSchemes;
887
+ DWORD dwFirstScheme;
888
+ DWORD dwSelectedScheme = 0 ;
889
+ DWORD dwAuthTarget;
890
+ credentials cred;
913
891
914
- if (!results)
892
+ // Obtain the supported and preferred schemes.
893
+ if (!WinHttpQueryAuthSchemes (
894
+ hRequestHandle,
895
+ &dwSupportedSchemes,
896
+ &dwFirstScheme,
897
+ &dwAuthTarget))
915
898
{
916
899
// This will return the authentication failure to the user, without reporting fatal errors
917
900
return false ;
918
901
}
919
902
920
- dwSelectedScheme = ChooseAuthScheme ( dwSupportedSchemes);
921
- if ( dwSelectedScheme == 0 )
903
+ dwSelectedScheme = ChooseAuthScheme (dwSupportedSchemes);
904
+ if (dwSelectedScheme == 0 )
922
905
{
923
906
// This will return the authentication failure to the user, without reporting fatal errors
924
907
return false ;
925
908
}
926
909
927
- if (response. status_code () == status_codes::ProxyAuthRequired /* 407 */ && !p_request_context->m_proxy_authentication_tried )
910
+ if (dwAuthTarget == WINHTTP_AUTH_TARGET_SERVER && !p_request_context->m_server_authentication_tried )
928
911
{
929
- // See if the credentials on the proxy were set. If not, there are no credentials to supply hence we cannot resend
930
- web_proxy proxy = p_request_context->m_http_client ->client_config ().proxy ();
931
- // No need to check if proxy is disabled, because disabled proxies cannot have credentials set on them
932
- credentials cred = proxy.credentials ();
933
- if (cred.is_set ())
934
- {
935
- username = cred.username ();
936
- password = cred.password ();
937
- dwTarget = WINHTTP_AUTH_TARGET_PROXY;
938
- got_credentials = !username.empty ();
939
- p_request_context->m_proxy_authentication_tried = true ;
940
- }
912
+ cred = p_request_context->m_http_client ->client_config ().credentials ();
913
+ p_request_context->m_server_authentication_tried = true ;
941
914
}
942
- else if (response. status_code () == status_codes::Unauthorized /* 401 */ && !p_request_context->m_server_authentication_tried )
915
+ else if (dwAuthTarget == WINHTTP_AUTH_TARGET_PROXY && !p_request_context->m_proxy_authentication_tried )
943
916
{
944
- username = p_request_context->m_http_client ->client_config ().credentials ().username ();
945
- password = p_request_context->m_http_client ->client_config ().credentials ().password ();
946
- dwTarget = WINHTTP_AUTH_TARGET_SERVER;
947
- got_credentials = !username.empty ();
948
- p_request_context->m_server_authentication_tried = true ;
917
+ cred = p_request_context->m_http_client ->client_config ().proxy ().credentials ();
918
+ p_request_context->m_proxy_authentication_tried = true ;
949
919
}
950
920
951
- if (!got_credentials)
921
+ // No credentials found so can't resend.
922
+ if (!cred.is_set ())
952
923
{
953
- // Either we cannot resend, or the user did not provide non-empty credentials.
954
- // Return the authentication failure to the user.
955
924
return false ;
956
925
}
957
-
958
- results = WinHttpSetCredentials ( hRequestHandle,
959
- dwTarget,
926
+ if (! WinHttpSetCredentials (
927
+ hRequestHandle,
928
+ dwAuthTarget,
960
929
dwSelectedScheme,
961
- username.c_str (),
962
- password.c_str (),
963
- nullptr );
964
- if (!results)
930
+ cred.username ().c_str (),
931
+ cred.password ().c_str (),
932
+ nullptr ))
965
933
{
966
- // This will return the authentication failure to the user, without reporting fatal errors
967
934
return false ;
968
935
}
969
936
}
0 commit comments