diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 567a98ebfb9..1b7cf3bfa9b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -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; @@ -350,13 +351,20 @@ public virtual string CustomMethod } private string _custommethod; - + /// /// Gets or sets the PreserveHttpMethodOnRedirect property. /// [Parameter] public virtual SwitchParameter PreserveHttpMethodOnRedirect { get; set; } + /// + /// Gets or sets the UnixSocket property. + /// + [Parameter] + [ValidateNotNullOrEmpty] + public virtual UnixDomainSocketEndPoint UnixSocket { get; set; } + #endregion Method #region NoProxy @@ -946,7 +954,20 @@ 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; @@ -954,7 +975,7 @@ internal virtual HttpClient GetHttpClient(bool handleRedirect) if (WebSession.UseDefaultCredentials) { // The UseDefaultCredentials flag overrides other supplied credentials - handler.UseDefaultCredentials = true; + handler.Credentials = CredentialCache.DefaultCredentials; } else if (WebSession.Credentials is not null) { @@ -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. @@ -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;