mirror of
https://github.com/golang/go
synced 2024-11-21 23:34:42 -07:00
net/http: return appropriate errors from ReadRequest
Fixes #3298 R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5783080
This commit is contained in:
parent
da8efae9fe
commit
e8deb3f828
@ -455,11 +455,13 @@ func ReadRequest(b *bufio.Reader) (req *Request, err error) {
|
|||||||
// First line: GET /index.html HTTP/1.0
|
// First line: GET /index.html HTTP/1.0
|
||||||
var s string
|
var s string
|
||||||
if s, err = tp.ReadLine(); err != nil {
|
if s, err = tp.ReadLine(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
err = io.ErrUnexpectedEOF
|
err = io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
return nil, err
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
var f []string
|
var f []string
|
||||||
if f = strings.SplitN(s, " ", 3); len(f) < 3 {
|
if f = strings.SplitN(s, " ", 3); len(f) < 3 {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package http_test
|
package http_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -177,6 +178,24 @@ func TestRequestMultipartCallOrder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var readRequestErrorTests = []struct {
|
||||||
|
in string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{"GET / HTTP/1.1\r\nheader:foo\r\n\r\n", nil},
|
||||||
|
{"GET / HTTP/1.1\r\nheader:foo\r\n", io.ErrUnexpectedEOF},
|
||||||
|
{"", io.EOF},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadRequestErrors(t *testing.T) {
|
||||||
|
for i, tt := range readRequestErrorTests {
|
||||||
|
_, err := ReadRequest(bufio.NewReader(strings.NewReader(tt.in)))
|
||||||
|
if err != tt.err {
|
||||||
|
t.Errorf("%d. got error = %v; want %v", i, err, tt.err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testMissingFile(t *testing.T, req *Request) {
|
func testMissingFile(t *testing.T, req *Request) {
|
||||||
f, fh, err := req.FormFile("missing")
|
f, fh, err := req.FormFile("missing")
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
@ -601,7 +601,7 @@ func (c *conn) serve() {
|
|||||||
// while they're still writing their
|
// while they're still writing their
|
||||||
// request. Undefined behavior.
|
// request. Undefined behavior.
|
||||||
msg = "413 Request Entity Too Large"
|
msg = "413 Request Entity Too Large"
|
||||||
} else if err == io.ErrUnexpectedEOF {
|
} else if err == io.EOF {
|
||||||
break // Don't reply
|
break // Don't reply
|
||||||
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
||||||
break // Don't reply
|
break // Don't reply
|
||||||
|
Loading…
Reference in New Issue
Block a user