]> Cypherpunks repositories - gostls13.git/commitdiff
http: use Header.Del not empty Set(k, "")
authorBrad Fitzpatrick <bradfitz@golang.org>
Sat, 12 Mar 2011 17:58:53 +0000 (09:58 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 12 Mar 2011 17:58:53 +0000 (09:58 -0800)
Also don't serialize empty headers.

R=dsymonds, rsc
CC=golang-dev
https://golang.org/cl/4275045

src/pkg/http/response.go
src/pkg/http/serve_test.go
src/pkg/http/server.go

index 7ac7fb81f3500e9a88b13bd85932eda555a0ad3e..1f725ecdddd0b786ebe65413f386e2137c77d377 100644 (file)
@@ -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
                        }
index 482acfd314176a57de4c3aa0f3234f1554db2a8b..6b881a2491da94ae58596d28ae136bdf74377dc5 100644 (file)
@@ -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")
+       }
+}
index 6a7c74efb039498823728a3ea931744a12b211d4..91caebc2dbc4dd34d9b1b08a41d1a863b1b5c427 100644 (file)
@@ -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