1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:44:39 -07:00

http: fix sniffing bug causing short writes

R=rsc
CC=golang-dev
https://golang.org/cl/5442045
This commit is contained in:
Brad Fitzpatrick 2011-11-28 11:51:34 -05:00
parent 7606079d9f
commit 1e85f41fd5
2 changed files with 24 additions and 1 deletions

View File

@ -467,7 +467,7 @@ func (w *response) Write(data []byte) (n int, err error) {
// determine the content type. Accumulate the // determine the content type. Accumulate the
// initial writes in w.conn.body. // initial writes in w.conn.body.
// Cap m so that append won't allocate. // Cap m so that append won't allocate.
m := cap(w.conn.body) - len(w.conn.body) m = cap(w.conn.body) - len(w.conn.body)
if m > len(data) { if m > len(data) {
m = len(data) m = len(data)
} }

View File

@ -6,12 +6,14 @@ package http_test
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
. "net/http" . "net/http"
"net/http/httptest" "net/http/httptest"
"strconv" "strconv"
"strings"
"testing" "testing"
) )
@ -112,3 +114,24 @@ func TestContentTypeWithCopy(t *testing.T) {
} }
resp.Body.Close() resp.Body.Close()
} }
func TestSniffWriteSize(t *testing.T) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
size, _ := strconv.Atoi(r.FormValue("size"))
written, err := io.WriteString(w, strings.Repeat("a", size))
if err != nil {
t.Errorf("write of %d bytes: %v", size, err)
return
}
if written != size {
t.Errorf("write of %d bytes wrote %d bytes", size, written)
}
}))
defer ts.Close()
for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} {
_, err := Get(fmt.Sprintf("%s/?size=%d", ts.URL, size))
if err != nil {
t.Fatalf("size %d: %v", size, err)
}
}
}