Added request preprocessor

This commit is contained in:
Yessiest 2023-04-27 21:50:24 +04:00
parent 695e32bbfa
commit 5a930f1bed
2 changed files with 19 additions and 2 deletions

View File

@ -109,6 +109,7 @@ module Hyde
@response = response @response = response
@handles = {} @handles = {}
@indexlist = [] @indexlist = []
@vars = {}
end end
attr_reader :request attr_reader :request
attr_reader :response attr_reader :response
@ -116,6 +117,7 @@ module Hyde
attr_accessor :path attr_accessor :path
attr_accessor :handles attr_accessor :handles
attr_accessor :indexlist attr_accessor :indexlist
attr_accessor :vars
end end
# Context object with safe path encapsulation # Context object with safe path encapsulation
@ -126,6 +128,8 @@ module Hyde
@request = request.request @request = request.request
@response = request.response @response = request.response
@handles = request.handles @handles = request.handles
@indexlist = request.indexlist
@vars = request.vars
end end
undef :path= undef :path=
undef :filepath= undef :filepath=
@ -237,8 +241,12 @@ module Hyde
def handle(code, &block) def handle(code, &block)
@handles[code] = block @handles[code] = block
end end
def preprocess(&block)
@preprocessor = block
end
def match(request) def match(request)
raise Exception, "Not permitted" if @lock_methods raise Exception, "Not permitted" if @lock_methods
self.instance_exec request, &@preprocessor if @preprocessor
if match? request.path then if match? request.path 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]

View File

@ -15,9 +15,18 @@ server = Hyde::Server.new Port: 8000 do
end end
end end
path "uploads" do path "uploads" do
preprocess { |request| puts request.inspect }
index ["index.html"] index ["index.html"]
serve "**/*.md", match_full_path: true serve ["**/*.md","*.html","**/*.html"], match_full_path: true
serve ["*.html","**/*.html"], match_full_path: true end
path ["vars_1","vars_2"] do
preprocess do |request|
request.vars["flag"] = "yes" if request.filepath.match "vars_1"
end
serve "view" do |ctx|
ctx.response.body = "Flag: #{ctx.vars["flag"]}"
ctx.response['Content-Type'] = "text/plain"
end
end end
end end