From a9f340153c41635e12afd846cfe49a82ec3920d6 Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Mon, 7 Aug 2023 14:04:55 -0400 Subject: [PATCH] Add directory traversal --- lib/skeksis.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/skeksis.rb b/lib/skeksis.rb index c92a15b..38e37e4 100644 --- a/lib/skeksis.rb +++ b/lib/skeksis.rb @@ -8,6 +8,19 @@ require_relative "skeksis/htmlize" module Skeksis #class Error < StandardError; end + Header = <<~HTML + + + Gembridge + + +

Directory Listing

+ HTML + + Footer = <<~HTML + + + HTML class GemBridge def initialize @@ -16,7 +29,8 @@ module Skeksis def query(path, env) 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) file = File.open(path, 'r') data = file.readlines @@ -29,5 +43,18 @@ module Skeksis def htmlize(data, env) Skeksis::Parser.parse(data, strip_blanks=true).htmlize(env['REQUEST_URI'], @gemini_uri) 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) + "#{i}
" + end.join("\n") + [Header + listing + Footer] + end end end