You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bu...@apache.org on 2016/05/26 18:18:56 UTC

[Bug 57665] support x-forwarded-host

https://bz.apache.org/bugzilla/show_bug.cgi?id=57665

--- Comment #2 from Robert <ro...@gmail.com> ---
The best workaround so far (which is really just a hack) is to extend
Http11NioProtocol as shown below:

/**
 * Custom Tomcat Protocol based off of Http11NioProtocol that looks for an
 * X-Forwarded-Host header and sets the serverName in the request to that 
 * value.
 * 
 * This couldn't be done in a Valve because a Valve is processed too late to 
 * handle a context name redirect. For example if the url
 * `https://example.org/book` is requested.  Really early in the request 
 * Tomcat will redirect this url to `https://example.org/book/`.
 * This protocol will provide the X-Forwarded-Host header value even for that
 * type of redirect.
 * 
 * To use simply set this class as the value of the
 * {@code Connector->protocol} attribute in server.xml
 */
public static class XForwardedHostHandlingHttp11NioProtocol extends
Http11NioProtocol
{
    @Override
    public void setAdapter(final Adapter adapter) {
        Adapter adapterFacade = (Adapter) Proxy.newProxyInstance(
                XForwardedHostHandlingHttp11NioProtocol.class.getClassLoader(),
                new Class[] {Adapter.class}, (proxy, method, args) ->
                {
                    if (method.getName().equals("service")) {
                        Request req = (Request)args[0];
                        String header = req.getHeader("X-Forwarded-Host");
                        if (header != null) {
                            req.serverName().setString(header);
                        }
                    }
                    return method.invoke(adapter, args);
                });
        super.setAdapter(adapterFacade);
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org