diff --git a/examples/server.rs b/examples/server.rs index d838d7d..7c1a213 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -5,17 +5,14 @@ //! hyper will automatically use HTTP/2 if a client starts talking HTTP/2, //! otherwise HTTP/1.1 will be used. +#![cfg(feature = "acceptor")] + use hyper::server::conn::AddrIncoming; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Method, Request, Response, Server, StatusCode}; use std::vec::Vec; use std::{env, fs, io}; -#[cfg(not(feature = "acceptor"))] -fn main() { - println!("This example requires the `acceptor` feature"); -} -#[cfg(feature = "acceptor")] fn main() { // Serve an echo service over HTTPS, with proper error handling. if let Err(e) = run_server() { @@ -50,7 +47,7 @@ async fn run_server() -> Result<(), Box> { let acceptor = TlsAcceptor::builder() .with_single_cert(certs, key) .map_err(|e| error(format!("{}", e)))? - .with_http_alpn() + .with_all_versions_alpn() .with_incoming(incoming); let service = make_service_fn(|_| async { Ok::<_, io::Error>(service_fn(echo)) }); let server = Server::builder(acceptor).serve(service); diff --git a/src/acceptor.rs b/src/acceptor.rs index de4884d..edd47ae 100644 --- a/src/acceptor.rs +++ b/src/acceptor.rs @@ -1,14 +1,15 @@ use core::task::{Context, Poll}; +use std::future::Future; +use std::io; +use std::pin::Pin; +use std::sync::Arc; + use futures_util::ready; use hyper::server::{ accept::Accept, conn::{AddrIncoming, AddrStream}, }; use rustls::ServerConfig; -use std::future::Future; -use std::io; -use std::pin::Pin; -use std::sync::Arc; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; mod builder; diff --git a/src/acceptor/builder.rs b/src/acceptor/builder.rs index 709ee47..f9a8e32 100644 --- a/src/acceptor/builder.rs +++ b/src/acceptor/builder.rs @@ -12,7 +12,7 @@ pub struct WantsTlsConfig(()); impl AcceptorBuilder { /// Creates a new [`AcceptorBuilder`] - pub fn new() -> Self { + pub(super) fn new() -> Self { Self(WantsTlsConfig(())) } @@ -42,12 +42,6 @@ impl AcceptorBuilder { } } -impl Default for AcceptorBuilder { - fn default() -> Self { - Self::new() - } -} - /// State of a builder that needs a incoming address next pub struct WantsAlpn(ServerConfig); @@ -68,13 +62,13 @@ impl AcceptorBuilder { } /// Configure ALPN to accep HTTP/1.1 - pub fn with_http11_alpn(mut self) -> AcceptorBuilder { + pub fn with_http1_alpn(mut self) -> AcceptorBuilder { self.0 .0.alpn_protocols = vec![b"http/1.1".to_vec()]; AcceptorBuilder(WantsIncoming(self.0 .0)) } /// Configure ALPN to accept HTTP/2, HTTP/1.1 in that order. - pub fn with_http_alpn(mut self) -> AcceptorBuilder { + pub fn with_all_versions_alpn(mut self) -> AcceptorBuilder { self.0 .0.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; AcceptorBuilder(WantsIncoming(self.0 .0)) } diff --git a/src/lib.rs b/src/lib.rs index 4392d9b..45666f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,15 +41,23 @@ //! //! 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).unwrap(); //! let certs =certs.into_iter().map(rustls::Certificate).collect(); +//! +//! //! // 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 keys = rustls_pemfile::rsa_private_keys(&mut reader).unwrap(); //! let key = rustls::PrivateKey(keys[0].clone()); @@ -62,7 +70,7 @@ //! let incoming = AddrIncoming::bind(&addr).unwrap(); //! let acceptor = TlsAcceptor::builder() //! .with_single_cert(certs, key).unwrap() -//! .with_http_alpn() +//! .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);