updates to redirects

This commit is contained in:
2018-10-11 12:22:33 -04:00
parent 151c7bd342
commit 3bcfe89f2a
3 changed files with 9 additions and 4 deletions
+1
View File
@@ -1,3 +1,4 @@
- Kenneth Reitz (primary)
- Tom Christie
- Bruno Oliveira
- serhii73
+1 -2
View File
@@ -12,7 +12,7 @@ def hello_world():
return "Hello, World from flask!"
api = responder.API()
api = responder.API(enable_hsts=True)
api.mount("/hello", app)
@@ -40,7 +40,6 @@ schema = graphene.Schema(query=Query)
# Alerntatively,
api.add_route("/graph", schema, graphiql=True)
print(
api.session()
.get(
+7 -2
View File
@@ -96,6 +96,11 @@ class API:
route = self.path_matches_route(req.path)
resp = models.Response(req=req)
if self.enable_hsts:
if req.url.startswith("http://"):
url = req.url.replace("http://", "https://", 1)
self.redirect(resp, location=url)
if route:
try:
params = self.routes[route].incoming_matches(req.path)
@@ -149,8 +154,8 @@ class API:
resp.status_code = HTTP_404
resp.text = "Not found."
def redirect(self, location, *, status_code=status_codes.HTTP_301):
rep.status_code = status_code
def redirect(self, resp, location, *, status_code=status_codes.HTTP_301):
resp.status_code = status_code
resp.text = f"Redirecting to: {location}"
resp.headers.update({"Location": location})