Set Content-Length for HEAD requests

This commit is contained in:
Unrud 2022-01-15 22:32:38 +01:00
parent b93842b10c
commit 4a0bcde7a3
3 changed files with 8 additions and 6 deletions

View File

@ -121,6 +121,9 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
def _handle_request(self, environ: types.WSGIEnviron def _handle_request(self, environ: types.WSGIEnviron
) -> _IntermediateResponse: ) -> _IntermediateResponse:
time_begin = datetime.datetime.now()
request_method = environ["REQUEST_METHOD"].upper()
"""Manage a request.""" """Manage a request."""
def response(status: int, headers: types.WSGIResponseHeaders, def response(status: int, headers: types.WSGIResponseHeaders,
answer: Union[None, str, bytes]) -> _IntermediateResponse: answer: Union[None, str, bytes]) -> _IntermediateResponse:
@ -144,7 +147,8 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
headers["Content-Encoding"] = "gzip" headers["Content-Encoding"] = "gzip"
headers["Content-Length"] = str(len(answer)) headers["Content-Length"] = str(len(answer))
answers.append(answer) if request_method != "HEAD":
answers.append(answer)
# Add extra headers set in configuration # Add extra headers set in configuration
headers.update(self._extra_headers) headers.update(self._extra_headers)
@ -161,8 +165,6 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
# Return response content # Return response content
return status_text, list(headers.items()), answers return status_text, list(headers.items()), answers
time_begin = datetime.datetime.now()
request_method = environ["REQUEST_METHOD"].upper()
unsafe_path = environ.get("PATH_INFO", "") unsafe_path = environ.get("PATH_INFO", "")
remote_host = "unknown" remote_host = "unknown"
if environ.get("REMOTE_HOST"): if environ.get("REMOTE_HOST"):

View File

@ -27,5 +27,4 @@ class ApplicationPartHead(ApplicationPartGet, ApplicationBase):
def do_HEAD(self, environ: types.WSGIEnviron, base_prefix: str, path: str, def do_HEAD(self, environ: types.WSGIEnviron, base_prefix: str, path: str,
user: str) -> types.WSGIResponse: user: str) -> types.WSGIResponse:
"""Manage HEAD request.""" """Manage HEAD request."""
status, headers, _ = self.do_GET(environ, base_prefix, path, user) return self.do_GET(environ, base_prefix, path, user)
return status, headers, None

View File

@ -382,8 +382,9 @@ permissions: RrWw""")
assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None assert xml.find(xmlutils.make_clark("C:no-uid-conflict")) is not None
def test_head(self) -> None: def test_head(self) -> None:
status, _, _ = self.request("HEAD", "/") status, headers, answer = self.request("HEAD", "/")
assert status == 302 assert status == 302
assert int(headers.get("Content-Length", "0")) > 0 and not answer
def test_options(self) -> None: def test_options(self) -> None:
status, headers, _ = self.request("OPTIONS", "/") status, headers, _ = self.request("OPTIONS", "/")