From e2fbf1e3219dc23f85abafc19f69b6bb8c0a370e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 00:55:14 +0100 Subject: [PATCH 01/25] Unix sockets --- .../Common/WebRequestPSCmdlet.Common.cs | 12 ++++++++- .../utility/WebCmdlet/WebRequestSession.cs | 26 ++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) 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 c1df22296e7..874ef55af1c 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; @@ -359,7 +360,7 @@ public virtual string CustomMethod set => _custommethod = value.ToUpperInvariant(); } - private string _custommethod; + internal static string _custommethod; /// /// Gets or sets the PreserveHttpMethodOnRedirect property. @@ -367,6 +368,15 @@ public virtual string CustomMethod [Parameter] public virtual SwitchParameter PreserveHttpMethodOnRedirect { get; set; } + /// + /// Gets or sets the UnixSocket property. + /// + [Parameter] + [ValidateNotNullOrEmpty] + public virtual UnixDomainSocketEndPoint UnixSocket { get => _unixSocket; set => _unixSocket = value; } + + internal static UnixDomainSocketEndPoint _unixSocket; + #endregion Method #region NoProxy diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs index 4e58503659e..0d429cf7a85 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using System.Net.Sockets; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.Threading; @@ -189,7 +190,19 @@ internal HttpClient GetHttpClient(bool suppressHttpClientRedirects, out bool cli private HttpClient CreateHttpClient() { - HttpClientHandler handler = new(); + SocketsHttpHandler handler = new(); + + if (WebRequestPSCmdlet._unixSocket is not null) + { + handler.ConnectCallback = async (context, token) => + { + Socket socket = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); + UnixDomainSocketEndPoint endpoint = WebRequestPSCmdlet._unixSocket; + await socket.ConnectAsync(endpoint).ConfigureAwait(false); + + return new NetworkStream(socket, ownsSocket: false); + }; + } handler.CookieContainer = Cookies; handler.AutomaticDecompression = DecompressionMethods.All; @@ -200,7 +213,7 @@ private HttpClient CreateHttpClient() } else { - handler.UseDefaultCredentials = UseDefaultCredentials; + handler.Credentials = CredentialCache.DefaultCredentials; } if (_noProxy) @@ -214,13 +227,12 @@ private HttpClient CreateHttpClient() if (Certificates is not null) { - handler.ClientCertificates.AddRange(Certificates); + handler.SslOptions.ClientCertificates = new X509CertificateCollection(Certificates); } if (_skipCertificateCheck) { - handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; - handler.ClientCertificateOptions = ClientCertificateOption.Manual; + handler.SslOptions.RemoteCertificateValidationCallback = delegate { return true; }; } handler.AllowAutoRedirect = _allowAutoRedirect; @@ -229,9 +241,9 @@ private HttpClient CreateHttpClient() handler.MaxAutomaticRedirections = MaximumRedirection; } - handler.SslProtocols = (SslProtocols)_sslProtocol; + handler.SslOptions.EnabledSslProtocols = (SslProtocols)_sslProtocol; - // Check timeout setting (in seconds instead of milliseconds as in HttpWebRequest) + // Check timeout setting (in seconds) return new HttpClient(handler) { Timeout = _timeoutSec is 0 ? TimeSpan.FromMilliseconds(Timeout.Infinite) : TimeSpan.FromSeconds(_timeoutSec) From 550707ec2a6bbc7b8f6ec5a3aef802a6eb03d867 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 01:00:46 +0100 Subject: [PATCH 02/25] rework _custommethod --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) 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 874ef55af1c..5e371fed800 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 @@ -353,14 +353,9 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable [Parameter(Mandatory = true, ParameterSetName = "CustomMethodNoProxy")] [Alias("CM")] [ValidateNotNullOrEmpty] - public virtual string CustomMethod - { - get => _custommethod; - - set => _custommethod = value.ToUpperInvariant(); - } + public virtual string CustomMethod { get => _custommethod; set => _custommethod = value.ToUpperInvariant(); } - internal static string _custommethod; + private string _custommethod; /// /// Gets or sets the PreserveHttpMethodOnRedirect property. From ec2fcf10b63cc8583ac86446fe1be5893c2dbf1b Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 10:47:22 +0100 Subject: [PATCH 03/25] use SetClassVar --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 9 ++++++--- .../commands/utility/WebCmdlet/WebRequestSession.cs | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) 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 5e371fed800..74ac048e5b6 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 @@ -368,9 +368,7 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable /// [Parameter] [ValidateNotNullOrEmpty] - public virtual UnixDomainSocketEndPoint UnixSocket { get => _unixSocket; set => _unixSocket = value; } - - internal static UnixDomainSocketEndPoint _unixSocket; + public virtual UnixDomainSocketEndPoint UnixSocket { get; set; } #endregion Method @@ -974,6 +972,11 @@ internal virtual void PrepareSession() WebSession.MaximumRedirection = MaximumRedirection; } + if (UnixSocket is not null) + { + WebSession.UnixSocket = UnixSocket; + } + WebSession.SkipCertificateCheck = SkipCertificateCheck.IsPresent; // Store the other supplied headers diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs index 0d429cf7a85..773421df11f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs @@ -30,6 +30,7 @@ public class WebRequestSession : IDisposable private bool _noProxy; private bool _disposed; private int _timeoutSec; + private UnixDomainSocketEndPoint _unixSocket; /// /// Contains true if an existing HttpClient had to be disposed and recreated since the WebSession was last used. @@ -139,6 +140,8 @@ public WebRequestSession() internal int TimeoutSec { set => SetStructVar(ref _timeoutSec, value); } + internal UnixDomainSocketEndPoint UnixSocket { set => SetClassVar(ref _unixSocket, value); } + internal bool NoProxy { set @@ -192,12 +195,12 @@ private HttpClient CreateHttpClient() { SocketsHttpHandler handler = new(); - if (WebRequestPSCmdlet._unixSocket is not null) + if (_unixSocket is not null) { handler.ConnectCallback = async (context, token) => { Socket socket = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); - UnixDomainSocketEndPoint endpoint = WebRequestPSCmdlet._unixSocket; + UnixDomainSocketEndPoint endpoint = _unixSocket; await socket.ConnectAsync(endpoint).ConfigureAwait(false); return new NetworkStream(socket, ownsSocket: false); From 84968e7fb1819e74d7a8af257a701d67fc4519ca Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 11:00:28 +0100 Subject: [PATCH 04/25] Pass UnixSocket to WebSession even if it's null --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 74ac048e5b6..f1d66ba523b 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 @@ -972,10 +972,7 @@ internal virtual void PrepareSession() WebSession.MaximumRedirection = MaximumRedirection; } - if (UnixSocket is not null) - { - WebSession.UnixSocket = UnixSocket; - } + WebSession.UnixSocket = UnixSocket; WebSession.SkipCertificateCheck = SkipCertificateCheck.IsPresent; From 30132a861e427c83143f799f527e21df9717739a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:09:16 +0100 Subject: [PATCH 05/25] UnixDomainSocketEndPoint nullable --- .../commands/utility/WebCmdlet/WebRequestSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs index ca3bfaea1a8..add282117f5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs @@ -34,7 +34,7 @@ public class WebRequestSession : IDisposable private bool _noProxy; private bool _disposed; private int _timeoutSec; - private UnixDomainSocketEndPoint _unixSocket; + private UnixDomainSocketEndPoint? _unixSocket; /// /// Contains true if an existing HttpClient had to be disposed and recreated since the WebSession was last used. From 43f2772e9bf4d80eb8b6cb619704b4622cee056a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:12:11 +0100 Subject: [PATCH 06/25] remove useless line --- .../commands/utility/WebCmdlet/WebRequestSession.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs index add282117f5..025193e7f60 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs @@ -206,8 +206,7 @@ private HttpClient CreateHttpClient() handler.ConnectCallback = async (context, token) => { Socket socket = new(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); - UnixDomainSocketEndPoint endpoint = _unixSocket; - await socket.ConnectAsync(endpoint).ConfigureAwait(false); + await socket.ConnectAsync(_unixSocket).ConfigureAwait(false); return new NetworkStream(socket, ownsSocket: false); }; From d9ec5c45fd2d2caea538af3679de453da0b7d752 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 02:08:22 +0200 Subject: [PATCH 07/25] Add Socket.OSSupportsUnixDomainSockets validation --- .../Common/WebRequestPSCmdlet.Common.cs | 23 ++++++++++++++++--- .../resources/WebCmdletStrings.resx | 3 +++ 2 files changed, 23 insertions(+), 3 deletions(-) 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 f1d66ba523b..6473ee03c78 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 @@ -353,9 +353,9 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable [Parameter(Mandatory = true, ParameterSetName = "CustomMethodNoProxy")] [Alias("CM")] [ValidateNotNullOrEmpty] - public virtual string CustomMethod { get => _custommethod; set => _custommethod = value.ToUpperInvariant(); } + public virtual string CustomMethod { get => _customMethod; set => _customMethod = value.ToUpperInvariant(); } - private string _custommethod; + private string _customMethod; /// /// Gets or sets the PreserveHttpMethodOnRedirect property. @@ -368,7 +368,24 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable /// [Parameter] [ValidateNotNullOrEmpty] - public virtual UnixDomainSocketEndPoint UnixSocket { get; set; } + public virtual UnixDomainSocketEndPoint UnixSocket + { + get => _unixSocket; + set + { + if (Socket.OSSupportsUnixDomainSockets is true) + { + _unixSocket = value; + } + else + { + ErrorRecord error = GetValidationError(WebCmdletStrings.UnixDomainSocketsNotSupported, "UnixDomainSocketsNotSupportedException"); + ThrowTerminatingError(error); + } + } + } + + private UnixDomainSocketEndPoint _unixSocket; #endregion Method diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index 5b1b33eae62..eddf5701ef6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -210,6 +210,9 @@ Unable to retrieve certificates because the thumbprint is not valid. Verify the thumbprint and retry. + + UnixDomainSockets are not supported on this platform. Reissue the command removing the -UnixSocket parameter. + Web request completed. (Number of bytes processed: {0}) From bd5c4d60614cfa524a2aa9b56c762c1e01801e00 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 02:16:45 +0200 Subject: [PATCH 08/25] move error to ValidateParameters() --- .../Common/WebRequestPSCmdlet.Common.cs | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) 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 6473ee03c78..6a995f318f9 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 @@ -368,24 +368,7 @@ public abstract class WebRequestPSCmdlet : PSCmdlet, IDisposable /// [Parameter] [ValidateNotNullOrEmpty] - public virtual UnixDomainSocketEndPoint UnixSocket - { - get => _unixSocket; - set - { - if (Socket.OSSupportsUnixDomainSockets is true) - { - _unixSocket = value; - } - else - { - ErrorRecord error = GetValidationError(WebCmdletStrings.UnixDomainSocketsNotSupported, "UnixDomainSocketsNotSupportedException"); - ThrowTerminatingError(error); - } - } - } - - private UnixDomainSocketEndPoint _unixSocket; + public virtual UnixDomainSocketEndPoint UnixSocket { get; set; } #endregion Method @@ -785,6 +768,13 @@ internal virtual void ValidateParameters() ErrorRecord error = GetValidationError(WebCmdletStrings.CredentialConflict, "WebCmdletCredentialConflictException"); ThrowTerminatingError(error); } + + // Method + if (UnixSocket is not null && Socket.OSSupportsUnixDomainSockets is false) + { + ErrorRecord error = GetValidationError(WebCmdletStrings.UnixDomainSocketsNotSupported, "UnixDomainSocketsNotSupportedException"); + ThrowTerminatingError(error); + } // Proxy server if (ProxyUseDefaultCredentials && ProxyCredential is not null) From f7ec7e84ca997ef62625f1b5c412481bd2100692 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:44:33 +0200 Subject: [PATCH 09/25] Update src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- .../resources/WebCmdletStrings.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index eddf5701ef6..b4da3fc0b25 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -210,7 +210,7 @@ Unable to retrieve certificates because the thumbprint is not valid. Verify the thumbprint and retry. - + UnixDomainSockets are not supported on this platform. Reissue the command removing the -UnixSocket parameter. From 2b9a2b901b403101be06f2ea5b92edfb9ce0df1d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:45:11 +0200 Subject: [PATCH 10/25] update error --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6a995f318f9..a467c0be36e 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 @@ -772,7 +772,7 @@ internal virtual void ValidateParameters() // Method if (UnixSocket is not null && Socket.OSSupportsUnixDomainSockets is false) { - ErrorRecord error = GetValidationError(WebCmdletStrings.UnixDomainSocketsNotSupported, "UnixDomainSocketsNotSupportedException"); + ErrorRecord error = GetValidationError(WebCmdletStrings.UnixSocketPlatformNotSupported, "UnixSocketPlatformNotSupportedException"); ThrowTerminatingError(error); } From e7090f3115db44b16db3ad616742d4cdb2b264e5 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 10:17:58 +0200 Subject: [PATCH 11/25] remove error --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 7 ------- .../resources/WebCmdletStrings.resx | 3 --- 2 files changed, 10 deletions(-) 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 a467c0be36e..86b5c0b2b70 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 @@ -768,13 +768,6 @@ internal virtual void ValidateParameters() ErrorRecord error = GetValidationError(WebCmdletStrings.CredentialConflict, "WebCmdletCredentialConflictException"); ThrowTerminatingError(error); } - - // Method - if (UnixSocket is not null && Socket.OSSupportsUnixDomainSockets is false) - { - ErrorRecord error = GetValidationError(WebCmdletStrings.UnixSocketPlatformNotSupported, "UnixSocketPlatformNotSupportedException"); - ThrowTerminatingError(error); - } // Proxy server if (ProxyUseDefaultCredentials && ProxyCredential is not null) diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index b4da3fc0b25..5b1b33eae62 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -210,9 +210,6 @@ Unable to retrieve certificates because the thumbprint is not valid. Verify the thumbprint and retry. - - UnixDomainSockets are not supported on this platform. Reissue the command removing the -UnixSocket parameter. - Web request completed. (Number of bytes processed: {0}) From 87ed139bc146f624c9ee9d2b61f30b35c8990bb2 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:30:24 +0200 Subject: [PATCH 12/25] fix UseDefaultCredentials --- .../commands/utility/WebCmdlet/WebRequestSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs index 025193e7f60..6846ac814a2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs @@ -219,7 +219,7 @@ private HttpClient CreateHttpClient() { handler.Credentials = Credentials; } - else + else if (UseDefaultCredentials) { handler.Credentials = CredentialCache.DefaultCredentials; } From 37129277357c970379885d264ee6cb15472d4428 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 28 Apr 2023 20:41:19 +0200 Subject: [PATCH 13/25] Add Tests --- build.psm1 | 3 +- .../WebCmdlets.Tests.ps1 | 19 +++ test/tools/Modules/UnixSocket/README.md | 17 +++ test/tools/Modules/UnixSocket/UnixSocket.psd1 | 16 +++ test/tools/Modules/UnixSocket/UnixSocket.psm1 | 115 ++++++++++++++++++ test/tools/UnixSocket/UnixSocket.cs | 36 ++++++ test/tools/UnixSocket/UnixSocket.csproj | 14 +++ 7 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 test/tools/Modules/UnixSocket/README.md create mode 100644 test/tools/Modules/UnixSocket/UnixSocket.psd1 create mode 100644 test/tools/Modules/UnixSocket/UnixSocket.psm1 create mode 100644 test/tools/UnixSocket/UnixSocket.cs create mode 100644 test/tools/UnixSocket/UnixSocket.csproj diff --git a/build.psm1 b/build.psm1 index 3d0be1a846b..525fe5d3dd2 100644 --- a/build.psm1 +++ b/build.psm1 @@ -1153,8 +1153,9 @@ function Publish-PSTestTools { $tools = @( @{ Path="${PSScriptRoot}/test/tools/TestAlc"; Output="library" } @{ Path="${PSScriptRoot}/test/tools/TestExe"; Output="exe" } - @{ Path="${PSScriptRoot}/test/tools/WebListener"; Output="exe" } @{ Path="${PSScriptRoot}/test/tools/TestService"; Output="exe" } + @{ Path="${PSScriptRoot}/test/tools/UnixSocket"; Output="exe" } + @{ Path="${PSScriptRoot}/test/tools/WebListener"; Output="exe" } ) $Options = Get-PSOptions -DefaultToNew diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index fe6db03a2a1..ea4d8d288aa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -4147,3 +4147,22 @@ Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI", "RequireAdmi { Invoke-RestMethod -Uri $uri -ContentType $null } | Should -Not -Throw } } + +Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { + BeforeAll { + $WebListener = Start-UnixSocket + } + + It "Execute Invoke-WebRequest with -UnixSocket" { + $uri = Get-UnixSocketUri + $result = Invoke-WebRequest $uri -UnixSocket "/tmp/foo.sock" + $result.StatusCode | Should -Be "200" + $result.Content | Should -Be "Hello World Unix Socket." + } + + It "Execute Invoke-RestMethod with -UnixSocket" { + $uri = Get-UnixSocketUri + $result = Invoke-RestMethod $uri -UnixSocket "/tmp/foo.sock" + $result | Should -Be "Hello World Unix Socket." + } +} diff --git a/test/tools/Modules/UnixSocket/README.md b/test/tools/Modules/UnixSocket/README.md new file mode 100644 index 00000000000..7270c95cdf4 --- /dev/null +++ b/test/tools/Modules/UnixSocket/README.md @@ -0,0 +1,17 @@ +# UnixSocket Module + +A PowerShell module for managing the UnixSocket App. + +# Running UnixSocket + +```powershell +Import-Module .\build.psm1 +Publish-PSTestTools +$Listener = Start-UnixSocket +``` + +# Stopping UnixSocket + +```powershell +Stop-UnixSocket +``` diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psd1 b/test/tools/Modules/UnixSocket/UnixSocket.psd1 new file mode 100644 index 00000000000..647e056d2e9 --- /dev/null +++ b/test/tools/Modules/UnixSocket/UnixSocket.psd1 @@ -0,0 +1,16 @@ +@{ + ModuleVersion = '1.0.0' + GUID = '86471f04-5b94-4136-a299-caf98464a06b' + Author = 'Carlo Toso' + Description = 'An UnixSocket Server for testing purposes' + RootModule = 'UnixSocket.psm1' + RequiredModules = @() + FunctionsToExport = @( + 'Get-UnixSocket' + 'Get-UnixSocketUri' + 'Start-UnixSocket' + 'Stop-UnixSocket' + ) + AliasesToExport = @() + CmdletsToExport = @() +} diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 new file mode 100644 index 00000000000..0465dcd4980 --- /dev/null +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -0,0 +1,115 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +Class UnixSocket +{ + [System.Management.Automation.Job]$Job + + UnixSocket () { } + + [String] GetStatus() + { + return $this.Job.JobStateInfo.State + } +} + +[UnixSocket]$UnixSocket + +function Get-UnixSocket +{ + [CmdletBinding(ConfirmImpact = 'Low')] + [OutputType([UnixSocket])] + param() + + process + { + return [UnixSocket]$Script:UnixSocket + } +} + +function Start-UnixSocket +{ + [CmdletBinding(ConfirmImpact = 'Low')] + [OutputType([UnixSocket])] + param() + + process + { + $runningListener = Get-UnixSocket + if ($null -ne $runningListener -and $runningListener.GetStatus() -eq 'Running') + { + return $runningListener + } + + $initTimeoutSeconds = 25 + $appExe = (Get-Command UnixSocket).Path + $initCompleteMessage = 'Now listening on' + $sleepMilliseconds = 100 + + $Job = Start-Job { + $path = Split-Path -Parent (Get-Command UnixSocket).Path -Verbose + Push-Location $path -Verbose + 'appEXE: {0}' -f $using:appExe + $env:ASPNETCORE_ENVIRONMENT = 'Development' + & $using:appExe + } + + $Script:UnixSocket = [UnixSocket]@{ + Job = $Job + } + + # Count iterations of $sleepMilliseconds instead of using system time to work around possible CI VM sleep/delays + $sleepCountRemaining = $initTimeoutSeconds * 1000 / $sleepMilliseconds + do + { + Start-Sleep -Milliseconds $sleepMilliseconds + $initStatus = $Job.ChildJobs[0].Output | Out-String + $isRunning = $initStatus -match $initCompleteMessage + $sleepCountRemaining-- + } + while (-not $isRunning -and $sleepCountRemaining -gt 0) + + if (-not $isRunning) + { + $jobErrors = $Job.ChildJobs[0].Error | Out-String + $jobOutput = $Job.ChildJobs[0].Output | Out-String + $jobVerbose = $Job.ChildJobs[0].Verbose | Out-String + $Job | Stop-Job + $Job | Remove-Job -Force + $message = 'UnixSocket did not start before the timeout was reached.{0}Errors:{0}{1}{0}Output:{0}{2}{0}Verbose:{0}{3}' -f ([System.Environment]::NewLine), $jobErrors, $jobOutput, $jobVerbose + throw $message + } + return $Script:UnixSocket + } +} + +function Stop-UnixSocket +{ + [CmdletBinding(ConfirmImpact = 'Low')] + [OutputType([Void])] + param() + + process + { + $Script:UnixSocket.Job | Stop-Job -PassThru | Remove-Job + $Script:UnixSocket = $null + } +} + +function Get-UnixSocketUri { + [CmdletBinding()] + [OutputType([Uri])] + param () + + process { + $runningListener = Get-UnixSocket + if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running') + { + return $null + } + $Uri = [System.UriBuilder]::new() + $Uri.Host = '127.0.0.0' + + return [Uri]$Uri.ToString() + } +} diff --git a/test/tools/UnixSocket/UnixSocket.cs b/test/tools/UnixSocket/UnixSocket.cs new file mode 100644 index 00000000000..2da7d5cba65 --- /dev/null +++ b/test/tools/UnixSocket/UnixSocket.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using Microsoft.AspNetCore.Builder; +using static Microsoft.AspNetCore.Hosting.WebHostBuilderKestrelExtensions; + +namespace UnixSocket +{ + public static class Program + { + public static void Main(string[] args) + { + const string UnixSocketPath = "/tmp/foo.sock"; + + if (!Directory.Exists("/tmp")) + { + Directory.CreateDirectory("/tmp"); + } + + if (File.Exists(UnixSocketPath)) + { + File.Delete(UnixSocketPath); + } + + var builder = WebApplication.CreateBuilder(); + builder.WebHost.ConfigureKestrel(options => + { + options.ListenUnixSocket(UnixSocketPath); + }); + + var app = builder.Build(); + app.MapGet("/", () => "Hello World Unix Socket."); + + app.Run(); + } + } +} diff --git a/test/tools/UnixSocket/UnixSocket.csproj b/test/tools/UnixSocket/UnixSocket.csproj new file mode 100644 index 00000000000..a4143270c8c --- /dev/null +++ b/test/tools/UnixSocket/UnixSocket.csproj @@ -0,0 +1,14 @@ + + + + + + A very simple ASP.NET Core app to provide an UnixSocket server for testing. + UnixSocket + Exe + true + true + win7-x86;win7-x64;osx-x64;linux-x64 + + + From f82c20ff20a6add7982059475aa6c93c85416df7 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 28 Apr 2023 22:16:54 +0200 Subject: [PATCH 14/25] fix codefactor --- .spelling | 1 + test/tools/UnixSocket/UnixSocket.cs | 49 +++++++++++++++-------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.spelling b/.spelling index 507ab9e16bd..a00619c240e 100644 --- a/.spelling +++ b/.spelling @@ -1031,6 +1031,7 @@ ubuntu22.04 uint un-versioned unicode +UnixSocket unregister-event unregister-packagesource unregister-psrepository diff --git a/test/tools/UnixSocket/UnixSocket.cs b/test/tools/UnixSocket/UnixSocket.cs index 2da7d5cba65..f90d2d47d00 100644 --- a/test/tools/UnixSocket/UnixSocket.cs +++ b/test/tools/UnixSocket/UnixSocket.cs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + using System; using System.IO; using Microsoft.AspNetCore.Builder; @@ -5,32 +8,32 @@ namespace UnixSocket { - public static class Program - { - public static void Main(string[] args) - { - const string UnixSocketPath = "/tmp/foo.sock"; + public static class Program + { + public static void Main(string[] args) + { + const string UnixSocketPath = "/tmp/foo.sock"; - if (!Directory.Exists("/tmp")) - { - Directory.CreateDirectory("/tmp"); - } + if (!Directory.Exists("/tmp")) + { + Directory.CreateDirectory("/tmp"); + } - if (File.Exists(UnixSocketPath)) - { - File.Delete(UnixSocketPath); - } + if (File.Exists(UnixSocketPath)) + { + File.Delete(UnixSocketPath); + } - var builder = WebApplication.CreateBuilder(); - builder.WebHost.ConfigureKestrel(options => - { - options.ListenUnixSocket(UnixSocketPath); - }); + var builder = WebApplication.CreateBuilder(); + builder.WebHost.ConfigureKestrel(options => + { + options.ListenUnixSocket(UnixSocketPath); + }); - var app = builder.Build(); - app.MapGet("/", () => "Hello World Unix Socket."); + var app = builder.Build(); + app.MapGet("/", () => "Hello World Unix Socket."); - app.Run(); - } - } + app.Run(); + } + } } From 5ba37ac4bc9f3c1ef550012cf7558931b76ecff1 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 15 May 2023 19:32:09 +0200 Subject: [PATCH 15/25] change author --- test/tools/Modules/UnixSocket/UnixSocket.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psd1 b/test/tools/Modules/UnixSocket/UnixSocket.psd1 index 647e056d2e9..73b2823c7bf 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psd1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psd1 @@ -1,7 +1,7 @@ @{ ModuleVersion = '1.0.0' GUID = '86471f04-5b94-4136-a299-caf98464a06b' - Author = 'Carlo Toso' + Author = 'PowerShell' Description = 'An UnixSocket Server for testing purposes' RootModule = 'UnixSocket.psm1' RequiredModules = @() From 47595c59361bcb8162fdecb14bb28a82368f8d61 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 21 May 2023 20:08:34 +0200 Subject: [PATCH 16/25] follow suggestions --- .../WebCmdlets.Tests.ps1 | 6 ++++-- test/tools/Modules/UnixSocket/UnixSocket.psd1 | 1 + test/tools/Modules/UnixSocket/UnixSocket.psm1 | 17 +++++++++++++++++ test/tools/UnixSocket/UnixSocket.cs | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ab61f698489..ab8df93af79 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -4344,14 +4344,16 @@ Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { It "Execute Invoke-WebRequest with -UnixSocket" { $uri = Get-UnixSocketUri - $result = Invoke-WebRequest $uri -UnixSocket "/tmp/foo.sock" + $unixSocket = Get-UnixSocketName + $result = Invoke-WebRequest $uri -UnixSocket $unixSocket $result.StatusCode | Should -Be "200" $result.Content | Should -Be "Hello World Unix Socket." } It "Execute Invoke-RestMethod with -UnixSocket" { $uri = Get-UnixSocketUri - $result = Invoke-RestMethod $uri -UnixSocket "/tmp/foo.sock" + $unixSocket = Get-UnixSocketName + $result = Invoke-RestMethod $uri -UnixSocket $unixSocket $result | Should -Be "Hello World Unix Socket." } } diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psd1 b/test/tools/Modules/UnixSocket/UnixSocket.psd1 index 73b2823c7bf..f1cb3a92215 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psd1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psd1 @@ -7,6 +7,7 @@ RequiredModules = @() FunctionsToExport = @( 'Get-UnixSocket' + 'Get-UnixSocketName' 'Get-UnixSocketUri' 'Start-UnixSocket' 'Stop-UnixSocket' diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 0465dcd4980..202ec995b7a 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -96,6 +96,23 @@ function Stop-UnixSocket } } +function Get-UnixSocketName { + [CmdletBinding()] + [OutputType([string])] + param () + + process { + $runningListener = Get-UnixSocket + if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running') + { + return $null + } + $unixSocketName = "/tmp/UnixSocket.sock" + + return $unixSocketName + } +} + function Get-UnixSocketUri { [CmdletBinding()] [OutputType([Uri])] diff --git a/test/tools/UnixSocket/UnixSocket.cs b/test/tools/UnixSocket/UnixSocket.cs index f90d2d47d00..99feed46549 100644 --- a/test/tools/UnixSocket/UnixSocket.cs +++ b/test/tools/UnixSocket/UnixSocket.cs @@ -12,7 +12,7 @@ public static class Program { public static void Main(string[] args) { - const string UnixSocketPath = "/tmp/foo.sock"; + const string UnixSocketPath = "/tmp/UnixSocket.sock"; if (!Directory.Exists("/tmp")) { From 35f1e0cb4ee2e072c01872d5b41b2a52aa7bd726 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 25 May 2023 23:24:35 +0200 Subject: [PATCH 17/25] follow suggestions @xtqqczze --- .../WebCmdlets.Tests.ps1 | 9 ++++++--- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 8 ++++---- test/tools/UnixSocket/UnixSocket.cs | 16 ++-------------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ab8df93af79..ae6b0f11a92 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -4339,12 +4339,12 @@ Describe 'Invoke-WebRequest and Invoke-RestMethod support Cancellation through C Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { BeforeAll { - $WebListener = Start-UnixSocket + $unixSocket = Get-UnixSocketName + $WebListener = Start-UnixSocket $unixSocket } It "Execute Invoke-WebRequest with -UnixSocket" { $uri = Get-UnixSocketUri - $unixSocket = Get-UnixSocketName $result = Invoke-WebRequest $uri -UnixSocket $unixSocket $result.StatusCode | Should -Be "200" $result.Content | Should -Be "Hello World Unix Socket." @@ -4352,8 +4352,11 @@ Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { It "Execute Invoke-RestMethod with -UnixSocket" { $uri = Get-UnixSocketUri - $unixSocket = Get-UnixSocketName $result = Invoke-RestMethod $uri -UnixSocket $unixSocket $result | Should -Be "Hello World Unix Socket." } + + AfterAll { + Remove-Item -Path $unixSocket -Force + } } diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 202ec995b7a..676bc34a1d3 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -31,7 +31,7 @@ function Start-UnixSocket { [CmdletBinding(ConfirmImpact = 'Low')] [OutputType([UnixSocket])] - param() + param([string] $socketPath) process { @@ -51,7 +51,7 @@ function Start-UnixSocket Push-Location $path -Verbose 'appEXE: {0}' -f $using:appExe $env:ASPNETCORE_ENVIRONMENT = 'Development' - & $using:appExe + & $using:appExe $socketPath } $Script:UnixSocket = [UnixSocket]@{ @@ -103,11 +103,11 @@ function Get-UnixSocketName { process { $runningListener = Get-UnixSocket - if ($null -eq $runningListener -or $runningListener.GetStatus() -ne 'Running') + if ($null -ne $runningListener -or $runningListener.GetStatus() -eq 'Running') { return $null } - $unixSocketName = "/tmp/UnixSocket.sock" + $unixSocketName = [System.IO.Path]::GetTempFileName() return $unixSocketName } diff --git a/test/tools/UnixSocket/UnixSocket.cs b/test/tools/UnixSocket/UnixSocket.cs index 99feed46549..f0acea7e533 100644 --- a/test/tools/UnixSocket/UnixSocket.cs +++ b/test/tools/UnixSocket/UnixSocket.cs @@ -12,22 +12,10 @@ public static class Program { public static void Main(string[] args) { - const string UnixSocketPath = "/tmp/UnixSocket.sock"; - - if (!Directory.Exists("/tmp")) - { - Directory.CreateDirectory("/tmp"); - } - - if (File.Exists(UnixSocketPath)) - { - File.Delete(UnixSocketPath); - } - - var builder = WebApplication.CreateBuilder(); + WebApplication builder = WebApplication.CreateBuilder(); builder.WebHost.ConfigureKestrel(options => { - options.ListenUnixSocket(UnixSocketPath); + options.ListenUnixSocket(args[0]); }); var app = builder.Build(); From 0e17d60fe1922243e8f090c54fb36fad98b2bd8f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 25 May 2023 23:33:45 +0200 Subject: [PATCH 18/25] fix --- test/tools/UnixSocket/UnixSocket.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/UnixSocket/UnixSocket.cs b/test/tools/UnixSocket/UnixSocket.cs index f0acea7e533..b5bd3ed4b5a 100644 --- a/test/tools/UnixSocket/UnixSocket.cs +++ b/test/tools/UnixSocket/UnixSocket.cs @@ -12,7 +12,7 @@ public static class Program { public static void Main(string[] args) { - WebApplication builder = WebApplication.CreateBuilder(); + WebApplicationBuilder builder = WebApplication.CreateBuilder(); builder.WebHost.ConfigureKestrel(options => { options.ListenUnixSocket(args[0]); From ac69e975a378442aea5ca547c4ed2be9a2affc64 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Thu, 25 May 2023 23:59:30 +0200 Subject: [PATCH 19/25] fix Get-UnixSocketName --- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 676bc34a1d3..6f6e3778065 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -102,14 +102,7 @@ function Get-UnixSocketName { param () process { - $runningListener = Get-UnixSocket - if ($null -ne $runningListener -or $runningListener.GetStatus() -eq 'Running') - { - return $null - } - $unixSocketName = [System.IO.Path]::GetTempFileName() - - return $unixSocketName + return [System.IO.Path]::GetTempFileName() } } From b719bd272ff44e0c55adf4785ee2e2914a235393 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 26 May 2023 11:22:48 +0200 Subject: [PATCH 20/25] fix --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ae6b0f11a92..3265fd6c0d8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -4357,6 +4357,6 @@ Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { } AfterAll { - Remove-Item -Path $unixSocket -Force + Remove-Item -Path $unixSocket -Force -ErrorAction Ignore } } diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 6f6e3778065..84a1c03a414 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -102,7 +102,8 @@ function Get-UnixSocketName { param () process { - return [System.IO.Path]::GetTempFileName() + $unixSocketName = "UnixSocket.sock" + return [System.IO.Path]::Join([System.IO.Path]::GetTempPath(), $unixSocketName) } } From 22ae762684fcc8d1e48019c4de303c4d1e54c1e8 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Fri, 26 May 2023 13:07:56 +0200 Subject: [PATCH 21/25] Add $using: --- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 84a1c03a414..0dcac23e3ca 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -51,7 +51,7 @@ function Start-UnixSocket Push-Location $path -Verbose 'appEXE: {0}' -f $using:appExe $env:ASPNETCORE_ENVIRONMENT = 'Development' - & $using:appExe $socketPath + & $using:appExe $using:socketPath } $Script:UnixSocket = [UnixSocket]@{ From aa5e0ed5292ade9fc2953b08ea390c4455dd6535 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 27 May 2023 14:14:16 +0200 Subject: [PATCH 22/25] Update test/tools/Modules/UnixSocket/UnixSocket.psm1 Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index 0dcac23e3ca..d3b4392be6a 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -102,8 +102,7 @@ function Get-UnixSocketName { param () process { - $unixSocketName = "UnixSocket.sock" - return [System.IO.Path]::Join([System.IO.Path]::GetTempPath(), $unixSocketName) + return [System.IO.Path]::Join([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()) } } From 8eda977ba5e45a61b8952910e70a09e87b51a4b3 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 27 May 2023 15:42:44 +0200 Subject: [PATCH 23/25] remove After-All --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 3265fd6c0d8..3ec3b9344eb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -4355,8 +4355,4 @@ Describe "Web cmdlets Unix Sockets tests" -Tags "CI", "RequireAdminOnWindows" { $result = Invoke-RestMethod $uri -UnixSocket $unixSocket $result | Should -Be "Hello World Unix Socket." } - - AfterAll { - Remove-Item -Path $unixSocket -Force -ErrorAction Ignore - } } From 62b1b88f87d1cf22048dd796855ef9581541b8b6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 29 May 2023 15:56:48 +0200 Subject: [PATCH 24/25] change extension to sock --- test/tools/Modules/UnixSocket/UnixSocket.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/Modules/UnixSocket/UnixSocket.psm1 b/test/tools/Modules/UnixSocket/UnixSocket.psm1 index d3b4392be6a..ffd2de42fab 100644 --- a/test/tools/Modules/UnixSocket/UnixSocket.psm1 +++ b/test/tools/Modules/UnixSocket/UnixSocket.psm1 @@ -102,7 +102,7 @@ function Get-UnixSocketName { param () process { - return [System.IO.Path]::Join([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName()) + return [System.IO.Path]::Join([System.IO.Path]::GetTempPath(), [System.IO.Path]::ChangeExtension([System.IO.Path]::GetRandomFileName(), "sock")) } } From 352b0500ef7927f53bd6469b3c86902193e1eca4 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 25 Jul 2023 00:01:52 +0200 Subject: [PATCH 25/25] fix MD025 - Multiple top-level headings in the same document --- test/tools/Modules/UnixSocket/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tools/Modules/UnixSocket/README.md b/test/tools/Modules/UnixSocket/README.md index 7270c95cdf4..af070f6449a 100644 --- a/test/tools/Modules/UnixSocket/README.md +++ b/test/tools/Modules/UnixSocket/README.md @@ -2,7 +2,7 @@ A PowerShell module for managing the UnixSocket App. -# Running UnixSocket +## Running UnixSocket ```powershell Import-Module .\build.psm1 @@ -10,7 +10,7 @@ Publish-PSTestTools $Listener = Start-UnixSocket ``` -# Stopping UnixSocket +## Stopping UnixSocket ```powershell Stop-UnixSocket