diff --git a/hyde.rb b/hyde.rb index 1c06088..e4f92bc 100644 --- a/hyde.rb +++ b/hyde.rb @@ -49,7 +49,7 @@ module Hyde @path = _normalize(path) if path.kind_of? String @path = path if path.kind_of? Regexp end - def _match?(path) + def _match?(path, ctx) # behvaiour used by "index" method return true if @path == "" split_path = path.split("/").filter { |x| x != "" } @@ -166,7 +166,7 @@ module Hyde @block = block_optional end def _match(request) - if @block and (_match? request.path) then + if @block and (_match? request.path, request) then @current_context = Hyde::ProtectedContext.new(request) return_later = self.instance_exec @current_context, &@block return return_later @@ -177,7 +177,7 @@ module Hyde class Serve < Hyde::Probe def _match(request) return super if @block - if _match? request.path then + if _match? request.path, request then match_path = _normalize_input(request.path).match(@path)[0] filepath = request.filepath+match_path begin @@ -193,25 +193,53 @@ module Hyde end end - ["get","post","put","patch","delete","options","link","unlink"].each { |m| - new_class = Class.new(Hyde::Probe) do - def initialize(*a, **b, &block) - raise Exception, "block required!" if not block - super(*a, **b, &block) - end - def _match(ctx) - if ctx.request.request_method == m.upcase then - super(ctx) - end + class GetMatch < Hyde::Probe + @match_method = "get" + def initialize(*a, **b, &block) + @match_method = (self.class.instance_variable_get :@match_method) + raise Exception, "block required!" if not block + super(*a, **b, &block) + end + def _match?(path, ctx) + if ctx.request.request_method == @match_method.upcase then + return super(path, ctx) + else + return false 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 def _match(request) - if _match? request.path then + if _match? request.path, request then puts "#{request.path} matched!" end end @@ -282,7 +310,7 @@ module Hyde end def _match(request) 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) next_path = match_path[0] request.path = cut_path = match_path.post_match @@ -301,7 +329,7 @@ module Hyde if cut_path.match /^\/?$/ then request.indexlist.each do |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 request.path = x return try_index._match request @@ -309,7 +337,7 @@ module Hyde end end # 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 unless next_pathspec then # throw 404 if nowhere to go diff --git a/test_combined.rb b/test_combined.rb index 36632f5..79aeed0 100644 --- a/test_combined.rb +++ b/test_combined.rb @@ -8,23 +8,27 @@ server = Hyde::Server.new Port: 8000 do preprocess do |ctx| puts "#{ctx} entered fully virtual directory!" end - serve "portal" do |ctx| + get "portal" do |ctx| ctx.vars[:ass] = true rewrite "/about/hyde" end - serve "webrick" do |ctx| + get "webrick" do |ctx| ctx.response.body = "WEBrick is a modular http server stack" ctx.response['Content-Type'] = "text/plain" end - serve "en_passant" do |ctx| + get "en_passant" do |ctx| puts "holy hell!" redirect "https://google.com/search?q=en+passant" end - serve "hyde" do |ctx| + get "hyde" do |ctx| 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['Content-Type'] = "text/plain" end + post "hyde" do |ctx| + ctx.response.body = "Your message: #{ctx.request.body}" + ctx.response['Content-Type'] = "text/plain" + end end path "uploads" do index ["index.html"]