1
1
use core:: cell:: RefCell ;
2
- use core:: error:: Error as StdError ;
3
2
use core:: ptr:: NonNull ;
4
3
use core:: time:: Duration ;
5
4
use std:: collections:: VecDeque ;
@@ -9,8 +8,6 @@ use std::string::{String, ToString};
9
8
use anyhow:: { anyhow, Result } ;
10
9
use bytes:: Bytes ;
11
10
use http:: Uri ;
12
- use http_body:: Body ;
13
- use http_body_util:: { BodyExt , Empty , Full } ;
14
11
use ngx:: allocator:: { Allocator , Box } ;
15
12
use ngx:: async_:: sleep;
16
13
use ngx:: collections:: Vec ;
@@ -85,14 +82,6 @@ fn try_get_header<K: http::header::AsHeaderName>(
85
82
headers. get ( key) . and_then ( |x| x. to_str ( ) . ok ( ) )
86
83
}
87
84
88
- pub async fn collect_body < T > ( body : T ) -> Result < Bytes , T :: Error >
89
- where
90
- T : Body ,
91
- T :: Error : StdError + Send + Sync + ' static ,
92
- {
93
- Ok ( body. collect ( ) . await ?. to_bytes ( ) )
94
- }
95
-
96
85
impl < ' a , Http > AcmeClient < ' a , Http >
97
86
where
98
87
Http : HttpClient ,
@@ -129,8 +118,7 @@ where
129
118
130
119
async fn get_directory ( & mut self ) -> Result < types:: Directory > {
131
120
let res = self . get ( & self . issuer . uri ) . await ?;
132
- let bytes = collect_body ( res) . await ?;
133
- let directory = serde_json:: from_slice ( & bytes) ?;
121
+ let directory = serde_json:: from_slice ( res. body ( ) ) ?;
134
122
135
123
Ok ( directory)
136
124
}
@@ -163,8 +151,7 @@ where
163
151
. and_then ( parse_retry_after)
164
152
. unwrap_or ( DEFAULT_RETRY_INTERVAL ) ;
165
153
166
- let bytes = collect_body ( res) . await ?;
167
- let result = serde_json:: from_slice ( & bytes) ?;
154
+ let result = serde_json:: from_slice ( res. body ( ) ) ?;
168
155
169
156
if predicate ( & result) {
170
157
return Ok ( result) ;
@@ -185,24 +172,23 @@ where
185
172
payload : P ,
186
173
) -> Result < T > {
187
174
let res = self . post ( url, payload) . await ?;
188
- let bytes = collect_body ( res) . await ?;
189
- Ok ( serde_json:: from_slice ( & bytes) ?)
175
+ Ok ( serde_json:: from_slice ( res. body ( ) ) ?)
190
176
}
191
177
192
- pub async fn get ( & self , url : & Uri ) -> Result < http:: Response < Http :: Body > > {
178
+ pub async fn get ( & self , url : & Uri ) -> Result < http:: Response < Bytes > > {
193
179
let req = http:: Request :: builder ( )
194
180
. uri ( url)
195
181
. method ( http:: Method :: GET )
196
182
. header ( http:: header:: CONTENT_LENGTH , 0 )
197
- . body ( Empty :: < Bytes > :: new ( ) ) ?;
183
+ . body ( String :: new ( ) ) ?;
198
184
Ok ( self . http . request ( req) . await ?)
199
185
}
200
186
201
187
pub async fn post < P : AsRef < [ u8 ] > > (
202
188
& self ,
203
189
url : & Uri ,
204
190
payload : P ,
205
- ) -> Result < http:: Response < Http :: Body > > {
191
+ ) -> Result < http:: Response < Bytes > > {
206
192
let mut fails = 0 ;
207
193
208
194
let mut nonce = if let Some ( nonce) = self . nonce . get ( ) {
@@ -220,7 +206,6 @@ where
220
206
& nonce,
221
207
payload. as_ref ( ) ,
222
208
) ?;
223
- let body = Bytes :: from ( body) ;
224
209
let req = http:: Request :: builder ( )
225
210
. uri ( url)
226
211
. method ( http:: Method :: POST )
@@ -229,7 +214,7 @@ where
229
214
http:: header:: CONTENT_TYPE ,
230
215
http:: HeaderValue :: from_static ( "application/jose+json" ) ,
231
216
)
232
- . body ( Full :: new ( body) ) ?;
217
+ . body ( body) ?;
233
218
234
219
let res = match self . http . request ( req) . await {
235
220
Ok ( res) => res,
@@ -259,8 +244,7 @@ where
259
244
. and_then ( parse_retry_after)
260
245
. unwrap_or ( DEFAULT_RETRY_INTERVAL ) ;
261
246
262
- let bytes = collect_body ( res) . await ?;
263
- let err: types:: Problem = serde_json:: from_slice ( & bytes) ?;
247
+ let err: types:: Problem = serde_json:: from_slice ( res. body ( ) ) ?;
264
248
265
249
let retriable = matches ! (
266
250
err. kind,
@@ -312,8 +296,7 @@ where
312
296
self . account = Some ( key_id) ;
313
297
self . nonce . add_from_response ( & res) ;
314
298
315
- let bytes = collect_body ( res) . await ?;
316
- Ok ( serde_json:: from_slice ( & bytes) ?)
299
+ Ok ( serde_json:: from_slice ( res. body ( ) ) ?)
317
300
}
318
301
319
302
pub fn is_ready ( & self ) -> bool {
@@ -352,8 +335,7 @@ where
352
335
. ok_or ( anyhow ! ( "no order URL" ) ) ?;
353
336
354
337
let order_url = Uri :: try_from ( order_url) ?;
355
- let bytes = collect_body ( res) . await ?;
356
- let order: types:: Order = serde_json:: from_slice ( & bytes) ?;
338
+ let order: types:: Order = serde_json:: from_slice ( res. body ( ) ) ?;
357
339
358
340
let mut authorizations: Vec < ( http:: Uri , types:: Authorization ) > = Vec :: new ( ) ;
359
341
for auth_url in order. authorizations {
@@ -397,7 +379,7 @@ where
397
379
self . do_authorization ( & order, url, authorization) . await ?;
398
380
}
399
381
400
- let mut bytes = collect_body ( self . post ( & order_url, & [ ] ) . await ?) . await ? ;
382
+ let mut bytes = self . post ( & order_url, & [ ] ) . await ?. into_body ( ) ;
401
383
let mut order: types:: Order = serde_json:: from_slice ( & bytes) ?;
402
384
403
385
if order. status != OrderStatus :: Ready {
@@ -410,7 +392,7 @@ where
410
392
match self . post ( & order. finalize , payload) . await {
411
393
Ok ( res) => {
412
394
drop ( order) ;
413
- bytes = collect_body ( res) . await ? ;
395
+ bytes = res. into_body ( ) ;
414
396
order = serde_json:: from_slice ( & bytes) ?;
415
397
}
416
398
Err ( err) => {
@@ -433,18 +415,13 @@ where
433
415
. and_then ( parse_retry_after)
434
416
. unwrap_or ( DEFAULT_RETRY_INTERVAL ) ;
435
417
drop ( order) ;
436
- bytes = collect_body ( res) . await ? ;
418
+ bytes = res. into_body ( ) ;
437
419
order = serde_json:: from_slice ( & bytes) ?;
438
420
}
439
421
440
422
let certificate = order. certificate . ok_or ( anyhow ! ( "certifcate not ready" ) ) ?;
441
423
442
- let chain = self
443
- . post ( & certificate, & [ ] )
444
- . await ?
445
- . collect ( )
446
- . await ?
447
- . to_bytes ( ) ;
424
+ let chain = self . post ( & certificate, & [ ] ) . await ?. into_body ( ) ;
448
425
449
426
// FIXME: avoid reallocation from std::vec::Vec.
450
427
let x509 = Vec :: from_iter ( X509 :: stack_from_pem ( & chain) ?) ;
0 commit comments