Add directory traversal
This commit is contained in:
		@@ -8,6 +8,19 @@ require_relative "skeksis/htmlize"
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
module Skeksis
 | 
					module Skeksis
 | 
				
			||||||
  #class Error < StandardError; end
 | 
					  #class Error < StandardError; end
 | 
				
			||||||
 | 
					  Header = <<~HTML
 | 
				
			||||||
 | 
					  <html>
 | 
				
			||||||
 | 
					  <head>
 | 
				
			||||||
 | 
					  <title>Gembridge</title>
 | 
				
			||||||
 | 
					  </head>
 | 
				
			||||||
 | 
					  <body>
 | 
				
			||||||
 | 
					  <h1>Directory Listing</h1>
 | 
				
			||||||
 | 
					  HTML
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Footer = <<~HTML
 | 
				
			||||||
 | 
					  </body>
 | 
				
			||||||
 | 
					  </html>
 | 
				
			||||||
 | 
					  HTML
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  class GemBridge
 | 
					  class GemBridge
 | 
				
			||||||
    def initialize
 | 
					    def initialize
 | 
				
			||||||
@@ -16,7 +29,8 @@ module Skeksis
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def query(path, env)
 | 
					    def query(path, env)
 | 
				
			||||||
      if Dir.exist?(path)
 | 
					      if Dir.exist?(path)
 | 
				
			||||||
        return Dir.each_child(path).map {|i| "#{i}\n"}
 | 
					        #return Dir.each_child(path).map {|i| "#{i}\n"}
 | 
				
			||||||
 | 
					        return create_dir_listing(path, env)
 | 
				
			||||||
      elsif File.exist?(path)
 | 
					      elsif File.exist?(path)
 | 
				
			||||||
        file = File.open(path, 'r')
 | 
					        file = File.open(path, 'r')
 | 
				
			||||||
        data = file.readlines
 | 
					        data = file.readlines
 | 
				
			||||||
@@ -29,5 +43,18 @@ module Skeksis
 | 
				
			|||||||
    def htmlize(data, env)
 | 
					    def htmlize(data, env)
 | 
				
			||||||
      Skeksis::Parser.parse(data, strip_blanks=true).htmlize(env['REQUEST_URI'], @gemini_uri)
 | 
					      Skeksis::Parser.parse(data, strip_blanks=true).htmlize(env['REQUEST_URI'], @gemini_uri)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def create_dir_listing(path, env)
 | 
				
			||||||
 | 
					      http = URI.parse(env['REQUEST_URI'])
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      listing = Dir.each_child(path).map do |i|
 | 
				
			||||||
 | 
					        path = Pathname.new(env['PATH_INFO']).join(i)
 | 
				
			||||||
 | 
					        uri = URI::HTTP.build(host: http.host, port: http.port, path: path.to_s)
 | 
				
			||||||
 | 
					        "<a href=\"#{uri.to_s}\">#{i}</a><br/>"
 | 
				
			||||||
 | 
					      end.join("\n")
 | 
				
			||||||
 | 
					      [Header + listing + Footer]
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user