Skip to content

Commit

Permalink
Use MIME in HTTP::StaticFileHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Mar 7, 2018
1 parent 520b572 commit da3e5ac
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions 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
Expand Down Expand Up @@ -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)
Expand All @@ -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?

Expand Down

0 comments on commit da3e5ac

Please sign in to comment.