1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:00:22 -07:00

textproto: prevent long lines in HTTP headers from causing HTTP 400 responses.

This fixes the issue without an extra copy in the average case.

R=golang-dev, ality, bradfitz
CC=golang-dev
https://golang.org/cl/5272049
This commit is contained in:
Mike Solomon 2011-11-01 10:31:29 -07:00 committed by Brad Fitzpatrick
parent cf7281e728
commit f753e3facd
2 changed files with 33 additions and 2 deletions

View File

@ -50,8 +50,22 @@ func (r *Reader) ReadLineBytes() ([]byte, os.Error) {
func (r *Reader) readLineSlice() ([]byte, os.Error) { func (r *Reader) readLineSlice() ([]byte, os.Error) {
r.closeDot() r.closeDot()
line, _, err := r.R.ReadLine() var line []byte
return line, err for {
l, more, err := r.R.ReadLine()
if err != nil {
return nil, err
}
// Avoid the copy if the first call produced a full line.
if line == nil && !more {
return l, nil
}
line = append(line, l...)
if !more {
break
}
}
return line, nil
} }
// ReadContinuedLine reads a possibly continued line from r, // ReadContinuedLine reads a possibly continued line from r,

View File

@ -139,6 +139,23 @@ func TestReadMIMEHeader(t *testing.T) {
} }
} }
func TestLargeReadMIMEHeader(t *testing.T) {
data := make([]byte, 16*1024)
for i := 0; i < len(data); i++ {
data[i] = 'x'
}
sdata := string(data)
r := reader("Cookie: " + sdata + "\r\n\n")
m, err := r.ReadMIMEHeader()
if err != nil {
t.Fatalf("ReadMIMEHeader: %v", err)
}
cookie := m.Get("Cookie")
if cookie != sdata {
t.Fatalf("ReadMIMEHeader: %v bytes, want %v bytes", len(cookie), len(sdata))
}
}
type readResponseTest struct { type readResponseTest struct {
in string in string
inCode int inCode int