Skip to content

Commit

Permalink
initial redirection support
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 18, 2016
1 parent 82c6a77 commit ea8a25f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/netius/extra/proxy_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(
regex = {},
hosts = {},
auth = {},
redirect = {},
strategy = "smart",
reuse = True,
*args,
Expand All @@ -78,6 +79,7 @@ def __init__(
regex = regex,
hosts = hosts,
auth = auth,
redirect = redirect,
strategy = strategy,
reuse = reuse,
robin = dict(),
Expand Down Expand Up @@ -113,6 +115,23 @@ def on_headers(self, connection, parser):
protocol = headers.get("x-forwarded-proto", None)
protocol = protocol or ("https" if is_secure else "http")

# tries to determine if a proper (client side) redirection should operation
# should be applied to the current request, if that's the case (match) an
# immediate response is returned with proper redirection instructions
redirect = self.redirect.get(host, None)
if redirect:
location = "%s://%s%s" % (protocol, redirect, path)
connection.send_response(
headers = dict(
location = location
),
version = version_s,
code = 303,
code_s = "See Other",
apply = True
)
return

# tries to extract the various attributes of the current connection
# that are going to be used for the routing operation, this attributes
# should avoid a new rule setting operation (expensive) and provide
Expand Down Expand Up @@ -387,9 +406,14 @@ def _on_prx_close(self, client, _connection):
auth = {
"host.com" : netius.PasswdAuth("extras/htpasswd")
}
redirect = {
"host.com" : "other.host.com"
}
server = ReverseProxyServer(
regex = regex,
hosts = hosts,
auth = auth,
redirect = redirect,
level = logging.INFO
)
server.serve(env = True)

0 comments on commit ea8a25f

Please sign in to comment.