This removes the `tcp` feature from hyper's `Cargo.toml`, and the code it enabled: - `HttpConnector` - `GaiResolver` - `AddrStream` And parts of `Client` and `Server` that used those types. Alternatives will be available in the `hyper-util` crate. Closes #2856 Co-authored-by: MrGunflame <mrgunflame@protonmail.com>
61 lines
1.6 KiB
Rust
61 lines
1.6 KiB
Rust
#![deny(warnings)]
|
|
|
|
use std::net::SocketAddr;
|
|
use std::task::{Context, Poll};
|
|
|
|
use futures_util::future;
|
|
use hyper::server::conn::Http;
|
|
use hyper::service::Service;
|
|
use hyper::{Body, Request, Response};
|
|
use tokio::net::TcpListener;
|
|
|
|
const ROOT: &str = "/";
|
|
|
|
#[derive(Debug)]
|
|
pub struct Svc;
|
|
|
|
impl Service<Request<Body>> for Svc {
|
|
type Response = Response<Body>;
|
|
type Error = hyper::Error;
|
|
type Future = future::Ready<Result<Self::Response, Self::Error>>;
|
|
|
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
Ok(()).into()
|
|
}
|
|
|
|
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
|
let rsp = Response::builder();
|
|
|
|
let uri = req.uri();
|
|
if uri.path() != ROOT {
|
|
let body = Body::from(Vec::new());
|
|
let rsp = rsp.status(404).body(body).unwrap();
|
|
return future::ok(rsp);
|
|
}
|
|
|
|
let body = Body::from(Vec::from(&b"heyo!"[..]));
|
|
let rsp = rsp.status(200).body(body).unwrap();
|
|
future::ok(rsp)
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
pretty_env_logger::init();
|
|
|
|
let addr: SocketAddr = "127.0.0.1:1337".parse().unwrap();
|
|
|
|
let listener = TcpListener::bind(addr).await?;
|
|
println!("Listening on http://{}", addr);
|
|
|
|
loop {
|
|
let (stream, _) = listener.accept().await?;
|
|
|
|
tokio::task::spawn(async move {
|
|
if let Err(err) = Http::new().serve_connection(stream, Svc).await {
|
|
println!("Failed to serve connection: {:?}", err);
|
|
}
|
|
});
|
|
}
|
|
}
|