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

net/http: add Request.LocalAddr #6732

Closed
gopherbot opened this Issue Nov 7, 2013 · 18 comments

Comments

Projects
None yet
8 participants
@gopherbot

gopherbot commented Nov 7, 2013

by joijoi360:

Before filing a bug, please check whether it has been fixed since the
latest release. Search the issue tracker and check that you're running the
latest version of Go:

Run "go version" and compare against
http://golang.org/doc/devel/release.html  If a newer version of Go exists,
install it and retry what you did to reproduce the problem.

Thanks.

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1.http://play.golang.org/p/-DIhMc_XjT
2.http://play.golang.org/p/55V_nyuKPR(There is revised code net/http/server.go)
3.http://play.golang.org/p/jroO70Cno8(There is revised code net/http/request.go)

What is the expected output?
Request.LocalAddr

What do you see instead?
The LocalAddr isn't here.

Which compiler are you using (5g, 6g, 8g, gccgo)?
gccgo

Which operating system are you using?
os x 10.9

Which version are you using?  (run 'go version')
go version go1.1.2 darwin/amd64

Please provide any additional information below.
because the network line and the operators do IP,there isn't LocalAddr in Rep
resource.requiring different
@gopherbot

This comment has been minimized.

gopherbot commented Nov 7, 2013

Comment 1 by joijoi360:

1.http://play.golang.org/p/-DIhMc_XjT
2.http://play.golang.org/p/55V_nyuKPR  (There is revised code net/http/server.go)
3.http://play.golang.org/p/jroO70Cno8  (There is revised code net/http/request.go)
@gopherbot

This comment has been minimized.

gopherbot commented Nov 7, 2013

Comment 2 by vslene:

LGTM. I think this means need a small enhanced. Add a new variable LocalAddr to
http.Request. When bind *:80, it can be used detect the http request which net address .
@rsc

This comment has been minimized.

Contributor

rsc commented Nov 27, 2013

Comment 3:

Labels changed: added go1.3maybe.

@dsymonds

This comment has been minimized.

Member

dsymonds commented Dec 2, 2013

Comment 4:

Labels changed: added priority-later, feature, packagechange, removed priority-triage.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 5:

Labels changed: added release-none, removed go1.3maybe.

@rsc

This comment has been minimized.

Contributor

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

@davecheney

This comment has been minimized.

Contributor

davecheney commented Jul 16, 2014

Comment 7:

Marking this as long term, I haven't seen a request for this in close to a year so
further justification would need to be provided.

Status changed to LongTerm.

@gopherbot

This comment has been minimized.

gopherbot commented Jul 16, 2014

Comment 8 by luke@hydrantlabs.org:

I am in need of this feature. In fact, I am currently running the same server bound to
different ips to get around this issue.
@davecheney

This comment has been minimized.

Contributor

davecheney commented Jul 17, 2014

Comment 9:

Luke, please write to golang-dev and explain why you need this feature, and specifically
what you cannot do today that justifies its addition.
@bradfitz

This comment has been minimized.

Member

bradfitz commented Jul 17, 2014

Comment 10:

Yeah, it seems like there are plenty of ways to do this already with a wrapper for
people who care (two people?). I'm inclined to say WontFix as I don't think it justifies
the API surface increase, nor the memory/CPU increase.
@gopherbot

This comment has been minimized.

gopherbot commented Sep 23, 2014

Comment 11 by sajal83:

I need this as well. I have a usecase where I want to return (very) slightly modified
response based on which IP the client used to connect to. And for a particular reason I
would like to use the same process to serve all IP. I could perhaps get away with
registering multiple servers each listening to a different IP and using custom handler
for each server... but it sounds ugly.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@fiorix

This comment has been minimized.

fiorix commented Apr 17, 2015

I also need this feature.

@bradfitz

This comment has been minimized.

Member

bradfitz commented Apr 17, 2015

Sorry, I'm going to close this issue without action. There are few enough cases where this is required that I don't think it's worth complicating the net/http package API and making the Request struct larger.

There's an easy enough workaround: run a Server on each interface with a Handler that sets a magic HTTP request header and then delegates to a shared root handler.

@bradfitz bradfitz closed this Apr 17, 2015

@fiorix

This comment has been minimized.

fiorix commented Apr 17, 2015

Well, the reason I needed this is to detect loops in a proxy. Using ConnState works for me.

@staunch

This comment has been minimized.

staunch commented Jun 10, 2015

Listening on 0.0.0.0 and then using LocalAddr for ACLs/partitioning traffic is an extremely efficient, convenient, and common pattern among unix servers. It's a real shame net/http can't do it. In my case, my program listens on hundreds of dynamic IPs and it's a real problem not having access to LocalAddr. Just another +1. Thanks for all your hard work.

@gopherbot

This comment has been minimized.

gopherbot commented May 1, 2016

CL https://golang.org/cl/22672 mentions this issue.

gopherbot pushed a commit that referenced this issue May 1, 2016

net/http: provide access to the listener address an HTTP request arri…
…ved 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>
@SpComb

This comment has been minimized.

SpComb commented Jan 17, 2017

With Go 1.7, there is a httpRequest.Context().Value(http.LocalAddrContextKey).(net.Addr), but this value is always the same listen address as passed to http.ListenAndServe. So for a server having multiple interfaces/local addresses, and starting a Go http server on :8000, it will always be [::]:8000, regardless of which server address the client connected to.

That makes this feature somewhat useless.

I think the desired behavior would be to return the actual net.Conn.LocalAddr() for the accepted connection. This would return different values for clients connecting to different local addresses on the same server having a single http.Sever listening on 0.0.0.0/::.

Workaround

Hijack the http.ResponseWriter and then get at the conn.LocalAddr()

@bradfitz

This comment has been minimized.

Member

bradfitz commented Jan 17, 2017

@SpComb, thanks for the note, but in general comments on closed issues are where action items go to die. I've opened #18686 so this can actually be tracked.

@golang golang locked and limited conversation to collaborators Jan 17, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.