From e5d5d3241f81fa7ad5052079ba9cf6b2daf7c25b Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 26 May 2012 11:35:42 -0700 Subject: [PATCH] Delay check for socket.family (and make it jython-friendly). Now HTTPConnection.address will always be the socket address, and the fake "0.0.0.0" IP is only used in contexts that want an IP (i.e. HTTPRequest.remote_ip) but the connection is a non-IP socket. --- tornado/httpserver.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 5df8da3e..e930e9f7 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -161,9 +161,6 @@ class HTTPConnection(object): def __init__(self, stream, address, request_callback, no_keep_alive=False, xheaders=False): self.stream = stream - if self.stream.socket.family not in (socket.AF_INET, socket.AF_INET6): - # Unix (or other) socket; fake the remote address - address = ('0.0.0.0', 0) self.address = address self.request_callback = request_callback self.no_keep_alive = no_keep_alive @@ -238,9 +235,20 @@ class HTTPConnection(object): if not version.startswith("HTTP/"): raise _BadRequestException("Malformed HTTP version in HTTP Request-Line") headers = httputil.HTTPHeaders.parse(data[eol:]) + + # HTTPRequest wants an IP, not a full socket address + if getattr(self.stream.socket, 'family', socket.AF_INET) in ( + socket.AF_INET, socket.AF_INET6): + # Jython 2.5.2 doesn't have the socket.family attribute, + # so just assume IP in that case. + remote_ip = self.address[0] + else: + # Unix (or other) socket; fake the remote address + remote_ip = '0.0.0.0' + self._request = HTTPRequest( connection=self, method=method, uri=uri, version=version, - headers=headers, remote_ip=self.address[0]) + headers=headers, remote_ip=remote_ip) content_length = headers.get("Content-Length") if content_length: