From a5870e7d24e5ec75c956bcf3e4423f55a2c4ff78 Mon Sep 17 00:00:00 2001 From: Serdar Dogruyol Date: Sat, 16 Jun 2018 18:03:00 +0300 Subject: [PATCH] Crystal 0.25.0 (#452) --- spec/param_parser_spec.cr | 2 +- spec/route_handler_spec.cr | 1 - spec/static_file_handler_spec.cr | 4 ++-- spec/websocket_handler_spec.cr | 18 ++++++++++-------- src/kemal.cr | 8 ++++---- src/kemal/config.cr | 2 +- src/kemal/helpers/helpers.cr | 4 ++-- src/kemal/param_parser.cr | 4 ++-- src/kemal/static_file_handler.cr | 2 +- 9 files changed, 23 insertions(+), 22 deletions(-) diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index 5b3b2ab..305a0a3 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 67d9b71..5705618 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 7b9b099..6da77a3 100644 --- a/spec/static_file_handler_spec.cr +++ b/spec/static_file_handler_spec.cr @@ -131,7 +131,7 @@ describe Kemal::StaticFileHandler do end it "should handle setting custom headers" do - headers = Proc(HTTP::Server::Response, String, File::Stat, Void).new do |response, path, stat| + 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 @@ -143,7 +143,7 @@ describe Kemal::StaticFileHandler do response = handle HTTP::Request.new("GET", "/dir/test.txt") 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") diff --git a/spec/websocket_handler_spec.cr b/spec/websocket_handler_spec.cr index a3c0634..bb867b2 100644 --- a/spec/websocket_handler_spec.cr +++ b/spec/websocket_handler_spec.cr @@ -25,27 +25,29 @@ describe "Kemal::WebSocketHandler" do ws "/" { |socket| socket.send("Match") } ws "/no_match" { |socket| socket.send "No Match" } headers = HTTP::Headers{ - "Upgrade" => "websocket", - "Connection" => "Upgrade", - "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", + "Upgrade" => "websocket", + "Connection" => "Upgrade", + "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", + "Sec-WebSocket-Version" => "13", } request = HTTP::Request.new("GET", "/", headers) io_with_context = create_ws_request_and_return_io(handler, request) - io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match") + io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match") end it "fetches named url parameters" do handler = Kemal::WebSocketHandler::INSTANCE ws "/:id" { |_, c| c.params.url["id"] } headers = HTTP::Headers{ - "Upgrade" => "websocket", - "Connection" => "Upgrade", - "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", + "Upgrade" => "websocket", + "Connection" => "Upgrade", + "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", + "Sec-WebSocket-Version" => "13", } request = HTTP::Request.new("GET", "/1234", headers) io_with_context = create_ws_request_and_return_io(handler, request) - io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n") + io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n") end it "matches correct verb" do diff --git a/src/kemal.cr b/src/kemal.cr index 514478f..5110a46 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -59,7 +59,7 @@ module Kemal end end - config.server ||= HTTP::Server.new(config.host_binding, config.port, config.handlers) + server = config.server ||= HTTP::Server.new(config.handlers) {% if !flag?(:without_openssl) %} config.server.not_nil!.tls = config.ssl @@ -68,13 +68,13 @@ module Kemal config.running = true yield config - config.server.not_nil!.listen if config.env != "test" && config.server + server.listen(config.host_binding, config.port) if config.env != "test" end def self.stop if config.running - if config.server - config.server.not_nil!.close + if server = config.server + server.close unless server.closed? config.running = false else raise "Kemal.config.server is not set. Please use Kemal.run to set the server." diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 729a9e0..d0c4ee2 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -21,7 +21,7 @@ module Kemal property host_binding, ssl, port, env, public_folder, logging, running property always_rescue, server : HTTP::Server?, extra_options, shutdown_message property serve_static : (Bool | Hash(String, Bool)) - property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)? + property static_headers : (HTTP::Server::Response, String, File::Info -> Void)? property powered_by_header : Bool = true def initialize diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index 9c79da6..3ed4045 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -119,7 +119,7 @@ def send_file(env : HTTP::Server::Context, path : String, mime_type : String? = 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) Kemal.config.static_headers.try(&.call(env.response, file_path, filestat)) @@ -234,6 +234,6 @@ end # response.headers.add("Content-Size", filestat.size.to_s) # end # ``` -def static_headers(&headers : HTTP::Server::Response, String, File::Stat -> Void) +def static_headers(&headers : HTTP::Server::Response, String, File::Info -> Void) Kemal.config.static_headers = headers end diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index e42a87a..188eb4f 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -8,7 +8,7 @@ module Kemal MULTIPART_FORM = "multipart/form-data" PARTS = %w(url query body json) # :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) @@ -89,7 +89,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 5cffa60..fc9e036 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -58,7 +58,7 @@ module Kemal end private def etag(context : HTTP::Server::Context, file_path : String) - etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"} + etag = %{W/"#{File.info(file_path).modification_time.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"