Skip to content

Commit

Permalink
sendfile detect mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Dec 4, 2020
1 parent 04c1e1e commit 7c76257
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tinyweb/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@
IS_UASYNCIO_V3 = hasattr(asyncio, "__version__") and asyncio.__version__ >= (3,)


MIME_TYPES_PER_EXT = {
".txt" : "text/plain",
".htm" : "text/html",
".html" : "text/html",
".css" : "text/css",
".csv" : "text/csv",
".js" : "application/javascript",
".xml" : "application/xml",
".xhtml" : "application/xhtml+xml",
".json" : "application/json",
".zip" : "application/zip",
".pdf" : "application/pdf",
".ts" : "application/typescript",
".woff" : "font/woff",
".woff2" : "font/woff2",
".ttf" : "font/ttf",
".otf" : "font/otf",
".jpg" : "image/jpeg",
".jpeg" : "image/jpeg",
".png" : "image/png",
".gif" : "image/gif",
".svg" : "image/svg+xml",
".ico" : "image/x-icon"
}


def urldecode_plus(s):
"""Decode urlencoded string (including '+' char).
Expand Down Expand Up @@ -277,9 +303,16 @@ async def send_file(self, filename, content_type=None, content_encoding=None, ma
stat = os.stat(filename)
slen = str(stat[6])
self.add_header('Content-Length', slen)
# Find content type

# Set contet-type, if possible
if content_type:
self.add_header('Content-Type', content_type)
else:
# Auto-detect mime type based on file extension (eg. .css)
file_extension = "." + filename.split(".")[-1]
if file_extension in MIME_TYPES_PER_EXT:
self.add_header('Content-Type', MIME_TYPES_PER_EXT[file_extension])

# Add content-encoding, if any
if content_encoding:
self.add_header('Content-Encoding', content_encoding)
Expand Down

0 comments on commit 7c76257

Please sign in to comment.