Compare commits

...

8 Commits

Author SHA1 Message Date
Sijawusz Pur Rahnama 7c47bbc150
Fix recent changes to `CustomException` (#673) 2024-02-14 13:33:05 +03:00
Serdar Dogruyol bef7351000 Don't call super on CustomException 2024-02-03 13:48:18 +03:00
Serdar Dogruyol - Sedo セド bb9105f202
Add message support to Kemal::Exceptions::CustomException (#671) 2024-02-01 12:50:40 +03:00
Sijawusz Pur Rahnama 9628043e4c
Bump `exception_page` dependency (#669) 2024-01-23 19:08:49 +03:00
Giorgi Kavrelishvili 0de0e990e9
Move to Process.on_interupt (#666) 2024-01-23 19:07:39 +03:00
Val Packett 13fd4f8b2f
helpers: Support building without_zlib (#667) 2023-10-31 13:34:15 +03:00
Thomas Fini Hansen cb9adcd188
Allow HTTP::Server::Context#redirect to take an URL (#659) 2023-09-24 09:35:08 +03:00
Giorgi Kavrelishvili a939a577de
Fixes the issues with Ameba types 2023-09-23 10:48:45 +03:00
7 changed files with 42 additions and 37 deletions

View File

@ -10,12 +10,11 @@ dependencies:
version: ~> 0.4.0
exception_page:
github: crystal-loot/exception_page
version: ~> 0.3.0
version: ~> 0.4.1
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 1.4.0
crystal: ">= 0.36.0"

View File

@ -38,7 +38,7 @@ describe "Kemal::WebSocketHandler" do
it "fetches named url parameters" do
handler = Kemal::WebSocketHandler::INSTANCE
ws "/:id" { |_, c| c.ws_route_lookup.params["id"] }
ws "/:id" { |_, context| context.ws_route_lookup.params["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",

View File

@ -89,7 +89,7 @@ module Kemal
end
private def self.setup_trap_signal
Signal::INT.trap do
Process.on_interrupt do
log "#{Kemal.config.app_name} is going to take a rest!" if Kemal.config.shutdown_message
Kemal.stop
exit

View File

@ -159,8 +159,8 @@ module Kemal
end
private def setup_filter_handlers
FILTER_HANDLERS.each do |h|
HANDLERS.insert(@handler_position, h)
FILTER_HANDLERS.each do |handler|
HANDLERS.insert(@handler_position, handler)
end
end
end

View File

@ -9,7 +9,7 @@ class HTTP::Server
STORE_MAPPINGS = [Nil, String, Int32, Int64, Float64, Bool]
macro finished
alias StoreTypes = Union({{ *STORE_MAPPINGS }})
alias StoreTypes = Union({{ STORE_MAPPINGS.splat }})
@store = {} of String => StoreTypes
end
@ -17,8 +17,8 @@ class HTTP::Server
@params ||= Kemal::ParamParser.new(@request, route_lookup.params)
end
def redirect(url : String, status_code : Int32 = 302, *, body : String? = nil, close : Bool = true)
@response.headers.add "Location", url
def redirect(url : String | URI, status_code : Int32 = 302, *, body : String? = nil, close : Bool = true)
@response.headers.add "Location", url.to_s
@response.status_code = status_code
@response.print(body) if body
@response.close if close

View File

@ -13,8 +13,9 @@ module Kemal::Exceptions
end
class CustomException < Exception
def initialize(context : HTTP::Server::Context)
super "Rendered error with #{context.response.status_code}"
def initialize(@context : HTTP::Server::Context, message : String? = nil)
message ||= "Rendered error with #{context.response.status_code}"
super message
end
end
end

View File

@ -1,4 +1,4 @@
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 && !flag?(:without_zlib) %}
require "compress/deflate"
require "compress/gzip"
{% end %}
@ -141,33 +141,38 @@ def send_file(env : HTTP::Server::Context, path : String, mime_type : String? =
next multipart(file, env)
end
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"
{% 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"
{% 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
{% if flag?(:without_zlib) %}
env.response.content_length = filesize
IO.copy(file, env.response)
end
{% else %}
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"
{% 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"
{% 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)
end
{% end %}
end
return
end