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

net/http: add Request.LocalAddr #6732

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

net/http: add Request.LocalAddr #6732

gopherbot opened this issue Nov 7, 2013 · 18 comments

Comments

@gopherbot
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 3:

Labels changed: added go1.3maybe.

@dsymonds
Copy link
Contributor

dsymonds commented Dec 2, 2013

Comment 4:

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

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 5:

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

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

@davecheney
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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
Copy link

fiorix commented Apr 17, 2015

I also need this feature.

@bradfitz
Copy link
Contributor

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.

@fiorix
Copy link

fiorix commented Apr 17, 2015

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

@staunch
Copy link

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
Copy link
Author

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

gopherbot pushed a commit that referenced this issue May 1, 2016
…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
Copy link

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
Copy link
Contributor

@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.
Projects
None yet
Development

No branches or pull requests

8 participants