private Http2LoopbackServer(Http2Options options) { _options = options; _listenSocket = new Socket(_options.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _listenSocket.Bind(new IPEndPoint(_options.Address, 0)); _listenSocket.Listen(_options.ListenBacklog); }
private static Http2Options CreateOptions(GenericLoopbackOptions options) { Http2Options http2Options = new Http2Options(); if (options != null) { http2Options.Address = options.Address; http2Options.UseSsl = options.UseSsl; http2Options.SslProtocols = options.SslProtocols; } return(http2Options); }
public override GenericLoopbackServer CreateServer(GenericLoopbackOptions options = null) { Http2Options http2Options = new Http2Options(); if (options != null) { http2Options.Address = options.Address; http2Options.UseSsl = options.UseSsl; http2Options.SslProtocols = options.SslProtocols; } return(Http2LoopbackServer.CreateServer(http2Options)); }
public Http2LoopbackConnection(Socket socket, Http2Options httpOptions) { _connectionSocket = socket; _connectionStream = new NetworkStream(_connectionSocket, true); if (httpOptions.UseSsl) { var sslStream = new SslStream(_connectionStream, false, delegate { return(true); }); using (var cert = Configuration.Certificates.GetServerCertificate()) { #if !NETFRAMEWORK SslServerAuthenticationOptions options = new SslServerAuthenticationOptions(); options.EnabledSslProtocols = httpOptions.SslProtocols; var protocols = new List <SslApplicationProtocol>(); protocols.Add(SslApplicationProtocol.Http2); protocols.Add(SslApplicationProtocol.Http11); options.ApplicationProtocols = protocols; options.ServerCertificate = cert; options.ClientCertificateRequired = httpOptions.ClientCertificateRequired; sslStream.AuthenticateAsServerAsync(options, CancellationToken.None).Wait(); #else sslStream.AuthenticateAsServerAsync(cert, httpOptions.ClientCertificateRequired, httpOptions.SslProtocols, checkCertificateRevocation: false).Wait(); #endif } _connectionStream = sslStream; } _prefix = new byte[24]; if (!FillBufferAsync(_prefix).Result) { throw new Exception("Connection stream closed while attempting to read connection preface."); } else if (Text.Encoding.ASCII.GetString(_prefix).Contains("HTTP/1.1")) { throw new Exception("HTTP 1.1 request received."); } }
public Http2LoopbackConnection(Socket socket, Http2Options httpOptions) { _connectionSocket = socket; _connectionStream = new NetworkStream(_connectionSocket, true); if (httpOptions.UseSsl) { var sslStream = new SslStream(_connectionStream, false, delegate { return(true); }); using (var cert = Configuration.Certificates.GetServerCertificate()) { SslServerAuthenticationOptions options = new SslServerAuthenticationOptions(); options.EnabledSslProtocols = httpOptions.SslProtocols; var protocols = new List <SslApplicationProtocol>(); protocols.Add(SslApplicationProtocol.Http2); protocols.Add(SslApplicationProtocol.Http11); options.ApplicationProtocols = protocols; options.ServerCertificate = cert; options.ClientCertificateRequired = false; sslStream.AuthenticateAsServerAsync(options, CancellationToken.None).Wait(); } _connectionStream = sslStream; } _prefix = new byte[24]; if (!FillBufferAsync(_prefix).Result) { throw new Exception("Connection stream closed while attempting to read connection preface."); } }
public static Http2LoopbackServer CreateServer(Http2Options options) { return(new Http2LoopbackServer(options)); }
public static async Task <Http2LoopbackConnection> CreateAsync(SocketWrapper socket, Stream stream, Http2Options httpOptions, TimeSpan timeout) { if (httpOptions.UseSsl) { var sslStream = new SslStream(stream, false, delegate { return(true); }); using (X509Certificate2 cert = Configuration.Certificates.GetServerCertificate()) { #if !NETFRAMEWORK SslServerAuthenticationOptions options = new SslServerAuthenticationOptions(); options.EnabledSslProtocols = httpOptions.SslProtocols; var protocols = new List <SslApplicationProtocol>(); protocols.Add(SslApplicationProtocol.Http2); options.ApplicationProtocols = protocols; options.ServerCertificate = cert; options.ClientCertificateRequired = httpOptions.ClientCertificateRequired; await sslStream.AuthenticateAsServerAsync(options, CancellationToken.None).ConfigureAwait(false); #else await sslStream.AuthenticateAsServerAsync(cert, httpOptions.ClientCertificateRequired, httpOptions.SslProtocols, checkCertificateRevocation : false).ConfigureAwait(false); #endif } stream = sslStream; } var con = new Http2LoopbackConnection(socket, stream, timeout, httpOptions.EnableTransparentPingResponse); await con.ReadPrefixAsync().ConfigureAwait(false); return(con); }
public static Task <Http2LoopbackConnection> CreateAsync(SocketWrapper socket, Stream stream, Http2Options httpOptions) { return(CreateAsync(socket, stream, httpOptions, Http2LoopbackServer.Timeout)); }
public static async Task CreateClientAndServerAsync(Func <Uri, Task> clientFunc, Func <Http2LoopbackServer, Task> serverFunc, Http2Options http2Options, int timeout = 60_000) { using (var server = Http2LoopbackServer.CreateServer(http2Options ?? new Http2Options())) { Task clientTask = clientFunc(server.Address); Task serverTask = serverFunc(server); await new Task[] { clientTask, serverTask }.WhenAllOrAnyFailed(timeout).ConfigureAwait(false); } }