using System; using System.Collections.Generic; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using Best.HTTP.Hosts.Connections; using Best.HTTP.HostSetting; using Best.HTTP.Shared.Extensions; using Best.HTTP.Shared.Logger; using Best.HTTP.Shared.PlatformSupport.Network.Tcp; namespace Best.HTTP.Hosts.Settings { #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR) /// /// Delegate for creating a TLS 1.3 client instance. /// /// The URI of the request. /// A list of supported TLS ALPN protocols. /// The logging context for the operation. /// A TLS 1.3 client instance. public delegate Best.HTTP.Shared.TLS.AbstractTls13Client TlsClientFactoryDelegate(Uri uri, List protocols, LoggingContext context); #endif /// /// Settings for HTTP requests. /// public class HTTRequestSettings { /// /// The timeout for establishing a connection. /// public TimeSpan ConnectTimeout = TimeSpan.FromSeconds(20); /// /// The maximum time allowed for the request to complete. /// public TimeSpan RequestTimeout = TimeSpan.MaxValue; } /// /// Settings for HTTP/1 connections. /// public class HTTP1ConnectionSettings { /// /// Indicates whether the connection should be open after receiving the response. /// /// /// If set to true, internal TCP connections will be reused whenever possible. /// If making rare requests to the server, it's recommended to change this to false. /// public bool TryToReuseConnections = true; /// /// The maximum time a connection can remain idle before being closed. /// public TimeSpan MaxConnectionIdleTime = TimeSpan.FromSeconds(20); /// /// Indicates whether the upload thread should use a ThreadPool thread instead of creating and using a Thread. /// /// The plugin tries to use ThreadPool threads for known short-living uploads like requests without upload body. With ForceUseThreadPool all HTTP/1 requests, including long uploads or downloads can be forced to use ThreadPool threads. public bool ForceUseThreadPool; } #if !UNITY_WEBGL || UNITY_EDITOR /// /// Delegate for selecting a client certificate. /// /// The target host. /// A collection of local certificates. /// The remote certificate. /// An array of acceptable certificate issuers. /// The selected X.509 certificate. public delegate X509Certificate ClientCertificateSelector(string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers); /// /// Available TLS handlers. /// public enum TLSHandlers { #if !BESTHTTP_DISABLE_ALTERNATE_SSL /// /// To use the 3rd party BouncyCastle implementation. /// BouncyCastle = 0x00, #endif /// /// To use .net's SslStream. /// Framework = 0x01 } /// /// Settings for Bouncy Castle TLS. /// public class BouncyCastleSettings { #if !BESTHTTP_DISABLE_ALTERNATE_SSL /// /// Delegate for creating a TLS 1.3 client instance using Bouncy Castle. /// public TlsClientFactoryDelegate TlsClientFactory; /// /// The default TLS 1.3 client factory. /// /// The URI of the request. /// A list of supported TLS ALPN protocols. /// The logging context for the operation. /// A TLS 1.3 client instance. public static Best.HTTP.Shared.TLS.AbstractTls13Client DefaultTlsClientFactory(Uri uri, List protocols, LoggingContext context) { // http://tools.ietf.org/html/rfc3546#section-3.1 // -It is RECOMMENDED that clients include an extension of type "server_name" in the client hello whenever they locate a server by a supported name type. // -Literal IPv4 and IPv6 addresses are not permitted in "HostName". // User-defined list has a higher priority List hostNames = null; // If there's no user defined one and the host isn't an IP address, add the default one if (!uri.IsHostIsAnIPAddress()) { hostNames = new List(1); hostNames.Add(new Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.ServerName(0, System.Text.Encoding.UTF8.GetBytes(uri.Host))); } return new Best.HTTP.Shared.TLS.DefaultTls13Client(hostNames, protocols, context); } #endif } /// /// Settings for .NET's SslStream based handler. /// public class FrameworkTLSSettings { /// /// The supported TLS versions. /// public System.Security.Authentication.SslProtocols TlsVersions = System.Security.Authentication.SslProtocols.Tls12; /// /// Indicates whether to check certificate revocation. /// public bool CheckCertificateRevocation = true; /// /// The default certification validator. /// public static Func DefaultCertificationValidator = (host, certificate, chain, sslPolicyErrors) => true; public Func CertificationValidator = DefaultCertificationValidator; /// /// Delegate for providing a client certificate. /// public ClientCertificateSelector ClientCertificationProvider; } /// /// Settings for TLS. /// public class TLSSettings { /// /// The selected TLS handler. /// public TLSHandlers TLSHandler #if !BESTHTTP_DISABLE_ALTERNATE_SSL = TLSHandlers.BouncyCastle; #else = TLSHandlers.Framework; #endif /// /// Settings for Bouncy Castle. /// public BouncyCastleSettings BouncyCastleSettings = new BouncyCastleSettings(); /// /// .NET's SslStream settings. /// public FrameworkTLSSettings FrameworkTLSSettings = new FrameworkTLSSettings(); } #endif /// /// Settings for s. /// public class HostVariantSettings { /// /// The maximum number of connections allowed per host variant. /// public int MaxConnectionPerVariant = 6; /// /// Factor used when calculations are made whether to open a new connection to the server or not. /// /// /// It has an effect on HTTP/2 connections only. /// Higher values (gte 1.0f) delay, lower values (lte 1.0f) bring forward creation of new connections. /// public float MaxAssignedRequestsFactor = 1.2f; /// /// Factory function to generate HostVariant or descendent instances. /// public Func VariantFactory = (settings, key) => new HostVariant(key); /// /// Factory function to generate custom connection implementations. /// public Func ConnectionFactory; } /// /// Represents the low-level TCP buffer settings for connections. /// public class LowLevelConnectionSettings { /// /// Gets or sets the size of the TCP write buffer in bytes. /// /// /// Default value is 1 MiB. /// This determines the maximum amount of data that that the class can buffer up if it's already in a write operation. /// Increasing this value can potentially improve write performance, especially for large messages or data streams. /// However, setting it too high might consume a significant amount of memory, especially if there are many active connections. /// /// /// The size of the TCP write buffer in bytes. public uint TCPWriteBufferSize = 1024 * 1024; /// /// Gets or sets the size of the read buffer in bytes. /// /// The size of the read buffer in bytes. /// /// Default value is 1 MiB. /// This determines the maximum amount of data that low level streams and the can buffer up for consuming by higher level layers. /// Adjusting this value can affect the read performance of the application. /// Like the write buffer, setting this too high might be memory-intensive, especially with many connections. /// It's advised to find a balance that suits the application's needs and resources. /// /// public uint ReadBufferSize = 1024 * 1024; } /// /// Contains settings that can be associated with a specific host or host variant. /// public class HostSettings { /// /// Gets or sets the low-level TCP buffer settings for connections associated with the host or host variant. /// /// The low-level TCP buffer settings. /// /// These settings determine the buffer sizes for reading from and writing to TCP connections, /// which can impact performance and memory usage. /// public LowLevelConnectionSettings LowLevelConnectionSettings = new LowLevelConnectionSettings(); /// /// Settings related to HTTP requests made to this host or host variant. /// public HTTRequestSettings RequestSettings = new HTTRequestSettings(); /// /// Settings related to HTTP/1.x connection behavior. /// public HTTP1ConnectionSettings HTTP1ConnectionSettings = new HTTP1ConnectionSettings(); #if !UNITY_WEBGL || UNITY_EDITOR /// /// Settings related to TCP Ringmaster used in non-webgl platforms. /// public TCPRingmasterSettings TCPRingmasterSettings = new TCPRingmasterSettings(); #if !BESTHTTP_DISABLE_ALTERNATE_SSL /// /// Settings related to HTTP/2 connection behavior. /// public Best.HTTP.Hosts.Connections.HTTP2.HTTP2ConnectionSettings HTTP2ConnectionSettings = new Connections.HTTP2.HTTP2ConnectionSettings(); #endif /// /// Settings related to TLS (Transport Layer Security) behavior. /// public TLSSettings TLSSettings = new TLSSettings(); #endif /// /// Settings related to behavior. /// public HostVariantSettings HostVariantSettings = new HostVariantSettings(); } }