`ConnectionClose()` is false for `Connection: Close` and for `Connection: TE, close`
A request whose Connection header is Close rather than close, or lists close alongside another option, is not read as asking for the connection to be closed: Request.ConnectionClose() is false where net/http's Request.Close is true, and a fasthttp server answers such a request with keep-alive and goes on serving requests on that connection:
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"strings"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func main() {
ln := fasthttputil.NewInmemoryListener()
go fasthttp.Serve(ln, func(ctx *fasthttp.RequestCtx) { ctx.SetBodyString("ok") })
for _, v := range []string{"close", "Close", "TE, close"} {
raw := "GET / HTTP/1.1\r\nHost: h\r\nConnection: " + v + "\r\n\r\n"
var req fasthttp.Request
if err := req.Read(bufio.NewReader(strings.NewReader(raw))); err != nil {
panic(err)
}
stdReq, err := http.ReadRequest(bufio.NewReader(strings.NewReader(raw)))
if err != nil {
panic(err)
}
fmt.Printf("Connection: %-11q fasthttp Request.ConnectionClose()=%-5v net/http Request.Close=%v\n",
v, req.ConnectionClose(), stdReq.Close)
conn, _ := ln.Dial()
r := bufio.NewReader(conn)
conn.Write([]byte(raw))
resp, err := http.ReadResponse(r, nil)
if err != nil {
panic(err)
}
io.Copy(io.Discard, resp.Body)
conn.Write([]byte("GET / HTTP/1.1\r\nHost: h\r\n\r\n"))
_, err = http.ReadResponse(r, nil)
fmt.Printf(" fasthttp server: response Close=%-5v second request on the same connection: %s\n",
resp.Close, result(err))
conn.Close()
}
}
func result(err error) string {
if err != nil {
return err.Error()
}
return "answered"
}Output:
Connection: "close" fasthttp Request.ConnectionClose()=true net/http Request.Close=true
fasthttp server: response Close=true second request on the same connection: unexpected EOF
Connection: "Close" fasthttp Request.ConnectionClose()=false net/http Request.Close=true
fasthttp server: response Close=false second request on the same connection: answered
Connection: "TE, close" fasthttp Request.ConnectionClose()=false net/http Request.Close=true
fasthttp server: response Close=false second request on the same connection: answeredOnly the exact value close is recognised. RFC 9110 section 7.6.1 defines Connection as a comma-separated list of case-insensitive connection options, with close anywhere in the list meaning the sender will close the connection after the current message, which is how net/http reads all three values above.
Tested on fasthttp v1.74.0 and on current master (5687435).
BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 6 other bugs in fasthttp. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/fasthttp. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.
Source: valyala/fasthttp