Skip to content

Commit ccbaff0

Browse files
committed
Merge pull request gopherjs#195 from encryptio/master
natives/net/http: set response ContentLength from headers on HEAD requests
2 parents b7e4414 + ec4e034 commit ccbaff0

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

compiler/natives/net/http/http.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"io/ioutil"
1010
"net/textproto"
11+
"strconv"
1112

1213
"github.com/gopherjs/gopherjs/js"
1314
)
@@ -38,11 +39,22 @@ func (t *XHRTransport) RoundTrip(req *Request) (*Response, error) {
3839
xhr.Set("onload", func() {
3940
header, _ := textproto.NewReader(bufio.NewReader(bytes.NewReader([]byte(xhr.Call("getAllResponseHeaders").String() + "\n")))).ReadMIMEHeader()
4041
body := js.Global.Get("Uint8Array").New(xhr.Get("response")).Interface().([]byte)
42+
43+
contentLength := int64(-1)
44+
if req.Method == "HEAD" {
45+
i, err := strconv.ParseInt(header.Get("Content-Length"), 10, 64)
46+
if err == nil {
47+
contentLength = i
48+
}
49+
} else {
50+
contentLength = int64(len(body))
51+
}
52+
4153
respCh <- &Response{
4254
Status: xhr.Get("status").String() + " " + xhr.Get("statusText").String(),
4355
StatusCode: xhr.Get("status").Int(),
4456
Header: Header(header),
45-
ContentLength: int64(len(body)),
57+
ContentLength: contentLength,
4658
Body: ioutil.NopCloser(bytes.NewReader(body)),
4759
Request: req,
4860
}

0 commit comments

Comments
 (0)