Support for passing server name as cli argument (#231)

This commit is contained in:
Avinash Raj 2018-12-28 19:12:39 +05:30 committed by Andrew Godwin
parent cc344c6e34
commit 9c574083ff
3 changed files with 13 additions and 1 deletions

View File

@ -150,6 +150,13 @@ class CommandLineInterface(object):
"application", "application",
help="The application to dispatch to as path.to.module:instance.path", 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 self.server = None
@ -268,5 +275,6 @@ class CommandLineInterface(object):
proxy_forwarded_proto_header="X-Forwarded-Proto" proxy_forwarded_proto_header="X-Forwarded-Proto"
if args.proxy_headers if args.proxy_headers
else None, else None,
server_name=args.server_name,
) )
self.server.run() self.server.run()

View File

@ -239,6 +239,8 @@ class WebRequest(http.Request):
# Write headers # Write headers
for header, value in message.get("headers", {}): for header, value in message.get("headers", {}):
self.responseHeaders.addRawHeader(header, value) 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( logger.debug(
"HTTP %s response started for %s", message["status"], self.client_addr "HTTP %s response started for %s", message["status"], self.client_addr
) )

View File

@ -54,6 +54,7 @@ class Server(object):
websocket_handshake_timeout=5, websocket_handshake_timeout=5,
application_close_timeout=10, application_close_timeout=10,
ready_callable=None, ready_callable=None,
server_name="Daphne",
# Deprecated and does not work, remove in version 2.2 # Deprecated and does not work, remove in version 2.2
ws_protocols=None, ws_protocols=None,
): ):
@ -77,6 +78,7 @@ class Server(object):
self.verbosity = verbosity self.verbosity = verbosity
self.abort_start = False self.abort_start = False
self.ready_callable = ready_callable self.ready_callable = ready_callable
self.server_name = server_name
# Check our construction is actually sensible # Check our construction is actually sensible
if not self.endpoints: if not self.endpoints:
logger.error("No endpoints. This server will not listen on anything.") logger.error("No endpoints. This server will not listen on anything.")
@ -87,7 +89,7 @@ class Server(object):
self.connections = {} self.connections = {}
# Make the factory # Make the factory
self.http_factory = HTTPFactory(self) 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( self.ws_factory.setProtocolOptions(
autoPingTimeout=self.ping_timeout, autoPingTimeout=self.ping_timeout,
allowNullOrigin=True, allowNullOrigin=True,