Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hijack): reset userValues after hijack handler execution #1199

Merged
merged 4 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions server.go
Expand Up @@ -2375,6 +2375,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {

if hijackHandler != nil {
var hjr io.Reader = c
hctx := ctx
if br != nil {
hjr = br
br = nil
Expand All @@ -2394,7 +2395,7 @@ func (s *Server) serveConn(c net.Conn) (err error) {
if err != nil {
break
}
go hijackConnHandler(hjr, c, s, hijackHandler)
go hijackConnHandler(hctx, hjr, c, s, hijackHandler)
err = errHijacked
break
}
Expand Down Expand Up @@ -2446,7 +2447,7 @@ func (s *Server) setState(nc net.Conn, state ConnState) {
}
}

func hijackConnHandler(r io.Reader, c net.Conn, s *Server, h HijackHandler) {
func hijackConnHandler(ctx *RequestCtx, r io.Reader, c net.Conn, s *Server, h HijackHandler) {
hjc := s.acquireHijackConn(r, c)
h(hjc)

Expand All @@ -2457,6 +2458,7 @@ func hijackConnHandler(r io.Reader, c net.Conn, s *Server, h HijackHandler) {
c.Close()
s.releaseHijackConn(hjc)
}
ctx.ResetUserValues()
}

func (s *Server) acquireHijackConn(r io.Reader, c net.Conn) *hijackConn {
Expand Down
31 changes: 31 additions & 0 deletions server_test.go
Expand Up @@ -2088,6 +2088,37 @@ func TestServeConnKeepRequestAndResponseUntilResetUserValues(t *testing.T) {
}
}

func TestServeConnHijackResetUserValues(t *testing.T) {
t.Parallel()

rw := &readWriter{}
rw.r.WriteString("GET /foo HTTP/1.0\r\nConnection: keep-alive\r\nHost: google.com\r\n\r\n")
rw.r.WriteString("")

ch := make(chan struct{})
go func() {
err := ServeConn(rw, func(ctx *RequestCtx) {
ctx.Hijack(func(c net.Conn) {})
ctx.SetUserValue("myKey", &closerWithRequestCtx{
closeFunc: func(_ *RequestCtx) error {
close(ch)

return nil
}},
)
})
if err != nil {
t.Errorf("unexpected error in ServeConn: %s", err)
}
}()

select {
case <-ch:
case <-time.After(time.Second):
t.Errorf("Timeout: UserValues should be reset")
}
}

func TestServeConnNonHTTP11KeepAlive(t *testing.T) {
t.Parallel()

Expand Down