Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#93 content-type should be set based on filetypes #94

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bobtail/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def _match(self) -> Dict:
self._set_metadata(k, path_segment, None, None)
return self.meta_data["routes"][k]
# Test route matches path
if route_segment[0] != "{" and route_segment != path_segment:
if route_segment and route_segment[0] != "{" and route_segment != path_segment:
if route_segment == "*":
self.meta_data["matched"] = k
return self.meta_data["routes"][k]
# No match, break out of this route class
break
if route_segment[0] == "{":
if route_segment and route_segment[0] == "{":
# If we reach this point then store the vars
n, t = route_segment[1:-1].split(":")
self._set_metadata(k, path_segment, t, n)
Expand Down
22 changes: 19 additions & 3 deletions bobtail/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

from bobtail.options import BaseOptions
from bobtail.exceptions import StaticFileError

from bobtail.static_files import (
AUDIO_FILETYPE ,
IMAGE_FILETYPE,
StaticFiles,
TXT_FILETYPE,
VIDEO_FILETYPE,
)

class Response:
"""
Expand Down Expand Up @@ -179,8 +185,18 @@ def get(self, req: Request, res: Response) -> None:
try:
file_suffix = path.split("/")[-1:][0].split(".")[-1:][0]
path = f"{self.options.STATIC_DIR}{path}"
if file_suffix in ("jpg", "jpeg", "png"):
self.set_headers({"Content-Type": "image/jpeg"})

if file_suffix in IMAGE_FILETYPE:
StaticFiles(self, "image").set_headers(file_suffix)
elif file_suffix == "css":
StaticFiles(self, "text").set_headers(file_suffix)
elif file_suffix in VIDEO_FILETYPE:
StaticFiles(self, "video").set_headers(file_suffix)
elif file_suffix in AUDIO_FILETYPE:
StaticFiles(self, "audio").set_headers(file_suffix)
else:
StaticFiles(self, "text").set_headers("plain")

except Exception as exc:
self.set_status(500)
raise StaticFileError(
Expand Down
50 changes: 50 additions & 0 deletions bobtail/static_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Any


IMAGE_FILETYPE = (
"jpg",
"jpeg",
"png",
"gif",
"tiff",
"svg+xml",
"x-icon",
)

TXT_FILETYPE = (
"css",
"csv",
"html",
"javascript",
"plain",
"xml",
)

VIDEO_FILETYPE = (
"mpeg",
"mp4",
"quicktime",
"webm",
"x-ms-wmv",
"x-msvideo",
"x-flv",
)

AUDIO_FILETYPE = (
"mpeg",
"x-ms-wma",
"vnd.rn-realaudio",
"x-wav",
)


class StaticFiles:
response: Any # Response
mimetypes: str

def __init__(self, response: Any, mimetype: str):
self.response = response
self.mimetype = mimetype

def set_headers(self, file_type: str) -> None:
self.response.set_headers({"Content-Type": f"{self.mimetype}/{file_type}"})
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="bobtail",
version="0.0.27",
version="0.0.28",
description="A little Python http framework",
packages=["bobtail"],
py_modules=["bobtail"],
Expand Down
Loading