I have a Flask application behind a Load balance that terminates SSL. I have code that "detects" when SSL is being used and mutates the request object:
@app.before_request
def before_request():
x_forwarded_proto = request.headers.get('X-Forwarded-Proto')
if x_forwarded_proto == 'https':
request.url = request.url.replace('http://', 'https://')
request.url_root = request.url_root.replace('http://', 'https://')
request.host_url = request.host_url.replace('http://', 'https://')
I then have a blueprint view function:
admin = Blueprint('admin', __name__, url_prefix='/admin')
@admin.route('/login')
def login():
print request.url
The output of this function is (when I go to /admin/login) is always http:// instead of https:// (even though it should have been mutated in the before_request
function.
Any ideas on how I can fix this?
Turns out
request
is a proxied object. I'm not sure of the internals but it's "reset" on each import. I solved the issue by subclassingRequest