From c863e2f378de29664bd1e680aca8115e3d045420 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 4 May 2016 11:30:37 -0700 Subject: [PATCH] Fixed #8: UNIX sockets now don't try and send client/server info --- daphne/http_protocol.py | 13 ++++++++++--- daphne/ws_protocol.py | 15 +++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 2c1ec97..192456f 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -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, }) diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 5794e7a..5990bc7 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -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")