aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/server.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2016-04-30 21:11:26 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2016-05-01 06:06:07 +0000
commita9cf0b1e1e2a66db547fcabb7188465e4ac54700 (patch)
treee813e25d816bae7a3d55dbfe13f28958a6b0e2b9 /src/net/http/server.go
parentabc1472d78c70888473634497b49b1c2e1bb6569 (diff)
downloadgo-a9cf0b1e1e2a66db547fcabb7188465e4ac54700.tar.xz
net/http: provide access to the listener address an HTTP request arrived on
This adds a context key named LocalAddrContextKey (for now, see #15229) to let users access the net.Addr of the net.Listener that accepted the connection that sent an HTTP request. This is similar to ServerContextKey which provides access to the *Server. (A Server may have multiple Listeners) Fixes #6732 Change-Id: I74296307b68aaaab8df7ad4a143e11b5227b5e62 Reviewed-on: https://go-review.googlesource.com/22672 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/net/http/server.go')
-rw-r--r--src/net/http/server.go7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/net/http/server.go b/src/net/http/server.go
index c36f5a06ba..23fb84fcda 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -176,6 +176,12 @@ var (
// started the handler. The associated value will be of
// type *Server.
ServerContextKey = &contextKey{"http-server"}
+
+ // LocalAddrContextKey is a context key. It can be used in
+ // HTTP handlers with context.WithValue to access the address
+ // the local address the connection arrived on.
+ // The associated value will be of type net.Addr.
+ LocalAddrContextKey = &contextKey{"local-addr"}
)
// A conn represents the server side of an HTTP connection.
@@ -2189,6 +2195,7 @@ func (srv *Server) Serve(l net.Listener) error {
// use cases yet.
baseCtx := context.Background()
ctx := context.WithValue(baseCtx, ServerContextKey, srv)
+ ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr())
for {
rw, e := l.Accept()
if e != nil {