hyper_rustls/lib.rs
1//! # hyper-rustls
2//!
3//! A pure-Rust HTTPS connector for [hyper](https://hyper.rs), based on
4//! [Rustls](https://github.com/rustls/rustls).
5//!
6//! ## Example client
7//!
8//! ```no_run
9//! # #[cfg(all(feature = "rustls-native-certs", feature = "tokio-runtime", feature = "http1"))]
10//! # fn main() {
11//! use hyper::{Body, Client, StatusCode, Uri};
12//!
13//! let mut rt = tokio::runtime::Runtime::new().unwrap();
14//! let url = ("https://hyper.rs").parse().unwrap();
15//! let https = hyper_rustls::HttpsConnectorBuilder::new()
16//! .with_native_roots()
17//! .https_only()
18//! .enable_http1()
19//! .build();
20//!
21//! let client: Client<_, hyper::Body> = Client::builder().build(https);
22//!
23//! let res = rt.block_on(client.get(url)).unwrap();
24//! assert_eq!(res.status(), StatusCode::OK);
25//! # }
26//! # #[cfg(not(all(feature = "rustls-native-certs", feature = "tokio-runtime", feature = "http1")))]
27//! # fn main() {}
28//! ```
29//!
30//! ## Example server
31//!
32//! ```no_run
33//! # #[cfg(all(feature = "rustls-native-certs", feature = "tokio-runtime", feature = "http1", feature = "acceptor"))]
34//! # fn main() {
35//! use hyper::server::conn::AddrIncoming;
36//! use hyper::service::{make_service_fn, service_fn};
37//! use hyper::{Body, Method, Request, Response, Server, StatusCode};
38//! use hyper_rustls::TlsAcceptor;
39//! use std::io;
40//! use std::fs::File;
41//!
42//! let mut rt = tokio::runtime::Runtime::new().unwrap();
43//! let addr = "127.0.0.1:1337".parse().unwrap();
44//!
45//! // Load public certificate.
46//! let certfile = File::open("examples/sample.pem").unwrap();
47//! let mut reader = io::BufReader::new(certfile);
48//!
49//! // Load and return certificate.
50//! let certs = rustls_pemfile::certs(&mut reader).unwrap();
51//! let certs = certs.into_iter().map(rustls::Certificate).collect();
52//!
53//! // Load private key. (see `examples/server.rs`)
54//! let keyfile = File::open("examples/sample.rsa").unwrap();
55//! let mut reader = io::BufReader::new(keyfile);
56//!
57//! // Load and return a single private key.
58//! let keys = rustls_pemfile::rsa_private_keys(&mut reader).unwrap();
59//! let key = rustls::PrivateKey(keys[0].clone());
60//! let https = hyper_rustls::HttpsConnectorBuilder::new()
61//! .with_native_roots()
62//! .https_only()
63//! .enable_http1()
64//! .build();
65//!
66//! let incoming = AddrIncoming::bind(&addr).unwrap();
67//! let acceptor = TlsAcceptor::builder()
68//! .with_single_cert(certs, key).unwrap()
69//! .with_all_versions_alpn()
70//! .with_incoming(incoming);
71//! let service = make_service_fn(|_| async { Ok::<_, io::Error>(service_fn(|_req|async {Ok::<_, io::Error>(Response::new(Body::empty()))})) });
72//! let server = Server::builder(acceptor).serve(service);
73//! // server.await.unwrap();
74//! # }
75//! # #[cfg(not(all(feature = "rustls-native-certs", feature = "tokio-runtime", feature = "http1")))]
76//! # fn main() {}
77//! ```
78
79#![warn(missing_docs, unreachable_pub, clippy::use_self)]
80#![cfg_attr(docsrs, feature(doc_cfg))]
81
82#[cfg(feature = "acceptor")]
83/// TLS acceptor implementing hyper's `Accept` trait.
84pub mod acceptor;
85mod config;
86mod connector;
87mod stream;
88
89#[cfg(feature = "logging")]
90mod log {
91 pub(crate) use log::{debug, trace};
92}
93
94#[cfg(not(feature = "logging"))]
95mod log {
96 macro_rules! trace ( ($($tt:tt)*) => {{}} );
97 macro_rules! debug ( ($($tt:tt)*) => {{}} );
98 pub(crate) use {debug, trace};
99}
100
101#[cfg(feature = "acceptor")]
102pub use crate::acceptor::{AcceptorBuilder, TlsAcceptor};
103pub use crate::config::ConfigBuilderExt;
104pub use crate::connector::builder::ConnectorBuilder as HttpsConnectorBuilder;
105pub use crate::connector::HttpsConnector;
106pub use crate::stream::MaybeHttpsStream;
107
108/// The various states of the [`HttpsConnectorBuilder`]
109pub mod builderstates {
110 #[cfg(feature = "http2")]
111 pub use crate::connector::builder::WantsProtocols3;
112 pub use crate::connector::builder::{
113 WantsProtocols1, WantsProtocols2, WantsSchemes, WantsTlsConfig,
114 };
115}