made the matching system work like sinatra's

This commit is contained in:
Yessiest 2023-04-30 03:57:33 +04:00
parent 77a5e9a6b5
commit c6e89afcfd
2 changed files with 55 additions and 23 deletions

56
hyde.rb
View File

@ -49,7 +49,7 @@ module Hyde
@path = _normalize(path) if path.kind_of? String @path = _normalize(path) if path.kind_of? String
@path = path if path.kind_of? Regexp @path = path if path.kind_of? Regexp
end end
def _match?(path) def _match?(path, ctx)
# behvaiour used by "index" method # behvaiour used by "index" method
return true if @path == "" return true if @path == ""
split_path = path.split("/").filter { |x| x != "" } split_path = path.split("/").filter { |x| x != "" }
@ -166,7 +166,7 @@ module Hyde
@block = block_optional @block = block_optional
end end
def _match(request) def _match(request)
if @block and (_match? request.path) then if @block and (_match? request.path, request) then
@current_context = Hyde::ProtectedContext.new(request) @current_context = Hyde::ProtectedContext.new(request)
return_later = self.instance_exec @current_context, &@block return_later = self.instance_exec @current_context, &@block
return return_later return return_later
@ -177,7 +177,7 @@ module Hyde
class Serve < Hyde::Probe class Serve < Hyde::Probe
def _match(request) def _match(request)
return super if @block return super if @block
if _match? request.path then if _match? request.path, request then
match_path = _normalize_input(request.path).match(@path)[0] match_path = _normalize_input(request.path).match(@path)[0]
filepath = request.filepath+match_path filepath = request.filepath+match_path
begin begin
@ -193,25 +193,53 @@ module Hyde
end end
end end
["get","post","put","patch","delete","options","link","unlink"].each { |m| class GetMatch < Hyde::Probe
new_class = Class.new(Hyde::Probe) do @match_method = "get"
def initialize(*a, **b, &block) def initialize(*a, **b, &block)
@match_method = (self.class.instance_variable_get :@match_method)
raise Exception, "block required!" if not block raise Exception, "block required!" if not block
super(*a, **b, &block) super(*a, **b, &block)
end end
def _match(ctx) def _match?(path, ctx)
if ctx.request.request_method == m.upcase then if ctx.request.request_method == @match_method.upcase then
super(ctx) return super(path, ctx)
else
return false
end end
end end
end end
const_set(m.capitalize+"Match", new_class) class PostMatch < GetMatch
} @match_method = "post"
end
class PutMatch < GetMatch
@match_method = "put"
end
class PatchMatch < GetMatch
@match_method = "patch"
end
class DeleteMatch < GetMatch
@match_method = "delete"
end
class OptionsMatch < GetMatch
@match_method = "options"
end
class LinkMatch < GetMatch
@match_method = "link"
end
class UnlinkMatch < GetMatch
@match_method = "unlink"
end
class PrintProbe < Hyde::Probe class PrintProbe < Hyde::Probe
def _match(request) def _match(request)
if _match? request.path then if _match? request.path, request then
puts "#{request.path} matched!" puts "#{request.path} matched!"
end end
end end
@ -282,7 +310,7 @@ module Hyde
end end
def _match(request) def _match(request)
self.instance_exec request, &@preprocessor if @preprocessor self.instance_exec request, &@preprocessor if @preprocessor
if _match? request.path then if _match? request.path, request then
match_path = _normalize_input(request.path).match(@path) match_path = _normalize_input(request.path).match(@path)
next_path = match_path[0] next_path = match_path[0]
request.path = cut_path = match_path.post_match request.path = cut_path = match_path.post_match
@ -301,7 +329,7 @@ module Hyde
if cut_path.match /^\/?$/ then if cut_path.match /^\/?$/ then
request.indexlist.each do |x| request.indexlist.each do |x|
puts "Trying index #{x}" puts "Trying index #{x}"
try_index = @chain.find { |y| y._match? x } try_index = @chain.find { |y| y._match? x, request }
if try_index then if try_index then
request.path = x request.path = x
return try_index._match request return try_index._match request
@ -309,7 +337,7 @@ module Hyde
end end
end end
# passthrough to the next path object # passthrough to the next path object
next_pathspec = @chain.find { |x| x._match? cut_path } next_pathspec = @chain.find { |x| x._match? cut_path, request }
next_pathspec._match request if next_pathspec next_pathspec._match request if next_pathspec
unless next_pathspec then unless next_pathspec then
# throw 404 if nowhere to go # throw 404 if nowhere to go

View File

@ -8,23 +8,27 @@ server = Hyde::Server.new Port: 8000 do
preprocess do |ctx| preprocess do |ctx|
puts "#{ctx} entered fully virtual directory!" puts "#{ctx} entered fully virtual directory!"
end end
serve "portal" do |ctx| get "portal" do |ctx|
ctx.vars[:ass] = true ctx.vars[:ass] = true
rewrite "/about/hyde" rewrite "/about/hyde"
end end
serve "webrick" do |ctx| get "webrick" do |ctx|
ctx.response.body = "WEBrick is a modular http server stack" ctx.response.body = "WEBrick is a modular http server stack"
ctx.response['Content-Type'] = "text/plain" ctx.response['Content-Type'] = "text/plain"
end end
serve "en_passant" do |ctx| get "en_passant" do |ctx|
puts "holy hell!" puts "holy hell!"
redirect "https://google.com/search?q=en+passant" redirect "https://google.com/search?q=en+passant"
end end
serve "hyde" do |ctx| get "hyde" do |ctx|
puts ctx.vars[:ass] puts ctx.vars[:ass]
ctx.response.body = "Hyde is the disgusting side of Jekyll, and, by extension, the thing that makes WEBrick usable." ctx.response.body = "Hyde is the disgusting side of Jekyll, and, by extension, the thing that makes WEBrick usable."
ctx.response['Content-Type'] = "text/plain" ctx.response['Content-Type'] = "text/plain"
end end
post "hyde" do |ctx|
ctx.response.body = "Your message: #{ctx.request.body}"
ctx.response['Content-Type'] = "text/plain"
end
end end
path "uploads" do path "uploads" do
index ["index.html"] index ["index.html"]