Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Security;
using System.Security.Authentication;
using System.Security.Cryptography;
Expand Down Expand Up @@ -350,13 +351,20 @@ public virtual string CustomMethod
}

private string _custommethod;

/// <summary>
/// Gets or sets the PreserveHttpMethodOnRedirect property.
/// </summary>
[Parameter]
public virtual SwitchParameter PreserveHttpMethodOnRedirect { get; set; }

/// <summary>
/// Gets or sets the UnixSocket property.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public virtual UnixDomainSocketEndPoint UnixSocket { get; set; }

#endregion Method

#region NoProxy
Expand Down Expand Up @@ -946,15 +954,28 @@ internal virtual void PrepareSession()

internal virtual HttpClient GetHttpClient(bool handleRedirect)
{
HttpClientHandler handler = new();
SocketsHttpHandler handler = new();

if (UnixSocket is not null)
{
handler.ConnectCallback = async (context, token) =>
{
Socket socket = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
UnixDomainSocketEndPoint endpoint = UnixSocket;
await socket.ConnectAsync(endpoint).ConfigureAwait(false);

return new NetworkStream(socket, ownsSocket: false);
};
}

handler.CookieContainer = WebSession.Cookies;
handler.AutomaticDecompression = DecompressionMethods.All;

// Set the credentials used by this request
if (WebSession.UseDefaultCredentials)
{
// The UseDefaultCredentials flag overrides other supplied credentials
handler.UseDefaultCredentials = true;
handler.Credentials = CredentialCache.DefaultCredentials;
}
else if (WebSession.Credentials is not null)
{
Expand All @@ -972,13 +993,12 @@ internal virtual HttpClient GetHttpClient(bool handleRedirect)

if (WebSession.Certificates is not null)
{
handler.ClientCertificates.AddRange(WebSession.Certificates);
handler.SslOptions.ClientCertificates = new X509CertificateCollection(WebSession.Certificates);
}

if (SkipCertificateCheck)
{
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslOptions.RemoteCertificateValidationCallback = delegate { return true; };
}

// This indicates GetResponse will handle redirects.
Expand All @@ -991,11 +1011,11 @@ internal virtual HttpClient GetHttpClient(bool handleRedirect)
handler.MaxAutomaticRedirections = WebSession.MaximumRedirection;
}

handler.SslProtocols = (SslProtocols)SslProtocol;
handler.SslOptions.EnabledSslProtocols = (SslProtocols)SslProtocol;

HttpClient httpClient = new(handler);

// Check timeout setting (in seconds instead of milliseconds as in HttpWebRequest)
// Check timeout setting (in seconds)
httpClient.Timeout = TimeoutSec is 0 ? TimeSpan.FromMilliseconds(Timeout.Infinite) : new TimeSpan(0, 0, TimeoutSec);

return httpClient;
Expand Down