Skip to content

Commit

Permalink
fix: curl - avoid zero-sized HEAD bodies
Browse files Browse the repository at this point in the history
If there is no body then don't construct a zero-lengthed body.

Fixes: http-rs/surf#321
Fixes: http-rs/surf#218
  • Loading branch information
Fishrock123 committed Aug 30, 2021
1 parent 3c0ed9a commit 44cba08
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/isahc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,41 @@ impl IsahcClient {
#[async_trait]
impl HttpClient for IsahcClient {
async fn send(&self, mut req: Request) -> Result<Response, Error> {
let mut builder = http::Request::builder()
.uri(req.url().as_str())
.method(http::Method::from_bytes(req.method().to_string().as_bytes()).unwrap());

for (name, value) in req.iter() {
builder = builder.header(name.as_str(), value.as_str());
}

let body = req.take_body();
let body = match body.len() {
Some(len) => isahc::Body::from_reader_sized(body, len as u64),
None => isahc::Body::from_reader(body),
};

let request = builder.body(body).unwrap();
let res = self.client.send_async(request).await.map_err(Error::from)?;
let maybe_metrics = res.metrics().cloned();
let (parts, body) = res.into_parts();
let body = Body::from_reader(BufReader::new(body), None);
// Code duplication to get around https://github.com/http-rs/surf/issues/321
// This is because `http::request` is generic (sized) over an existent and non-existent body differently, which is less than ideal.
let (parts, response_body, maybe_metrics) = match body.len() {
Some(len) => {
let mut builder = http::Request::builder()
.uri(req.url().as_str())
.method(http::Method::from_bytes(req.method().to_string().as_bytes()).unwrap());

for (name, value) in req.iter() {
builder = builder.header(name.as_str(), value.as_str());
}
let body = isahc::Body::from_reader_sized(body, len as u64);
let request = builder.body(body).unwrap();
let res = self.client.send_async(request).await.map_err(Error::from)?;
let maybe_metrics = res.metrics().cloned();
let (parts, body) = res.into_parts();
(parts, Body::from_reader(BufReader::new(body), None), maybe_metrics)
}
None => {
let mut builder = http::Request::builder()
.uri(req.url().as_str())
.method(http::Method::from_bytes(req.method().to_string().as_bytes()).unwrap());

for (name, value) in req.iter() {
builder = builder.header(name.as_str(), value.as_str());
}
let request = builder.body(()).unwrap();
let res = self.client.send_async(request).await.map_err(Error::from)?;
let maybe_metrics = res.metrics().cloned();
let (parts, body) = res.into_parts();
(parts, Body::from_reader(BufReader::new(body), None), maybe_metrics)
}
};
let mut response = http_types::Response::new(parts.status.as_u16());
for (name, value) in &parts.headers {
response.append_header(name.as_str(), value.to_str().unwrap());
Expand All @@ -69,7 +85,7 @@ impl HttpClient for IsahcClient {
response.ext_mut().insert(metrics);
}

response.set_body(body);
response.set_body(response_body);
Ok(response)
}

Expand Down

0 comments on commit 44cba08

Please sign in to comment.