Fix: serve_resource/serve_folder ignore mimetypes and fallback_mimetype parameters

httputils._serve_traversable looked up the Content-Type in the
module-level MIMETYPES/FALLBACK_MIMETYPE constants instead of the
mimetypes/fallback_mimetype parameters that serve_resource() and
serve_folder() accept and pass through. The parameters exist since the
helper was extracted for use by web plugins (33fcda7c, "Extract
httputils.serve_folder"), and the sibling parameters path_prefix and
index_file are honored, but a custom web plugin passing its own
mimetype mapping (e.g. to serve .json, .ico or .mjs files with a
correct Content-Type) silently got the built-in mapping and
application/octet-stream fallback instead.

Use the parameters for the lookup. No behavior change for the built-in
web module, which relies on the defaults.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
TowyTowy
2026-07-18 12:10:37 +02:00
parent 99f1862c82
commit 651e30211c
3 changed files with 21 additions and 2 deletions

View File

@@ -209,8 +209,8 @@ def _serve_traversable(
traversable = traversable.joinpath(index_file)
if not traversable.is_file():
return NOT_FOUND
content_type = MIMETYPES.get(
os.path.splitext(traversable.name)[1].lower(), FALLBACK_MIMETYPE)
content_type = mimetypes.get(
os.path.splitext(traversable.name)[1].lower(), fallback_mimetype)
headers = {
"Content-Type": content_type,
}

View File

@@ -19,6 +19,7 @@ Test web plugin.
"""
from radicale import httputils
from radicale.tests import BaseTest
@@ -52,3 +53,20 @@ class TestBaseWebRequests(BaseTest):
assert answer == "custom"
_, answer = self.post("/.web", "body content")
assert answer == "echo:body content"
def test_serve_resource_custom_mimetypes(self) -> None:
"""serve_resource must honor mimetypes and fallback_mimetype."""
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html")
assert status == 200
assert dict(headers)["Content-Type"] == "text/html"
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html",
mimetypes={".html": "text/x-custom"})
assert status == 200
assert dict(headers)["Content-Type"] == "text/x-custom"
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html",
mimetypes={}, fallback_mimetype="application/x-fallback")
assert status == 200
assert dict(headers)["Content-Type"] == "application/x-fallback"