Compare commits

..

No commits in common. "c41c29a9011949f79235aa5d6af514747285f40d" and "d8cddf7479487d4e97bd76934f7af57488fda94a" have entirely different histories.

2 changed files with 23 additions and 76 deletions

74
hyde.rb
View File

@ -41,53 +41,26 @@ module Hyde
return path return path
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 initialize(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
@block = block_optional @lock_methods = true
self.instance_eval &block_optional if block_given?
@lock_methods = false
end end
def match(request) def match(path)
if match? request.path then if match? path then
p_request = Hyde::ProtectedRequest.new(request) exec if defined? exec
@lock_methods = true
self.instance_exec p_request, &@block if @block
@lock_methods = false
end end
end end
end end
class PrintProbe < Hyde::Probe class PrintProbe < Hyde::Probe
def match(request) def match(path)
if match? request.path then if match? path then
puts "#{request.path} matched!" puts "#{path} matched!"
end end
end end
end end
@ -111,38 +84,25 @@ module Hyde
@root_override = root_path @root_override = root_path
@remap = false @remap = false
@lock_methods = true @lock_methods = true
self.instance_exec &block self.instance_eval &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)
@lock_methods = false @root_override = path
@root_override = "/"+normalize_input(path)
@lock_methods = true
end end
def remap(path) def remap(path)
@lock_methods = false @root_override = path
@root_override = "/"+normalize_input(path)
@lock_methods = true
@remap = true @remap = true
end end
def match(request) def match(path)
raise Exception, "Not permitted" if @lock_methods raise Exception, "Not permitted" if @lock_methods
if match? request.path then if match? path then
cut_path = normalize_input(request.path).sub(@path, "") cut_path = normalize_input(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 request if next_pathspec next_pathspec.match cut_path if next_pathspec
end end
end end
end end

View File

@ -1,30 +1,17 @@
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"
Hyde::Request.new("/about/speen",nil,nil), path.match "/about/test_2"
Hyde::Request.new("/about/test",nil,nil), path.match "/docs/speen"
Hyde::Request.new("/about/test_2",nil,nil), path.match "/docs/test"
Hyde::Request.new("/docs/speen",nil,nil), path.match "/docs/test_2"
Hyde::Request.new("/docs/test",nil,nil),
Hyde::Request.new("/docs/test_3",nil,nil),
].each { |x| path.match(x) }