Skip to content

Commit

Permalink
Add HTTP/1 and HTTP/2 to axum::serve (#2241)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshith-ravi committed Oct 1, 2023
1 parent 17993c5 commit 8854e66
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 173 deletions.
5 changes: 3 additions & 2 deletions axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ matched-path = []
multipart = ["dep:multer"]
original-uri = []
query = ["dep:serde_urlencoded"]
tokio = ["dep:tokio", "hyper/server", "hyper/tcp", "hyper/runtime", "tower/make"]
tokio = ["dep:tokio", "dep:hyper-util", "hyper/server", "hyper/tcp", "hyper/runtime", "tower/make"]
tower-log = ["tower/log"]
tracing = ["dep:tracing", "axum-core/tracing"]
ws = ["tokio", "dep:tokio-tungstenite", "dep:sha1", "dep:base64"]
Expand Down Expand Up @@ -51,12 +51,13 @@ tower-layer = "0.3.2"
tower-service = "0.3"

# wont need this when axum uses http-body 1.0
hyper1 = { package = "hyper", version = "=1.0.0-rc.4", features = ["server", "http1"] }
hyper1 = { package = "hyper", version = "=1.0.0-rc.4", features = ["server", "http1", "http2"] }
tower-hyper-http-body-compat = { version = "0.2", features = ["server", "http1"] }

# optional dependencies
axum-macros = { path = "../axum-macros", version = "0.3.7", optional = true }
base64 = { version = "0.21.0", optional = true }
hyper-util = { git = "https://github.com/hyperium/hyper-util", rev = "d97181a", features = ["auto"], optional = true }
multer = { version = "2.0.0", optional = true }
serde_json = { version = "1.0", features = ["raw_value"], optional = true }
serde_path_to_error = { version = "0.1.8", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion axum/src/extract/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

use self::rejection::*;
use super::FromRequestParts;
use crate::{body::Bytes, hyper1_tokio_io::TokioIo, response::Response, Error};
use crate::{body::Bytes, response::Response, Error};
use async_trait::async_trait;
use axum_core::body::Body;
use futures_util::{
Expand All @@ -104,6 +104,7 @@ use http::{
request::Parts,
Method, StatusCode,
};
use hyper_util::rt::TokioIo;
use sha1::{Digest, Sha1};
use std::{
borrow::Cow,
Expand Down
161 changes: 0 additions & 161 deletions axum/src/hyper1_tokio_io.rs

This file was deleted.

2 changes: 0 additions & 2 deletions axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,6 @@ mod boxed;
mod extension;
#[cfg(feature = "form")]
mod form;
#[cfg(feature = "tokio")]
mod hyper1_tokio_io;
#[cfg(feature = "json")]
mod json;
mod service_ext;
Expand Down
15 changes: 8 additions & 7 deletions axum/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

use std::{convert::Infallible, io, net::SocketAddr};

use crate::hyper1_tokio_io::TokioIo;
use axum_core::{body::Body, extract::Request, response::Response};
use futures_util::{future::poll_fn, FutureExt};
use hyper1::server::conn::http1;
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto::Builder,
};
use tokio::net::{TcpListener, TcpStream};
use tower_hyper_http_body_compat::{HttpBody04ToHttpBody1, HttpBody1ToHttpBody04};
use tower_service::Service;
Expand All @@ -15,7 +17,7 @@ use tower_service::Service;
/// This method of running a service is intentionally simple and doesn't support any configuration.
/// Use hyper or hyper-util if you need configuration.
///
/// It only supports HTTP/1.
/// It supports both HTTP/1 as well as HTTP/2.
///
/// # Examples
///
Expand Down Expand Up @@ -138,10 +140,9 @@ where
});

tokio::task::spawn(async move {
match http1::Builder::new()
.serve_connection(tcp_stream, service)
// for websockets
.with_upgrades()
match Builder::new(TokioExecutor::new())
// upgrades needed for websockets
.serve_connection_with_upgrades(tcp_stream.into_inner(), service)
.await
{
Ok(()) => {}
Expand Down

0 comments on commit 8854e66

Please sign in to comment.