diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1a04376..d8ba199 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,4 @@ - Kenneth Reitz (primary) +- Tom Christie - Bruno Oliveira - serhii73 diff --git a/app.py b/app.py index 7b64e92..04d9128 100644 --- a/app.py +++ b/app.py @@ -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( diff --git a/responder/api.py b/responder/api.py index 9b3abfb..1e377d0 100644 --- a/responder/api.py +++ b/responder/api.py @@ -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})