From cce8f7ca321968749b683a2c9bf46a99f2514500 Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 6 Apr 2020 14:07:21 +0200 Subject: [PATCH] Fixed case when no hijacker is added but no-response is true --- server.go | 2 +- server_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 5c6c96c423..6b66c29716 100644 --- a/server.go +++ b/server.go @@ -2087,7 +2087,7 @@ func (s *Server) serveConn(c net.Conn) (err error) { hijackHandler = ctx.hijackHandler ctx.hijackHandler = nil - hijackNoResponse = ctx.hijackNoResponse + hijackNoResponse = ctx.hijackNoResponse && hijackHandler != nil ctx.hijackNoResponse = false ctx.userValues.Reset() diff --git a/server_test.go b/server_test.go index 0386f5364a..a7ee71c93f 100644 --- a/server_test.go +++ b/server_test.go @@ -2148,6 +2148,43 @@ func TestRequestCtxHijackNoResponse(t *testing.T) { } } +func TestRequestCtxNoHijackNoResponse(t *testing.T) { + t.Parallel() + + s := &Server{ + Handler: func(ctx *RequestCtx) { + io.WriteString(ctx, "test") + ctx.HijackSetNoResponse(true) + }, + } + + rw := &readWriter{} + rw.r.WriteString("GET /foo HTTP/1.1\r\nHost: google.com\r\nContent-Length: 0\r\n\r\n") + + ch := make(chan error) + go func() { + ch <- s.ServeConn(rw) + }() + + select { + case err := <-ch: + if err != nil { + t.Fatalf("Unexpected error from serveConn: %s", err) + } + case <-time.After(100 * time.Millisecond): + t.Fatal("timeout") + } + + bf := bufio.NewReader( + strings.NewReader(rw.w.String()), + ) + resp := AcquireResponse() + resp.Read(bf) + if got := string(resp.Body()); got != "test" { + t.Errorf(`expected "test", got %q`, got) + } +} + func TestRequestCtxInit(t *testing.T) { var ctx RequestCtx var logger testLogger