add content-type header

This commit is contained in:
Swrup 2025-09-28 19:37:49 +02:00
parent 0447857d1e
commit 06332cac8c
4 changed files with 66 additions and 57 deletions

View file

@ -1,29 +1,36 @@
(* TODO Taler documentation
markdown mimetype should be the prefered one, and be supported, according to DD
we take text/plain as default instead for now *)
let default_mimetype = ("text", "plain")
let default_extension = "txt"
let is_valid_filename, media_to_extension =
let mimetype_ext_assoc =
[
(("text", "plain"), "txt"); (("text", "markdown"), "md")
; (("text", "html"), "html"); (("text", "html"), "htm")
; (("application", "pdf"), "pdf"); (("image", "jpeg"), "jpg")
; (("image", "jpeg"), "jpeg"); (("image", "png"), "png")
; (("image", "gif"), "gif")
]
in
(* <> than the actual set of "supported" extension (which depends on config files) *)
let all_known_extensions = mimetype_ext_assoc |> List.split |> snd in
let is_valid_filename path = Fpath.mem_ext all_known_extensions path in
let media_to_extension = function
| Cohttp.Accept.MediaType (m, m_sub) ->
List.assoc_opt (m, m_sub) mimetype_ext_assoc
| AnyMediaSubtype m ->
List.find_map
(fun ((m', _), ext) ->
match String.equal m m' with false -> None | true -> Some ext)
mimetype_ext_assoc
| AnyMedia -> Some default_extension
in
(is_valid_filename, media_to_extension)
let mimetype_ext_assoc =
[
(("text", "plain"), "txt"); (("text", "markdown"), "md")
; (("text", "html"), "html"); (("text", "html"), "htm")
; (("application", "pdf"), "pdf"); (("image", "jpeg"), "jpg")
; (("image", "jpeg"), "jpeg"); (("image", "png"), "png")
; (("image", "gif"), "gif")
]
(* <> than the actual set of "supported" extension (which depends on config files) *)
let all_known_mimetypes, all_known_extensions = List.split mimetype_ext_assoc
let is_valid_filename path = Fpath.mem_ext all_known_extensions path
let media_to_known_mimetype = function
| Cohttp.Accept.MediaType (m, m_sub) ->
List.find_opt (( = ) (m, m_sub)) all_known_mimetypes
| AnyMediaSubtype m ->
List.find_opt (fun (m', _) -> String.equal m m') all_known_mimetypes
| AnyMedia -> Some default_mimetype
let mimetype_to_extension (m, m_sub) =
assert (m <> "*");
assert (m_sub <> "*");
List.assoc_opt (m, m_sub) mimetype_ext_assoc
let extension_to_mimetype ext =
List.find_map
(fun (mime, ext') ->
match String.equal ext ext' with false -> None | true -> Some mime)
mimetype_ext_assoc