@@ -3,10 +3,11 @@ use core::future;
3
3
use core:: ptr:: NonNull ;
4
4
use std:: io;
5
5
6
+ use bytes:: Bytes ;
6
7
use http:: uri:: Scheme ;
7
8
use http:: { Request , Response } ;
8
9
use http_body:: Body ;
9
- use hyper :: body ;
10
+ use http_body_util :: BodyExt ;
10
11
use nginx_sys:: { ngx_log_t, ngx_resolver_t, NGX_LOG_WARN } ;
11
12
use ngx:: allocator:: Box ;
12
13
use ngx:: async_:: spawn;
@@ -17,6 +18,10 @@ use super::peer_conn::PeerConnection;
17
18
use super :: resolver:: Resolver ;
18
19
use crate :: conf:: ssl:: NgxSsl ;
19
20
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
+
20
25
const NGINX_VER : & str = match nginx_sys:: NGINX_VER . to_str ( ) {
21
26
Ok ( x) => x. trim_ascii ( ) ,
22
27
_ => unreachable ! ( ) ,
@@ -32,10 +37,9 @@ const NGX_ACME_USER_AGENT: &str = constcat::concat!(
32
37
33
38
#[ allow( async_fn_in_trait) ]
34
39
pub trait HttpClient {
35
- type Body : http_body:: Body < Error : StdError + Send + Sync > + ' static ;
36
40
type Error : StdError + Send + Sync + ' static ;
37
41
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 >
39
43
where
40
44
B : Body + Send + ' static ,
41
45
<B as Body >:: Data : Send ,
@@ -51,6 +55,8 @@ pub struct NgxHttpClient<'a> {
51
55
52
56
#[ derive( Debug , Error ) ]
53
57
pub enum HttpClientError {
58
+ #[ error( "response body read error: {0}" ) ]
59
+ Body ( std:: boxed:: Box < dyn StdError + Send + Sync > ) ,
54
60
#[ error( "request error: {0}" ) ]
55
61
Http ( #[ from] hyper:: Error ) ,
56
62
#[ error( "name resolution error: {0}" ) ]
@@ -88,10 +94,9 @@ impl<'a> NgxHttpClient<'a> {
88
94
}
89
95
90
96
impl HttpClient for NgxHttpClient < ' _ > {
91
- type Body = hyper:: body:: Incoming ;
92
97
type Error = HttpClientError ;
93
98
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 >
95
100
where
96
101
B : Body + Send + ' static ,
97
102
<B as Body >:: Data : Send ,
@@ -160,6 +165,14 @@ impl HttpClient for NgxHttpClient<'_> {
160
165
. detach ( ) ;
161
166
162
167
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) )
164
177
}
165
178
}
0 commit comments