From 281e28567384a66dca097163ddcafb65c0849061 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Fri, 4 Nov 2022 13:32:04 +0100 Subject: [PATCH] add charset to various mime types --- lib/builder_web.ml | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/builder_web.ml b/lib/builder_web.ml index 77508f3..e424566 100644 --- a/lib/builder_web.ml +++ b/lib/builder_web.ml @@ -46,16 +46,26 @@ let safe_seg path = else Fmt.kstr (fun s -> Error (`Msg s)) "unsafe path %S" path (* mime lookup with orb knowledge *) +let append_charset = function + (* mime types from nginx: + http://nginx.org/en/docs/http/ngx_http_charset_module.html#charset_types *) + | "text/html" | "text/xml" | "text/plain" | "text/vnd.wap.wml" + | "application/javascript" | "application/rss+xml" | "application/atom+xml" + as content_type -> + content_type ^ "; charset=utf-8" (* default to utf-8 *) + | content_type -> content_type + let mime_lookup path = - match Fpath.to_string path with - | "build-environment" | "opam-switch" | "system-packages" -> - "text/plain" - | _ -> - if Fpath.has_ext "build-hashes" path - then "text/plain" - else if Fpath.is_prefix Fpath.(v "bin/") path - then "application/octet-stream" - else Magic_mime.lookup (Fpath.to_string path) + append_charset + (match Fpath.to_string path with + | "build-environment" | "opam-switch" | "system-packages" -> + "text/plain" + | _ -> + if Fpath.has_ext "build-hashes" path + then "text/plain" + else if Fpath.is_prefix Fpath.(v "bin/") path + then "application/octet-stream" + else Magic_mime.lookup (Fpath.to_string path)) let string_of_html = Format.asprintf "%a" (Tyxml.Html.pp ())