-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: *Request.URL.Scheme returns an empty string. No alternative way present to get request url scheme. #28940
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
Comments
You won't get the scheme here. It is http by virtue of this being a plain (Non-TLS listener). A https request could not have worked. |
I think checking this field for nil on http.Request is the closest you will get: // TLS allows HTTP servers and other software to record
// information about the TLS connection on which the request
// was received. This field is not filled in by ReadRequest.
// The HTTP server in this package sets the field for
// TLS-enabled connections before invoking a handler;
// otherwise it leaves the field nil.
// This field is ignored by the HTTP client.
TLS *tls.ConnectionState |
FWIW, since often what you will be wanting this for is to construct a URL non-relative URL for a client, it is desirable to try and respect the x-forwarded-proto header. The client may have used TLS, even though the server processing the request did not receive it over TLS. |
Thanks. Checking for a nil |
Checking TLS is all well and good if the only schemes in existence were http and https, but the real world is far broader than that. I arrived here looking for the best way to check for a ws or wss scheme for proxying purposes. I'm sure others have use cases for other schemes as well. |
@ChrisSalisbury the scheme simply doesn't exist as part of the HTTP protocol. It's not in the request at all, there's no way to populate the field. The scheme is used when a client interprets a URL for the client to decide which language to use when talking to the server, there's no point where the client is mandate to tell the server what "language" is being used to talk to to it (there's an assumption that if we are communicating at all, then we must already know what language we are talking). |
I am new to golang and I found the URL API very confusing, IMHO, for developer convenience there should be an API somewhere (could be elsewhere than in http or url) to get the full URL from any http(s) request. I sorted it out by re-constructing from above mentioned hints. For the record here is a link to mentioned RFC: |
Checking on the request URL TLS does the trick, thanks. But feels weird 😕 |
The suggestions here focus on disambiguating |
The fundamental point here is that we simply do not know what the scheme is. Scheme is part of the URI spec. As unpalatable as it is, the http requests never gets the URL the client requested, it only gets the path (and, most of the time, the hostname), |
@lthibault websockets start off as a normal HTTP(S) GET requests that come with headers that look like:
|
@DisposaBoy Right, but I was hoping to route requests at a given endpoint (e.g. Sounds like this isn't possible, so I'll likely have to use a query parameter such as |
@lthibault if you just want to know if it's a websocket request, can't you look for the connection upgrade headers? |
@lthibault I'm not sure I understand. If you can detect that it's a WS connection by the presence of a |
if you want to check ws: vs wss: then you can do both the check for an existing TLS negotiation (or X-Forwarded-For-Scheme), and the upgrade headers. |
@DisposaBoy @tcolgate Of course... how did I not think of that. 🤦♂ Many thanks! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes!
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
What did you see instead?
The SCHEME: is empty!
Yes!
I read this part of godoc!
The most focused part is:
But there should be an alternative way to not get an empty string. As
r.URL.Host
has an alternativer.Host
. But what aboutr.URL.Scheme
?No way!
The text was updated successfully, but these errors were encountered: