Struct native_tls::TlsAcceptor [] [src]

pub struct TlsAcceptor(_);

A builder for server-side TLS connections.

Examples

use native_tls::{Pkcs12, TlsAcceptor, TlsStream};
use std::fs::File;
use std::io::{Read};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::thread;

let mut file = File::open("identity.pfx").unwrap();
let mut pkcs12 = vec![];
file.read_to_end(&mut pkcs12).unwrap();
let pkcs12 = Pkcs12::from_der(&pkcs12, "hunter2").unwrap();

let listener = TcpListener::bind("0.0.0.0:8443").unwrap();
let acceptor = TlsAcceptor::builder(pkcs12).unwrap().build().unwrap();
let acceptor = Arc::new(acceptor);

fn handle_client(stream: TlsStream<TcpStream>) {
    // ...
}

for stream in listener.incoming() {
    match stream {
        Ok(stream) => {
            let acceptor = acceptor.clone();
            thread::spawn(move || {
                let stream = acceptor.accept(stream).unwrap();
                handle_client(stream);
            });
        }
        Err(e) => { /* connection failed */ }
    }
}

Methods

impl TlsAcceptor
[src]

Returns a new builder for a TlsAcceptor.

This builder is created with a key/certificate pair in the pkcs12 archived passed in. The returned builder will use that key/certificate to send to clients which it connects to.

Initiates a TLS handshake.

If the socket is nonblocking and a WouldBlock error is returned during the handshake, a HandshakeError::Interrupted error will be returned which can be used to restart the handshake when the socket is ready again.