Skip to content
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

Handlers that handle proxy specific things, such as resolving a full url for outside use, or knowing if https #157

Closed
apatrida opened this issue Jul 17, 2015 · 6 comments
Milestone

Comments

@apatrida
Copy link

A lot of people write common code to deal with things like:

"am I on secure connection?" (have to look at proxy / load balancer headers to know)

"I need a fully qualified link to one of my routes for an outside resource to use" (for example, giving an oauth service the callback URL and making it correct for the given server, protocol, host, etc)

@apatrida
Copy link
Author

Yes, the auto redirects are to solve the issues that are common, for the common headers that load balancers / proxies use. Currently, everyone has to learn how to do that themselves or do some hacky way (config for what my root path of my app is, parse the URL, shove in the new part of the URL, redirect) which breaks down when you do more virtual hosts and more complicated scenarios.

For example, one framework I see does about this much work which is pretty standard that I would prefer everyone not try to figure out on their own:

open class RedirectResult(val url: String) : ActionResult {
    override fun writeResponse(context: ActionContext) {
        val scheme = context.request.getHeader("X-Forwarded-Proto") let { scheme ->
            val finalShceme = if (scheme.isNullOrBlank()) { context.request.getScheme() } else scheme
            finalScheme
        }
        val host = context.request.getHeader("X-Forwarded-Host") let {  host ->
            val hostWithPossiblePort = if (host.isNullOrBlank()) { context.request.getServerName() } else { host }
            hostWithPossiblePort.substringBefore(':')
        }
        val port = context.request.getHeader("X-Forwarded-Port") let  { port ->
            val tempPort = if (port.isNullOrBlank())  { context.request.getServerPort()  } else { port }
            val finalPort = if (scheme == "https" && tempPort == "443") { "" }
                            else if (scheme == "http" && tempPort == "80") { "" }
                            else ":$tempPort"
            finalPort
        }

        val finalUrl = if (url.startsWith("http://") || url.startsWith("https://")) {
            context.response.sendRedirect(url)
        }
        else if (url.startsWith("//")) {
            val goto = "$scheme:$url"
            context.response.sendRedirect(goto)
        } else if (url.startsWith("/")) {
            val goto = "$scheme://$host$port$url"
            context.response.sendRedirect(goto)
        } else {
            val oldUri = URI(context.request.getRequestURI())
            val newUri = oldUri.resolve(url)
            val builder = buildUri(newUri).setScheme(scheme).setHost(host)
            val newRedirect = builder.toString()
            context.response.sendRedirect(newRedirect)
        }
        finalUrl
    }
}

That is the minimal redirect, and makes assumptions that are likely correct for http https protocol URL's.

@apatrida
Copy link
Author

And if your deployment has a root context (such as servlet base path) that has to be taken into account, etc.

@pmlopes
Copy link
Member

pmlopes commented Aug 31, 2015

@apartida if you could translate that to Java and submit a PR then it would help getting this into master as something like SecureUpgradeHandler or some better name...

@slayful
Copy link
Contributor

slayful commented Jan 8, 2016

Need some help with that?

@slayful
Copy link
Contributor

slayful commented Jan 9, 2016

Here's a non-working prototype: slayful@8cb7b74
Is this the right direction?

@pmlopes pmlopes added this to the 4.0.0 milestone Dec 12, 2019
@pmlopes
Copy link
Member

pmlopes commented Dec 12, 2019

This has been fixed with: 136ff03 and other improvements.

@pmlopes pmlopes closed this as completed Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants