/// <summary> /// Creates a <see cref="IHttpClientContext"/> that handles a connected client. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <returns> /// A creates <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateContext(Socket socket) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint; return(CreateContext(false, remoteEndPoint, networkStream, socket)); }
/// <summary> /// Create a secure <see cref="IHttpClientContext"/>. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <param name="certificate">HTTPS certificate to use.</param> /// <param name="protocol">Kind of HTTPS protocol. Usually TLS or SSL.</param> /// <returns> /// A created <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateSecureContext(Socket socket, X509Certificate certificate, SslProtocols protocol) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint; var sslStream = new SslStream(networkStream, false); try { //TODO: this may fail sslStream.AuthenticateAsServer(certificate, false, protocol, false); return(CreateContext(true, remoteEndPoint, sslStream, socket)); } catch (IOException err) { if (UseTraceLogs) { _logWriter.Write(this, LogPrio.Trace, err.Message); } } catch (ObjectDisposedException err) { if (UseTraceLogs) { _logWriter.Write(this, LogPrio.Trace, err.Message); } } return(null); }
/// <summary> /// Create a secure <see cref="IHttpClientContext"/>. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <param name="certificate">HTTPS certificate to use.</param> /// <param name="protocol">Kind of HTTPS protocol. Usually TLS or SSL.</param> /// <param name="requireClientCert">Require an SSL client certificate</param> /// <returns> /// A created <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateSecureContext(Socket socket, X509Certificate certificate, SslProtocols protocol, bool requireClientCert) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint; ClientCertificate clientCertificate = null; SslStream sslStream = new SslStream(networkStream, false, delegate(object sender, X509Certificate receivedCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { clientCertificate = new ClientCertificate(receivedCertificate, chain, sslPolicyErrors); return(!(requireClientCert && receivedCertificate == null)); } ); try { sslStream.AuthenticateAsServer(certificate, requireClientCert, protocol, false); return(CreateContext(true, remoteEndPoint, clientCertificate, sslStream, socket)); } catch (IOException err) { if (UseTraceLogs) { _logWriter.Write(this, LogPrio.Trace, err.Message); } } catch (ObjectDisposedException err) { if (UseTraceLogs) { _logWriter.Write(this, LogPrio.Trace, err.Message); } } catch (AuthenticationException err) { _logWriter.Write(this, LogPrio.Warning, (err.InnerException != null) ? err.InnerException.Message : err.Message); } return(null); }
/// <summary> /// Disconnect from client /// </summary> /// <param name="error">error to report in the <see cref="Disconnected"/> event.</param> public void Disconnect(SocketError error) { // Disconnect may not throw any exceptions try { if (error == SocketError.Success) { ReusableSocketNetworkStream reusableSocketNetworkStream = Stream as ReusableSocketNetworkStream; if (reusableSocketNetworkStream != null) { reusableSocketNetworkStream.Flush(); } } Disconnected(this, new DisconnectedEventArgs(error)); } catch (Exception err) { LogWriter.Write(this, LogPrio.Error, "Disconnect threw an exception: " + err); } }
/// <summary> /// Creates a <see cref="IHttpClientContext"/> that handles a connected client. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <returns> /// A creates <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateContext(Socket socket) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint) socket.RemoteEndPoint; return CreateContext(false, remoteEndPoint, networkStream, socket); }
/// <summary> /// Create a secure <see cref="IHttpClientContext"/>. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <param name="certificate">HTTPS certificate to use.</param> /// <param name="protocol">Kind of HTTPS protocol. Usually TLS or SSL.</param> /// <returns> /// A created <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateSecureContext(Socket socket, X509Certificate certificate, SslProtocols protocol) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint) socket.RemoteEndPoint; var sslStream = new SslStream(networkStream, false); try { //TODO: this may fail sslStream.AuthenticateAsServer(certificate, false, protocol, false); return CreateContext(true, remoteEndPoint, sslStream, socket); } catch (IOException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } catch (ObjectDisposedException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } return null; }
/// <summary> /// Create a secure <see cref="IHttpClientContext"/>. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <param name="certificate">HTTPS certificate to use.</param> /// <param name="protocol">Kind of HTTPS protocol. Usually TLS or SSL.</param> /// <param name="requireClientCert">Require an SSL client certificate</param> /// <returns> /// A created <see cref="IHttpClientContext"/>. /// </returns> public IHttpClientContext CreateSecureContext(Socket socket, X509Certificate certificate, SslProtocols protocol, bool requireClientCert) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint) socket.RemoteEndPoint; ClientCertificate clientCertificate = null; SslStream sslStream = new SslStream(networkStream, false, delegate(object sender, X509Certificate receivedCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { clientCertificate = new ClientCertificate(receivedCertificate, chain, sslPolicyErrors); return !(requireClientCert && receivedCertificate == null); } ); try { sslStream.AuthenticateAsServer(certificate, requireClientCert, protocol, false); return CreateContext(true, remoteEndPoint, clientCertificate, sslStream); } catch (IOException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } catch (ObjectDisposedException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } catch (AuthenticationException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Warning, (err.InnerException != null) ? err.InnerException.Message : err.Message); } return null; }
/// <summary> /// Create a secure <see cref="IHttpClientContext"/>. /// </summary> /// <param name="socket">Client socket (accepted by the <see cref="HttpListener"/>).</param> /// <param name="certificate">HTTPS certificate to use.</param> /// <param name="protocol">Kind of HTTPS protocol. Usually TLS or SSL.</param> /// <returns> /// A created <see cref="IHttpClientContext"/>. /// </returns> public HttpClientContext CreateSecureContext(Socket socket, X509Certificate certificate, SslProtocols protocol) { var networkStream = new ReusableSocketNetworkStream(socket, true); var remoteEndPoint = (IPEndPoint) socket.RemoteEndPoint; // by Fumi.Iseki //var sslStream = new SslStream(networkStream, false); SslStream sslStream = null; try { //TODO: this may fail // bu Fumi.Iseki //sslStream.AuthenticateAsServer(certificate, false, protocol, false); if (_clientCallback == null) { sslStream = new SslStream(networkStream, false); sslStream.AuthenticateAsServer(certificate, false, protocol, false); } else { sslStream = new SslStream(networkStream, false, new RemoteCertificateValidationCallback(_clientCallback)); sslStream.AuthenticateAsServer(certificate, true, protocol, false); } return CreateContext(true, remoteEndPoint, sslStream, socket); } catch (IOException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } catch (ObjectDisposedException err) { if (UseTraceLogs) _logWriter.Write(this, LogPrio.Trace, err.Message); } return null; }