Skip to content

Commit c8f857d

Browse files
authored
Omitting filters fix for lowercase methods requests (#647)
1 parent 93521b7 commit c8f857d

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

spec/filters_spec.cr

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "./spec_helper"
2+
3+
describe "Kemal::FilterHandler" do
4+
it "handles with upcased 'POST'" do
5+
filter_handler = Kemal::FilterHandler.new
6+
filter_handler._add_route_filter("POST", "*", :before) do |env|
7+
env.set "sensitive", "1"
8+
end
9+
Kemal.config.add_filter_handler(filter_handler)
10+
11+
post "/sensitive_post" do |env|
12+
env.get "sensitive"
13+
end
14+
15+
request = HTTP::Request.new("POST", "/sensitive_post")
16+
client_response = call_request_on_app(request)
17+
client_response.status_code.should eq(200)
18+
client_response.body.should eq("1")
19+
end
20+
21+
it "handles with downcased 'post'" do
22+
filter_handler = Kemal::FilterHandler.new
23+
filter_handler._add_route_filter("POST", "*", :before) do |env|
24+
env.set "sensitive", "1"
25+
end
26+
Kemal.config.add_filter_handler(filter_handler)
27+
28+
post "/sensitive_post" do
29+
"sensitive"
30+
end
31+
32+
request = HTTP::Request.new("post", "/sensitive_post")
33+
client_response = call_request_on_app(request)
34+
client_response.status_code.should eq(200)
35+
client_response.body.should eq("")
36+
end
37+
end

spec/spec_helper.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ end
8585

8686
Spec.after_each do
8787
Kemal.config.clear
88+
Kemal::FilterHandler::INSTANCE.tree = Radix::Tree(Array(Kemal::FilterHandler::FilterBlock)).new
8889
Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new
8990
Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new
9091
Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new

src/kemal/filter_handler.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Kemal
33
class FilterHandler
44
include HTTP::Handler
55
INSTANCE = new
6+
property tree
67

78
# This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made.
89
def initialize

src/kemal/handler.cr

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ module Kemal
1111

1212
macro only(paths, method = "GET")
1313
class_name = {{@type.name}}
14-
method_downcase = {{method.downcase}}
15-
class_name_method = "#{class_name}/#{method_downcase}"
14+
class_name_method = "#{class_name}/#{{{method}}}"
1615
({{paths}}).each do |path|
17-
@@only_routes_tree.add class_name_method + path, '/' + method_downcase + path
16+
@@only_routes_tree.add class_name_method + path, '/' + {{method}} + path
1817
end
1918
end
2019

2120
macro exclude(paths, method = "GET")
2221
class_name = {{@type.name}}
23-
method_downcase = {{method.downcase}}
24-
class_name_method = "#{class_name}/#{method_downcase}"
22+
class_name_method = "#{class_name}/#{{{method}}}"
2523
({{paths}}).each do |path|
26-
@@exclude_routes_tree.add class_name_method + path, '/' + method_downcase + path
24+
@@exclude_routes_tree.add class_name_method + path, '/' + {{method}} + path
2725
end
2826
end
2927

@@ -74,7 +72,7 @@ module Kemal
7472
end
7573

7674
private def radix_path(method : String, path : String)
77-
"#{self.class}/#{method.downcase}#{path}"
75+
"#{self.class}/#{method}#{path}"
7876
end
7977
end
8078
end

src/kemal/route_handler.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Kemal
5757
end
5858

5959
private def radix_path(method, path)
60-
'/' + method.downcase + path
60+
'/' + method + path
6161
end
6262

6363
private def add_to_radix_tree(method, path, route)

0 commit comments

Comments
 (0)