From 9c574083ff04037cd728bfda351b5ca3327f1fff Mon Sep 17 00:00:00 2001 From: Avinash Raj Date: Fri, 28 Dec 2018 19:12:39 +0530 Subject: [PATCH] Support for passing server name as cli argument (#231) --- daphne/cli.py | 8 ++++++++ daphne/http_protocol.py | 2 ++ daphne/server.py | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/daphne/cli.py b/daphne/cli.py index 5a10d7a..60daed7 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -150,6 +150,13 @@ class CommandLineInterface(object): "application", help="The application to dispatch to as path.to.module:instance.path", ) + self.parser.add_argument( + "-s", + "--server-name", + dest="server_name", + help="specify which value should be passed to response header Server attribute", + default="Daphne", + ) self.server = None @@ -268,5 +275,6 @@ class CommandLineInterface(object): proxy_forwarded_proto_header="X-Forwarded-Proto" if args.proxy_headers else None, + server_name=args.server_name, ) self.server.run() diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 2c8d840..0116db4 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -239,6 +239,8 @@ class WebRequest(http.Request): # Write headers for header, value in message.get("headers", {}): self.responseHeaders.addRawHeader(header, value) + if self.server.server_name and self.server.server_name.lower() != "daphne": + self.setHeader(b"server", self.server.server_name.encode("utf-8")) logger.debug( "HTTP %s response started for %s", message["status"], self.client_addr ) diff --git a/daphne/server.py b/daphne/server.py index 2dbe476..de04b8a 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -54,6 +54,7 @@ class Server(object): websocket_handshake_timeout=5, application_close_timeout=10, ready_callable=None, + server_name="Daphne", # Deprecated and does not work, remove in version 2.2 ws_protocols=None, ): @@ -77,6 +78,7 @@ class Server(object): self.verbosity = verbosity self.abort_start = False self.ready_callable = ready_callable + self.server_name = server_name # Check our construction is actually sensible if not self.endpoints: logger.error("No endpoints. This server will not listen on anything.") @@ -87,7 +89,7 @@ class Server(object): self.connections = {} # Make the factory self.http_factory = HTTPFactory(self) - self.ws_factory = WebSocketFactory(self, server="Daphne") + self.ws_factory = WebSocketFactory(self, server=self.server_name) self.ws_factory.setProtocolOptions( autoPingTimeout=self.ping_timeout, allowNullOrigin=True,