Added filepath handling, request abstractions, locked requests
This commit is contained in:
parent
d8cddf7479
commit
98d940073b
73
hyde.rb
73
hyde.rb
|
@ -41,26 +41,52 @@ module Hyde
|
|||
return path
|
||||
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
|
||||
class Probe
|
||||
include Hyde::PatternMatching
|
||||
def initialize (path, &block_optional)
|
||||
prep_path path
|
||||
@lock_methods = true
|
||||
self.instance_eval &block_optional if block_given?
|
||||
@lock_methods = false
|
||||
@block = block_optional
|
||||
end
|
||||
def match(path)
|
||||
if match? path then
|
||||
exec if defined? exec
|
||||
def match(request)
|
||||
if match? request.path then
|
||||
@lock_methods = true
|
||||
self.instance_exec request, &@block if @block
|
||||
@lock_methods = false
|
||||
end
|
||||
end
|
||||
end
|
||||
class PrintProbe < Hyde::Probe
|
||||
def match(path)
|
||||
if match? path then
|
||||
puts "#{path} matched!"
|
||||
def match(request)
|
||||
if match? request.path then
|
||||
puts "#{request.path} matched!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -84,25 +110,38 @@ module Hyde
|
|||
@root_override = root_path
|
||||
@remap = false
|
||||
@lock_methods = true
|
||||
self.instance_eval &block
|
||||
self.instance_exec &block
|
||||
@lock_methods = false
|
||||
end
|
||||
def path(*a, **b, &block)
|
||||
@chain.append Hyde::Pathspec.new *a, **b, &block
|
||||
end
|
||||
def root(path)
|
||||
@root_override = path
|
||||
@lock_methods = false
|
||||
@root_override = "/"+normalize_input(path)
|
||||
@lock_methods = true
|
||||
end
|
||||
def remap(path)
|
||||
@root_override = path
|
||||
@lock_methods = false
|
||||
@root_override = "/"+normalize_input(path)
|
||||
@lock_methods = true
|
||||
@remap = true
|
||||
end
|
||||
def match(path)
|
||||
def match(request)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
if match? path then
|
||||
cut_path = normalize_input(path).sub(@path, "")
|
||||
if match? request.path then
|
||||
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.match cut_path if next_pathspec
|
||||
next_pathspec.match request if next_pathspec
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
25
test_hyde.rb
25
test_hyde.rb
|
@ -1,17 +1,30 @@
|
|||
require_relative "hyde"
|
||||
path = Hyde::Pathspec.new "/" do
|
||||
root "/var/www"
|
||||
path "about" do
|
||||
printProbe "test"
|
||||
printProbe "test_*"
|
||||
end
|
||||
path "docs" do
|
||||
remap "/var/www/markdown_compiled/"
|
||||
printProbe "test"
|
||||
printProbe "test_*"
|
||||
probe "speen" do |request|
|
||||
puts "Vinny pls say funny word"
|
||||
pp request
|
||||
end
|
||||
end
|
||||
end
|
||||
path.match "/about/speen"
|
||||
path.match "/about/test"
|
||||
path.match "/about/test_2"
|
||||
path.match "/docs/speen"
|
||||
path.match "/docs/test"
|
||||
path.match "/docs/test_2"
|
||||
|
||||
[
|
||||
Hyde::Request.new("/about/speen",nil,nil),
|
||||
Hyde::Request.new("/about/test",nil,nil),
|
||||
Hyde::Request.new("/about/test_2",nil,nil),
|
||||
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) }
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue