1

I'm using one of the golang samples to build a WebSocket server. According to the samples the struct Client access websocket.Conn here and websocket.Conn has a Request method returning *http.Request.

My idea is to subscribe the user to different channels depending on the subdomain they use to enter the app.

So is it possible to get the full URL the user hit when subscribed to the websocket? Using the sample code I thought ws.Conn.Request().URL.Host would give me something like ws://channel1.lvh.me:8080/sync but instead I'm getting the path: "/chat".

Is it possible to get the full URL the user used to connect to the websocket? Thanks in advance.

Saul Martínez
  • 920
  • 13
  • 28
  • Related: [How to get URL in http.Request](http://stackoverflow.com/questions/23151827/how-to-get-url-in-http-request), also: [Why are request.URL.Host and Scheme blank in the development server?](http://stackoverflow.com/questions/6899069/why-are-request-url-host-and-scheme-blank-in-the-development-server) Also useful: [Is the full, absolute URL of an HTTP request directly available?](http://grokbase.com/t/gg/golang-nuts/144j758twr/go-nuts-is-the-full-absolute-url-of-an-http-request-directly-available) – icza Feb 25 '16 at 08:50

2 Answers2

1

Examine the host header to get the requested host:

host := c.Request().Host

The Request.URL field is parsed from the HTTP request URI. For most requests, the request URI contains path and query only.

An application can reconstruct a full URL from the host header and knowledge of the protocols the application is serving. For example, if the application is listening for HTTP requests (or WS request that use HTTP for handshake), the full URL is:

u := *c.Request().URL
u.Host = c.Request().Host
u.Scheme = "http"
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
0

func (*Conn) Request returns *http.Request

func (ws *Conn) Request() *http.Request

Checking the http.Request from the http package you will observe that it has a URL *url.URL type. This means that you should get the full url with:

ws.Request().URL.Path
Endre Simo
  • 11,330
  • 2
  • 40
  • 49