rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.74k stars 1.07k forks source link

Should web_sys::Headers allow opaque bytes? #1287

Open liamcurry opened 5 years ago

liamcurry commented 5 years ago

I'm writing some code to convert an http::Request to a web_sys::Request, and I noticed in the http crate there is a struct HeaderValue with this note:

In practice, HTTP header field values are usually valid ASCII. However, the HTTP spec allows for a header value to contain opaque bytes as well. In this case, the header field value is not able to be represented as a string.

The functions for web_sys::Headers don't take this into account and all rely on strings. Would that be considered a bug? If not then please feel welcome to close this issue. Thanks!

fitzgen commented 5 years ago

The Web IDL for Headers#set takes a ByteString which is "the set of all possible sequences of bytes", so yes it seems we should allow passing arbitrary slices here.

However, we translate ByteString to &str in our Web IDL frontend. I can't remember if there is a particular reason for that, or if this is just an oversight. +cc @alexcrichton

alexcrichton commented 5 years ago

This was configured in https://github.com/rustwasm/wasm-bindgen/pull/511 and was intended to match the documentation at https://developer.mozilla.org/en-US/docs/Web/API/ByteString. This may be a WebIDL/JS mismatch?

One thing we could do, if JS supports it, is also generate methods taking Uint8Array if a ByteString is accepted