From: Michael Fraenkel Date: Thu, 19 Jan 2017 04:47:54 +0000 (-0500) Subject: net/http: make LocalAddrContext handle wildcard interface X-Git-Tag: go1.9beta1~470 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=819d1cce6e1cfc4439219a22f9356f6885546029;p=gostls13.git net/http: make LocalAddrContext handle wildcard interface The LocalAddrContext should have the network address of the actual interface. Fixes #18686 Change-Id: I9c401eda312f3a0e7e65b013af827aeeef3b4d3d Reviewed-on: https://go-review.googlesource.com/35490 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 5ead2c00eb..e140721c91 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -4561,13 +4561,6 @@ func testServerContext_ServerContextKey(t *testing.T, h2 bool) { if _, ok := got.(*Server); !ok { t.Errorf("context value = %T; want *http.Server", got) } - - got = ctx.Value(LocalAddrContextKey) - if addr, ok := got.(net.Addr); !ok { - t.Errorf("local addr value = %T; want net.Addr", got) - } else if fmt.Sprint(addr) != r.Host { - t.Errorf("local addr = %v; want %v", addr, r.Host) - } })) defer cst.close() res, err := cst.c.Get(cst.ts.URL) @@ -4577,6 +4570,37 @@ func testServerContext_ServerContextKey(t *testing.T, h2 bool) { res.Body.Close() } +func TestServerContext_LocalAddrContextKey_h1(t *testing.T) { + testServerContext_LocalAddrContextKey(t, h1Mode) +} +func TestServerContext_LocalAddrContextKey_h2(t *testing.T) { + testServerContext_LocalAddrContextKey(t, h2Mode) +} +func testServerContext_LocalAddrContextKey(t *testing.T, h2 bool) { + setParallel(t) + defer afterTest(t) + ch := make(chan interface{}, 1) + cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { + ch <- r.Context().Value(LocalAddrContextKey) + })) + defer cst.close() + if _, err := cst.c.Head(cst.ts.URL); err != nil { + t.Fatal(err) + } + + host := cst.ts.Listener.Addr().String() + select { + case got := <-ch: + if addr, ok := got.(net.Addr); !ok { + t.Errorf("local addr value = %T; want net.Addr", got) + } else if fmt.Sprint(addr) != host { + t.Errorf("local addr = %v; want %v", addr, host) + } + case <-time.After(5 * time.Second): + t.Error("timed out") + } +} + // https://golang.org/issue/15960 func TestHandlerSetTransferEncodingChunked(t *testing.T) { setParallel(t) diff --git a/src/net/http/server.go b/src/net/http/server.go index f29fa1272c..a9d7396106 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -1714,6 +1714,7 @@ func isCommonNetReadError(err error) bool { // Serve a new connection. func (c *conn) serve(ctx context.Context) { c.remoteAddr = c.rwc.RemoteAddr().String() + ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr()) defer func() { if err := recover(); err != nil && err != ErrAbortHandler { const size = 64 << 10 @@ -2680,7 +2681,6 @@ func (srv *Server) Serve(l net.Listener) error { baseCtx := context.Background() // base is always background, per Issue 16220 ctx := context.WithValue(baseCtx, ServerContextKey, srv) - ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr()) for { rw, e := l.Accept() if e != nil {