Added request preprocessor
This commit is contained in:
		
							parent
							
								
									695e32bbfa
								
							
						
					
					
						commit
						5a930f1bed
					
				
							
								
								
									
										8
									
								
								hyde.rb
								
								
								
								
							
							
						
						
									
										8
									
								
								hyde.rb
								
								
								
								
							| 
						 | 
				
			
			@ -109,6 +109,7 @@ module Hyde
 | 
			
		|||
      @response = response
 | 
			
		||||
      @handles = {}
 | 
			
		||||
      @indexlist = []
 | 
			
		||||
      @vars = {}
 | 
			
		||||
    end
 | 
			
		||||
    attr_reader :request
 | 
			
		||||
    attr_reader :response
 | 
			
		||||
| 
						 | 
				
			
			@ -116,6 +117,7 @@ module Hyde
 | 
			
		|||
    attr_accessor :path
 | 
			
		||||
    attr_accessor :handles
 | 
			
		||||
    attr_accessor :indexlist
 | 
			
		||||
    attr_accessor :vars
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Context object with safe path encapsulation
 | 
			
		||||
| 
						 | 
				
			
			@ -126,6 +128,8 @@ module Hyde
 | 
			
		|||
      @request = request.request
 | 
			
		||||
      @response = request.response
 | 
			
		||||
      @handles = request.handles
 | 
			
		||||
      @indexlist = request.indexlist
 | 
			
		||||
      @vars = request.vars
 | 
			
		||||
    end
 | 
			
		||||
    undef :path=
 | 
			
		||||
    undef :filepath=
 | 
			
		||||
| 
						 | 
				
			
			@ -237,8 +241,12 @@ module Hyde
 | 
			
		|||
    def handle(code, &block)
 | 
			
		||||
      @handles[code] = block
 | 
			
		||||
    end
 | 
			
		||||
    def preprocess(&block) 
 | 
			
		||||
      @preprocessor = block
 | 
			
		||||
    end
 | 
			
		||||
    def match(request) 
 | 
			
		||||
      raise Exception, "Not permitted" if @lock_methods
 | 
			
		||||
      self.instance_exec request, &@preprocessor if @preprocessor
 | 
			
		||||
      if match? request.path then
 | 
			
		||||
        match_path = normalize_input(request.path).match(@path)
 | 
			
		||||
        next_path = match_path[0]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,9 +15,18 @@ server = Hyde::Server.new Port: 8000 do
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
  path "uploads" do
 | 
			
		||||
    preprocess { |request| puts request.inspect }
 | 
			
		||||
    index ["index.html"]
 | 
			
		||||
    serve "**/*.md", match_full_path: true
 | 
			
		||||
    serve ["*.html","**/*.html"], match_full_path: true
 | 
			
		||||
    serve ["**/*.md","*.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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue