mirror of
https://github.com/django/daphne.git
synced 2024-11-24 00:43:43 +03:00
Set a default Server
header for HTTP responses (#396)
Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
This commit is contained in:
parent
87bc5a7975
commit
54745d0f83
|
@ -11,7 +11,14 @@ Unreleased
|
||||||
range of versions does not represent a good use of maintainer time. Going
|
range of versions does not represent a good use of maintainer time. Going
|
||||||
forward the latest Twisted version will be required.
|
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)
|
3.0.2 (2021-04-07)
|
||||||
------------------
|
------------------
|
||||||
|
|
|
@ -93,7 +93,7 @@ class CommandLineInterface:
|
||||||
self.parser.add_argument(
|
self.parser.add_argument(
|
||||||
"--log-fmt",
|
"--log-fmt",
|
||||||
help="Log format to use",
|
help="Log format to use",
|
||||||
default="%(asctime)-15s %(levelname)-8s %(message)s"
|
default="%(asctime)-15s %(levelname)-8s %(message)s",
|
||||||
)
|
)
|
||||||
self.parser.add_argument(
|
self.parser.add_argument(
|
||||||
"--ping-interval",
|
"--ping-interval",
|
||||||
|
@ -162,7 +162,10 @@ class CommandLineInterface:
|
||||||
"--server-name",
|
"--server-name",
|
||||||
dest="server_name",
|
dest="server_name",
|
||||||
help="specify which value should be passed to response header Server attribute",
|
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
|
self.server = None
|
||||||
|
|
|
@ -249,8 +249,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":
|
if self.server.server_name and not self.responseHeaders.hasHeader("server"):
|
||||||
self.setHeader(b"server", self.server.server_name.encode("utf-8"))
|
self.setHeader(b"server", self.server.server_name.encode())
|
||||||
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
|
||||||
)
|
)
|
||||||
|
|
|
@ -56,7 +56,7 @@ class Server:
|
||||||
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",
|
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,
|
||||||
):
|
):
|
||||||
|
|
|
@ -240,3 +240,18 @@ class TestCLIInterface(TestCase):
|
||||||
exc.exception.message,
|
exc.exception.message,
|
||||||
"--proxy-headers has to be passed for this parameter.",
|
"--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": ""})
|
||||||
|
|
|
@ -13,9 +13,12 @@ class TestHTTPResponse(DaphneTestCase):
|
||||||
Lowercases and sorts headers, and strips transfer-encoding ones.
|
Lowercases and sorts headers, and strips transfer-encoding ones.
|
||||||
"""
|
"""
|
||||||
return sorted(
|
return sorted(
|
||||||
|
[(b"server", b"daphne")]
|
||||||
|
+ [
|
||||||
(name.lower(), value.strip())
|
(name.lower(), value.strip())
|
||||||
for name, value in headers
|
for name, value in headers
|
||||||
if name.lower() != b"transfer-encoding"
|
if name.lower() not in (b"server", b"transfer-encoding")
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
def encode_headers(self, headers):
|
def encode_headers(self, headers):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user