From aab22b1891ba55f0a260d7a786baa18da1e548f8 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Mon, 17 Dec 2018 18:58:02 +0300 Subject: [PATCH] WIP --- spec/param_parser_spec.cr | 2 +- spec/route_handler_spec.cr | 1 - spec/static_file_handler_spec.cr | 4 ++-- src/kemal/base.cr | 14 ++++++++++---- src/kemal/config.cr | 2 +- src/kemal/file_upload.cr | 4 ++-- src/kemal/helpers/file_helpers.cr | 2 +- src/kemal/main.cr | 1 - src/kemal/param_parser.cr | 4 ++-- src/kemal/static_file_handler.cr | 19 ++++++++----------- 10 files changed, 27 insertions(+), 26 deletions(-) diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index b61350ab..b415366c 100644 --- a/spec/param_parser_spec.cr +++ b/spec/param_parser_spec.cr @@ -174,7 +174,7 @@ describe "ParamParser" do body_params.to_s.should eq("") json_params = Kemal::ParamParser.new(request).json - json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)) + json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any)) end end diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index 24dda9b3..0ed640bf 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -85,7 +85,6 @@ describe "Kemal::RouteHandler" do post "/" do |env| skills = env.params.json["skills"].as(Array) skills_from_languages = skills.map do |skill| - skill = skill.as(Hash) skill["language"] end "Skills #{skills_from_languages.each.join(',')}" diff --git a/spec/static_file_handler_spec.cr b/spec/static_file_handler_spec.cr index ff3b885a..db0f2f8d 100644 --- a/spec/static_file_handler_spec.cr +++ b/spec/static_file_handler_spec.cr @@ -144,7 +144,7 @@ describe Kemal::StaticFileHandler do it "should handle setting custom headers" do config = default_config - config.static_headers = Proc(HTTP::Server::Response, String, File::Stat, Void).new do |response, path, stat| + config.static_headers = Proc(HTTP::Server::Response, String, File::Info, Void).new do |response, path, stat| if path =~ /\.html$/ response.headers.add("Access-Control-Allow-Origin", "*") end @@ -154,7 +154,7 @@ describe Kemal::StaticFileHandler do response = handle HTTP::Request.new("GET", "/dir/test.txt"), config response.headers.has_key?("Access-Control-Allow-Origin").should be_false response.headers["Content-Size"].should eq( - File.stat("#{__DIR__}/static/dir/test.txt").size.to_s + File.info("#{__DIR__}/static/dir/test.txt").size.to_s ) response = handle HTTP::Request.new("GET", "/dir/index.html"), config diff --git a/src/kemal/base.cr b/src/kemal/base.cr index e1451012..bbb073a5 100644 --- a/src/kemal/base.cr +++ b/src/kemal/base.cr @@ -80,12 +80,18 @@ class Kemal::Base end private def start_server(port) - @server = server = HTTP::Server.new(@config.host_binding, port || @config.port, @handlers) - {% if !flag?(:without_openssl) %} - server.tls = config.ssl + @server = server = HTTP::Server.new(@handlers) + + {% if flag?(:without_openssl) %} + server.bind_tcp(@config.host_binding, port || @config.port) + {% else %} + if ssl = config.ssl + server.bind_tls(@config.host_binding, port || @config.port, ssl) + else + server.bind_tcp(@config.host_binding, port || @config.port) + end {% end %} - server.bind @running = true yield diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 878da9ff..1e09ffa1 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -20,7 +20,7 @@ module Kemal property? always_rescue = true property? shutdown_message = true property extra_options : (OptionParser ->)? - property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)? + property static_headers : (HTTP::Server::Response, String, File::Info -> Void)? # Creates a config with default values. def initialize( diff --git a/src/kemal/file_upload.cr b/src/kemal/file_upload.cr index 562c18b1..e3bdd4ea 100644 --- a/src/kemal/file_upload.cr +++ b/src/kemal/file_upload.cr @@ -1,7 +1,7 @@ module Kemal # :nodoc: struct FileUpload - getter tmpfile : Tempfile + getter tmpfile : File getter filename : String? getter headers : HTTP::Headers getter creation_time : Time? @@ -10,7 +10,7 @@ module Kemal getter size : UInt64? def initialize(upload) - @tmpfile = Tempfile.new(filename) + @tmpfile = File.tempfile ::File.open(@tmpfile.path, "w") do |file| IO.copy(upload.body, file) end diff --git a/src/kemal/helpers/file_helpers.cr b/src/kemal/helpers/file_helpers.cr index cdc2b65c..ade82880 100644 --- a/src/kemal/helpers/file_helpers.cr +++ b/src/kemal/helpers/file_helpers.cr @@ -22,7 +22,7 @@ module Kemal::FileHelpers minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ?? request_headers = env.request.headers filesize = File.size(file_path) - filestat = File.stat(file_path) + filestat = File.info(file_path) config.static_headers.try(&.call(env.response, file_path, filestat)) gzip = config.serve_static?("gzip") diff --git a/src/kemal/main.cr b/src/kemal/main.cr index c0b3bcac..4a66d1a5 100644 --- a/src/kemal/main.cr +++ b/src/kemal/main.cr @@ -1,7 +1,6 @@ require "http" require "json" require "uri" -require "tempfile" require "./application" require "./base_log_handler" require "./cli" diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index d2893cbc..f4f36deb 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -7,7 +7,7 @@ module Kemal APPLICATION_JSON = "application/json" MULTIPART_FORM = "multipart/form-data" # :nodoc: - alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) + alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any) getter files def initialize(@request : HTTP::Request) @@ -88,7 +88,7 @@ module Kemal case json = JSON.parse(body).raw when Hash json.each do |key, value| - @json[key] = value.as(AllParamTypes) + @json[key] = value.raw end when Array @json["_json"] = json diff --git a/src/kemal/static_file_handler.cr b/src/kemal/static_file_handler.cr index ddf559df..50cb6047 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -55,21 +55,18 @@ module Kemal return call_next(context) end elsif File.exists?(file_path) - return if etag(context, file_path) + last_modified = modification_time(file_path) + add_cache_headers(context.response.headers, last_modified) + + if cache_request?(context, last_modified) + context.response.status_code = 304 + return + end + FileHelpers.send_file(context, file_path, config) else call_next(context) end end - - private def etag(context : HTTP::Server::Context, file_path : String) - etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"} - context.response.headers["ETag"] = etag - return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag - context.response.headers.delete "Content-Type" - context.response.content_length = 0 - context.response.status_code = 304 # not modified - return true - end end end