Skip to content

Instantly share code, notes, and snippets.

@sbadame
Created April 19, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbadame/6672153feb5a5430a0129d40b263d8e9 to your computer and use it in GitHub Desktop.
Save sbadame/6672153feb5a5430a0129d40b263d8e9 to your computer and use it in GitHub Desktop.
Reproduction for net/http: Response.Write has bogus Connection header after auto-ungzipped Content-Length response
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
)
// A reproduction of a bug where http.transport.RoundTrip returns an http.Response
// that contains both "Connection: close" and "Connection: keep-alive" headers.
// Pass in a file with the response to parse as the first argument.
func main() {
// Minimal response to reproduce the bug. Content is a gzipped UTF-8 "FOO".
response, _ := ioutil.ReadFile(os.Args[1])
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
conn.Write(response)
}
}()
t := &http.Transport{}
req, _ := http.NewRequest("GET", "http://localhost:8080", nil)
res, rt_err := t.RoundTrip(req)
if rt_err != nil {
log.Fatalf("ERROR RT: %v\n", rt_err)
}
buf, err := httputil.DumpResponse(res, false)
if err != nil {
log.Fatalf("ERROR DR: %v\n", err)
}
fmt.Printf("http.Transport.RoundTrip response: %s\n", buf)
res, rt_err = http.DefaultClient.Do(req)
if rt_err != nil {
log.Fatalf("ERROR RT: %v\n", rt_err)
}
buf, err = httputil.DumpResponse(res, false)
if err != nil {
log.Fatalf("ERROR DR: %v\n", err)
}
fmt.Printf("http.DefaultClient.Do Response: %s\n", buf)
}
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Length: 23
Connection: keep-alive
Keep-Alive: timeout=7200
\8B\00\00\00\00\00\00\00s\F3\F7\00\AB'\D4\00\00\00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment