Compare commits

..

5 commits

Author SHA1 Message Date
c3cd44205f Merge remote-tracking branch 'upstream/master' into master 2020-09-01 18:09:52 -03:00
Brian J. Cardiff
a819d4792b
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
2020-08-15 18:09:53 +03:00
Anton Maminov
dfe7dca08f
fix websocket issue (#577) 2020-07-08 14:42:08 +03:00
Anton Maminov
2e749a2987
fix an issues with responding with long strings (#576) 2020-07-08 14:41:16 +03:00
Brian J. Cardiff
a8c0f09b85
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
2020-06-10 21:11:43 +03:00
8 changed files with 45 additions and 23 deletions

View file

@ -18,7 +18,7 @@ dependencies:
development_dependencies: development_dependencies:
ameba: ameba:
github: crystal-ameba/ameba github: crystal-ameba/ameba
version: ~> 0.10.0 version: ~> 0.12.0
crystal: 0.30.0 crystal: 0.30.0

View file

@ -10,6 +10,17 @@ describe "Kemal::RouteHandler" do
client_response.body.should eq("hello") client_response.body.should eq("hello")
end 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 it "routes should only return strings" do
get "/" do get "/" do
100 100

View file

@ -5,12 +5,12 @@ private def run(code)
require "./src/kemal" require "./src/kemal"
#{code} #{code}
CR CR
String.build do |stdout|
stderr = String.build do |io| stdout = IO::Memory.new
Process.new("crystal", ["eval"], input: IO::Memory.new(code), output: stdout, error: io).wait stderr = IO::Memory.new
end status = Process.new("crystal", ["eval"], input: IO::Memory.new(code), output: stdout, error: stderr).wait
fail(stderr) unless stderr.empty? fail(stderr.to_s) unless status.success?
end stdout.to_s
end end
describe "Run" do describe "Run" do

View file

@ -48,6 +48,9 @@ def create_ws_request_and_return_io_and_context(handler, request)
rescue IO::Error rescue IO::Error
# Raises because the IO::Memory is empty # Raises because the IO::Memory is empty
end end
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
response.upgrade_handler.try &.call(io)
{% end %}
io.rewind io.rewind
{io, context} {io, context}
end end

View file

@ -8,7 +8,7 @@ class HTTP::Server::Response
ensure_headers_written ensure_headers_written
super previous_def
end end
end end
end end

View file

@ -1,5 +1,7 @@
require "flate" {% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
require "gzip" require "compress/deflate"
require "compress/gzip"
{% end %}
require "mime" require "mime"
# Adds given `Kemal::Handler` to handlers chain. # 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) 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") if condition && request_headers.includes_word?("Accept-Encoding", "gzip")
env.response.headers["Content-Encoding"] = "gzip" env.response.headers["Content-Encoding"] = "gzip"
{% 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| Gzip::Writer.open(env.response) do |deflate|
IO.copy(file, deflate) IO.copy(file, deflate)
end end
{% end %}
elsif condition && request_headers.includes_word?("Accept-Encoding", "deflate") elsif condition && request_headers.includes_word?("Accept-Encoding", "deflate")
env.response.headers["Content-Encoding"] = "deflate" env.response.headers["Content-Encoding"] = "deflate"
{% 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| Flate::Writer.open(env.response) do |deflate|
IO.copy(file, deflate) IO.copy(file, deflate)
end end
{% end %}
else else
env.response.content_length = filesize env.response.content_length = filesize
IO.copy(file, env.response) IO.copy(file, env.response)

View file

@ -1,7 +1,3 @@
{% if !flag?(:without_zlib) %}
require "zlib"
{% end %}
module Kemal module Kemal
class StaticFileHandler < HTTP::StaticFileHandler class StaticFileHandler < HTTP::StaticFileHandler
# ameba:disable Metrics/CyclomaticComplexity # ameba:disable Metrics/CyclomaticComplexity

View file

@ -31,9 +31,7 @@ module Kemal
def call(context : HTTP::Server::Context) def call(context : HTTP::Server::Context)
return call_next(context) unless context.ws_route_found? && websocket_upgrade_request?(context) return call_next(context) unless context.ws_route_found? && websocket_upgrade_request?(context)
content = context.websocket.call(context) context.websocket.call(context)
context.response.print(content)
context
end end
def lookup_ws_route(path : String) def lookup_ws_route(path : String)