Skip to content

Commit e0c100d

Browse files
committed
fix: Refactor MIME type handling by adding guess_type function and updating usage in HttpServer
1 parent 34d4d19 commit e0c100d

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

server/src/user_interface/web_server/http_server.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pathlib
77
import traceback
88
from datetime import datetime, timedelta
9-
from mimetypes import guess_type
109
from typing import Any, Callable, TypeVar, Union
1110

1211
import argon2.exceptions
@@ -18,7 +17,7 @@
1817
from ...bus import BusData
1918
from ...minecraft import McServersModules
2019
from ...utils.hash import hash_string, verify_hash
21-
from ...utils.misc import str2bool, time_from_now
20+
from ...utils.misc import str2bool, time_from_now, guess_type
2221
from ...utils.regex import RE_MC_SERVER_NAME
2322
from ..Base_interface import BaseInterface
2423
from ..database.types import AccessLevel
@@ -180,14 +179,8 @@ def static_proxy(path : str):
180179
Logger.trace(f"Serving file: {full_path}")
181180

182181
content = pathlib.Path(full_path).read_bytes()
183-
mimetype = guess_type(path)[0] or 'text/html'
182+
mimetype = guess_type(full_path)
184183
# Only allow known-safe mimetypes
185-
allowed_mimetypes = (
186-
'text/html', 'text/css', 'text/javascript', 'text/json', 'image/png', 'image/jpeg', 'image/gif', 'image/svg+xml', 'image/webp', 'font/woff', 'font/woff2', 'font/ttf', 'font/otf'
187-
)
188-
if mimetype not in allowed_mimetypes:
189-
Logger.warning(f"Unknown mimetype {mimetype} for file {path}, defaulting to application/octet-stream")
190-
mimetype = 'application/octet-stream'
191184
Logger.trace(f"Serving {STATIC_PATH}/{path} ({len(content)} bytes) with mimetype {mimetype})")
192185
return content, HTTP.OK, {'Content-Type': mimetype}
193186
except Exception as e:

server/src/utils/misc.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ def split_with_nested(s: str, sep: str = ",") -> list[str]:
124124
return parts
125125

126126

127+
def guess_type(filename: str) -> str:
128+
"""
129+
Guess the MIME type of a file based on its extension.
130+
"""
131+
mimetypes = {
132+
'html': 'text/html',
133+
'css': 'text/css',
134+
'js': 'application/javascript',
135+
'json': 'application/json',
136+
'png': 'image/png',
137+
'jpeg': 'image/jpeg',
138+
'gif': 'image/gif',
139+
'svg': 'image/svg+xml',
140+
'webp': 'image/webp',
141+
'woff': 'font/woff',
142+
'woff2': 'font/woff2',
143+
'ttf': 'font/ttf',
144+
'otf': 'font/otf'
145+
}
146+
ext = filename.split('.')[-1].lower()
147+
if ext not in mimetypes:
148+
Logger.warning(f"Unknown file extension: {ext}, defaulting to application/octet-stream")
149+
return 'application/octet-stream'
150+
return mimetypes[ext]
151+
152+
153+
127154

128155
if __name__ == "__main__":
129156
# # test is_types_equals

0 commit comments

Comments
 (0)