Seperate websocket and websocket handler. Fixes #395

This commit is contained in:
Serdar Dogruyol 2017-09-10 14:41:07 +03:00
parent 3050b75f0a
commit fe9d193418
10 changed files with 76 additions and 39 deletions

View file

@ -27,7 +27,7 @@ describe "Config" do
config = Kemal.config config = Kemal.config
config.add_handler CustomTestHandler.new config.add_handler CustomTestHandler.new
Kemal.config.setup Kemal.config.setup
config.handlers.size.should eq(6) config.handlers.size.should eq(7)
end end
it "toggles the shutdown message" do it "toggles the shutdown message" do

View file

@ -12,7 +12,7 @@ describe "Macros" do
it "adds a custom handler" do it "adds a custom handler" do
add_handler CustomTestHandler.new add_handler CustomTestHandler.new
Kemal.config.setup Kemal.config.setup
Kemal.config.handlers.size.should eq 6 Kemal.config.handlers.size.should eq 7
end end
end end

View file

@ -48,8 +48,7 @@ def create_ws_request_and_return_io(handler, request)
rescue IO::Error rescue IO::Error
# Raises because the IO::Memory is empty # Raises because the IO::Memory is empty
end end
response.close io.rewind
io
end end
def call_request_on_app(request) def call_request_on_app(request)
@ -83,5 +82,5 @@ end
Spec.after_each do Spec.after_each do
Kemal.config.clear Kemal.config.clear
Kemal::RouteHandler::INSTANCE.http_routes = Radix::Tree(Route).new Kemal::RouteHandler::INSTANCE.http_routes = Radix::Tree(Route).new
Kemal::RouteHandler::INSTANCE.ws_routes = Radix::Tree(String).new Kemal::WebSocketHandler::INSTANCE.ws_routes = Radix::Tree(WebSocket).new
end end

View file

@ -2,32 +2,47 @@ require "./spec_helper"
describe "Kemal::WebSocketHandler" do describe "Kemal::WebSocketHandler" do
it "doesn't match on wrong route" do it "doesn't match on wrong route" do
handler = Kemal::WebSocketHandler.new "/" { } handler = Kemal::WebSocketHandler::INSTANCE
handler.next = Kemal::CommonExceptionHandler::INSTANCE
ws "/" { }
headers = HTTP::Headers{ headers = HTTP::Headers{
"Upgrade" => "websocket", "Upgrade" => "websocket",
"Connection" => "Upgrade", "Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
} }
request = HTTP::Request.new("GET", "/asd", headers) request = HTTP::Request.new("GET", "/asd", headers)
io_with_context = create_request_and_return_io(handler, request) io = IO::Memory.new
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
begin
handler.call context
rescue IO::Error
# Raises because the IO::Memory is empty
end
response.close
io.rewind
client_response = HTTP::Client::Response.from_io(io, decompress: false)
client_response.status_code.should eq(404) client_response.status_code.should eq(404)
end end
it "matches on given route" do it "matches on given route" do
handler = Kemal::WebSocketHandler.new "/" { } handler = Kemal::WebSocketHandler::INSTANCE
ws "/" { |socket, context| socket.send("Match") }
ws "/no_match" { |socket, context| socket.send "No Match" }
headers = HTTP::Headers{ headers = HTTP::Headers{
"Upgrade" => "websocket", "Upgrade" => "websocket",
"Connection" => "Upgrade", "Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
} }
request = HTTP::Request.new("GET", "/", headers) request = HTTP::Request.new("GET", "/", headers)
io_with_context = create_ws_request_and_return_io(handler, request) io_with_context = create_ws_request_and_return_io(handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n") io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
end end
it "fetches named url parameters" do it "fetches named url parameters" do
handler = Kemal::WebSocketHandler.new "/:id" { |s, c| c.params.url["id"] } handler = Kemal::WebSocketHandler::INSTANCE
ws "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{ headers = HTTP::Headers{
"Upgrade" => "websocket", "Upgrade" => "websocket",
"Connection" => "Upgrade", "Connection" => "Upgrade",

View file

@ -109,9 +109,9 @@ module Kemal
setup_static_file_handler setup_static_file_handler
setup_custom_handlers setup_custom_handlers
setup_filter_handlers setup_filter_handlers
setup_websocket_handlers
@default_handlers_setup = true @default_handlers_setup = true
@router_included = true @router_included = true
HANDLERS.insert(HANDLERS.size, Kemal::WebSocketHandler::INSTANCE)
HANDLERS.insert(HANDLERS.size, Kemal::RouteHandler::INSTANCE) HANDLERS.insert(HANDLERS.size, Kemal::RouteHandler::INSTANCE)
end end
end end

View file

@ -16,7 +16,7 @@ FILTER_METHODS = %w(get post put patch delete options all)
def ws(path : String, &block : HTTP::WebSocket, HTTP::Server::Context -> Void) def ws(path : String, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
raise Kemal::Exceptions::InvalidPathStartException.new("ws", path) unless Kemal::Utils.path_starts_with_slash?(path) raise Kemal::Exceptions::InvalidPathStartException.new("ws", path) unless Kemal::Utils.path_starts_with_slash?(path)
Kemal::WebSocketHandler.new path, &block Kemal::WebSocketHandler::INSTANCE.add_ws_route path, &block
end end
def error(status_code : Int32, &block : HTTP::Server::Context, Exception -> _) def error(status_code : Int32, &block : HTTP::Server::Context, Exception -> _)

View file

@ -13,13 +13,7 @@ class HTTP::Server
end end
def params def params
connection_type = @request.headers.fetch("Connection", nil) @request.url_params ||= route_lookup.params
@request.url_params ||= unless connection_type == "Upgrade"
route_lookup.params
else
ws_route_lookup.params
end
@params ||= if @request.param_parser @params ||= if @request.param_parser
@request.param_parser.not_nil! @request.param_parser.not_nil!
else else
@ -36,6 +30,10 @@ class HTTP::Server
route_lookup.payload route_lookup.payload
end end
def websocket
ws_route_lookup.payload
end
def route_lookup def route_lookup
Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method.as(String), @request.path) Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method.as(String), @request.path)
end end
@ -45,7 +43,7 @@ class HTTP::Server
end end
def ws_route_lookup def ws_route_lookup
Kemal::RouteHandler::INSTANCE.lookup_ws_route(@request.path) Kemal::WebSocketHandler::INSTANCE.lookup_ws_route(@request.path)
end end
def ws_route_defined? def ws_route_defined?

