mirror of
https://github.com/Open-Argon/http.ar.git
synced 2026-10-28 09:43:55 +00:00
add contentType
This commit is contained in:
1 parent
a54b21114e
commit
a47bb14657
3 files changed
+76
-3
No files matched your search
@@ -0,0 +1,66 @@
|
||||
let knownContentTypes = {}
|
||||
knownContentTypes["html"] = "text/html"
|
||||
knownContentTypes["css"] = "text/css"
|
||||
knownContentTypes["js"] = "application/javascript"
|
||||
knownContentTypes["json"] = "application/json"
|
||||
knownContentTypes["png"] = "image/png"
|
||||
knownContentTypes["jpg"] = "image/jpeg"
|
||||
knownContentTypes["jpeg"] = "image/jpeg"
|
||||
knownContentTypes["gif"] = "image/gif"
|
||||
knownContentTypes["svg"] = "image/svg+xml"
|
||||
knownContentTypes["ico"] = "image/x-icon"
|
||||
knownContentTypes["txt"] = "text/plain"
|
||||
knownContentTypes["md"] = "text/markdown"
|
||||
knownContentTypes["xml"] = "application/xml"
|
||||
knownContentTypes["pdf"] = "application/pdf"
|
||||
knownContentTypes["zip"] = "application/zip"
|
||||
knownContentTypes["gz"] = "application/gzip"
|
||||
knownContentTypes["tar"] = "application/x-tar"
|
||||
knownContentTypes["ogg"] = "audio/ogg"
|
||||
knownContentTypes["mp3"] = "audio/mpeg"
|
||||
knownContentTypes["mp4"] = "video/mp4"
|
||||
knownContentTypes["webm"] = "video/webm"
|
||||
knownContentTypes["webp"] = "image/webp"
|
||||
knownContentTypes["woff"] = "font/woff"
|
||||
knownContentTypes["woff2"] = "font/woff2"
|
||||
knownContentTypes["ttf"] = "font/ttf"
|
||||
knownContentTypes["otf"] = "font/otf"
|
||||
knownContentTypes["eot"] = "application/vnd.ms-fontobject"
|
||||
knownContentTypes["sfnt"] = "application/font-sfnt"
|
||||
knownContentTypes["wasm"] = "application/wasm"
|
||||
knownContentTypes["csv"] = "text/csv"
|
||||
knownContentTypes["tsv"] = "text/tab-separated-values"
|
||||
knownContentTypes["rtf"] = "application/rtf"
|
||||
knownContentTypes["7z"] = "application/x-7z-compressed"
|
||||
knownContentTypes["rar"] = "application/x-rar-compressed"
|
||||
knownContentTypes["bz"] = "application/x-bzip"
|
||||
knownContentTypes["bz2"] = "application/x-bzip2"
|
||||
knownContentTypes["xz"] = "application/x-xz"
|
||||
knownContentTypes["exe"] = "application/x-msdownload"
|
||||
knownContentTypes["swf"] = "application/x-shockwave-flash"
|
||||
knownContentTypes["flv"] = "video/x-flv"
|
||||
knownContentTypes["wmv"] = "video/x-ms-wmv"
|
||||
knownContentTypes["avi"] = "video/x-msvideo"
|
||||
knownContentTypes["mpg"] = "video/mpeg"
|
||||
knownContentTypes["mpeg"] = "video/mpeg"
|
||||
knownContentTypes["mov"] = "video/quicktime"
|
||||
knownContentTypes["weba"] = "audio/webm"
|
||||
knownContentTypes["wav"] = "audio/wav"
|
||||
knownContentTypes["flac"] = "audio/flac"
|
||||
knownContentTypes["midi"] = "audio/midi"
|
||||
knownContentTypes["mid"] = "audio/midi"
|
||||
knownContentTypes["m4a"] = "audio/m4a"
|
||||
knownContentTypes["m4v"] = "video/m4v"
|
||||
knownContentTypes["3gp"] = "video/3gpp"
|
||||
knownContentTypes["3g2"] = "video/3gpp2"
|
||||
knownContentTypes["ogv"] = "video/ogg"
|
||||
knownContentTypes["m3u8"] = "application/vnd.apple.mpegurl"
|
||||
knownContentTypes["ts"] = "video/mp2t"
|
||||
knownContentTypes["flv"] = "video/x-flv"
|
||||
knownContentTypes["wmv"] = "video/x-ms-wmv"
|
||||
|
||||
|
||||
let getter(p) = do
|
||||
let extension = p.split(".")[-1].lower()
|
||||
if (extension in knownContentTypes) return knownContentTypes[extension]
|
||||
return "application/octet-stream"
|
||||
@@ -4,6 +4,9 @@ import "pathCompare" as pathCompare
|
||||
import "url" as url
|
||||
import "version" as version
|
||||
import "renderTemplate" as renderTemplate
|
||||
import "contentType" as contentType
|
||||
|
||||
let __initHeaders() = {"content-type": "text/html; charset=utf-8", "server": "http.ar/"+version.version+" Argon/"+ArgonVersion}
|
||||
|
||||
let handle(routes, conn, onErr) = do
|
||||
try do
|
||||
@@ -17,7 +20,7 @@ let handle(routes, conn, onErr) = do
|
||||
request.on.end = []
|
||||
let bufferSize = 1024*1024*10
|
||||
request.headersSent = false
|
||||
let response = {code:200, headers:{"content-type": "text/html; charset=utf-8", "server": "http.ar/"+version.version+" Argon/"+ArgonVersion}}
|
||||
let response = {code:200, headers:__initHeaders()}
|
||||
|
||||
request.isClosed = conn.isClosed
|
||||
|
||||
@@ -40,7 +43,7 @@ let handle(routes, conn, onErr) = do
|
||||
if ("attachment" in f) do
|
||||
attachment = f.attachment
|
||||
let FILE = file.read(path)
|
||||
response.headers["content-type"] = FILE.contentType()
|
||||
response.headers["content-type"] = contentType.getter(path)
|
||||
if (attachment) do
|
||||
response.headers["content-disposition"] = "attachment; filename="+attachment
|
||||
else do
|
||||
@@ -92,7 +95,7 @@ let handle(routes, conn, onErr) = do
|
||||
if (onErr) onErr(err)
|
||||
else term.log(err)
|
||||
response.code = 500
|
||||
response.headers = {"content-type": "text/html; charset=utf-8", "server": "http.ar/"+version.version+" Argon/"+ArgonVersion}
|
||||
response.headers = __initHeaders()
|
||||
response.send("<h1>500 Internal Server Error</h1>")
|
||||
|
||||
response.json(data) = do
|
||||
@@ -134,6 +137,7 @@ let handle(routes, conn, onErr) = do
|
||||
response.onErr(err)
|
||||
else do
|
||||
response.code = 404
|
||||
response.headers = __initHeaders()
|
||||
response.send("<h1>404 Not Found</h1>")
|
||||
response.next = next
|
||||
next()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import "handleRequest" as handleRequest
|
||||
|
||||
let static(root, path) = do
|
||||
if (root == "/") root = ""
|
||||
let middleware(req, res) = do
|
||||
@@ -12,5 +14,6 @@ let static(root, path) = do
|
||||
catch (_) do
|
||||
continue
|
||||
if (req.isClosed()) return
|
||||
res.headers = handleRequest.__initHeaders()
|
||||
res.next()
|
||||
return middleware
|
||||
Reference in new issue
Block a user