Skip to content

Commit

Permalink
Update to http 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Nov 15, 2023
1 parent 8746d3b commit 6942b8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ log = "0.4"
bytes = "1.0"
futures-util = { version = "0.3", default-features = false }
memchr = "2.4"
http = "0.2"
http = "1.0"
httparse = "1.3"
mime = "0.3"
encoding_rs = "0.8"
Expand All @@ -42,7 +42,9 @@ tokio-util = { version = "0.7", features = ["io"], optional = true }
[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["full"] }
hyper = { version = "0.14", features = ["server", "http1", "stream", "tcp"] }
hyper = { version = "1.0", features = ["server", "http1"] }
http-body-util = "0.1"
hyper-util = { git = "https://github.com/hyperium/hyper-util" }

[build-dependencies]
version_check = "0.9"
Expand Down
41 changes: 26 additions & 15 deletions examples/hyper_server_example.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{convert::Infallible, net::SocketAddr};

use hyper::server::Server;
use hyper::service::{make_service_fn, service_fn};
use hyper::{header::CONTENT_TYPE, Body, Request, Response, StatusCode};
use futures_util::StreamExt;
use bytes::Bytes;
use http_body_util::{BodyStream, Full};
use hyper::{body::Incoming, header::CONTENT_TYPE, Request, Response, StatusCode};
// Import the multer types.
use multer::Multipart;

// A handler for incoming requests.
async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
async fn handle(req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
// Extract the `multipart/form-data` boundary from the headers.
let boundary = req
.headers()
Expand All @@ -19,25 +20,29 @@ async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
if boundary.is_none() {
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("BAD REQUEST"))
.body(Full::from("BAD REQUEST"))
.unwrap());
}

// Process the multipart e.g. you can store them in files.
if let Err(err) = process_multipart(req.into_body(), boundary.unwrap()).await {
return Ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("INTERNAL SERVER ERROR: {}", err)))
.body(Full::from(format!("INTERNAL SERVER ERROR: {}", err)))
.unwrap());
}

Ok(Response::new(Body::from("Success")))
Ok(Response::new(Full::from("Success")))
}

// Process the request body as multipart/form-data.
async fn process_multipart(body: Body, boundary: String) -> multer::Result<()> {
async fn process_multipart(body: Incoming, boundary: String) -> multer::Result<()> {
// Convert the body into a stream of data frames.
let body_stream = BodyStream::new(body)
.filter_map(|result| async move { result.map(|frame| frame.into_data().ok()).transpose() });

// Create a Multipart instance from the request body.
let mut multipart = Multipart::new(body, boundary);
let mut multipart = Multipart::new(body_stream, boundary);

// Iterate over the fields, `next_field` method will return the next field if
// available.
Expand Down Expand Up @@ -72,13 +77,19 @@ async fn process_multipart(body: Body, boundary: String) -> multer::Result<()> {
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
println!("Server running at: {}", addr);

let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });

let server = Server::bind(&addr).serve(make_svc);
let service = hyper::service::service_fn(handle);

println!("Server running at: {}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
loop {
let (socket, _remote_addr) = listener.accept().await.unwrap();
let socket = hyper_util::rt::TokioIo::new(socket);
if let Err(e) = hyper::server::conn::http1::Builder::new()
.serve_connection(socket, service)
.await
{
eprintln!("server error: {}", e);
}
}
}

0 comments on commit 6942b8e

Please sign in to comment.