View file

@ -26,19 +26,11 @@ module Kemal
add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET" add_to_http_radix_tree("HEAD", path, Route.new("HEAD", path) { |ctx| "" }) if method == "GET"
end end
def add_ws_route(path : String)
add_to_ws_radix_tree path
end
# Check if a route is defined and returns the lookup # Check if a route is defined and returns the lookup
def lookup_route(verb : String, path : String) def lookup_route(verb : String, path : String)
@http_routes.find radix_path(verb, path) @http_routes.find radix_path(verb, path)
end end
def lookup_ws_route(path : String)
@ws_routes.find "/ws#{path}"
end
# Processes the route if it's a match. Otherwise renders 404. # Processes the route if it's a match. Otherwise renders 404.
private def process_request(context) private def process_request(context)
raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined? raise Kemal::Exceptions::RouteNotFound.new(context) unless context.route_defined?
@ -59,10 +51,5 @@ module Kemal
node = radix_path method, path node = radix_path method, path
@http_routes.add node, route @http_routes.add node, route
end end
private def add_to_ws_radix_tree(path)
node = radix_path "ws", path
@ws_routes.add node, node
end
end end
end end

15
src/kemal/websocket.cr Normal file
View file

@ -0,0 +1,15 @@
module Kemal
# Route is the main building block of Kemal.
# It takes 3 parameters: Method, path and a block to specify
# what action to be done if the route is matched.
class WebSocket < HTTP::WebSocketHandler
getter proc
def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void)
end
def call(context : HTTP::Server::Context)
super
end
end
end

View file

@ -1,15 +1,38 @@
module Kemal module Kemal
# Kemal::WebSocketHandler is used for building a WebSocket route. # Kemal::WebSocketHandler is used for building a WebSocket route.
# For each WebSocket route a new handler is created and registered to global handlers. # For each WebSocket route a new handler is created and registered to global handlers.
class WebSocketHandler < HTTP::WebSocketHandler class WebSocketHandler
def initialize(@path : String, &@proc : HTTP::WebSocket, HTTP::Server::Context -> Void) include HTTP::Handler
Kemal.config.add_handler self INSTANCE = new
Kemal::RouteHandler::INSTANCE.add_ws_route @path property ws_routes
def initialize
@ws_routes = Radix::Tree(WebSocket).new
end end
def call(context : HTTP::Server::Context) def call(context : HTTP::Server::Context)
return call_next(context) unless context.ws_route_defined? return call_next(context) unless context.ws_route_defined?
super context.request.url_params ||= context.ws_route_lookup.params
content = context.websocket.call(context)
context.response.print(content)
context
end
def lookup_ws_route(path : String)
@ws_routes.find "/ws#{path}"
end
def add_ws_route(path : String, &handler : HTTP::WebSocket, HTTP::Server::Context -> Void)
add_to_ws_radix_tree path, WebSocket.new(path, &handler)
end
private def add_to_ws_radix_tree(path, websocket)
node = radix_path "ws", path
@ws_routes.add node, websocket
end
private def radix_path(method, path)
"/#{method.downcase}#{path}"
end end
end end
end end