1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:34:48 -07:00

http: new error for reading a body after it's been closed

R=adg
CC=golang-dev
https://golang.org/cl/4433094
This commit is contained in:
Brad Fitzpatrick 2011-04-30 19:54:08 -07:00
parent 0d1f76ded2
commit cb375ffbb0

View File

@ -439,9 +439,29 @@ type body struct {
hdr interface{} // non-nil (Response or Request) value means read trailer hdr interface{} // non-nil (Response or Request) value means read trailer
r *bufio.Reader // underlying wire-format reader for the trailer r *bufio.Reader // underlying wire-format reader for the trailer
closing bool // is the connection to be closed after reading body? closing bool // is the connection to be closed after reading body?
closed bool
}
// ErrBodyReadAferClose is returned when reading a Request Body after
// the body has been closed. This typically happens when the body is
// read after an HTTP Handler calls WriteHeader or Write on its
// ResponseWriter.
var ErrBodyReadAferClose = os.NewError("http: invalid Read on closed request Body")
func (b *body) Read(p []byte) (n int, err os.Error) {
if b.closed {
return 0, ErrBodyReadAferClose
}
return b.Reader.Read(p)
} }
func (b *body) Close() os.Error { func (b *body) Close() os.Error {
if b.closed {
return nil
}
defer func() {
b.closed = true
}()
if b.hdr == nil && b.closing { if b.hdr == nil && b.closing {
// no trailer and closing the connection next. // no trailer and closing the connection next.
// no point in reading to EOF. // no point in reading to EOF.