Fixed #8: UNIX sockets now don't try and send client/server info

This commit is contained in:
Andrew Godwin 2016-05-04 11:30:37 -07:00
parent ad8511e144
commit c863e2f378
2 changed files with 21 additions and 7 deletions

View File

@ -103,6 +103,13 @@ class WebRequest(http.Request):
self.clean_headers.append((name.lower(), value))
logger.debug("HTTP %s request for %s", self.method, self.reply_channel)
self.content.seek(0, 0)
# Get client address if possible
if hasattr(self.client, "host") and hasattr(self.client, "port"):
self.client_addr = [self.client.host, self.client.port]
self.server_addr = [self.host.host, self.host.port]
else:
self.client_addr = None
self.server_addr = None
# Send message
self.factory.channel_layer.send("http.request", {
"reply_channel": self.reply_channel,
@ -114,8 +121,8 @@ class WebRequest(http.Request):
"query_string": self.unquote(self.query_string),
"headers": self.clean_headers,
"body": self.content.read(),
"client": [self.client.host, self.client.port],
"server": [self.host.host, self.host.port],
"client": self.client_addr,
"server": self.server_addr,
})
@classmethod
@ -185,7 +192,7 @@ class WebRequest(http.Request):
"path": self.path.decode("ascii"),
"status": self.code,
"method": self.method.decode("ascii"),
"client": "%s:%s" % (self.client.host, self.client.port),
"client": "%s:%s" % tuple(self.client_addr) if self.client_addr else None,
"time_taken": self.duration(),
"size": self.sentLength,
})

View File

@ -41,14 +41,21 @@ class WebSocketProtocol(WebSocketServerProtocol):
self.reply_channel = self.channel_layer.new_channel("websocket.send!")
# Tell main factory about it
self.main_factory.reply_protocols[self.reply_channel] = self
# Get client address if possible
if hasattr(self.transport.getPeer(), "host") and hasattr(self.transport.getPeer(), "port"):
self.client_addr = [self.transport.getPeer().host, self.transport.getPeer().port]
self.server_addr = [self.transport.getHost().host, self.transport.getHost().port]
else:
self.client_addr = None
self.server_addr = None
# Make initial request info dict from request (we only have it here)
self.path = request.path.encode("ascii")
self.request_info = {
"path": self.unquote(self.path),
"headers": clean_headers,
"query_string": self.unquote(query_string),
"client": [self.transport.getPeer().host, self.transport.getPeer().port],
"server": [self.transport.getHost().host, self.transport.getHost().port],
"client": self.client_addr,
"server": self.server_addr,
"reply_channel": self.reply_channel,
"order": 0,
}
@ -78,7 +85,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
self.channel_layer.send("websocket.connect", self.request_info)
self.factory.log_action("websocket", "connected", {
"path": self.request.path,
"client": "%s:%s" % (self.transport.getPeer().host, self.transport.getPeer().port),
"client": "%s:%s" % tuple(self.client_addr) if self.client_addr else None,
})
def onMessage(self, payload, isBinary):
@ -128,7 +135,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
})
self.factory.log_action("websocket", "disconnected", {
"path": self.request.path,
"client": "%s:%s" % (self.transport.getPeer().host, self.transport.getPeer().port),
"client": "%s:%s" % tuple(self.client_addr) if self.client_addr else None,
})
else:
logger.debug("WebSocket closed before handshake established")