#hyper-server #https-server #https #hyper #tls #tls-server #rustls

flexible-hyper-server-tls

Easily choose between HTTP or HTTPS when using hyper

9 releases (5 breaking)

Uses new Rust 2024

new 0.6.0 Mar 10, 2025
0.5.0 Oct 21, 2024
0.4.0 Sep 4, 2024
0.3.0 Mar 15, 2024
0.1.1 Jul 26, 2023

#1148 in Network programming

Download history 276/week @ 2024-11-21 203/week @ 2024-11-28 225/week @ 2024-12-05 186/week @ 2024-12-12 237/week @ 2024-12-19 71/week @ 2024-12-26 45/week @ 2025-01-02 216/week @ 2025-01-09 116/week @ 2025-01-16 137/week @ 2025-01-23 29/week @ 2025-01-30 6/week @ 2025-02-06 34/week @ 2025-02-13 15/week @ 2025-02-20 12/week @ 2025-02-27 614/week @ 2025-03-06

675 downloads per month

MIT/Apache

16KB
182 lines

Flexible Hyper Server TLS

A library that lets you choose whether to accept HTTPS or HTTP connections with Hyper. Very useful for situations where applications are self-hosted and the user gets to optionally provide their own HTTPS certificates.

This library also provides some helper functions that simplify the TLS setup by using the safe defaults from Rustls.

The aim of this library is to be simple and have minimal extra dependencies, while still allowing the user to customize things like TLS config.

For situations where you don't need to choose between HTTP and HTTPS, check out simple-hyper-server-tls or tls-listener.

Usage

use flexible_hyper_server_tls::*;
use http_body_util::Full;
use hyper::body::{Bytes, Incoming};
use hyper::service::service_fn;
use hyper::{Request, Response};
use std::convert::Infallible;
use tokio::net::TcpListener;

async fn hello_world(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
    Ok(Response::new(Full::<Bytes>::from("Hello, World!")))
}

#[tokio::main]
async fn main() {
    let use_tls = true;

    let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();

    let mut acceptor = HttpOrHttpsAcceptor::new(listener)
        .with_err_handler(|err| eprintln!("Error serving connection: {err:?}"));

    if use_tls {
        let tls = rustls_helpers::get_tlsacceptor_from_files("./cert.cer", "./key.pem").unwrap();
        acceptor = acceptor.with_tls(tls);
    }

    loop {
        acceptor.accept(service_fn(hello_world)).await;
    }
}

Dependencies

~12–29MB
~619K SLoC