Skip to content

Commit a4e7b98

Browse files
authored
feat(client): Make HTTP/TLS config options publicly accessible (#783)
1 parent 929d917 commit a4e7b98

10 files changed

Lines changed: 242 additions & 143 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,12 @@ name = "form"
228228
path = "examples/form.rs"
229229

230230
[[example]]
231-
name = "hickory_dns"
232-
path = "examples/hickory_dns.rs"
233-
required-features = ["hickory-dns", "tracing"]
231+
name = "http1_send_case_sensitive_headers"
232+
path = "examples/http1_send_case_sensitive_headers.rs"
234233

235234
[[example]]
236-
name = "http1_case_sensitive_headers"
237-
path = "examples/http1_case_sensitive_headers.rs"
235+
name = "http1_recv_case_sensitive_headers"
236+
path = "examples/http1_recv_case_sensitive_headers.rs"
238237

239238
[[example]]
240239
name = "emulation_firefox"

examples/hickory_dns.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/http1_case_sensitive_headers.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use wreq::{OriginalHeaders, http1::Http1Config};
2+
3+
#[tokio::main]
4+
async fn main() -> wreq::Result<()> {
5+
let client = wreq::Client::builder()
6+
.configure_http1(
7+
Http1Config::builder()
8+
.preserve_header_case(true)
9+
.http09_responses(true)
10+
.build(),
11+
)
12+
.http1_only()
13+
.build()?;
14+
15+
// Use the API you're already familiar with
16+
let resp = client.post("https://httpbin.org").send().await?;
17+
if let Some(original) = resp.extensions().get::<OriginalHeaders>() {
18+
for (name, raw_name) in original.iter() {
19+
println!(
20+
"Header: {} (original: {})",
21+
name,
22+
String::from_utf8_lossy(raw_name)
23+
);
24+
}
25+
}
26+
Ok(())
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use http::{HeaderMap, HeaderName, HeaderValue};
2+
use wreq::OriginalHeaders;
3+
4+
#[tokio::main]
5+
async fn main() -> wreq::Result<()> {
6+
let client = wreq::Client::builder()
7+
.cert_verification(false)
8+
.http1_only()
9+
.build()?;
10+
11+
// Create a request with a case-sensitive header
12+
let mut original_headers = OriginalHeaders::new();
13+
original_headers.insert("Host");
14+
original_headers.insert("X-custom-Header1");
15+
original_headers.extend(["x-Custom-Header2"]);
16+
original_headers.insert(HeaderName::from_static("x-custom-header3"));
17+
18+
// Use the API you're already familiar with
19+
let resp = client
20+
.get("https://tls.peet.ws/api/all")
21+
.original_headers(original_headers)
22+
.headers({
23+
let mut headers = HeaderMap::new();
24+
headers.insert("x-custom-header1", HeaderValue::from_static("value1"));
25+
headers.insert("x-custom-header2", HeaderValue::from_static("value2"));
26+
headers.insert("x-custom-header3", HeaderValue::from_static("value3"));
27+
headers
28+
})
29+
.send()
30+
.await?;
31+
println!("{}", resp.text().await?);
32+
33+
Ok(())
34+
}

src/client/client/macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ macro_rules! take_err {
1717
}
1818
};
1919
}
20+
21+
macro_rules! apply_option {
22+
($self:expr, $emulation:expr, $(($field:ident, $method:ident)),*) => {
23+
$(
24+
if let Some(value) = $emulation.$field {
25+
$self = $self.$method(value);
26+
}
27+
)*
28+
};
29+
}

0 commit comments

Comments
 (0)