Skip to content

Commit

Permalink
Correctly include port in host header
Browse files Browse the repository at this point in the history
  • Loading branch information
robalar committed Oct 30, 2024
1 parent 83d77a2 commit eaa9de4
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/reqwest_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ impl RequestLike for reqwest::Request {

impl ClientRequestLike for reqwest::Request {
fn host(&self) -> Option<String> {
self.url().host_str().map(Into::into)
let host = self.url().host_str();
let port = self.url().port();

host.map(|host| match port {
Some(port) => format!("{}:{}", host, port),
None => host.into(),
})
}
fn compute_digest(&mut self, digest: &dyn HttpDigest) -> Option<String> {
self.body()?.as_bytes().map(|b| digest.http_digest(b))
Expand Down Expand Up @@ -52,7 +58,13 @@ impl RequestLike for reqwest::blocking::Request {

impl ClientRequestLike for reqwest::blocking::Request {
fn host(&self) -> Option<String> {
self.url().host_str().map(Into::into)
let host = self.url().host_str();
let port = self.url().port();

host.map(|host| match port {
Some(port) => format!("{}:{}", host, port),
None => host.into(),
})
}
fn compute_digest(&mut self, digest: &dyn HttpDigest) -> Option<String> {
let bytes_to_digest = self.body_mut().as_mut()?.buffer().ok()?;
Expand Down Expand Up @@ -102,6 +114,29 @@ mod tests {
);
}

#[test]
fn sets_host_header_with_port_correctly() {
let config = SigningConfig::new_default("test_key", "abcdefgh".as_bytes());
let client = reqwest::Client::new();

let without_sig = client
.post("http://localhost:8080/foo/bar")
.header(CONTENT_TYPE, "application/json")
.header(
DATE,
Utc.ymd(2014, 7, 8)

Check warning on line 127 in src/reqwest_impls.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated method `chrono::TimeZone::ymd`: use `with_ymd_and_hms()` instead
.and_hms(9, 10, 11)

Check warning on line 128 in src/reqwest_impls.rs

View workflow job for this annotation

GitHub Actions / Test

use of deprecated method `chrono::Date::<Tz>::and_hms`: Use and_hms_opt() instead
.format("%a, %d %b %Y %T GMT")
.to_string(),
)
.body(&br#"{ "x": 1, "y": 2}"#[..])
.build()
.unwrap();

let with_sig = without_sig.signed(&config).unwrap();
assert_eq!(with_sig.headers().get("host").unwrap(), "localhost:8080");
}

#[test]
#[ignore]
fn it_can_talk_to_reference_integration() {
Expand Down

0 comments on commit eaa9de4

Please sign in to comment.