Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3b43fdf

Browse files
committed
Removed DumpHeaders dependency & coded to avoid unnecessary splitting
modified: System/Net/Http/Unix/CurlHandler.cs
1 parent 69ac65b commit 3b43fdf

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/System.Net.Http/src/System/Net/Http/Unix/CurlHandler.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ internal partial class CurlHandler : HttpMessageHandler
3939
private const string EncodingNameDeflate = "deflate";
4040
private readonly static string[] AuthenticationSchemes = { "Negotiate", "Digest", "Basic" }; // the order in which libcurl goes over authentication schemes
4141
private readonly static ulong[] AuthSchemePriorityOrder = { CURLAUTH.Negotiate, CURLAUTH.Digest, CURLAUTH.Basic };
42-
private static readonly string[] s_headerDelimiters = new string[] { "\r\n" };
4342

4443
private const int s_requestBufferSize = 16384; // Default used by libcurl
4544
private const string NoTransferEncoding = HttpKnownHeaderNames.TransferEncoding + ":";
@@ -837,29 +836,35 @@ private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequest
837836
contentHeaders = request.Content.Headers;
838837
}
839838

840-
string[] allHeaders = HeaderUtilities.DumpHeaders(request.Headers, contentHeaders)
841-
.Split(s_headerDelimiters, StringSplitOptions.RemoveEmptyEntries);
842839
bool gotReference = false;
843840
try
844841
{
845842
retVal.DangerousAddRef(ref gotReference);
846843
IntPtr rawHandle = IntPtr.Zero;
847-
for (int i = 0; i < allHeaders.Length; i++)
844+
845+
if (request.Headers != null)
848846
{
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);
856855
}
857856

858857
// Since libcurl always adds a Transfer-Encoding header, we need to explicitly block
859858
// it if caller specifically does not want to set the header
860859
if (request.Headers.TransferEncodingChunked.HasValue && !request.Headers.TransferEncodingChunked.Value)
861860
{
862861
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+
863868
retVal.SetHandle(rawHandle);
864869
}
865870

@@ -879,6 +884,28 @@ private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequest
879884
return retVal;
880885
}
881886

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+
882909
private static void SetChunkedModeForSend(HttpRequestMessage request)
883910
{
884911
bool chunkedMode = request.Headers.TransferEncodingChunked.GetValueOrDefault();

0 commit comments

Comments
 (0)