6 releases (breaking)
0.5.0 | Jun 5, 2024 |
---|---|
0.4.0 | Aug 26, 2023 |
0.3.1 | Jun 6, 2023 |
0.2.0 | Jun 3, 2023 |
0.1.0 | Jun 1, 2023 |
#18 in #http2
337 downloads per month
22KB
370 lines
h2x
h2x
provides a wrapper around the h2 crate, offering additional functionality and utility functions for working with the HTTP/2 protocol.
It aims to simplify the usage of the h2
crate and provide a more ergonomic API for building HTTP/2 servers.
Goals
- Managing TCP connections
- TLS
If you only need HTTP/2 server and can't sacrifice any overhead this library is for you.
Getting Started
To use h2x
in your Rust project, add it as a dependency in your Cargo.toml
file:
[dependencies]
h2x = "0.4"
Example
You can run this example with: cargo run --example hello_world
use h2x::*;
use http::{Method, StatusCode};
use std::{io, net::SocketAddr};
#[derive(Clone)]
struct Service {
addr: SocketAddr,
}
impl Incoming for Service {
async fn stream(self, req: Request, mut res: Response) {
println!("From: {} at {}", self.addr, req.uri.path());
let _ = match (&req.method, req.uri.path()) {
(&Method::GET, "/") => res.write("<H1>Hello, World</H1>").await,
_ => {
res.status = StatusCode::NOT_FOUND;
res.write(format!("{req:#?}\n")).await
}
};
}
async fn close(self) {
println!("[{}] CONNECTION CLOSE", self.addr)
}
}
#[tokio::main]
async fn main() -> io::Result<()> {
let conf = Server::config("examples/key.pem", "examples/cert.pem")?;
let server = Server::bind("127.0.0.1:4433", conf).await?;
println!("Goto: https://{}", server.local_addr()?);
loop {
if let Ok((conn, addr)) = server.accept().await {
println!("[{}] NEW CONNECTION", addr);
conn.incoming(Service { addr });
}
}
}
For more examples, see ./examples directory.
Dependencies
~12–21MB
~383K SLoC