Add directory traversal

This commit is contained in:
maddiebaka 2023-08-07 14:04:55 -04:00
parent a68a88bd1a
commit a9f340153c

View File

@ -8,6 +8,19 @@ require_relative "skeksis/htmlize"
module Skeksis
#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
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)
"<a href=\"#{uri.to_s}\">#{i}</a><br/>"
end.join("\n")
[Header + listing + Footer]
end
end
end