Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a default Server header for HTTP responses #396

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Unreleased
range of versions does not represent a good use of maintainer time. Going
forward the latest Twisted version will be required.

* Added `log-fmt` CLI argument.
* Set ``daphne`` as default ``Server`` header.

This can be configured with the ``--server-name`` CLI argument.

Added the new ``--no-server-name`` CLI argument to disable the ``Server``
header, which is equivalent to ``--server-name=` (an empty name).

* Added ``--log-fmt`` CLI argument.

3.0.2 (2021-04-07)
------------------
Expand Down
7 changes: 5 additions & 2 deletions daphne/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self):
self.parser.add_argument(
"--log-fmt",
help="Log format to use",
default="%(asctime)-15s %(levelname)-8s %(message)s"
default="%(asctime)-15s %(levelname)-8s %(message)s",
)
self.parser.add_argument(
"--ping-interval",
Expand Down Expand Up @@ -162,7 +162,10 @@ def __init__(self):
"--server-name",
dest="server_name",
help="specify which value should be passed to response header Server attribute",
default="Daphne",
default="daphne",
)
self.parser.add_argument(
"--no-server-name", dest="server_name", action="store_const", const=""
)

self.server = None
Expand Down
4 changes: 2 additions & 2 deletions daphne/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def handle_reply(self, message):
# 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"))
if self.server.server_name and not self.responseHeaders.hasHeader("server"):
self.setHeader(b"server", self.server.server_name.encode())
adamchainz marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(
"HTTP %s response started for %s", message["status"], self.client_addr
)
Expand Down
2 changes: 1 addition & 1 deletion daphne/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
websocket_handshake_timeout=5,
application_close_timeout=10,
ready_callable=None,
server_name="Daphne",
server_name="daphne",
# Deprecated and does not work, remove in version 2.2
ws_protocols=None,
):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,18 @@ def test_custom_proxyport(self):
exc.exception.message,
"--proxy-headers has to be passed for this parameter.",
)

def test_custom_servername(self):
"""
Passing `--server-name` will set the default server header
from 'daphne' to the passed one.
"""
self.assertCLI([], {"server_name": "daphne"})
self.assertCLI(["--server-name", ""], {"server_name": ""})
self.assertCLI(["--server-name", "python"], {"server_name": "python"})

def test_no_servername(self):
"""
Passing `--no-server-name` will set server name to '' (empty string)
"""
self.assertCLI(["--no-server-name"], {"server_name": ""})
9 changes: 6 additions & 3 deletions tests/test_http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def normalize_headers(self, headers):
Lowercases and sorts headers, and strips transfer-encoding ones.
"""
return sorted(
(name.lower(), value.strip())
for name, value in headers
if name.lower() != b"transfer-encoding"
[(b"server", b"daphne")]
+ [
(name.lower(), value.strip())
for name, value in headers
if name.lower() not in (b"server", b"transfer-encoding")
]
)

def encode_headers(self, headers):
Expand Down