aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2011-11-28 11:51:34 -0500
committerBrad Fitzpatrick <bradfitz@golang.org>2011-11-28 11:51:34 -0500
commit1e85f41fd512d570b04f87d906d44e456f1c2108 (patch)
treefdb26b1d9829929b6a76c3590da03622618c10a7
parent7606079d9f54351e0aff78da804ce72814475446 (diff)
downloadgo-1e85f41fd512d570b04f87d906d44e456f1c2108.tar.gz
go-1e85f41fd512d570b04f87d906d44e456f1c2108.zip
http: fix sniffing bug causing short writes
R=rsc CC=golang-dev https://golang.org/cl/5442045
-rw-r--r--src/pkg/net/http/server.go2
-rw-r--r--src/pkg/net/http/sniff_test.go23
2 files changed, 24 insertions, 1 deletions
diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go
index 27040c7be5..0e8580a6ff 100644
--- a/src/pkg/net/http/server.go
+++ b/src/pkg/net/http/server.go
@@ -467,7 +467,7 @@ func (w *response) Write(data []byte) (n int, err error) {
// determine the content type. Accumulate the
// initial writes in w.conn.body.
// 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) {
m = len(data)
}
diff --git a/src/pkg/net/http/sniff_test.go b/src/pkg/net/http/sniff_test.go
index 86744eeb56..6efa8ce1ca 100644
--- a/src/pkg/net/http/sniff_test.go
+++ b/src/pkg/net/http/sniff_test.go
@@ -6,12 +6,14 @@ package http_test
import (
"bytes"
+ "fmt"
"io"
"io/ioutil"
"log"
. "net/http"
"net/http/httptest"
"strconv"
+ "strings"
"testing"
)
@@ -112,3 +114,24 @@ func TestContentTypeWithCopy(t *testing.T) {
}
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)
+ }
+ }
+}