Skip to content

Commit d5ed1d0

Browse files
committed
fixup! ACME: certificate issue and renewal implementation.
1 parent ee88a99 commit d5ed1d0

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

src/acme.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::cell::RefCell;
2-
use core::error::Error as StdError;
32
use core::ptr::NonNull;
43
use core::time::Duration;
54
use std::collections::VecDeque;
@@ -9,8 +8,6 @@ use std::string::{String, ToString};
98
use anyhow::{anyhow, Result};
109
use bytes::Bytes;
1110
use http::Uri;
12-
use http_body::Body;
13-
use http_body_util::{BodyExt, Empty, Full};
1411
use ngx::allocator::{Allocator, Box};
1512
use ngx::async_::sleep;
1613
use ngx::collections::Vec;
@@ -85,14 +82,6 @@ fn try_get_header<K: http::header::AsHeaderName>(
8582
headers.get(key).and_then(|x| x.to_str().ok())
8683
}
8784

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-
9685
impl<'a, Http> AcmeClient<'a, Http>
9786
where
9887
Http: HttpClient,
@@ -129,8 +118,7 @@ where
129118

130119
async fn get_directory(&mut self) -> Result<types::Directory> {
131120
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())?;
134122

135123
Ok(directory)
136124
}
@@ -163,8 +151,7 @@ where
163151
.and_then(parse_retry_after)
164152
.unwrap_or(DEFAULT_RETRY_INTERVAL);
165153

166-
let bytes = collect_body(res).await?;
167-
let result = serde_json::from_slice(&bytes)?;
154+
let result = serde_json::from_slice(res.body())?;
168155

169156
if predicate(&result) {
170157
return Ok(result);
@@ -185,24 +172,23 @@ where
185172
payload: P,
186173
) -> Result<T> {
187174
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())?)
190176
}
191177

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>> {
193179
let req = http::Request::builder()
194180
.uri(url)
195181
.method(http::Method::GET)
196182
.header(http::header::CONTENT_LENGTH, 0)
197-
.body(Empty::<Bytes>::new())?;
183+
.body(String::new())?;
198184
Ok(self.http.request(req).await?)
199185
}
200186

201187
pub async fn post<P: AsRef<[u8]>>(
202188
&self,
203189
url: &Uri,
204190
payload: P,
205-
) -> Result<http::Response<Http::Body>> {
191+
) -> Result<http::Response<Bytes>> {
206192
let mut fails = 0;
207193

208194
let mut nonce = if let Some(nonce) = self.nonce.get() {
@@ -220,7 +206,6 @@ where
220206
&nonce,
221207
payload.as_ref(),
222208
)?;
223-
let body = Bytes::from(body);
224209
let req = http::Request::builder()
225210
.uri(url)
226211
.method(http::Method::POST)
@@ -229,7 +214,7 @@ where
229214
http::header::CONTENT_TYPE,
230215
http::HeaderValue::from_static("application/jose+json"),
231216
)
232-
.body(Full::new(body))?;
217+
.body(body)?;
233218

234219
let res = match self.http.request(req).await {
235220
Ok(res) => res,
@@ -259,8 +244,7 @@ where
259244
.and_then(parse_retry_after)
260245
.unwrap_or(DEFAULT_RETRY_INTERVAL);
261246

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())?;
264248

265249
let retriable = matches!(
266250
err.kind,
@@ -312,8 +296,7 @@ where
312296
self.account = Some(key_id);
313297
self.nonce.add_from_response(&res);
314298

315-
let bytes = collect_body(res).await?;
316-
Ok(serde_json::from_slice(&bytes)?)
299+
Ok(serde_json::from_slice(res.body())?)
317300
}
318301

319302
pub fn is_ready(&self) -> bool {
@@ -352,8 +335,7 @@ where
352335
.ok_or(anyhow!("no order URL"))?;
353336

354337
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())?;
357339

358340
let mut authorizations: Vec<(http::Uri, types::Authorization)> = Vec::new();
359341
for auth_url in order.authorizations {
@@ -397,7 +379,7 @@ where
397379
self.do_authorization(&order, url, authorization).await?;
398380
}
399381

400-
let mut bytes = collect_body(self.post(&order_url, &[]).await?).await?;
382+
let mut bytes = self.post(&order_url, &[]).await?.into_body();
401383
let mut order: types::Order = serde_json::from_slice(&bytes)?;
402384

403385
if order.status != OrderStatus::Ready {
@@ -410,7 +392,7 @@ where
410392
match self.post(&order.finalize, payload).await {
411393
Ok(res) => {
412394
drop(order);
413-
bytes = collect_body(res).await?;
395+
bytes = res.into_body();
414396
order = serde_json::from_slice(&bytes)?;
415397
}
416398
Err(err) => {
@@ -433,18 +415,13 @@ where
433415
.and_then(parse_retry_after)
434416
.unwrap_or(DEFAULT_RETRY_INTERVAL);
435417
drop(order);
436-
bytes = collect_body(res).await?;
418+
bytes = res.into_body();
437419
order = serde_json::from_slice(&bytes)?;
438420
}
439421

440422
let certificate = order.certificate.ok_or(anyhow!("certifcate not ready"))?;
441423

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();
448425

449426
// FIXME: avoid reallocation from std::vec::Vec.
450427
let x509 = Vec::from_iter(X509::stack_from_pem(&chain)?);

0 commit comments

Comments
 (0)