Full URL navigation support

This commit is contained in:
maddiebaka 2023-08-07 21:02:09 -04:00
parent a9f340153c
commit 22cf104f6e
6 changed files with 31 additions and 13 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@
# rspec failure tracking
.rspec_status
pub_gemini
config.yml

3
config.example.yml Normal file
View File

@ -0,0 +1,3 @@
skeksis_config:
gemini_uri: "gemini://gemini.example.com/"
serve_dir: "/var/gemini"

View File

@ -1,7 +1,5 @@
require 'skeksis'
SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini"
class SkeksisApp
def initialize
@skeksis = Skeksis::GemBridge.new
@ -10,7 +8,7 @@ class SkeksisApp
def call(env)
status = 200
headers = { "content-type" => "text/html" }
body = @skeksis.query(SERVE_DIR + env['PATH_INFO'], env)
body = @skeksis.query(env['PATH_INFO'], env)
[status, headers, body]
end

View File

@ -1,2 +0,0 @@
skeksis_config:
gemini_uri: "gemini://gemini.hackers.town/"

View File

@ -24,15 +24,25 @@ module Skeksis
class GemBridge
def initialize
@gemini_uri = YAML.load(File.read("config.yml"))['skeksis_config']['gemini_uri']
config = YAML.load(File.read("config.yml"))
@gemini_uri = config['skeksis_config']['gemini_uri']
@serve_dir = config['skeksis_config']['serve_dir']
end
def query(path, env)
if Dir.exist?(path)
#return Dir.each_child(path).map {|i| "#{i}\n"}
return create_dir_listing(path, env)
elsif File.exist?(path)
file = File.open(path, 'r')
# Chomps the first / and builds path
full_path = Pathname.new(@serve_dir) + path[1..-1]
puts full_path.to_s
if Dir.exist?(full_path)
index_file = full_path + "index.gmi"
if File.exist?(index_file)
data = File.open(index_file, 'r').readlines
[htmlize(data, env)]
else
create_dir_listing(full_path, env)
end
elsif File.exist?(full_path)
file = File.open(full_path, 'r')
data = file.readlines
[htmlize(data, env)]
else # path is invalid

View File

@ -60,13 +60,21 @@ module Skeksis
html
end
# TODO: Relative and absolute links need to be properly handled, both proxied and non
def build_uri(link)
uri = URI.parse(link)
if uri.relative? then puts "relative" else puts "absolute" end
if uri.relative?
path = Pathname.new(@http.path).join(uri.path)
return URI::HTTP.build(host: @http.host, path: path.to_s, port: @http.port)
end
if uri.host == @proxied_uri.host
URI::HTTP.build(host: @http.host, path: uri.path, port: @http.port)
return URI::HTTP.build(host: @http.host, path: uri.path, port: @http.port)
else
link
return link
end
end
end