From defce20dcae96b5f1fab685d17add69fa65b3405 Mon Sep 17 00:00:00 2001 From: Cedric RICARD Date: Thu, 24 Jul 2008 18:14:29 +0000 Subject: [PATCH] Improving MIME type --- calexium_lib/web/CXM_mime.anubis | 75 +++++++++++++++++++++++++++++++++++---------------------------------------- calexium_lib/web/CXM_multihost_http_server.anubis | 36 +++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/calexium_lib/web/CXM_mime.anubis b/calexium_lib/web/CXM_mime.anubis index 5f24b1f..d322802 100644 --- a/calexium_lib/web/CXM_mime.anubis +++ b/calexium_lib/web/CXM_mime.anubis @@ -15,52 +15,47 @@ read tools/basis.anubis read system/string.anubis public type MIME: - mime(String name, - String file_extension). + mime(String type, + String sybtype, + List(String) file_extensions). + + +public define Bool + MIME x = MIME y + = + if x is mime(x_type, x_subtype, _) then + if y is mime(y_type, y_subtype, _) then + insensitive_equal(x_type, y_type) & insensitive_equal(x_subtype, y_subtype). public define List(MIME) known_mime_types = [ - mime("application/octet-stream", ".exe"), - mime("application/x-pdf", ".pdf"), - mime("application/x-pdf", ".PDF"), - mime("image/bmp", ".bmp"), - mime("image/gif", ".gif"), - mime("image/gif", ".GIF"), - mime("image/jpeg", ".jpg"), - mime("image/jpeg", ".JPG"), - mime("image/jpeg", ".jpeg"), - mime("image/png", ".png"), - mime("image/png", ".PNG"), - mime("image/x-icon", ".ico"), - mime("text/html", ".html"), - mime("text/html", ".htm"), - mime("text/css", ".css"), - mime("text/javascript", ".js"), - mime("text/plain", ".txt"), - mime("text/plain", ".anubis"), - mime("text/plain", ".c"), - mime("text/plain", ".h"), - mime("text/plain", ".y"), - mime("text/plain", "/Makefile"), - mime("text/comma-separated-values", ".csv"), - mime("application/msword", ".doc"), - mime("application/octet-stream", ".emz"), - mime("application/octet-stream", ".xml"), - mime("application/octet-stream", ".mso"), - mime("application/octet-stream", ".wmf"), - mime("application/octet-stream", ".gz"), - mime("application/octet-stream", ".rar"), - mime("application/octet-stream", ".zip"), - mime("application/octet-stream", ".card"), - mime("application/octet-stream", ".ankh"), - mime("application/octet-stream", ".adm"), - mime("application/octet-stream", ".swf"), - mime("application/octet-stream", ".downloaded"), - mime("audio/x-mpeg", ".mp3"), - mime("video/x-msvideo", ".avi"), + mime("application", "octet-stream", [".exe"]), + mime("application", "x-pdf", [".pdf"]), + mime("image", "bmp", [".bmp"]), + mime("image", "gif", [".gif"]), + mime("image", "jpeg", [".jpg", ".jpeg"]), + mime("image", "png", [".png"]), + mime("image", "x-icon", [".ico"]), + mime("text", "html", [".html", ".htm"]), + mime("text", "css", [".css"]), + mime("text", "javascript", [".js"]), + mime("text", "plain", [".txt", ".anubis", ".c", ".h", ".y", "/Makefile"]), + mime("text", "comma-separated-values", [".csv"]), + mime("application", "msword", [".doc"]), + mime("application", "octet-stream", [".emz", ".xml", ".mso", ".wmf", ".gz", ".rar", ".zip", ".card", ".ankh", ".adm", ".swf", ".downloaded"]), + mime("audio", "x-mpeg", [".mp3"]), + mime("video", "x-msvideo", [".avi"]), ]. + +public define String + to_String + ( + MIME mime_type + ) = + if mime_type is mime(type, subtype, _) then + type + "/" + subtype. public define String to_MIME_text diff --git a/calexium_lib/web/CXM_multihost_http_server.anubis b/calexium_lib/web/CXM_multihost_http_server.anubis index 988628a..65e90b7 100644 --- a/calexium_lib/web/CXM_multihost_http_server.anubis +++ b/calexium_lib/web/CXM_multihost_http_server.anubis @@ -166,6 +166,7 @@ read CXM_common.anubis read tools/basis.anubis read system/string.anubis read system/files.anubis +read system/lists.anubis read CXM_mime.anubis @@ -1904,9 +1905,22 @@ public define String get_uri_extension_aux(uri, length(uri)-1). // search starts at the right end +public define Bool + contains_no_case + ( + List(String) l, + String val + ) = + if l is + { + [] then false, + [h . t] then + if insensitive_equal(h, val) then true + else contains_no_case(t, val) + }. -define Maybe(String) +define Maybe(MIME) recognize_mime_type_from_ext ( String ext, @@ -1914,14 +1928,14 @@ define Maybe(String) ) = if l is { - [ ] then success("application/octet-stream"), // failure, - [h . t] then if h is mime(mime_type,extension) then - if ext = extension - then success(mime_type) + [ ] then success(mime("application", "octet-stream", [])), // failure, + [h . t] then if h is mime(type, subtype, extensions) then + if contains_no_case(extensions, ext) + then success(h) else recognize_mime_type_from_ext(ext,t) }. -define Maybe(String) +define Maybe(MIME) recognize_mime_type_from_uri ( Web_Site_Description desc, @@ -2023,14 +2037,14 @@ public define String define List(HTTP_header) headers_for_send_file ( - String mime_type, + MIME mime_type, Int size, String etag, Maybe(FileTimes) mb_ftimes, ) = with headers = (List(HTTP_header)) [ - http_header("Content-Type",mime_type), + http_header("Content-Type", to_String(mime_type)), http_header("Etag", etag), http_header("Content-Length",to_decimal(size)), ], @@ -2108,8 +2122,8 @@ define One Int size, Connection file, String filename, - String full_path, - String mime_type, + String full_path, + MIME mime_type, One -> One action_before_send_file ) = action_before_send_file(unique); @@ -2202,7 +2216,7 @@ define One success(f) then with size = file_size(f), mime_type = if recognize_mime_type_from_uri(desc,uri) is { - failure then "application/octet-stream" + failure then mime("application", "octet-stream", []), success(mime_type) then mime_type }, send_file(desc, -- libgit2 0.21.4