@@ -3,8 +3,10 @@ use std::net::SocketAddr;
33use std:: sync:: atomic:: Ordering ;
44use std:: sync:: Arc ;
55
6- use axum:: body:: { Body , Bytes } ;
7- use axum:: http:: { HeaderName , HeaderValue , Request , Response , StatusCode } ;
6+ use bytes:: Bytes ;
7+ use http:: { HeaderName , HeaderValue , Request , Response , StatusCode } ;
8+ use http_body_util:: combinators:: BoxBody ;
9+ use http_body_util:: { BodyExt , Empty , Full , Limited } ;
810use hyper_util:: rt:: { TokioExecutor , TokioIo , TokioTimer } ;
911use hyper_util:: server:: conn:: auto;
1012use hyper_util:: server:: graceful:: GracefulShutdown ;
@@ -13,9 +15,14 @@ use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
1315use tokio_rustls:: rustls:: ServerConfig ;
1416use tokio_rustls:: TlsAcceptor ;
1517
18+ use hyper:: body:: Incoming ;
19+
1620use crate :: config:: Config ;
1721use crate :: runtime:: { Runtime , SHUTDOWN } ;
1822
23+ /// One response body type covering both a buffered `Vec` and a live chunk channel.
24+ type Body = BoxBody < Bytes , std:: convert:: Infallible > ;
25+
1926static MAX_BODY : std:: sync:: atomic:: AtomicUsize = std:: sync:: atomic:: AtomicUsize :: new ( 0 ) ;
2027
2128pub struct Job {
@@ -108,7 +115,7 @@ pub fn bind(addr: SocketAddr, backlog: i32) -> std::io::Result<std::net::TcpList
108115 Ok ( socket. into ( ) )
109116}
110117
111- async fn dispatch ( peer : SocketAddr , req : Request < hyper :: body :: Incoming > ) -> Response < Body > {
118+ async fn dispatch ( peer : SocketAddr , req : Request < Incoming > ) -> Response < Body > {
112119 let ( parts, body) = req. into_parts ( ) ;
113120
114121 if parts. uri . path ( ) == "/__stoke_ping" {
@@ -120,8 +127,8 @@ async fn dispatch(peer: SocketAddr, req: Request<hyper::body::Incoming>) -> Resp
120127 }
121128
122129 let max_body = MAX_BODY . load ( Ordering :: Relaxed ) ;
123- let body = match axum :: body :: to_bytes ( Body :: new ( body) , max_body) . await {
124- Ok ( bytes ) => bytes ,
130+ let body = match Limited :: new ( body, max_body) . collect ( ) . await {
131+ Ok ( collected ) => collected . to_bytes ( ) ,
125132 Err ( _) => return render ( Reply :: plain ( 413 , "Content Too Large" ) ) ,
126133 } ;
127134
@@ -155,7 +162,7 @@ async fn dispatch(peer: SocketAddr, req: Request<hyper::body::Incoming>) -> Resp
155162 path,
156163 query,
157164 target,
158- version_minor : if parts. version == axum :: http:: Version :: HTTP_10 {
165+ version_minor : if parts. version == http:: Version :: HTTP_10 {
159166 0
160167 } else {
161168 1
@@ -215,16 +222,16 @@ fn render(reply: Reply) -> Response<Body> {
215222 }
216223
217224 let body = match reply. body {
218- ReplyBody :: Full ( bytes) => Body :: from ( bytes) ,
219- ReplyBody :: Stream ( chunks) => Body :: new ( ChannelBody { chunks } ) ,
225+ ReplyBody :: Full ( bytes) => Full :: new ( Bytes :: from ( bytes) ) . boxed ( ) ,
226+ ReplyBody :: Stream ( chunks) => ChannelBody { chunks } . boxed ( ) ,
220227 } ;
221228
222229 builder
223230 . body ( body)
224231 . unwrap_or_else ( |_| {
225232 Response :: builder ( )
226233 . status ( StatusCode :: INTERNAL_SERVER_ERROR )
227- . body ( Body :: empty ( ) )
234+ . body ( Empty :: new ( ) . boxed ( ) )
228235 . expect ( "fallback response" )
229236 } )
230237}
0 commit comments