Commit ce536837 authored by Johan Brandhorst's avatar Johan Brandhorst Committed by Richard Musiol

net/http: ensure null body in Fetch response is not read

The Fetch API returns a null body if there is no response body,
on browsers that support streaming the response body. This
change ensures we check for both undefined and null bodies
before attempting to read the body.

Fixes #27196

Change-Id: I0da86b61284fe394418b4b431495e715a037f335
Reviewed-on: https://go-review.googlesource.com/131236Reviewed-by: default avatarRichard Musiol <neelance@gmail.com>
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent fdefabad
...@@ -116,7 +116,9 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) { ...@@ -116,7 +116,9 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
b := result.Get("body") b := result.Get("body")
var body io.ReadCloser var body io.ReadCloser
if b != js.Undefined() { // The body is undefined when the browser does not support streaming response bodies (Firefox),
// and null in certain error cases, i.e. when the request is blocked because of CORS settings.
if b != js.Undefined() && b != js.Null() {
body = &streamReader{stream: b.Call("getReader")} body = &streamReader{stream: b.Call("getReader")}
} else { } else {
// Fall back to using ArrayBuffer // Fall back to using ArrayBuffer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment