Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow strings or byte sequences as bodies? #1073

Closed
domenic opened this issue Aug 13, 2020 · 4 comments · Fixed by #1083
Closed

Allow strings or byte sequences as bodies? #1073

domenic opened this issue Aug 13, 2020 · 4 comments · Fixed by #1083

Comments

@domenic
Copy link
Member

domenic commented Aug 13, 2020

This is several issues in one...

HTML always invokes Fetch with byte sequence request bodies. The two invocations are:

I think we should allow this, instead of requiring HTML or other callers to create new ReadableStream instances. Fetch could, very early in the Fetch algorithm, construct a fixed ReadableStream instance.

HTML also has a few places where it wants to supply a string as a response body in a synthetic response:

These are interesting because I know that Blink, at least, has a direct path for javascript: URLs and iframe srcdoc to set the resulting document's value as a string. Bytes never get involved, and the charset defined for the document is ignored. (I don't know if the HTML spec defines the charset of the produced documents very well for those cases...)

I'm not sure what we should do here. Maybe HTML's navigate algorithm should grow a new type of "resource", the string resource, since this seems very specific to that case? Or HTML could UTF-8 encode and created a fixed stream itself. Maybe with a helper defined in Fetch, like "created a fixed string body"?

I can't think of an easy way for Fetch to handle this in general, because we probably want to confine string-bodies to these navigation special cases; certainly any cases that might pass through service workers need to be stream-bodies.

Finally, HTML also treats script response bodies as byte sequences:

But this is already covered in #661, so we can ignore it for this issue.

@annevk
Copy link
Member

annevk commented Aug 14, 2020

Apart from fetch() and XMLHttpRequest, navigation is the only place that can produce request bodies in a browser. Is it hard for HTML to invoke https://fetch.spec.whatwg.org/#concept-bodyinit-extract similar to how XMLHttpRequest does it?

@domenic
Copy link
Member Author

domenic commented Aug 14, 2020

I guess it wouldn't be too bad to have HTML do this, if you really don't think other specs will have request bodies. (No specs want to ping a server?) "Extract" isn't a great fit as we don't want the MIME type, but maybe we can factor out some common primitive...

@annevk
Copy link
Member

annevk commented Aug 16, 2020

Hmm, maybe the Reporting API?

cc @clelland

@clelland
Copy link
Contributor

Yep, Reporting creates POST requests with bodies -- it sets the MIME type, and provides a byte sequence to Fetch.

The byte sequence used is always the output of https://infra.spec.whatwg.org/#serialize-json-to-bytes, so it could probably use a string just as well, if the Fetch primitive were to be refactored.

annevk added a commit that referenced this issue Aug 25, 2020
This simplifies things for several callers.

Fixes #1073.
annevk added a commit that referenced this issue Aug 27, 2020
This simplifies things for several callers. This also cleans up several ReadableStream object operations in the process.

Fixes #1073.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@clelland @domenic @annevk and others