Commit defce20dcae96b5f1fab685d17add69fa65b3405
1 parent
b206a5d0
Improving MIME type
Showing
2 changed files
with
60 additions
and
51 deletions
Show diff stats
calexium_lib/web/CXM_mime.anubis
| @@ -15,52 +15,47 @@ read tools/basis.anubis | @@ -15,52 +15,47 @@ read tools/basis.anubis | ||
| 15 | read system/string.anubis | 15 | read system/string.anubis |
| 16 | 16 | ||
| 17 | public type MIME: | 17 | public type MIME: |
| 18 | - mime(String name, | ||
| 19 | - String file_extension). | 18 | + mime(String type, |
| 19 | + String sybtype, | ||
| 20 | + List(String) file_extensions). | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +public define Bool | ||
| 24 | + MIME x = MIME y | ||
| 25 | + = | ||
| 26 | + if x is mime(x_type, x_subtype, _) then | ||
| 27 | + if y is mime(y_type, y_subtype, _) then | ||
| 28 | + insensitive_equal(x_type, y_type) & insensitive_equal(x_subtype, y_subtype). | ||
| 20 | 29 | ||
| 21 | public define List(MIME) | 30 | public define List(MIME) |
| 22 | known_mime_types | 31 | known_mime_types |
| 23 | = | 32 | = |
| 24 | [ | 33 | [ |
| 25 | - mime("application/octet-stream", ".exe"), | ||
| 26 | - mime("application/x-pdf", ".pdf"), | ||
| 27 | - mime("application/x-pdf", ".PDF"), | ||
| 28 | - mime("image/bmp", ".bmp"), | ||
| 29 | - mime("image/gif", ".gif"), | ||
| 30 | - mime("image/gif", ".GIF"), | ||
| 31 | - mime("image/jpeg", ".jpg"), | ||
| 32 | - mime("image/jpeg", ".JPG"), | ||
| 33 | - mime("image/jpeg", ".jpeg"), | ||
| 34 | - mime("image/png", ".png"), | ||
| 35 | - mime("image/png", ".PNG"), | ||
| 36 | - mime("image/x-icon", ".ico"), | ||
| 37 | - mime("text/html", ".html"), | ||
| 38 | - mime("text/html", ".htm"), | ||
| 39 | - mime("text/css", ".css"), | ||
| 40 | - mime("text/javascript", ".js"), | ||
| 41 | - mime("text/plain", ".txt"), | ||
| 42 | - mime("text/plain", ".anubis"), | ||
| 43 | - mime("text/plain", ".c"), | ||
| 44 | - mime("text/plain", ".h"), | ||
| 45 | - mime("text/plain", ".y"), | ||
| 46 | - mime("text/plain", "/Makefile"), | ||
| 47 | - mime("text/comma-separated-values", ".csv"), | ||
| 48 | - mime("application/msword", ".doc"), | ||
| 49 | - mime("application/octet-stream", ".emz"), | ||
| 50 | - mime("application/octet-stream", ".xml"), | ||
| 51 | - mime("application/octet-stream", ".mso"), | ||
| 52 | - mime("application/octet-stream", ".wmf"), | ||
| 53 | - mime("application/octet-stream", ".gz"), | ||
| 54 | - mime("application/octet-stream", ".rar"), | ||
| 55 | - mime("application/octet-stream", ".zip"), | ||
| 56 | - mime("application/octet-stream", ".card"), | ||
| 57 | - mime("application/octet-stream", ".ankh"), | ||
| 58 | - mime("application/octet-stream", ".adm"), | ||
| 59 | - mime("application/octet-stream", ".swf"), | ||
| 60 | - mime("application/octet-stream", ".downloaded"), | ||
| 61 | - mime("audio/x-mpeg", ".mp3"), | ||
| 62 | - mime("video/x-msvideo", ".avi"), | 34 | + mime("application", "octet-stream", [".exe"]), |
| 35 | + mime("application", "x-pdf", [".pdf"]), | ||
| 36 | + mime("image", "bmp", [".bmp"]), | ||
| 37 | + mime("image", "gif", [".gif"]), | ||
| 38 | + mime("image", "jpeg", [".jpg", ".jpeg"]), | ||
| 39 | + mime("image", "png", [".png"]), | ||
| 40 | + mime("image", "x-icon", [".ico"]), | ||
| 41 | + mime("text", "html", [".html", ".htm"]), | ||
| 42 | + mime("text", "css", [".css"]), | ||
| 43 | + mime("text", "javascript", [".js"]), | ||
| 44 | + mime("text", "plain", [".txt", ".anubis", ".c", ".h", ".y", "/Makefile"]), | ||
| 45 | + mime("text", "comma-separated-values", [".csv"]), | ||
| 46 | + mime("application", "msword", [".doc"]), | ||
| 47 | + mime("application", "octet-stream", [".emz", ".xml", ".mso", ".wmf", ".gz", ".rar", ".zip", ".card", ".ankh", ".adm", ".swf", ".downloaded"]), | ||
| 48 | + mime("audio", "x-mpeg", [".mp3"]), | ||
| 49 | + mime("video", "x-msvideo", [".avi"]), | ||
| 63 | ]. | 50 | ]. |
| 51 | + | ||
| 52 | +public define String | ||
| 53 | + to_String | ||
| 54 | + ( | ||
| 55 | + MIME mime_type | ||
| 56 | + ) = | ||
| 57 | + if mime_type is mime(type, subtype, _) then | ||
| 58 | + type + "/" + subtype. | ||
| 64 | 59 | ||
| 65 | public define String | 60 | public define String |
| 66 | to_MIME_text | 61 | to_MIME_text |
calexium_lib/web/CXM_multihost_http_server.anubis
| @@ -166,6 +166,7 @@ read CXM_common.anubis | @@ -166,6 +166,7 @@ read CXM_common.anubis | ||
| 166 | read tools/basis.anubis | 166 | read tools/basis.anubis |
| 167 | read system/string.anubis | 167 | read system/string.anubis |
| 168 | read system/files.anubis | 168 | read system/files.anubis |
| 169 | +read system/lists.anubis | ||
| 169 | read CXM_mime.anubis | 170 | read CXM_mime.anubis |
| 170 | 171 | ||
| 171 | 172 | ||
| @@ -1904,9 +1905,22 @@ public define String | @@ -1904,9 +1905,22 @@ public define String | ||
| 1904 | get_uri_extension_aux(uri, | 1905 | get_uri_extension_aux(uri, |
| 1905 | length(uri)-1). // search starts at the right end | 1906 | length(uri)-1). // search starts at the right end |
| 1906 | 1907 | ||
| 1908 | +public define Bool | ||
| 1909 | + contains_no_case | ||
| 1910 | + ( | ||
| 1911 | + List(String) l, | ||
| 1912 | + String val | ||
| 1913 | + ) = | ||
| 1914 | + if l is | ||
| 1915 | + { | ||
| 1916 | + [] then false, | ||
| 1917 | + [h . t] then | ||
| 1918 | + if insensitive_equal(h, val) then true | ||
| 1919 | + else contains_no_case(t, val) | ||
| 1920 | + }. | ||
| 1907 | 1921 | ||
| 1908 | 1922 | ||
| 1909 | -define Maybe(String) | 1923 | +define Maybe(MIME) |
| 1910 | recognize_mime_type_from_ext | 1924 | recognize_mime_type_from_ext |
| 1911 | ( | 1925 | ( |
| 1912 | String ext, | 1926 | String ext, |
| @@ -1914,14 +1928,14 @@ define Maybe(String) | @@ -1914,14 +1928,14 @@ define Maybe(String) | ||
| 1914 | ) = | 1928 | ) = |
| 1915 | if l is | 1929 | if l is |
| 1916 | { | 1930 | { |
| 1917 | - [ ] then success("application/octet-stream"), // failure, | ||
| 1918 | - [h . t] then if h is mime(mime_type,extension) then | ||
| 1919 | - if ext = extension | ||
| 1920 | - then success(mime_type) | 1931 | + [ ] then success(mime("application", "octet-stream", [])), // failure, |
| 1932 | + [h . t] then if h is mime(type, subtype, extensions) then | ||
| 1933 | + if contains_no_case(extensions, ext) | ||
| 1934 | + then success(h) | ||
| 1921 | else recognize_mime_type_from_ext(ext,t) | 1935 | else recognize_mime_type_from_ext(ext,t) |
| 1922 | }. | 1936 | }. |
| 1923 | 1937 | ||
| 1924 | -define Maybe(String) | 1938 | +define Maybe(MIME) |
| 1925 | recognize_mime_type_from_uri | 1939 | recognize_mime_type_from_uri |
| 1926 | ( | 1940 | ( |
| 1927 | Web_Site_Description desc, | 1941 | Web_Site_Description desc, |
| @@ -2023,14 +2037,14 @@ public define String | @@ -2023,14 +2037,14 @@ public define String | ||
| 2023 | define List(HTTP_header) | 2037 | define List(HTTP_header) |
| 2024 | headers_for_send_file | 2038 | headers_for_send_file |
| 2025 | ( | 2039 | ( |
| 2026 | - String mime_type, | 2040 | + MIME mime_type, |
| 2027 | Int size, | 2041 | Int size, |
| 2028 | String etag, | 2042 | String etag, |
| 2029 | Maybe(FileTimes) mb_ftimes, | 2043 | Maybe(FileTimes) mb_ftimes, |
| 2030 | ) = | 2044 | ) = |
| 2031 | with headers = (List(HTTP_header)) | 2045 | with headers = (List(HTTP_header)) |
| 2032 | [ | 2046 | [ |
| 2033 | - http_header("Content-Type",mime_type), | 2047 | + http_header("Content-Type", to_String(mime_type)), |
| 2034 | http_header("Etag", etag), | 2048 | http_header("Etag", etag), |
| 2035 | http_header("Content-Length",to_decimal(size)), | 2049 | http_header("Content-Length",to_decimal(size)), |
| 2036 | ], | 2050 | ], |
| @@ -2108,8 +2122,8 @@ define One | @@ -2108,8 +2122,8 @@ define One | ||
| 2108 | Int size, | 2122 | Int size, |
| 2109 | Connection file, | 2123 | Connection file, |
| 2110 | String filename, | 2124 | String filename, |
| 2111 | - String full_path, | ||
| 2112 | - String mime_type, | 2125 | + String full_path, |
| 2126 | + MIME mime_type, | ||
| 2113 | One -> One action_before_send_file | 2127 | One -> One action_before_send_file |
| 2114 | ) = | 2128 | ) = |
| 2115 | action_before_send_file(unique); | 2129 | action_before_send_file(unique); |
| @@ -2202,7 +2216,7 @@ define One | @@ -2202,7 +2216,7 @@ define One | ||
| 2202 | success(f) then with size = file_size(f), | 2216 | success(f) then with size = file_size(f), |
| 2203 | mime_type = if recognize_mime_type_from_uri(desc,uri) is | 2217 | mime_type = if recognize_mime_type_from_uri(desc,uri) is |
| 2204 | { | 2218 | { |
| 2205 | - failure then "application/octet-stream" | 2219 | + failure then mime("application", "octet-stream", []), |
| 2206 | success(mime_type) then mime_type | 2220 | success(mime_type) then mime_type |
| 2207 | }, | 2221 | }, |
| 2208 | send_file(desc, | 2222 | send_file(desc, |