Commit bbbc6589 authored by Roman Kollár's avatar Roman Kollár Committed by Brad Fitzpatrick

net/http: fix Server.ConnContext modifying context for all new connections

Fixes #35750

Change-Id: I65d38cfc5ddd66131777e104c269cc3559b2471d
GitHub-Last-Rev: 953fdfd49b2be665be43f8148d2a6180dae3b91c
GitHub-Pull-Request: golang/go#35751
Reviewed-on: https://go-review.googlesource.com/c/go/+/208318Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 94e9a5e1
...@@ -6126,6 +6126,39 @@ func TestServerContextsHTTP2(t *testing.T) { ...@@ -6126,6 +6126,39 @@ func TestServerContextsHTTP2(t *testing.T) {
} }
} }
// Issue 35750: check ConnContext not modifying context for other connections
func TestConnContextNotModifyingAllContexts(t *testing.T) {
setParallel(t)
defer afterTest(t)
type connKey struct{}
ts := httptest.NewUnstartedServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
rw.Header().Set("Connection", "close")
}))
ts.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
if got := ctx.Value(connKey{}); got != nil {
t.Errorf("in ConnContext, unexpected context key = %#v", got)
}
return context.WithValue(ctx, connKey{}, "conn")
}
ts.Start()
defer ts.Close()
var res *Response
var err error
res, err = ts.Client().Get(ts.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
res, err = ts.Client().Get(ts.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
}
// Issue 30710: ensure that as per the spec, a server responds // Issue 30710: ensure that as per the spec, a server responds
// with 501 Not Implemented for unsupported transfer-encodings. // with 501 Not Implemented for unsupported transfer-encodings.
func TestUnsupportedTransferEncodingsReturn501(t *testing.T) { func TestUnsupportedTransferEncodingsReturn501(t *testing.T) {
......
...@@ -2920,16 +2920,17 @@ func (srv *Server) Serve(l net.Listener) error { ...@@ -2920,16 +2920,17 @@ func (srv *Server) Serve(l net.Listener) error {
} }
return err return err
} }
connCtx := ctx
if cc := srv.ConnContext; cc != nil { if cc := srv.ConnContext; cc != nil {
ctx = cc(ctx, rw) connCtx = cc(connCtx, rw)
if ctx == nil { if connCtx == nil {
panic("ConnContext returned nil") panic("ConnContext returned nil")
} }
} }
tempDelay = 0 tempDelay = 0
c := srv.newConn(rw) c := srv.newConn(rw)
c.setState(c.rwc, StateNew) // before Serve can return c.setState(c.rwc, StateNew) // before Serve can return
go c.serve(ctx) go c.serve(connCtx)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment