2016-01-24 18:50:54 +00:00
|
|
|
require "radix"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
module Kemal
|
|
|
|
# Kemal::RouteHandler is the main handler which handles all the HTTP requests. Routing, parsing, rendering e.g
|
|
|
|
# are done in this handler.
|
2016-12-24 11:22:44 +00:00
|
|
|
class RouteHandler
|
|
|
|
include HTTP::Handler
|
2016-07-17 11:43:13 +00:00
|
|
|
INSTANCE = new
|
2014-07-30 22:27:25 +00:00
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
property http_routes
|
|
|
|
property ws_routes
|
2016-02-16 20:07:58 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
def initialize
|
2017-08-20 17:01:49 +00:00
|
|
|
@http_routes = Radix::Tree(Route).new
|
|
|
|
@ws_routes = Radix::Tree(String).new
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
def call(context)
|
|
|
|
process_request(context)
|
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
|
|
|
|
# a corresponding `HEAD` route.
|
2017-08-20 17:01:49 +00:00
|
|
|
def add_http_route(method, path, &handler : HTTP::Server::Context -> _)
|
|
|
|
add_to_http_radix_tree method, path, Route.new(method, path, &handler)
|
|
|
|
add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_ws_route(path)
|
|
|
|
add_to_ws_radix_tree path
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Check if a route is defined and returns the lookup
|
|
|
|
def lookup_route(verb, path)
|
2017-08-20 17:01:49 +00:00
|
|
|
@http_routes.find radix_path(verb, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def lookup_ws_route(path)
|
|
|
|
@ws_routes.find "/ws#{path}"
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2016-02-16 20:07:58 +00:00
|
|
|
|
2016-07-17 11:43:13 +00:00
|
|
|
# Processes the route if it's a match. Otherwise renders 404.
|
2017-02-13 20:39:40 +00:00
|
|
|
private def process_request(context)
|
2016-07-17 11:43:13 +00:00
|
|
|
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
|
|
|
|
route = context.route_lookup.payload.as(Route)
|
|
|
|
content = route.handler.call(context)
|
2017-02-11 13:33:42 +00:00
|
|
|
ensure
|
|
|
|
remove_tmpfiles(context)
|
2016-07-17 11:43:13 +00:00
|
|
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
|
|
|
raise Kemal::Exceptions::CustomException.new(context)
|
|
|
|
end
|
|
|
|
context.response.print(content)
|
|
|
|
context
|
2016-05-05 19:35:36 +00:00
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
|
2017-02-11 13:33:42 +00:00
|
|
|
private def remove_tmpfiles(context)
|
|
|
|
context.params.files.each do |field, file|
|
2017-02-21 19:24:11 +00:00
|
|
|
File.delete(file.tmpfile.path) if ::File.exists?(file.tmpfile.path)
|
2017-02-11 13:33:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
private def radix_path(method, path)
|
2016-07-17 11:43:13 +00:00
|
|
|
"/#{method.downcase}#{path}"
|
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
|
2017-08-20 17:01:49 +00:00
|
|
|
private def add_to_http_radix_tree(method, path, route)
|
2016-07-17 11:43:13 +00:00
|
|
|
node = radix_path method, path
|
2017-08-20 17:01:49 +00:00
|
|
|
@http_routes.add node, route
|
|
|
|
end
|
|
|
|
|
|
|
|
private def add_to_ws_radix_tree(path)
|
|
|
|
node = radix_path "ws", path
|
|
|
|
@ws_routes.add node, node
|
2016-07-17 11:43:13 +00:00
|
|
|
end
|
2016-01-22 20:14:22 +00:00
|
|
|
end
|
2014-06-11 23:41:02 +00:00
|
|
|
end
|