Skip to content

Commit ee88a99

Browse files
committed
fixup! Net: add a simple HTTP client implementation.
1 parent 86715c5 commit ee88a99

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/net/http.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use core::future;
33
use core::ptr::NonNull;
44
use std::io;
55

6+
use bytes::Bytes;
67
use http::uri::Scheme;
78
use http::{Request, Response};
89
use http_body::Body;
9-
use hyper::body;
10+
use http_body_util::BodyExt;
1011
use nginx_sys::{ngx_log_t, ngx_resolver_t, NGX_LOG_WARN};
1112
use ngx::allocator::Box;
1213
use ngx::async_::spawn;
@@ -17,6 +18,10 @@ use super::peer_conn::PeerConnection;
1718
use super::resolver::Resolver;
1819
use crate::conf::ssl::NgxSsl;
1920

21+
// The largest response we can reasonably expect is a certificate chain, which should not exceed
22+
// a few kilobytes.
23+
const NGX_ACME_MAX_BODY_SIZE: usize = 64 * 1024;
24+
2025
const NGINX_VER: &str = match nginx_sys::NGINX_VER.to_str() {
2126
Ok(x) => x.trim_ascii(),
2227
_ => unreachable!(),
@@ -32,10 +37,9 @@ const NGX_ACME_USER_AGENT: &str = constcat::concat!(
3237

3338
#[allow(async_fn_in_trait)]
3439
pub trait HttpClient {
35-
type Body: http_body::Body<Error: StdError + Send + Sync> + 'static;
3640
type Error: StdError + Send + Sync + 'static;
3741

38-
async fn request<B>(&self, req: Request<B>) -> Result<Response<Self::Body>, Self::Error>
42+
async fn request<B>(&self, req: Request<B>) -> Result<Response<Bytes>, Self::Error>
3943
where
4044
B: Body + Send + 'static,
4145
<B as Body>::Data: Send,
@@ -51,6 +55,8 @@ pub struct NgxHttpClient<'a> {
5155

5256
#[derive(Debug, Error)]
5357
pub enum HttpClientError {
58+
#[error("response body read error: {0}")]
59+
Body(std::boxed::Box<dyn StdError + Send + Sync>),
5460
#[error("request error: {0}")]
5561
Http(#[from] hyper::Error),
5662
#[error("name resolution error: {0}")]
@@ -88,10 +94,9 @@ impl<'a> NgxHttpClient<'a> {
8894
}
8995

9096
impl HttpClient for NgxHttpClient<'_> {
91-
type Body = hyper::body::Incoming;
9297
type Error = HttpClientError;
9398

94-
async fn request<B>(&self, mut req: Request<B>) -> Result<Response<body::Incoming>, Self::Error>
99+
async fn request<B>(&self, mut req: Request<B>) -> Result<Response<Bytes>, Self::Error>
95100
where
96101
B: Body + Send + 'static,
97102
<B as Body>::Data: Send,
@@ -160,6 +165,14 @@ impl HttpClient for NgxHttpClient<'_> {
160165
.detach();
161166

162167
let resp = sender.send_request(req).await?;
163-
Ok(resp)
168+
let (parts, body) = resp.into_parts();
169+
170+
let body = http_body_util::Limited::new(body, NGX_ACME_MAX_BODY_SIZE)
171+
.collect()
172+
.await
173+
.map_err(HttpClientError::Body)?
174+
.to_bytes();
175+
176+
Ok(Response::from_parts(parts, body))
164177
}
165178
}

0 commit comments

Comments
 (0)