Move some stuff to Utils
This commit is contained in:
parent
bc764b7af7
commit
23cd325def
3 changed files with 36 additions and 34 deletions
|
@ -1,7 +1,30 @@
|
||||||
module Kemal
|
module Kemal
|
||||||
class Utils
|
module Utils
|
||||||
def self.path_starts_with_backslash?(path)
|
def self.path_starts_with_backslash?(path)
|
||||||
path.starts_with?("/")
|
path.starts_with?("/")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,13 @@ class HTTP::Request
|
||||||
@override_method ||= check_for_method_override!
|
@override_method ||= check_for_method_override!
|
||||||
end
|
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
|
# Checks if request params contain _method param to override request incoming method
|
||||||
private def check_for_method_override!
|
private def check_for_method_override!
|
||||||
@override_method = @method
|
@override_method = @method
|
||||||
|
@ -18,11 +25,4 @@ class HTTP::Request
|
||||||
end
|
end
|
||||||
@override_method
|
@override_method
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
require "zlib"
|
require "zlib"
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
|
require "./helpers/utils"
|
||||||
|
|
||||||
module Kemal
|
module Kemal
|
||||||
class StaticFileHandler < HTTP::StaticFileHandler
|
class StaticFileHandler < HTTP::StaticFileHandler
|
||||||
def call(context)
|
def call(context)
|
||||||
|
@ -52,16 +54,16 @@ module Kemal
|
||||||
elsif File.exists?(file_path)
|
elsif File.exists?(file_path)
|
||||||
return if self.etag(context, 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 ??
|
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
|
request_headers = context.request.headers
|
||||||
filesize = File.size(file_path)
|
filesize = File.size(file_path)
|
||||||
File.open(file_path) do |file|
|
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"
|
context.response.headers["Content-Encoding"] = "gzip"
|
||||||
Zlib::Deflate.gzip(context.response) do |deflate|
|
Zlib::Deflate.gzip(context.response) do |deflate|
|
||||||
IO.copy(file, deflate)
|
IO.copy(file, deflate)
|
||||||
end
|
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"
|
context.response.headers["Content-Encoding"] = "deflate"
|
||||||
Zlib::Deflate.new(context.response) do |deflate|
|
Zlib::Deflate.new(context.response) do |deflate|
|
||||||
IO.copy(file, deflate)
|
IO.copy(file, deflate)
|
||||||
|
@ -85,28 +87,5 @@ module Kemal
|
||||||
context.response.status_code = 304 # not modified
|
context.response.status_code = 304 # not modified
|
||||||
return true
|
return true
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue