Crate hyper_rustls

source ·
Expand description

§hyper-rustls

A pure-Rust HTTPS connector for hyper, based on Rustls.

§Example client

use hyper::{Body, Client, StatusCode, Uri};

let mut rt = tokio::runtime::Runtime::new().unwrap();
let url = ("https://hyper.rs").parse().unwrap();
let https = hyper_rustls::HttpsConnectorBuilder::new()
    .with_native_roots()
    .expect("no native root CA certificates found")
    .https_only()
    .enable_http1()
    .build();

let client: Client<_, hyper::Body> = Client::builder().build(https);

let res = rt.block_on(client.get(url)).unwrap();
assert_eq!(res.status(), StatusCode::OK);

§Example server

use std::io;
use std::fs::File;

use hyper::server::conn::AddrIncoming;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use hyper_rustls::TlsAcceptor;
use pki_types::{CertificateDer, PrivateKeyDer};

let mut rt = tokio::runtime::Runtime::new().unwrap();
let addr = "127.0.0.1:1337".parse().unwrap();

// Load public certificate.
let certfile = File::open("examples/sample.pem").unwrap();
let mut reader = io::BufReader::new(certfile);

// Load and return certificate.
let certs = rustls_pemfile::certs(&mut reader).collect::<Result<Vec<_>, _>>().unwrap();

// Load private key. (see `examples/server.rs`)
let keyfile = File::open("examples/sample.rsa").unwrap();
let mut reader = io::BufReader::new(keyfile);

// Load and return a single private key.
let key = rustls_pemfile::private_key(&mut reader).unwrap().unwrap();
let https = hyper_rustls::HttpsConnectorBuilder::new()
    .with_native_roots()
    .expect("no native root CA certificates found")
    .https_only()
    .enable_http1()
    .build();

let incoming = AddrIncoming::bind(&addr).unwrap();
let acceptor = TlsAcceptor::builder()
    .with_single_cert(certs, key.into()).unwrap()
    .with_all_versions_alpn()
    .with_incoming(incoming);
let service = make_service_fn(|_| async { Ok::<_, io::Error>(service_fn(|_req|async {Ok::<_, io::Error>(Response::new(Body::empty()))})) });
let server = Server::builder(acceptor).serve(service);
// server.await.unwrap();

Re-exports§

Modules§

Structs§

Enums§

Traits§