| | 1 | | // src/CSharp/Kestrun.Net/KrHttp.cs |
| | 2 | | using System.IO.Pipes; |
| | 3 | | using System.Net; |
| | 4 | | using System.Net.Security; |
| | 5 | | using System.Net.Sockets; |
| | 6 | |
|
| | 7 | | namespace Kestrun.Client; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Factory methods to create HttpClient instances for different transport types. |
| | 11 | | /// </summary> |
| | 12 | | public static class KrHttpClientFactory |
| | 13 | | { |
| | 14 | | // ---- Internal helper ---------------------------------------------------- |
| | 15 | | private static SocketsHttpHandler CreateHandler(KrHttpClientOptions opts) |
| | 16 | | { |
| 0 | 17 | | var effectiveTimeout = (opts.Timeout <= TimeSpan.Zero) ? TimeSpan.FromSeconds(100) : opts.Timeout; |
| | 18 | |
|
| 0 | 19 | | var handler = new SocketsHttpHandler |
| 0 | 20 | | { |
| 0 | 21 | | AutomaticDecompression = opts.Decompression, |
| 0 | 22 | | ConnectTimeout = effectiveTimeout, |
| 0 | 23 | |
|
| 0 | 24 | | // Redirects |
| 0 | 25 | | AllowAutoRedirect = opts.AllowAutoRedirect, |
| 0 | 26 | | MaxAutomaticRedirections = Math.Max(1, opts.MaxAutomaticRedirections), |
| 0 | 27 | |
|
| 0 | 28 | | // Cookies/session |
| 0 | 29 | | UseCookies = opts.Cookies is not null, |
| 0 | 30 | | CookieContainer = opts.Cookies ?? new CookieContainer(), |
| 0 | 31 | |
|
| 0 | 32 | | // Proxy |
| 0 | 33 | | UseProxy = opts.UseProxy && opts.Proxy is not null, |
| 0 | 34 | | Proxy = opts.Proxy, |
| 0 | 35 | |
|
| 0 | 36 | | // Server auth |
| 0 | 37 | | Credentials = opts.UseDefaultCredentials |
| 0 | 38 | | ? CredentialCache.DefaultCredentials |
| 0 | 39 | | : opts.Credentials |
| 0 | 40 | | }; |
| | 41 | |
|
| | 42 | | // Proxy auth wiring if provided |
| 0 | 43 | | if (opts.Proxy is not null) |
| | 44 | | { |
| 0 | 45 | | if (opts.ProxyUseDefaultCredentials) |
| | 46 | | { |
| 0 | 47 | | opts.Proxy.Credentials = CredentialCache.DefaultCredentials; |
| | 48 | | } |
| 0 | 49 | | else if (opts.Proxy.Credentials is null && opts.Credentials is not null) |
| | 50 | | { |
| | 51 | | // If caller didn't set proxy creds explicitly but provided server creds, |
| | 52 | | // reuse them for the proxy (common IWR behavior). |
| 0 | 53 | | opts.Proxy.Credentials = opts.Credentials; |
| | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | if (opts.IgnoreCertErrors) |
| | 58 | | { |
| 0 | 59 | | handler.SslOptions = new SslClientAuthenticationOptions |
| 0 | 60 | | { |
| 0 | 61 | | RemoteCertificateValidationCallback = static (sender, certificate, chain, errors) => true |
| 0 | 62 | | }; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | return handler; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | private static HttpClient MakeClient(HttpMessageHandler handler, Uri baseAddress, TimeSpan timeout) |
| 0 | 69 | | => new(handler) |
| 0 | 70 | | { |
| 0 | 71 | | Timeout = (timeout <= TimeSpan.Zero) ? TimeSpan.FromSeconds(100) : timeout, |
| 0 | 72 | | BaseAddress = baseAddress |
| 0 | 73 | | }; |
| | 74 | |
|
| | 75 | | // ---- Named Pipe --------------------------------------------------------- |
| | 76 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe.</summary> |
| | 77 | | public static HttpClient CreateNamedPipeClient(string pipeName, TimeSpan timeout) |
| 0 | 78 | | => CreateNamedPipeClient(pipeName, timeout, ignoreCertErrors: false); |
| | 79 | |
|
| | 80 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe (legacy overload).</summary> |
| | 81 | | public static HttpClient CreateNamedPipeClient(string pipeName, TimeSpan timeout, bool ignoreCertErrors) |
| | 82 | | { |
| 0 | 83 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| 0 | 84 | | return CreateNamedPipeClient(pipeName, opts); |
| | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe (full options).</summary> |
| | 88 | | public static HttpClient CreateNamedPipeClient(string pipeName, KrHttpClientOptions opts) |
| | 89 | | { |
| 0 | 90 | | if (string.IsNullOrWhiteSpace(pipeName)) |
| | 91 | | { |
| 0 | 92 | | throw new ArgumentNullException(nameof(pipeName)); |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | var h = CreateHandler(opts); |
| | 96 | |
|
| | 97 | | // capture pipeName in the lambda (works on .NET 6/7/8) |
| 0 | 98 | | h.ConnectCallback = (ctx, ct) => |
| 0 | 99 | | { |
| 0 | 100 | | var stream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); |
| 0 | 101 | | stream.Connect(); |
| 0 | 102 | | return new ValueTask<Stream>(stream); |
| 0 | 103 | | }; |
| | 104 | |
|
| 0 | 105 | | return MakeClient(h, new Uri("http://localhost"), opts.Timeout); |
| | 106 | | } |
| | 107 | |
|
| | 108 | | // ---- Unix Domain Socket ------------------------------------------------- |
| | 109 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket.</summary> |
| | 110 | | public static HttpClient CreateUnixSocketClient(string socketPath, TimeSpan timeout) |
| 0 | 111 | | => CreateUnixSocketClient(socketPath, timeout, ignoreCertErrors: false); |
| | 112 | |
|
| | 113 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket (legacy overload).</summary> |
| | 114 | | public static HttpClient CreateUnixSocketClient(string socketPath, TimeSpan timeout, bool ignoreCertErrors) |
| | 115 | | { |
| 0 | 116 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| 0 | 117 | | return CreateUnixSocketClient(socketPath, opts); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket (full options).</summary> |
| | 121 | | public static HttpClient CreateUnixSocketClient(string socketPath, KrHttpClientOptions opts) |
| | 122 | | { |
| 0 | 123 | | if (string.IsNullOrWhiteSpace(socketPath)) |
| | 124 | | { |
| 0 | 125 | | throw new ArgumentNullException(nameof(socketPath)); |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | var h = CreateHandler(opts); |
| | 129 | |
|
| 0 | 130 | | h.ConnectCallback = (ctx, ct) => |
| 0 | 131 | | { |
| 0 | 132 | | var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); |
| 0 | 133 | | sock.Connect(new UnixDomainSocketEndPoint(socketPath)); |
| 0 | 134 | | return new ValueTask<Stream>(new NetworkStream(sock, ownsSocket: true)); |
| 0 | 135 | | }; |
| | 136 | |
|
| 0 | 137 | | return MakeClient(h, new Uri("http://localhost"), opts.Timeout); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // ---- TCP (HTTP/HTTPS) --------------------------------------------------- |
| | 141 | | /// <summary>Classic TCP HttpClient (normal HTTP/S).</summary> |
| | 142 | | public static HttpClient CreateTcpClient(Uri baseUri, TimeSpan timeout) |
| 0 | 143 | | => CreateTcpClient(baseUri, timeout, ignoreCertErrors: false); |
| | 144 | |
|
| | 145 | | /// <summary>Classic TCP HttpClient (normal HTTP/S, legacy overload).</summary> |
| | 146 | | public static HttpClient CreateTcpClient(Uri baseUri, TimeSpan timeout, bool ignoreCertErrors) |
| | 147 | | { |
| 0 | 148 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| 0 | 149 | | return CreateTcpClient(baseUri, opts); |
| | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary>Classic TCP HttpClient (normal HTTP/S, full options).</summary> |
| | 153 | | public static HttpClient CreateTcpClient(Uri baseUri, KrHttpClientOptions opts) |
| | 154 | | { |
| 0 | 155 | | ArgumentNullException.ThrowIfNull(baseUri); |
| 0 | 156 | | var h = CreateHandler(opts); |
| 0 | 157 | | return MakeClient(h, baseUri, opts.Timeout); |
| | 158 | | } |
| | 159 | | } |