From d9da99ac48fd865ef5b8b86d41df0a1b597f5b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sat, 3 Mar 2018 22:40:40 +0000 Subject: [PATCH] Use MIME in HTTP::StaticFileHandler --- src/http/server/handlers/static_file_handler.cr | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/http/server/handlers/static_file_handler.cr b/src/http/server/handlers/static_file_handler.cr index fdc17bcfc54d..38317bcab358 100644 --- a/src/http/server/handlers/static_file_handler.cr +++ b/src/http/server/handlers/static_file_handler.cr @@ -1,6 +1,7 @@ require "ecr/macros" require "html" require "uri" +require "mime" # A simple handler that lists directories and serves files under a given public directory. class HTTP::StaticFileHandler @@ -80,7 +81,7 @@ class HTTP::StaticFileHandler end end - context.response.content_type = mime_type(file_path) + context.response.content_type = MIME.fetch(File.extname(file_path), "application/octet-stream") context.response.content_length = File.size(file_path) File.open(file_path) do |file| IO.copy(file, context.response) @@ -103,17 +104,6 @@ class HTTP::StaticFileHandler context.response.headers.add "Location", url end - private def mime_type(path) - case File.extname(path) - when ".txt" then "text/plain" - when ".htm", ".html" then "text/html" - when ".css" then "text/css" - when ".js" then "application/javascript" - when ".svg" then "image/svg+xml" - else "application/octet-stream" - end - end - record DirectoryListing, request_path : String, path : String do @escaped_request_path : String?