Added filepath handling, request abstractions, locked requests

This commit is contained in:
Yessiest 2023-04-15 01:47:29 +04:00
parent d8cddf7479
commit 98d940073b
2 changed files with 75 additions and 23 deletions

71
hyde.rb
View File

@ -42,25 +42,51 @@ module Hyde
end end
end end
# Request control class
class Request
def initialize(path,request,response)
@path = path
@filepath = ""
@request = request
@response = response
end
attr_reader :request
attr_reader :response
attr_accessor :filepath
attr_accessor :path
end
# Request object with safe path encapsulation
class ProtectedRequest < Request
def self.from_request(request)
@path = request.path
@filepath = request.filepath
@request = request.request
@response = request.response
end
undef :path=
undef :filepath=
end
# Handler classes # Handler classes
class Probe class Probe
include Hyde::PatternMatching include Hyde::PatternMatching
def initialize (path, &block_optional) def initialize (path, &block_optional)
prep_path path prep_path path
@lock_methods = true @block = block_optional
self.instance_eval &block_optional if block_given?
@lock_methods = false
end end
def match(path) def match(request)
if match? path then if match? request.path then
exec if defined? exec @lock_methods = true
self.instance_exec request, &@block if @block
@lock_methods = false
end end
end end
end end
class PrintProbe < Hyde::Probe class PrintProbe < Hyde::Probe
def match(path) def match(request)
if match? path then if match? request.path then
puts "#{path} matched!" puts "#{request.path} matched!"
end end
end end
end end
@ -84,25 +110,38 @@ module Hyde
@root_override = root_path @root_override = root_path
@remap = false @remap = false
@lock_methods = true @lock_methods = true
self.instance_eval &block self.instance_exec &block
@lock_methods = false @lock_methods = false
end end
def path(*a, **b, &block) def path(*a, **b, &block)
@chain.append Hyde::Pathspec.new *a, **b, &block @chain.append Hyde::Pathspec.new *a, **b, &block
end end
def root(path) def root(path)
@root_override = path @lock_methods = false
@root_override = "/"+normalize_input(path)
@lock_methods = true
end end
def remap(path) def remap(path)
@root_override = path @lock_methods = false
@root_override = "/"+normalize_input(path)
@lock_methods = true
@remap = true @remap = true
end end
def match(path) def match(request)
raise Exception, "Not permitted" if @lock_methods raise Exception, "Not permitted" if @lock_methods
if match? path then if match? request.path then
cut_path = normalize_input(path).sub(@path, "") cut_path = normalize_input(request.path).sub(@path, "")
next_path = normalize_input(request.path).match(@path)[0]
request.path = cut_path
if @root_override then
request.filepath = if @remap then
@root_override+"/"
else @root_override+next_path+"/" end
else
request.filepath = request.filepath+next_path+"/"
end
next_pathspec = @chain.find { |x| x.match? cut_path } next_pathspec = @chain.find { |x| x.match? cut_path }
next_pathspec.match cut_path if next_pathspec next_pathspec.match request if next_pathspec
end end
end end
end end

View File

@ -1,17 +1,30 @@
require_relative "hyde" require_relative "hyde"
path = Hyde::Pathspec.new "/" do path = Hyde::Pathspec.new "/" do
root "/var/www"
path "about" do path "about" do
printProbe "test" printProbe "test"
printProbe "test_*" printProbe "test_*"
end end
path "docs" do path "docs" do
remap "/var/www/markdown_compiled/"
printProbe "test" printProbe "test"
printProbe "test_*" printProbe "test_*"
probe "speen" do |request|
puts "Vinny pls say funny word"
pp request
end
end end
end end
path.match "/about/speen"
path.match "/about/test" [
path.match "/about/test_2" Hyde::Request.new("/about/speen",nil,nil),
path.match "/docs/speen" Hyde::Request.new("/about/test",nil,nil),
path.match "/docs/test" Hyde::Request.new("/about/test_2",nil,nil),
path.match "/docs/test_2" Hyde::Request.new("/docs/speen",nil,nil),
Hyde::Request.new("/docs/test",nil,nil),
Hyde::Request.new("/docs/test_3",nil,nil),
].each { |x| path.match(x) }