Skip to content

Commit a08ec7f

Browse files
author
Joe Gasewicz
authored
Merge pull request #94 from joegasewicz/filetypes-#93
#93 content-type should be set based on filetypes
2 parents 2101aab + 9f9379d commit a08ec7f

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

bobtail/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def _match(self) -> Dict:
5757
self._set_metadata(k, path_segment, None, None)
5858
return self.meta_data["routes"][k]
5959
# Test route matches path
60-
if route_segment[0] != "{" and route_segment != path_segment:
60+
if route_segment and route_segment[0] != "{" and route_segment != path_segment:
6161
if route_segment == "*":
6262
self.meta_data["matched"] = k
6363
return self.meta_data["routes"][k]
6464
# No match, break out of this route class
6565
break
66-
if route_segment[0] == "{":
66+
if route_segment and route_segment[0] == "{":
6767
# If we reach this point then store the vars
6868
n, t = route_segment[1:-1].split(":")
6969
self._set_metadata(k, path_segment, t, n)

bobtail/response.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
from bobtail.options import BaseOptions
55
from bobtail.exceptions import StaticFileError
6-
6+
from bobtail.static_files import (
7+
AUDIO_FILETYPE ,
8+
IMAGE_FILETYPE,
9+
StaticFiles,
10+
TXT_FILETYPE,
11+
VIDEO_FILETYPE,
12+
)
713

814
class Response:
915
"""
@@ -179,8 +185,18 @@ def get(self, req: Request, res: Response) -> None:
179185
try:
180186
file_suffix = path.split("/")[-1:][0].split(".")[-1:][0]
181187
path = f"{self.options.STATIC_DIR}{path}"
182-
if file_suffix in ("jpg", "jpeg", "png"):
183-
self.set_headers({"Content-Type": "image/jpeg"})
188+
189+
if file_suffix in IMAGE_FILETYPE:
190+
StaticFiles(self, "image").set_headers(file_suffix)
191+
elif file_suffix == "css":
192+
StaticFiles(self, "text").set_headers(file_suffix)
193+
elif file_suffix in VIDEO_FILETYPE:
194+
StaticFiles(self, "video").set_headers(file_suffix)
195+
elif file_suffix in AUDIO_FILETYPE:
196+
StaticFiles(self, "audio").set_headers(file_suffix)
197+
else:
198+
StaticFiles(self, "text").set_headers("plain")
199+
184200
except Exception as exc:
185201
self.set_status(500)
186202
raise StaticFileError(

bobtail/static_files.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import Any
2+
3+
4+
IMAGE_FILETYPE = (
5+
"jpg",
6+
"jpeg",
7+
"png",
8+
"gif",
9+
"tiff",
10+
"svg+xml",
11+
"x-icon",
12+
)
13+
14+
TXT_FILETYPE = (
15+
"css",
16+
"csv",
17+
"html",
18+
"javascript",
19+
"plain",
20+
"xml",
21+
)
22+
23+
VIDEO_FILETYPE = (
24+
"mpeg",
25+
"mp4",
26+
"quicktime",
27+
"webm",
28+
"x-ms-wmv",
29+
"x-msvideo",
30+
"x-flv",
31+
)
32+
33+
AUDIO_FILETYPE = (
34+
"mpeg",
35+
"x-ms-wma",
36+
"vnd.rn-realaudio",
37+
"x-wav",
38+
)
39+
40+
41+
class StaticFiles:
42+
response: Any # Response
43+
mimetypes: str
44+
45+
def __init__(self, response: Any, mimetype: str):
46+
self.response = response
47+
self.mimetype = mimetype
48+
49+
def set_headers(self, file_type: str) -> None:
50+
self.response.set_headers({"Content-Type": f"{self.mimetype}/{file_type}"})

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name="bobtail",
10-
version="0.0.27",
10+
version="0.0.28",
1111
description="A little Python http framework",
1212
packages=["bobtail"],
1313
py_modules=["bobtail"],

0 commit comments

Comments
 (0)