Commit 1c96562f authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http: use Header.Del not empty Set(k, "")

Also don't serialize empty headers.

R=dsymonds, rsc
CC=golang-dev
https://golang.org/cl/4275045
parent a7528f1b
......@@ -224,9 +224,12 @@ func writeSortedHeader(w io.Writer, h Header, exclude map[string]bool) os.Error
sort.SortStrings(keys)
for _, k := range keys {
for _, v := range h[k] {
v = strings.TrimSpace(v)
v = strings.Replace(v, "\n", " ", -1)
v = strings.Replace(v, "\r", " ", -1)
v = strings.TrimSpace(v)
if v == "" {
continue
}
if _, err := fmt.Fprintf(w, "%s: %s\r\n", k, v); err != nil {
return err
}
......
......@@ -15,6 +15,7 @@ import (
"io/ioutil"
"os"
"net"
"reflect"
"strings"
"testing"
"time"
......@@ -427,3 +428,25 @@ func TestSetsRemoteAddr(t *testing.T) {
t.Fatalf("Expected local addr; got %q", ip)
}
}
func TestChunkedResponseHeaders(t *testing.T) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Length", "intentional gibberish") // we check that this is deleted
fmt.Fprintf(w, "I am a chunked response.")
}))
defer ts.Close()
res, _, err := Get(ts.URL)
if err != nil {
t.Fatalf("Get error: %v", err)
}
if g, e := res.ContentLength, int64(-1); g != e {
t.Errorf("expected ContentLength of %d; got %d", e, g)
}
if g, e := res.TransferEncoding, []string{"chunked"}; !reflect.DeepEqual(g, e) {
t.Errorf("expected TransferEncoding of %v; got %v", e, g)
}
if _, haveCL := res.Header["Content-Length"]; haveCL {
t.Errorf("Unexpected Content-Length")
}
}
......@@ -236,7 +236,7 @@ func (w *response) WriteHeader(code int) {
hasCL = true
} else {
log.Printf("http: invalid Content-Length of %q sent", clenStr)
w.header.Set("Content-Length", "")
w.header.Del("Content-Length")
}
}
......@@ -247,7 +247,7 @@ func (w *response) WriteHeader(code int) {
// For now just ignore the Content-Length.
log.Printf("http: WriteHeader called with both Transfer-Encoding of %q and a Content-Length of %d",
te, contentLength)
w.header.Set("Content-Length", "")
w.header.Del("Content-Length")
hasCL = false
}
......@@ -286,7 +286,7 @@ func (w *response) WriteHeader(code int) {
// Cannot use Content-Length with non-identity Transfer-Encoding.
if w.chunking {
w.header.Set("Content-Length", "")
w.header.Del("Content-Length")
}
if !w.req.ProtoAtLeast(1, 0) {
return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment