mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
fix client_addr following observation of @dacisp and reading
http://en.wikipedia.org/wiki/X-Forwarded-For .
This commit is contained in:
parent
c3801b5283
commit
5796651a78
@ -38,11 +38,8 @@ class Request(object):
|
|||||||
def __init__(self, socket, client_address, server_address, debug=False):
|
def __init__(self, socket, client_address, server_address, debug=False):
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.socket = socket
|
self.socket = socket
|
||||||
|
|
||||||
# authors should be aware that REMOTE_HOST and REMOTE_ADDR
|
self.client_address = client_address
|
||||||
# may not qualify the remote addr:
|
|
||||||
# http://www.ietf.org/rfc/rfc3875
|
|
||||||
self.client_address = client_address or ('127.0.0.1', '')
|
|
||||||
self.server_address = server_address
|
self.server_address = server_address
|
||||||
self.response_status = None
|
self.response_status = None
|
||||||
self.response_headers = {}
|
self.response_headers = {}
|
||||||
@ -87,11 +84,34 @@ class Request(object):
|
|||||||
wsgi_multiprocess = True
|
wsgi_multiprocess = True
|
||||||
|
|
||||||
|
|
||||||
# Try to server address from headers
|
|
||||||
if 'X-Forwarded-For' in self.parser.headers_dict:
|
|
||||||
server_address = self.parser.headers_dict.get('X-Forwarded-For')
|
# authors should be aware that REMOTE_HOST and REMOTE_ADDR
|
||||||
|
# may not qualify the remote addr:
|
||||||
|
# http://www.ietf.org/rfc/rfc3875
|
||||||
|
try:
|
||||||
|
if 'X-Forwarded-For' in self.parser.headers_dict:
|
||||||
|
forward_adress = self.parser.headers_dict.get('X-Forwarded-For')
|
||||||
|
|
||||||
|
# we only took the last one
|
||||||
|
# http://en.wikipedia.org/wiki/X-Forwarded-For
|
||||||
|
if "," in forward_adress:
|
||||||
|
forward_adress = forward_adress.split(",")[-1].strip()
|
||||||
|
|
||||||
|
if ":" in forward_adress:
|
||||||
|
remote_addr, remote_port = forward_adress.split(':')
|
||||||
|
else:
|
||||||
|
remote_addr, remote_port = (forward_adress, '')
|
||||||
|
elif self.client_adress:
|
||||||
|
remote_addr, remote_port = self.client_adress
|
||||||
|
else:
|
||||||
|
remote_addr, remote_port = ('127.0.0.1', '')
|
||||||
|
except:
|
||||||
|
remote_addr, remote_port = ('127.0.0.1', '')
|
||||||
|
|
||||||
elif 'Host' in self.parser.headers_dict:
|
|
||||||
|
# Try to server address from headers
|
||||||
|
if 'Host' in self.parser.headers_dict:
|
||||||
server_address = self.parser.headers_dict.get('Host')
|
server_address = self.parser.headers_dict.get('Host')
|
||||||
else:
|
else:
|
||||||
server_address = self.server_address
|
server_address = self.server_address
|
||||||
@ -133,6 +153,7 @@ class Request(object):
|
|||||||
if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'):
|
if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'):
|
||||||
environ[key] = value
|
environ[key] = value
|
||||||
|
|
||||||
|
self.log.info(environ)
|
||||||
return environ
|
return environ
|
||||||
|
|
||||||
def start_response(self, status, response_headers, exc_info=None):
|
def start_response(self, status, response_headers, exc_info=None):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user