Skip to content

Commit

Permalink
(responders + routes) optimise check for head requests
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Jan 17, 2019
1 parent 008a767 commit 003d157
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/action-controller/base.cr
Expand Up @@ -105,10 +105,11 @@ abstract class ActionController::Base
"destroy" => {"delete", "/:id"},
}

def initialize(@context : HTTP::Server::Context, @action_name = :index)
def initialize(@context : HTTP::Server::Context, @action_name = :index, @__head_request__ = false)
@render_called = false
end

getter context
getter action_name : Symbol
getter render_called : Bool
getter __session__ : Session?
Expand Down Expand Up @@ -277,7 +278,7 @@ abstract class ActionController::Base

def self.__init_routes__(router)
{% for name, details in ROUTES %}
router.{{details[0].id}} "{{NAMESPACE[0].id}}{{details[1].id}}".gsub("//", "/") do |context|
router.{{details[0].id}} "{{NAMESPACE[0].id}}{{details[1].id}}".gsub("//", "/") do |context, head_request|
{% is_websocket = details[3] %}

# Check if force SSL is set and redirect to HTTPS if HTTP
Expand Down Expand Up @@ -308,7 +309,7 @@ abstract class ActionController::Base
{% end %}

# Create an instance of the controller
instance = {{@type.name}}.new(context, :{{name}})
instance = {{@type.name}}.new(context, :{{name}}, head_request)

# Check for errors
{% if !RESCUE.empty? %}
Expand Down
2 changes: 1 addition & 1 deletion src/action-controller/responders.cr
Expand Up @@ -133,7 +133,7 @@ module ActionController::Responders
{% end %}

{% if json || xml || html || yaml || text || binary %}
%response << %output unless self.request.method == "HEAD"
%response << %output unless @__head_request__
{% end %}

@render_called = true
Expand Down
6 changes: 3 additions & 3 deletions src/action-controller/router.cr
Expand Up @@ -2,17 +2,17 @@ require "./router/server_context"
require "./router/route_handler"

module ActionController::Router
alias Action = HTTP::Server::Context -> HTTP::Server::Context
alias Action = (HTTP::Server::Context, Bool) -> HTTP::Server::Context
HTTP_METHODS = %w(get post put patch delete options head)

getter route_handler : RouteHandler = ::ActionController::Router::RouteHandler.new

# Define each method for supported http methods
{% for http_method in HTTP_METHODS %}
def {{http_method.id}}(path : String, &block : Action)
@route_handler.add_route("/{{http_method.id.upcase}}" + path, block)
@route_handler.add_route("/{{http_method.id.upcase}}" + path, {block, false})
{% if http_method == "get" %}
@route_handler.add_route("/HEAD" + path, block)
@route_handler.add_route("/HEAD" + path, {block, true})
{% end %}
end
{% end %}
Expand Down
10 changes: 5 additions & 5 deletions src/action-controller/router/route_handler.cr
Expand Up @@ -4,11 +4,11 @@ class ActionController::Router::RouteHandler
include HTTP::Handler

def initialize
@tree = Radix::Tree(Action).new
@static_routes = {} of String => Action
@tree = Radix::Tree(Tuple(Action, Bool)).new
@static_routes = {} of String => Tuple(Action, Bool)
end

def search_route(context : HTTP::Server::Context) : Action?
def search_route(context : HTTP::Server::Context) : Tuple(Action, Bool)?
search_path = "/#{context.request.method}#{context.request.path}"
action = @static_routes.fetch(search_path) do
route = @tree.find(search_path)
Expand All @@ -22,13 +22,13 @@ class ActionController::Router::RouteHandler

def call(context : HTTP::Server::Context)
if action = search_route(context)
action.call(context)
action[0].call(context, action[1])
else
call_next(context)
end
end

def add_route(key : String, action : Action)
def add_route(key : String, action : Tuple(Action, Bool))
if key.includes?(':') || key.includes?('*')
@tree.add(key, action)
else
Expand Down

0 comments on commit 003d157

Please sign in to comment.