Commit 51f321d8 authored by Georgios Dagkakis's avatar Georgios Dagkakis

WSGITask: Patch should be enabled only if waitress egg is available

parent 19add837
# -*- coding: utf-8 -*-
import ZPublisher.HTTPRequest
from waitress.task import WSGITask
try:
from waitress.task import WSGITask
except ImportError:
WSGITask = None
WSGITask_get_environment = WSGITask.get_environment
# Patch should be enabled only if waitress egg is available
if WSGITask is not None:
WSGITask_get_environment = WSGITask.get_environment
def get_environment(self):
if ZPublisher.HTTPRequest.trusted_proxies == ('0.0.0.0',): # Magic value to enable this functionality
# Frontend-facing proxy is responsible for sanitising
# X_FORWARDED_FOR, and only trusted accesses should bypass
# that proxy. So trust first entry.
forwarded_for = dict(self.request.headers).get('X_FORWARDED_FOR', '').split(',', 1)[0].strip()
else:
forwarded_for = None
def get_environment(self):
if ZPublisher.HTTPRequest.trusted_proxies == ('0.0.0.0',): # Magic value to enable this functionality
# Frontend-facing proxy is responsible for sanitising
# X_FORWARDED_FOR, and only trusted accesses should bypass
# that proxy. So trust first entry.
forwarded_for = dict(self.request.headers).get('X_FORWARDED_FOR', '').split(',', 1)[0].strip()
else:
forwarded_for = None
environ = WSGITask_get_environment(self)
environ = WSGITask_get_environment(self)
if forwarded_for:
environ['REMOTE_HOST'] = environ['REMOTE_ADDR'] = forwarded_for
if forwarded_for:
environ['REMOTE_HOST'] = environ['REMOTE_ADDR'] = forwarded_for
return environ
return environ
WSGITask.get_environment = get_environment
WSGITask.get_environment = get_environment
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment