Skip to content

Commit

Permalink
fix: wasix http client: properly forward response headers
Browse files Browse the repository at this point in the history
Resolves the TODO, and really needed to be done...
  • Loading branch information
theduke committed Jan 16, 2023
1 parent 5e7517f commit ed15165
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/wasi/src/http/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ impl ReqwestHttpClient {

let status = response.status().as_u16();
let status_text = response.status().as_str().to_string();
// TODO: prevent redundant header copy.

This comment has been minimized.

Copy link
@Michael-F-Bryan

Michael-F-Bryan Jan 17, 2023

Contributor

FYI - I don't think this is possible in general because http::HeaderMap stores headers as a HashMap<HeaderName, Vec<HeaderValue>>, but the consuming into_iter() will yield (Option<HeaderName>, HeaderValue>, where you only get Some for the first occurrence of a particular header.

If we are allowed to silently drop/overwrite duplicate headers, then we can implement it without copies.

let mut headers = Vec::new();

let (parts, body) = response.into_parts();

for (name, value) in parts.headers {
  if let Some(name) = name {
    headers.push(name, value);
  }
}

... Although the HttpResponse type's headers field is a Vec<(String, String)> rather than a Vec<(HeaderName, HeaderValue)>, so you'd need to call .to_string() on things anyway, and we're back where we started.

TL;DR: The string copies are annoying, but we'll probably need to keep them if we want to respect HTTP semantics so you can remove the TODO.

This comment has been minimized.

Copy link
@theduke

theduke Jan 17, 2023

Author Contributor

The iterator design has annoyed me for years, because it forces you to keep a stateful mutable variable with the most recent header name.

The bindings already look like this:

record header {
  key: string,
  value: list<u8>,
}

type header-list = list<header>

So it's already possible to avoid the copies, I was just in a rush and didn't implement it.

let headers = response
.headers()
.iter()
.map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
.collect();
let data = response.bytes().await?.to_vec();

// TODO: forward the response headers.

Ok(HttpResponse {
pos: 0usize,
ok: true,
status,
status_text,
redirected: false,
body: Some(data),
headers: Vec::new(),
headers,
})
}
}
Expand Down

0 comments on commit ed15165

Please sign in to comment.