Project

General

Profile

How to retrieve host information if WEnvironment is not available?

Added by Plug Gulp 4 months ago

I am not using WApplication but having a REST API to access resources. In that case I do not have access to WEnvironment. I want to extract hostname(as seen by the client) when a request is received. WEnvironment::hostname does that and as I do not have access to it I need some alternate way to get hostname. One way I can extract that information is from HOST header. But that is not reliable. Is there any other way that hostname can be retrieved from a request?

Thanks and kind regards,

~Plug


Replies (8)

RE: How to retrieve host information if WEnvironment is not available? - Added by Roel Standaert 4 months ago

Why would the Host header not be reliable? It's possible when behind a reverse proxy that does not forward the Host header from the original request, that it does forward it through the X-Forwarded-Host header, but that's really all that WEnvironment::hostName() does.

RE: How to retrieve host information if WEnvironment is not available? - Added by Plug Gulp 4 months ago

Why would the Host header not be reliable?

There is a comment in the Wt source code suggesting that, at the place where the Host header is examined and information stored in host_ variable. The comment says that Host header may not be always available in HTTP 1.0.

It's possible when behind a reverse proxy that does not forward the Host header from the original request, that it does forward it through the X-Forwarded-Host header, but that's really all that WEnvironment::hostName() does.

And that's the reason for my query. It will help if WEnvironment is available to a Wt application even when the app does not create WApplication. If behind the scene WEnvironment is already being created for non-WApplication type of application then exposing it to these apps will help them query other useful WEnvironment parameters as well.

RE: How to retrieve host information if WEnvironment is not available? - Added by Roel Standaert 4 months ago

The comment says that Host header may not be always available in HTTP 1.0.

Yes, I don't think WEnvironment will help you in that case either. While Wt can work with it, HTTP 1.0 is ancient, and you don't usually encounter it in practice anymore.

A WEnvironment is not created for resources, since WEnvironment is not limited to things that are clear from the first request, it also (if possible) contains information that is only obtainable through JavaScript.

Usually, we'd add common things to Wt::Http::Request, though I do think that there's something to be said for having a bit of a common interface there. One idea that's come up before is handling requests in a resource-like manner, but then upgrading to a WApplication.

RE: How to retrieve host information if WEnvironment is not available? - Added by Plug Gulp 4 months ago

One idea that's come up before is handling requests in a resource-like manner, but then upgrading to a WApplication.

That is a nice idea and makes a lot of sense as well. It will also build the foundation for making REST APIs first class citizens in Wt.

RE: How to retrieve host information if WEnvironment is not available? - Added by Plug Gulp 4 months ago

A WEnvironment is not created for resources

Ok.

BTW, just out of curiosity what is the embeddedEnv_ member variable in WebSession class?

RE: How to retrieve host information if WEnvironment is not available? - Added by Roel Standaert 4 months ago

That's just the WEnvironment, which is embedded inside of the WebSession object. The exceptional case where the WEnvironment is not part of the WebSession object is when using WTestEnvironment. In any case, the env_ pointer refers to whatever WEnvironment is actually being used.

RE: How to retrieve host information if WEnvironment is not available? - Added by Plug Gulp 4 months ago

In any case, the env_ pointer refers to whatever WEnvironment is actually being used.

So that environment object can be exposed via HTTP::Request object? The request object already holds a reference to the websession which in turn holds a reference to environment object. It will help if the request handler knows under what environment the request has arrived when there is no application object available.

RE: How to retrieve host information if WEnvironment is not available? - Added by Roel Standaert 4 months ago

If there is a websession, there is an application object available. Static resources do not have a websession, so they don't have an environment.

WEnvironment and Http::Request actually both pull a lot of information from the underlying WebRequest. In the case of WEnvironment that is the initial request that the browser made starting the new session. In the case of Http::Request that is simply the underlying request.

    (1-8/8)