Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
  • Loading branch information
Licenser committed May 23, 2023
1 parent 9747bc1 commit 9479d02
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
9 changes: 3 additions & 6 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -50,7 +47,7 @@ async fn run_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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);
Expand Down
9 changes: 5 additions & 4 deletions src/acceptor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
12 changes: 3 additions & 9 deletions src/acceptor/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct WantsTlsConfig(());

impl AcceptorBuilder<WantsTlsConfig> {
/// Creates a new [`AcceptorBuilder`]
pub fn new() -> Self {
pub(super) fn new() -> Self {
Self(WantsTlsConfig(()))
}

Expand Down Expand Up @@ -42,12 +42,6 @@ impl AcceptorBuilder<WantsTlsConfig> {
}
}

impl Default for AcceptorBuilder<WantsTlsConfig> {
fn default() -> Self {
Self::new()
}
}

/// State of a builder that needs a incoming address next
pub struct WantsAlpn(ServerConfig);

Expand All @@ -68,13 +62,13 @@ impl AcceptorBuilder<WantsAlpn> {
}

/// Configure ALPN to accep HTTP/1.1
pub fn with_http11_alpn(mut self) -> AcceptorBuilder<WantsIncoming> {
pub fn with_http1_alpn(mut self) -> AcceptorBuilder<WantsIncoming> {
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<WantsIncoming> {
pub fn with_all_versions_alpn(mut self) -> AcceptorBuilder<WantsIncoming> {
self.0 .0.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
AcceptorBuilder(WantsIncoming(self.0 .0))
}
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit 9479d02

Please sign in to comment.