From e99129ab507dae1518659e0f08e9dce239fb8f01 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Mon, 5 Aug 2019 18:34:26 +0300 Subject: [PATCH] fix process request when a response already closed (#550) --- spec/helpers_spec.cr | 18 ++++++++++++++++++ src/kemal/route_handler.cr | 1 + 2 files changed, 19 insertions(+) diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index 77737bd..76fc318 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -63,6 +63,24 @@ describe "Macros" do end end + describe "#callbacks" do + it "can break block with halt macro from before_* callback" do + filter_middleware = Kemal::FilterHandler.new + filter_middleware._add_route_filter("GET", "/", :before) do |env| + halt env, status_code: 400, response: "Missing origin." + end + + get "/" do |env| + "Hello world" + end + + request = HTTP::Request.new("GET", "/") + client_response = call_request_on_app(request) + client_response.status_code.should eq(400) + client_response.body.should eq("Missing origin.") + end + end + describe "#headers" do it "can add headers" do get "/headers" do |env| diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index 528d773..216616a 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -45,6 +45,7 @@ module Kemal # Processes the route if it's a match. Otherwise renders 404. private def process_request(context) raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_found? + return if context.response.closed? content = context.route.handler.call(context) if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(context.response.status_code)