Skip to content

Commit

Permalink
Allow setting custom agent when converting from http crate types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper-Bekkers committed Feb 28, 2024
1 parent 4e3169f commit d1ef52c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
67 changes: 39 additions & 28 deletions src/http_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,31 @@ impl From<Response> for http::Response<Vec<u8>> {
/// ```
impl From<http::request::Builder> for Request {
fn from(value: http::request::Builder) -> Self {
let mut new_request = crate::agent().request(
value.method_ref().map_or("GET", |m| m.as_str()),
&value
.uri_ref()
.map_or("https://example.com".to_string(), |u| u.to_string()),
);
convert_from_http_builder_with_agent(value, crate::agent())
}
}

if let Some(headers) = value.headers_ref() {
for (name, value) in headers {
let mut raw_header: Vec<u8> = name.to_string().into_bytes();
raw_header.extend(b": ");
raw_header.extend(value.as_bytes());
let header = HeaderLine::from(raw_header).into_header().unwrap();
/// Converts an [`http::request::Builder`] into a [`Request`] with a custom [`Agent`].

Check failure on line 195 in src/http_interop.rs

View workflow job for this annotation

GitHub Actions / Docs

unresolved link to `Agent`
pub fn convert_from_http_builder_with_agent(value: http::request::Builder, agent: crate::Agent) -> Request {
let mut new_request = agent.request(
value.method_ref().map_or("GET", |m| m.as_str()),
&value
.uri_ref()
.map_or("https://example.com".to_string(), |u| u.to_string()),
);

if let Some(headers) = value.headers_ref() {
for (name, value) in headers {
let mut raw_header: Vec<u8> = name.to_string().into_bytes();
raw_header.extend(b": ");
raw_header.extend(value.as_bytes());
let header = HeaderLine::from(raw_header).into_header().unwrap();

crate::header::add_header(&mut new_request.headers, header)
}
crate::header::add_header(&mut new_request.headers, header)
}

new_request
}

new_request
}

/// Converts [`http::request::Parts`] into a [`Request`].
Expand All @@ -227,23 +232,29 @@ impl From<http::request::Builder> for Request {
/// ```
impl From<http::request::Parts> for Request {
fn from(value: http::request::Parts) -> Self {
let mut new_request = crate::agent().request(value.method.as_str(), &value.uri.to_string());
convert_from_http_parts_with_agent(value, crate::agent())
}
}

for (name, value) in &value.headers {
// TODO: Aren't complete header values available as raw byte slices?
let mut raw_header: Vec<u8> = name.to_string().into_bytes();
raw_header.extend(b": ");
raw_header.extend(value.as_bytes());
/// Converts a [`http::request::Parts`] into a [`Request`] with a custom [`Agent`].

Check failure on line 239 in src/http_interop.rs

View workflow job for this annotation

GitHub Actions / Docs

unresolved link to `Agent`
pub fn convert_from_http_parts_with_agent(value: http::request::Parts, agent: crate::Agent) -> Request {
let mut new_request = agent.request(value.method.as_str(), &value.uri.to_string());

let header = HeaderLine::from(raw_header)
.into_header()
.expect("Unreachable");
for (name, value) in &value.headers {
// TODO: Aren't complete header values available as raw byte slices?
let mut raw_header: Vec<u8> = name.to_string().into_bytes();
raw_header.extend(b": ");
raw_header.extend(value.as_bytes());

crate::header::add_header(&mut new_request.headers, header)
}
let header = HeaderLine::from(raw_header)
.into_header()
.expect("Unreachable");

new_request
crate::header::add_header(&mut new_request.headers, header)
}

new_request

}

/// Converts a [`Request`] into an [`http::request::Builder`].
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mod testserver;

#[cfg(feature = "http-interop")]
// 0.2 version dependency (deprecated)
mod http_interop;
pub mod http_interop;

#[cfg(feature = "http-crate")]
// 1.0 version dependency.
Expand Down

0 comments on commit d1ef52c

Please sign in to comment.