Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e2fbf1e
Unix sockets
CarloToso Mar 15, 2023
550707e
rework _custommethod
CarloToso Mar 16, 2023
ec2fcf1
use SetClassVar
CarloToso Mar 16, 2023
84968e7
Pass UnixSocket to WebSession even if it's null
CarloToso Mar 16, 2023
95d2b33
Merge branch 'PowerShell:master' into WebCmdlets-UnixSockets2
CarloToso Mar 16, 2023
30132a8
UnixDomainSocketEndPoint nullable
CarloToso Mar 16, 2023
43f2772
remove useless line
CarloToso Mar 16, 2023
d9ec5c4
Add Socket.OSSupportsUnixDomainSockets validation
CarloToso Apr 12, 2023
bd5c4d6
move error to ValidateParameters()
CarloToso Apr 12, 2023
f7ec7e8
Update src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletS…
CarloToso Apr 12, 2023
2b9a2b9
update error
CarloToso Apr 12, 2023
e7090f3
remove error
CarloToso Apr 12, 2023
87ed139
fix UseDefaultCredentials
CarloToso Apr 24, 2023
3712927
Add Tests
CarloToso Apr 28, 2023
f173a78
Merge branch 'master' into WebCmdlets-UnixSockets2
CarloToso Apr 28, 2023
f82c20f
fix codefactor
CarloToso Apr 28, 2023
5ba37ac
change author
CarloToso May 15, 2023
47595c5
follow suggestions
CarloToso May 21, 2023
35f1e0c
follow suggestions @xtqqczze
CarloToso May 25, 2023
0e17d60
fix
CarloToso May 25, 2023
ac69e97
fix Get-UnixSocketName
CarloToso May 25, 2023
b719bd2
fix
CarloToso May 26, 2023
22ae762
Add $using:
CarloToso May 26, 2023
aa5e0ed
Update test/tools/Modules/UnixSocket/UnixSocket.psm1
CarloToso May 27, 2023
8eda977
remove After-All
CarloToso May 27, 2023
62b1b88
change extension to sock
CarloToso May 29, 2023
3627347
Merge branch 'master' into WebCmdlets-UnixSockets2
CarloToso Jul 10, 2023
48df0f8
Merge branch 'PowerShell:master' into WebCmdlets-UnixSockets2
CarloToso Jul 24, 2023
352b050
fix MD025 - Multiple top-level headings in the same document
CarloToso Jul 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -352,21 +353,23 @@ 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;

/// <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 @@ -765,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)
Expand Down Expand Up @@ -969,6 +979,8 @@ internal virtual void PrepareSession()
WebSession.MaximumRedirection = MaximumRedirection;
}

WebSession.UnixSocket = UnixSocket;

WebSession.SkipCertificateCheck = SkipCertificateCheck.IsPresent;

// Store the other supplied headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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;
Expand All @@ -33,6 +34,7 @@ public class WebRequestSession : IDisposable
private bool _noProxy;
private bool _disposed;
private int _timeoutSec;
private UnixDomainSocketEndPoint? _unixSocket;

/// <summary>
/// Contains true if an existing HttpClient had to be disposed and recreated since the WebSession was last used.
Expand Down Expand Up @@ -144,6 +146,8 @@ public WebRequestSession()

internal int TimeoutSec { set => SetStructVar(ref _timeoutSec, value); }

internal UnixDomainSocketEndPoint UnixSocket { set => SetClassVar(ref _unixSocket, value); }

internal bool NoProxy
{
set
Expand Down Expand Up @@ -195,7 +199,18 @@ internal HttpClient GetHttpClient(bool suppressHttpClientRedirects, out bool cli

private HttpClient CreateHttpClient()
{
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);
await socket.ConnectAsync(_unixSocket).ConfigureAwait(false);

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

handler.CookieContainer = Cookies;
handler.AutomaticDecompression = DecompressionMethods.All;
Expand All @@ -206,7 +221,7 @@ private HttpClient CreateHttpClient()
}
else
{
handler.UseDefaultCredentials = UseDefaultCredentials;
handler.Credentials = CredentialCache.DefaultCredentials;
}

if (_noProxy)
Expand All @@ -220,13 +235,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;
Expand All @@ -235,9 +249,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
<data name="ThumbprintNotFound" xml:space="preserve">
<value>Unable to retrieve certificates because the thumbprint is not valid. Verify the thumbprint and retry. </value>
</data>
<data name="UnixDomainSocketsNotSupported" xml:space="preserve">
<value>UnixDomainSockets are not supported on this platform. Reissue the command removing the -UnixSocket parameter. </value>
</data>
<data name="WriteRequestComplete" xml:space="preserve">
<value>Web request completed. (Number of bytes processed: {0})</value>
</data>
Expand Down