| | | 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 handler = CreateBasicHandler(opts); |
| | | 18 | | |
| | | 19 | | // Proxy auth wiring if provided |
| | 0 | 20 | | if (opts.Proxy is not null) |
| | | 21 | | { |
| | 0 | 22 | | if (opts.ProxyUseDefaultCredentials) |
| | | 23 | | { |
| | 0 | 24 | | opts.Proxy.Credentials = CredentialCache.DefaultCredentials; |
| | | 25 | | } |
| | 0 | 26 | | else if (opts.Proxy.Credentials is null && opts.Credentials is not null) |
| | | 27 | | { |
| | | 28 | | // If caller didn't set proxy creds explicitly but provided server creds, |
| | | 29 | | // reuse them for the proxy (common IWR behavior). |
| | 0 | 30 | | opts.Proxy.Credentials = opts.Credentials; |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | if (opts.IgnoreCertErrors) |
| | | 35 | | { |
| | 0 | 36 | | handler.SslOptions = new SslClientAuthenticationOptions |
| | 0 | 37 | | { |
| | 0 | 38 | | RemoteCertificateValidationCallback = static (sender, certificate, chain, errors) => true |
| | 0 | 39 | | }; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return handler; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Creates a basic SocketsHttpHandler with common options applied. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="opts">The HTTP client options.</param> |
| | | 49 | | /// <returns>A configured SocketsHttpHandler instance.</returns> |
| | | 50 | | private static SocketsHttpHandler CreateBasicHandler(KrHttpClientOptions opts) |
| | | 51 | | { |
| | 0 | 52 | | var effectiveTimeout = (opts.Timeout <= TimeSpan.Zero) ? TimeSpan.FromSeconds(100) : opts.Timeout; |
| | | 53 | | |
| | 0 | 54 | | return new SocketsHttpHandler |
| | 0 | 55 | | { |
| | 0 | 56 | | AutomaticDecompression = opts.Decompression, |
| | 0 | 57 | | ConnectTimeout = effectiveTimeout, |
| | 0 | 58 | | |
| | 0 | 59 | | // Redirects |
| | 0 | 60 | | AllowAutoRedirect = opts.AllowAutoRedirect, |
| | 0 | 61 | | MaxAutomaticRedirections = Math.Max(1, opts.MaxAutomaticRedirections), |
| | 0 | 62 | | |
| | 0 | 63 | | // Cookies/session |
| | 0 | 64 | | UseCookies = opts.Cookies is not null, |
| | 0 | 65 | | CookieContainer = opts.Cookies ?? new CookieContainer(), |
| | 0 | 66 | | |
| | 0 | 67 | | // Proxy |
| | 0 | 68 | | UseProxy = opts.UseProxy && opts.Proxy is not null, |
| | 0 | 69 | | Proxy = opts.Proxy, |
| | 0 | 70 | | |
| | 0 | 71 | | // Server auth |
| | 0 | 72 | | Credentials = opts.UseDefaultCredentials |
| | 0 | 73 | | ? CredentialCache.DefaultCredentials |
| | 0 | 74 | | : opts.Credentials |
| | 0 | 75 | | }; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Creates an HttpClient with the specified handler, base address, and timeout. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="handler">The HTTP message handler.</param> |
| | | 82 | | /// <param name="baseAddress">The base address for the HTTP client.</param> |
| | | 83 | | /// <param name="timeout">The timeout for HTTP requests.</param> |
| | | 84 | | /// <returns>A configured HttpClient instance.</returns> |
| | | 85 | | private static HttpClient MakeClient(HttpMessageHandler handler, Uri baseAddress, TimeSpan timeout) |
| | 0 | 86 | | => new(handler) |
| | 0 | 87 | | { |
| | 0 | 88 | | Timeout = (timeout <= TimeSpan.Zero) ? TimeSpan.FromSeconds(100) : timeout, |
| | 0 | 89 | | BaseAddress = baseAddress |
| | 0 | 90 | | }; |
| | | 91 | | |
| | | 92 | | // ---- Named Pipe --------------------------------------------------------- |
| | | 93 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe.</summary> |
| | | 94 | | public static HttpClient CreateNamedPipeClient(string pipeName, TimeSpan timeout) |
| | 0 | 95 | | => CreateNamedPipeClient(pipeName, timeout, ignoreCertErrors: false); |
| | | 96 | | |
| | | 97 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe (legacy overload).</summary> |
| | | 98 | | public static HttpClient CreateNamedPipeClient(string pipeName, TimeSpan timeout, bool ignoreCertErrors) |
| | | 99 | | { |
| | 0 | 100 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| | 0 | 101 | | return CreateNamedPipeClient(pipeName, opts); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Create an HttpClient that talks HTTP over a Windows Named Pipe (full options).</summary> |
| | | 105 | | public static HttpClient CreateNamedPipeClient(string pipeName, KrHttpClientOptions opts) |
| | | 106 | | { |
| | 0 | 107 | | if (string.IsNullOrWhiteSpace(pipeName)) |
| | | 108 | | { |
| | 0 | 109 | | throw new ArgumentNullException(nameof(pipeName)); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | var h = CreateHandler(opts); |
| | | 113 | | |
| | | 114 | | // capture pipeName in the lambda (works on .NET 6/7/8) |
| | 0 | 115 | | h.ConnectCallback = (ctx, ct) => |
| | 0 | 116 | | { |
| | 0 | 117 | | var stream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); |
| | 0 | 118 | | stream.Connect(); |
| | 0 | 119 | | return new ValueTask<Stream>(stream); |
| | 0 | 120 | | }; |
| | | 121 | | |
| | 0 | 122 | | return MakeClient(h, new Uri("http://localhost"), opts.Timeout); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | // ---- Unix Domain Socket ------------------------------------------------- |
| | | 126 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket.</summary> |
| | | 127 | | public static HttpClient CreateUnixSocketClient(string socketPath, TimeSpan timeout) |
| | 0 | 128 | | => CreateUnixSocketClient(socketPath, timeout, ignoreCertErrors: false); |
| | | 129 | | |
| | | 130 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket (legacy overload).</summary> |
| | | 131 | | public static HttpClient CreateUnixSocketClient(string socketPath, TimeSpan timeout, bool ignoreCertErrors) |
| | | 132 | | { |
| | 0 | 133 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| | 0 | 134 | | return CreateUnixSocketClient(socketPath, opts); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary>Create an HttpClient that talks HTTP over a Unix Domain Socket (full options).</summary> |
| | | 138 | | public static HttpClient CreateUnixSocketClient(string socketPath, KrHttpClientOptions opts) |
| | | 139 | | { |
| | 0 | 140 | | if (string.IsNullOrWhiteSpace(socketPath)) |
| | | 141 | | { |
| | 0 | 142 | | throw new ArgumentNullException(nameof(socketPath)); |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | var h = CreateHandler(opts); |
| | | 146 | | |
| | 0 | 147 | | h.ConnectCallback = (ctx, ct) => |
| | 0 | 148 | | { |
| | 0 | 149 | | var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); |
| | 0 | 150 | | sock.Connect(new UnixDomainSocketEndPoint(socketPath)); |
| | 0 | 151 | | return new ValueTask<Stream>(new NetworkStream(sock, ownsSocket: true)); |
| | 0 | 152 | | }; |
| | | 153 | | |
| | 0 | 154 | | return MakeClient(h, new Uri("http://localhost"), opts.Timeout); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // ---- TCP (HTTP/HTTPS) --------------------------------------------------- |
| | | 158 | | /// <summary>Classic TCP HttpClient (normal HTTP/S).</summary> |
| | | 159 | | public static HttpClient CreateTcpClient(Uri baseUri, TimeSpan timeout) |
| | 0 | 160 | | => CreateTcpClient(baseUri, timeout, ignoreCertErrors: false); |
| | | 161 | | |
| | | 162 | | /// <summary>Classic TCP HttpClient (normal HTTP/S, legacy overload).</summary> |
| | | 163 | | public static HttpClient CreateTcpClient(Uri baseUri, TimeSpan timeout, bool ignoreCertErrors) |
| | | 164 | | { |
| | 0 | 165 | | var opts = new KrHttpClientOptions { Timeout = timeout, IgnoreCertErrors = ignoreCertErrors }; |
| | 0 | 166 | | return CreateTcpClient(baseUri, opts); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary>Classic TCP HttpClient (normal HTTP/S, full options).</summary> |
| | | 170 | | public static HttpClient CreateTcpClient(Uri baseUri, KrHttpClientOptions opts) |
| | | 171 | | { |
| | 0 | 172 | | ArgumentNullException.ThrowIfNull(baseUri); |
| | 0 | 173 | | var h = CreateHandler(opts); |
| | 0 | 174 | | return MakeClient(h, baseUri, opts.Timeout); |
| | | 175 | | } |
| | | 176 | | } |