@@ -39,7 +39,6 @@ internal partial class CurlHandler : HttpMessageHandler
39
39
private const string EncodingNameDeflate = "deflate" ;
40
40
private readonly static string [ ] AuthenticationSchemes = { "Negotiate" , "Digest" , "Basic" } ; // the order in which libcurl goes over authentication schemes
41
41
private readonly static ulong [ ] AuthSchemePriorityOrder = { CURLAUTH . Negotiate , CURLAUTH . Digest , CURLAUTH . Basic } ;
42
- private static readonly string [ ] s_headerDelimiters = new string [ ] { "\r \n " } ;
43
42
44
43
private const int s_requestBufferSize = 16384 ; // Default used by libcurl
45
44
private const string NoTransferEncoding = HttpKnownHeaderNames . TransferEncoding + ":" ;
@@ -837,29 +836,35 @@ private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequest
837
836
contentHeaders = request . Content . Headers ;
838
837
}
839
838
840
- string [ ] allHeaders = HeaderUtilities . DumpHeaders ( request . Headers , contentHeaders )
841
- . Split ( s_headerDelimiters , StringSplitOptions . RemoveEmptyEntries ) ;
842
839
bool gotReference = false ;
843
840
try
844
841
{
845
842
retVal . DangerousAddRef ( ref gotReference ) ;
846
843
IntPtr rawHandle = IntPtr . Zero ;
847
- for ( int i = 0 ; i < allHeaders . Length ; i ++ )
844
+
845
+ if ( request . Headers != null )
848
846
{
849
- string header = allHeaders [ i ] . Trim ( ) ;
850
- if ( header . Equals ( "{" ) || header . Equals ( "}" ) )
851
- {
852
- continue ;
853
- }
854
- rawHandle = Interop . libcurl . curl_slist_append ( rawHandle , header ) ;
855
- retVal . SetHandle ( rawHandle ) ;
847
+ // Add request headers
848
+ AddRequestHeaders ( request . Headers , retVal , ref rawHandle ) ;
849
+ }
850
+
851
+ if ( contentHeaders != null )
852
+ {
853
+ // Add content request headers
854
+ AddRequestHeaders ( contentHeaders , retVal , ref rawHandle ) ;
856
855
}
857
856
858
857
// Since libcurl always adds a Transfer-Encoding header, we need to explicitly block
859
858
// it if caller specifically does not want to set the header
860
859
if ( request . Headers . TransferEncodingChunked . HasValue && ! request . Headers . TransferEncodingChunked . Value )
861
860
{
862
861
rawHandle = Interop . libcurl . curl_slist_append ( rawHandle , NoTransferEncoding ) ;
862
+
863
+ if ( rawHandle == null )
864
+ {
865
+ throw new HttpRequestException ( SR . net_http_client_execution_error ) ;
866
+ }
867
+
863
868
retVal . SetHandle ( rawHandle ) ;
864
869
}
865
870
@@ -879,6 +884,28 @@ private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequest
879
884
return retVal ;
880
885
}
881
886
887
+ /// <summary>
888
+ /// Add request headers to curl API
889
+ /// </summary>
890
+ /// <param name="headers"></param>
891
+ /// <param name="handle"></param>
892
+ /// <param name="rawHandle"></param>
893
+ private static void AddRequestHeaders ( HttpHeaders headers , SafeCurlSlistHandle handle , ref IntPtr rawHandle )
894
+ {
895
+ foreach ( KeyValuePair < string , IEnumerable < string > > header in headers )
896
+ {
897
+ string headerStr = header . Key + ": " + headers . GetHeaderString ( header . Key ) ;
898
+ rawHandle = Interop . libcurl . curl_slist_append ( rawHandle , headerStr ) ;
899
+
900
+ if ( rawHandle == null )
901
+ {
902
+ throw new HttpRequestException ( SR . net_http_client_execution_error ) ;
903
+ }
904
+
905
+ handle . SetHandle ( rawHandle ) ;
906
+ }
907
+ }
908
+
882
909
private static void SetChunkedModeForSend ( HttpRequestMessage request )
883
910
{
884
911
bool chunkedMode = request . Headers . TransferEncodingChunked . GetValueOrDefault ( ) ;
0 commit comments