go - Why are request.URL.Host and Scheme blank in the development server? -
i'm new go. tried first hello, world documentation, , wanted read host , scheme request:
package hello import ( "fmt" "http" ) func init() { http.handlefunc("/", handler) } func handler(w http.responsewriter, r *http.request) { fmt.fprint(w, "host: " + r.url.host + " scheme: " + r.url.scheme) }
but values both blank. why?
basically, since you're accessing http server not http proxy, browser can issue relative http request, so:
get / http/1.1 host: localhost:8080
(given that, of course, server listening on localhost port 8080).
now, if accessing said server using proxy, proxy may use absolute url:
get http://localhost:8080/ http/1.1 host: localhost:8080
in both cases, go's http.request.url
raw url (as parsed library). in case you're getting, you're accessing url relative path, hence lack of host or scheme in url object.
if want http host, may want access host
attribute of http.request
struct. see http://golang.org/pkg/http/#request
you can validate using netcat
, appropriately formatted http request (you can copy above blocks, make sure there's trailing blank line after in file). try out:
cat my-http-request-file | nc localhost 8080
additionally, check in server/handler whether relative or absolute url in request calling isabs()
method:
isabsoluteurl := r.url.isabs()
Comments
Post a Comment