Skip to content

Commit

Permalink
Use 'magic-mime' to determine content type
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Reis <[email protected]>
  • Loading branch information
benjamreis committed Apr 2, 2024
1 parent 65428f6 commit 3ea7bb0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 35 deletions.
1 change: 1 addition & 0 deletions ocaml/xapi/dune
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
hex
http_lib
ipaddr
magic-mime
message-switch-core
message-switch-unix
mirage-crypto
Expand Down
36 changes: 1 addition & 35 deletions ocaml/xapi/fileserver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,8 @@ let missing uri =
^ " was not found on this server.</p> <hr><address>Xapi \
Server</address></body></html>"

let get_extension filename =
try
let basename = Filename.basename filename in
let i = String.rindex basename '.' in
Some (String.sub basename (i + 1) (String.length basename - i - 1))
with _ -> None

let application_octet_stream = "application/octet-stream"

let mime_of_extension = function
| "html" | "htm" ->
"text/html"
| "css" ->
"text/css"
| "js" ->
"application/javascript"
| "gif" ->
"image/gif"
| "png" ->
"image/png"
| "jpg" | "jpeg" ->
"image/jpeg"
| "xml" ->
"application/xml"
| "rpm" ->
"application/x-rpm"
| "svg" ->
"image/svg+xml"
| _ ->
application_octet_stream

let response_file s file_path =
let mime_content_type =
let ext = Option.map String.lowercase_ascii (get_extension file_path) in
Option.fold ~none:application_octet_stream ~some:mime_of_extension ext
in
let mime_content_type = Mime_types.map_file file_path in
let hsts_time = !Xapi_globs.hsts_max_age in
Http_svr.response_file ~mime_content_type ~hsts_time s file_path

Expand Down

0 comments on commit 3ea7bb0

Please sign in to comment.