aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohan Brandhorst <johan.brandhorst@gmail.com>2018-08-24 12:10:01 +0100
committerBrad Fitzpatrick <bradfitz@golang.org>2018-09-24 17:10:10 +0000
commitb5ed6ec14092b04156adcbaba101958dc9d9d74b (patch)
treec0b7eeb2ed3fb24649c566ed8d1c02c9a16b3695
parentf2113d94c15dfa82d5afb46bc750f009284e2f64 (diff)
downloadgo-b5ed6ec14092b04156adcbaba101958dc9d9d74b.tar.gz
go-b5ed6ec14092b04156adcbaba101958dc9d9d74b.zip
[release-branch.go1.11] 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 #27424 Change-Id: I0da86b61284fe394418b4b431495e715a037f335 Reviewed-on: https://go-review.googlesource.com/131236 Reviewed-by: Richard Musiol <neelance@gmail.com> Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> (cherry picked from commit ce536837d8e53f1bf0c7ef450d4580d19f7d6f52) Reviewed-on: https://go-review.googlesource.com/136915
-rw-r--r--src/net/http/roundtrip_js.go4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go
index 16b7b891c8..38e4f5573e 100644
--- a/src/net/http/roundtrip_js.go
+++ b/src/net/http/roundtrip_js.go
@@ -116,7 +116,9 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
b := result.Get("body")
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")}
} else {
// Fall back to using ArrayBuffer