mirror of
https://github.com/golang/go
synced 2024-11-20 01:44:42 -07:00
net/http: fix EOF handling on response body
http.Response is currently returning 0, nil on EOF. R=golang-dev, bradfitz, bradfitz CC=golang-dev https://golang.org/cl/5394047
This commit is contained in:
parent
881f2076fb
commit
c9355596cd
@ -26,6 +26,31 @@ var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
|
||||
})
|
||||
|
||||
// pedanticReadAll works like ioutil.ReadAll but additionally
|
||||
// verifies that r obeys the documented io.Reader contract.
|
||||
func pedanticReadAll(r io.Reader) (b []byte, err error) {
|
||||
var bufa [64]byte
|
||||
buf := bufa[:]
|
||||
for {
|
||||
n, err := r.Read(buf)
|
||||
if n == 0 && err == nil {
|
||||
return nil, fmt.Errorf("Read: n=0 with err=nil")
|
||||
}
|
||||
b = append(b, buf[:n]...)
|
||||
if err == io.EOF {
|
||||
n, err := r.Read(buf)
|
||||
if n != 0 || err != io.EOF {
|
||||
return nil, fmt.Errorf("Read: n=%d err=%#v after EOF", n, err)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func TestClient(t *testing.T) {
|
||||
ts := httptest.NewServer(robotsTxtHandler)
|
||||
defer ts.Close()
|
||||
@ -33,7 +58,7 @@ func TestClient(t *testing.T) {
|
||||
r, err := Get(ts.URL)
|
||||
var b []byte
|
||||
if err == nil {
|
||||
b, err = ioutil.ReadAll(r.Body)
|
||||
b, err = pedanticReadAll(r.Body)
|
||||
r.Body.Close()
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -537,7 +537,9 @@ func (b *body) Read(p []byte) (n int, err error) {
|
||||
|
||||
// Read the final trailer once we hit EOF.
|
||||
if err == io.EOF && b.hdr != nil {
|
||||
err = b.readTrailer()
|
||||
if e := b.readTrailer(); e != nil {
|
||||
err = e
|
||||
}
|
||||
b.hdr = nil
|
||||
}
|
||||
return n, err
|
||||
|
Loading…
Reference in New Issue
Block a user