From 23cd325def21b89470c9b35b46e9626789f5e7f0 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Sat, 19 Nov 2016 00:05:22 +0300 Subject: [PATCH] Move some stuff to Utils --- src/kemal/helpers/utils.cr | 25 ++++++++++++++++++++++++- src/kemal/request.cr | 14 +++++++------- src/kemal/static_file_handler.cr | 31 +++++-------------------------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/kemal/helpers/utils.cr b/src/kemal/helpers/utils.cr index be7f813..33a1ca5 100644 --- a/src/kemal/helpers/utils.cr +++ b/src/kemal/helpers/utils.cr @@ -1,7 +1,30 @@ module Kemal - class Utils + module Utils def self.path_starts_with_backslash?(path) path.starts_with?("/") end + + def self.zip_types(path) # https://github.com/h5bp/server-configs-nginx/blob/master/nginx.conf + [".htm", ".html", ".txt", ".css", ".js", ".svg", ".json", ".xml", ".otf", ".ttf", ".woff", ".woff2"].includes? File.extname(path) + end + + def self.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 ".png" then "image/png" + when ".jpg", ".jpeg" then "image/jpeg" + when ".gif" then "image/gif" + when ".svg" then "image/svg+xml" + when ".xml" then "application/xml" + when ".json" then "application/json" + when ".otf", ".ttf" then "application/font-sfnt" + when ".woff" then "application/font-woff" + when ".woff2" then "font/woff2" + else "application/octet-stream" + end + end end end diff --git a/src/kemal/request.cr b/src/kemal/request.cr index 946dfed..71eba31 100644 --- a/src/kemal/request.cr +++ b/src/kemal/request.cr @@ -7,6 +7,13 @@ class HTTP::Request @override_method ||= check_for_method_override! end + # Checks if method contained in _method param is valid one + def self.override_method_valid?(override_method) + return false unless override_method.is_a?(String) + override_method = override_method.upcase + override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE" + end + # Checks if request params contain _method param to override request incoming method private def check_for_method_override! @override_method = @method @@ -18,11 +25,4 @@ class HTTP::Request end @override_method end - - # Checks if method contained in _method param is valid one - def self.override_method_valid?(override_method) - return false unless override_method.is_a?(String) - override_method = override_method.upcase - override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE" - end end diff --git a/src/kemal/static_file_handler.cr b/src/kemal/static_file_handler.cr index 4e26d51..fe6221e 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -2,6 +2,8 @@ require "zlib" {% end %} +require "./helpers/utils" + module Kemal class StaticFileHandler < HTTP::StaticFileHandler def call(context) @@ -52,16 +54,16 @@ module Kemal elsif File.exists?(file_path) return if self.etag(context, file_path) minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ?? - context.response.content_type = mime_type(file_path) + context.response.content_type = Utils.mime_type(file_path) request_headers = context.request.headers filesize = File.size(file_path) File.open(file_path) do |file| - if request_headers.includes_word?("Accept-Encoding", "gzip") && config.is_a?(Hash) && config["gzip"] == true && filesize > minsize && self.zip_types(file_path) + if request_headers.includes_word?("Accept-Encoding", "gzip") && config.is_a?(Hash) && config["gzip"] == true && filesize > minsize && Utils.zip_types(file_path) context.response.headers["Content-Encoding"] = "gzip" Zlib::Deflate.gzip(context.response) do |deflate| IO.copy(file, deflate) end - elsif request_headers.includes_word?("Accept-Encoding", "deflate") && config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && self.zip_types(file_path) + elsif request_headers.includes_word?("Accept-Encoding", "deflate") && config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Utils.zip_types(file_path) context.response.headers["Content-Encoding"] = "deflate" Zlib::Deflate.new(context.response) do |deflate| IO.copy(file, deflate) @@ -85,28 +87,5 @@ module Kemal context.response.status_code = 304 # not modified return true end - - def zip_types(path) # https://github.com/h5bp/server-configs-nginx/blob/master/nginx.conf - [".htm", ".html", ".txt", ".css", ".js", ".svg", ".json", ".xml", ".otf", ".ttf", ".woff", ".woff2"].includes? File.extname(path) - end - - 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 ".png" then "image/png" - when ".jpg", ".jpeg" then "image/jpeg" - when ".gif" then "image/gif" - when ".svg" then "image/svg+xml" - when ".xml" then "application/xml" - when ".json" then "application/json" - when ".otf", ".ttf" then "application/font-sfnt" - when ".woff" then "application/font-woff" - when ".woff2" then "font/woff2" - else "application/octet-stream" - end - end end end