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.
This commit is contained in:
Ben Darnell 2012-05-26 11:35:42 -07:00
parent 7d4623bcca
commit e5d5d3241f
1 changed files with 12 additions and 4 deletions

View File

@ -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: