Skip to content

Commit d80c838

Browse files
styris-amedjc
authored andcommitted
Add USER_AGENT header to requests (fixes #144)
1 parent a0cf801 commit d80c838

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/account.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::time::{Duration, SystemTime};
77
use base64::prelude::{BASE64_URL_SAFE_NO_PAD, Engine};
88
use http::header::LOCATION;
99
#[cfg(feature = "time")]
10+
use http::header::USER_AGENT;
11+
#[cfg(feature = "time")]
1012
use http::{Method, Request};
1113
#[cfg(feature = "hyper-rustls")]
1214
use rustls::RootCertStore;
@@ -29,7 +31,7 @@ use crate::types::{
2931
#[cfg(feature = "time")]
3032
use crate::types::{CertificateIdentifier, RenewalInfo};
3133
#[cfg(feature = "time")]
32-
use crate::{BodyWrapper, retry_after};
34+
use crate::{BodyWrapper, CRATE_USER_AGENT, retry_after};
3335
use crate::{BytesResponse, Client, Error, HttpClient, crypto, nonce_from_response};
3436

3537
/// An ACME account as described in RFC 8555 (section 7.1.2)
@@ -195,6 +197,7 @@ impl Account {
195197
let request = Request::builder()
196198
.method(Method::GET)
197199
.uri(format!("{renewal_info_url}/{certificate_id}"))
200+
.header(USER_AGENT, CRATE_USER_AGENT)
198201
.body(BodyWrapper::default())?;
199202

200203
let rsp = self.inner.client.http.request(request).await?;

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::time::{Duration, SystemTime};
1515

1616
use async_trait::async_trait;
1717
use bytes::{Buf, Bytes};
18-
use http::header::{CONTENT_TYPE, RETRY_AFTER};
18+
use http::header::{CONTENT_TYPE, RETRY_AFTER, USER_AGENT};
1919
use http::{Method, Request, Response, StatusCode};
2020
use http_body::{Frame, SizeHint};
2121
use http_body_util::BodyExt;
@@ -61,11 +61,12 @@ struct Client {
6161

6262
impl Client {
6363
async fn new(directory_url: String, http: Box<dyn HttpClient>) -> Result<Self, Error> {
64-
let req = Request::builder()
64+
let request = Request::builder()
6565
.uri(&directory_url)
66+
.header(USER_AGENT, CRATE_USER_AGENT)
6667
.body(BodyWrapper::default())
6768
.expect("infallible error should not occur");
68-
let rsp = http.request(req).await?;
69+
let rsp = http.request(request).await?;
6970
let body = rsp.body().await.map_err(Error::Other)?;
7071
Ok(Client {
7172
http,
@@ -123,6 +124,7 @@ impl Client {
123124
let request = Request::builder()
124125
.method(Method::POST)
125126
.uri(url)
127+
.header(USER_AGENT, CRATE_USER_AGENT)
126128
.header(CONTENT_TYPE, JOSE_JSON)
127129
.body(BodyWrapper::from(serde_json::to_vec(&body)?))?;
128130

@@ -137,6 +139,7 @@ impl Client {
137139
let request = Request::builder()
138140
.method(Method::HEAD)
139141
.uri(&self.directory.new_nonce)
142+
.header(USER_AGENT, CRATE_USER_AGENT)
140143
.body(BodyWrapper::default())
141144
.expect("infallible error should not occur");
142145

@@ -400,6 +403,7 @@ mod crypto {
400403
}
401404
}
402405

406+
const CRATE_USER_AGENT: &str = concat!("instant-acme/", env!("CARGO_PKG_VERSION"));
403407
const JOSE_JSON: &str = "application/jose+json";
404408
const REPLAY_NONCE: &str = "Replay-Nonce";
405409

0 commit comments

Comments
 (0)