getting the underlying connection from an http.Request?

1,390 views
Skip to first unread message

Tycho Andersen

unread,
Jun 10, 2015, 1:07:40 AM6/10/15
to golan...@googlegroups.com
Hi all,

I'm writing an HTTP server that listens on a unix socket and serves a
typical REST API. The twist is that I'm interested in figuring out
which process a particular request came from, which I can do using
unix creds, using net.UnixConn.{Read,Write}MsgUnix on the underlying
connection that the HTTP request came from.

I'd like to use net/http to serve this content, but as near as I can
tell there's no way to access the underlying net.Conn for a
http.Request. I don't want to Hijack(), because all I want to do is
use the pid that the request came from when rendering the content of
the request.

Is there a nice way to do this?

Thanks,

Tycho

Benjamin Measures

unread,
Jun 10, 2015, 8:21:24 PM6/10/15
to golan...@googlegroups.com, tycho.a...@canonical.com
On Wednesday, 10 June 2015 06:07:40 UTC+1, Tycho Andersen wrote:
I'm writing an HTTP server that listens on a unix socket [...] using
unix creds, using net.UnixConn.{Read,Write}MsgUnix on the underlying
connection

Since you're going to need to set socket options, you may as well implement your own net.listener:

Example wrapper:

Tycho Andersen

unread,
Jun 10, 2015, 9:40:46 PM6/10/15
to Benjamin Measures, golan...@googlegroups.com
On Wed, Jun 10, 2015 at 05:21:24PM -0700, Benjamin Measures wrote:
> On Wednesday, 10 June 2015 06:07:40 UTC+1, Tycho Andersen wrote:
> >
> > I'm writing an HTTP server that listens on a unix socket [...] using
> > unix creds, using net.UnixConn.{Read,Write}MsgUnix on the underlying
> > connection
>
>
> Since you're going to need to set socket options, you may as well implement
> your own net.listener:
> http://golang.org/pkg/net/#Listener

I thought about this, but supposing I allow the connection, how do I
"pass" which pid it came from through to the code that's doing the
writing to http.ResponseWriter?

Thanks,

Tycho

> Example wrapper:
> http://golang.org/src/net/http/server.go#L1967

Matt Harden

unread,
Jun 11, 2015, 4:33:08 PM6/11/15
to Tycho Andersen, Benjamin Measures, golan...@googlegroups.com
Consider using "golang.org/x/net/context". See http://blog.golang.org/context. You could create a map from remote address to context, and extract the userid from the context. Of course, if you only need the userid, then the map values could just be strings, but context is more general and flexible.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tycho Andersen

unread,
Jun 11, 2015, 4:56:13 PM6/11/15
to Matt Harden, Benjamin Measures, golan...@googlegroups.com
Hi Matt,

On Thu, Jun 11, 2015 at 08:32:37PM +0000, Matt Harden wrote:
> Consider using "golang.org/x/net/context". See
> http://blog.golang.org/context. You could create a map from remote address
> to context, and extract the userid from the context. Of course, if you only
> need the userid, then the map values could just be strings, but context is
> more general and flexible.

Thanks. I don't see how a Context helps me pass the values, though.
http.Server.Serve just takes a net.Listener, which can only return
net.Conns, and no additional information for a connection like a
Context. Is there some piece here I'm missing?

Thanks,

Tycho

Matt Harden

unread,
Jun 12, 2015, 10:43:13 AM6/12/15
to Tycho Andersen, golan...@googlegroups.com

You get the source address of the connection with conn.RemoteAddr().String() and use that as a key to store your contexts in a global map. Then the http handler can use req.RemoteAddr() to look up the context. Of course remember to delete the map entry when the connection is closed so the map doesn't grow without bound.

Creating and destroying Contexts (adding and removing entries in the global map) could be done with the Server.ConnState hook, which gives you access to the underlying net.Conn. See golang.org/pkg/net/http/#ConnState.

Reply all
Reply to author
Forward
0 new messages