Add context to WebSocket. This allows one to use context.params like url,query, body e.g

This commit is contained in:
Sdogruyol 2017-08-20 20:01:49 +03:00
parent 5a83522866
commit 2b604dfb30
9 changed files with 71 additions and 41 deletions

View file

@ -10,7 +10,7 @@ FILTER_METHODS = %w(get post put patch delete options all)
{% for method in HTTP_METHODS %}
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
raise Kemal::Exceptions::InvalidPathStartException.new({{method}}, path) unless Kemal::Utils.path_starts_with_slash?(path)
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
Kemal::RouteHandler::INSTANCE.add_http_route({{method}}.upcase, path, &block)
end
{% end %}

View file

@ -13,7 +13,12 @@ class HTTP::Server
end
def params
@request.url_params ||= route_lookup.params
websocket? = @request.headers.has_key?("Upgrade")
@request.url_params ||= unless websocket?
route_lookup.params
else
ws_route_lookup.params
end
@params ||= if @request.param_parser
@request.param_parser.not_nil!
else
@ -34,6 +39,14 @@ class HTTP::Server
route_lookup.found?
end
def ws_route_lookup
Kemal::RouteHandler::INSTANCE.lookup_ws_route(@request.path)
end
def ws_route_defined?
ws_route_lookup.found?
end
def get(name)
@store[name]
end

View file

@ -7,10 +7,12 @@ module Kemal
include HTTP::Handler
INSTANCE = new
property tree
property http_routes
property ws_routes
def initialize
@tree = Radix::Tree(Route).new
@http_routes = Radix::Tree(Route).new
@ws_routes = Radix::Tree(String).new
end
def call(context)
@ -19,14 +21,22 @@ module Kemal
# Adds a given route to routing tree. As an exception each `GET` route additionaly defines
# a corresponding `HEAD` route.
def add_route(method, path, &handler : HTTP::Server::Context -> _)
add_to_radix_tree method, path, Route.new(method, path, &handler)
add_to_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
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
end
# Check if a route is defined and returns the lookup
def lookup_route(verb, path)
@tree.find radix_path(verb, path)
@http_routes.find radix_path(verb, path)
end
def lookup_ws_route(path)
@ws_routes.find "/ws#{path}"
end
# Processes the route if it's a match. Otherwise renders 404.
@ -49,13 +59,18 @@ module Kemal
end
end
private def radix_path(method : String, path)
private def radix_path(method, path)
"/#{method.downcase}#{path}"
end
private def add_to_radix_tree(method, path, route)
private def add_to_http_radix_tree(method, path, route)
node = radix_path method, path
@tree.add node, route
@http_routes.add node, route
end
private def add_to_ws_radix_tree(path)
node = radix_path "ws", path
@ws_routes.add node, node
end
end
end

View file

@ -4,10 +4,11 @@ module Kemal
class WebSocketHandler < HTTP::WebSocketHandler
def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void)
Kemal.config.add_handler self
Kemal::RouteHandler::INSTANCE.add_ws_route @path
end
def call(context)
return call_next(context) unless context.request.path.not_nil! == @path
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.ws_route_defined?
super
end
end