From a8c0f09b858162bd13c96663febef5527b322a32 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Wed, 10 Jun 2020 15:11:43 -0300 Subject: [PATCH 1/4] Upgrade to Crystal 0.35.0 (#570) * Update to ameba ~> 0.12.0 * Use Compress::Deflate and Compress::Gzip * Drop unused require * Fix spec due to defer request upgrade --- shard.yml | 2 +- spec/spec_helper.cr | 3 +++ src/kemal/helpers/helpers.cr | 30 ++++++++++++++++++++++-------- src/kemal/static_file_handler.cr | 4 ---- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/shard.yml b/shard.yml index d605e2c..428149a 100644 --- a/shard.yml +++ b/shard.yml @@ -18,7 +18,7 @@ dependencies: development_dependencies: ameba: github: crystal-ameba/ameba - version: ~> 0.10.0 + version: ~> 0.12.0 crystal: 0.30.0 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 0bc127a..803aacf 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -48,6 +48,9 @@ def create_ws_request_and_return_io_and_context(handler, request) rescue IO::Error # Raises because the IO::Memory is empty end + {% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %} + response.upgrade_handler.try &.call(io) + {% end %} io.rewind {io, context} end diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index 7f14755..bf212a2 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -1,5 +1,7 @@ -require "flate" -require "gzip" +{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %} + require "compress/deflate" + require "compress/gzip" +{% end %} require "mime" # Adds given `Kemal::Handler` to handlers chain. @@ -142,14 +144,26 @@ def send_file(env : HTTP::Server::Context, path : String, mime_type : String? = condition = config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Kemal::Utils.zip_types(file_path) if condition && request_headers.includes_word?("Accept-Encoding", "gzip") env.response.headers["Content-Encoding"] = "gzip" - Gzip::Writer.open(env.response) do |deflate| - IO.copy(file, deflate) - end + {% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %} + Compress::Gzip::Writer.open(env.response) do |deflate| + IO.copy(file, deflate) + end + {% else %} + Gzip::Writer.open(env.response) do |deflate| + IO.copy(file, deflate) + end + {% end %} elsif condition && request_headers.includes_word?("Accept-Encoding", "deflate") env.response.headers["Content-Encoding"] = "deflate" - Flate::Writer.open(env.response) do |deflate| - IO.copy(file, deflate) - end + {% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %} + Compress::Deflate::Writer.open(env.response) do |deflate| + IO.copy(file, deflate) + end + {% else %} + Flate::Writer.open(env.response) do |deflate| + IO.copy(file, deflate) + end + {% end %} else env.response.content_length = filesize IO.copy(file, env.response) diff --git a/src/kemal/static_file_handler.cr b/src/kemal/static_file_handler.cr index 7f3e061..109e971 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -1,7 +1,3 @@ -{% if !flag?(:without_zlib) %} - require "zlib" -{% end %} - module Kemal class StaticFileHandler < HTTP::StaticFileHandler # ameba:disable Metrics/CyclomaticComplexity From 2e749a298791bfc708d7eb82e07b591d8d7bac1d Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Wed, 8 Jul 2020 14:41:16 +0300 Subject: [PATCH 2/4] fix an issues with responding with long strings (#576) --- spec/route_handler_spec.cr | 11 +++++++++++ src/kemal/ext/response.cr | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index 4ec111b..93cf0f0 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -10,6 +10,17 @@ describe "Kemal::RouteHandler" do client_response.body.should eq("hello") end + it "routes with long response body" do + long_response_body = "string" * 10_000 + + get "/" do + long_response_body + end + request = HTTP::Request.new("GET", "/") + client_response = call_request_on_app(request) + client_response.body.should eq(long_response_body) + end + it "routes should only return strings" do get "/" do 100 diff --git a/src/kemal/ext/response.cr b/src/kemal/ext/response.cr index faf9db1..6fd4c01 100644 --- a/src/kemal/ext/response.cr +++ b/src/kemal/ext/response.cr @@ -8,7 +8,7 @@ class HTTP::Server::Response ensure_headers_written - super + previous_def end end end From dfe7dca08f4c9a9456d6132af5f6b59fcd6865e4 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Wed, 8 Jul 2020 14:42:08 +0300 Subject: [PATCH 3/4] fix websocket issue (#577) --- src/kemal/websocket_handler.cr | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/kemal/websocket_handler.cr b/src/kemal/websocket_handler.cr index addbecf..b1432a0 100644 --- a/src/kemal/websocket_handler.cr +++ b/src/kemal/websocket_handler.cr @@ -11,9 +11,7 @@ module Kemal def call(context : HTTP::Server::Context) return call_next(context) unless context.ws_route_found? && websocket_upgrade_request?(context) - content = context.websocket.call(context) - context.response.print(content) - context + context.websocket.call(context) end def lookup_ws_route(path : String) From a819d4792bbc63d993d6dd2b15f9af5efb0c2d8c Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Sat, 15 Aug 2020 12:09:53 -0300 Subject: [PATCH 4/4] Refactor run_spec to use process exit instead of empty stderr (#584) Bonus: It also allows run_spec to pass if using bin/crystal wrapper --- spec/run_spec.cr | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/run_spec.cr b/spec/run_spec.cr index 818cbe9..ec89d23 100644 --- a/spec/run_spec.cr +++ b/spec/run_spec.cr @@ -5,12 +5,12 @@ private def run(code) require "./src/kemal" #{code} CR - String.build do |stdout| - stderr = String.build do |io| - Process.new("crystal", ["eval"], input: IO::Memory.new(code), output: stdout, error: io).wait - end - fail(stderr) unless stderr.empty? - end + + stdout = IO::Memory.new + stderr = IO::Memory.new + status = Process.new("crystal", ["eval"], input: IO::Memory.new(code), output: stdout, error: stderr).wait + fail(stderr.to_s) unless status.success? + stdout.to_s end describe "Run" do