very functional prototype

This commit is contained in:
Yessiest 2023-04-17 23:56:46 +04:00
parent 8593315b95
commit 6dcf46f40a
3 changed files with 59 additions and 50 deletions

64
hyde.rb
View File

@ -2,22 +2,30 @@ require 'mime-types'
require 'webrick'
module Hyde
# 404 text
def default404(filepath)
return "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">
<HTML>
<HEAD><TITLE>File not found</TITLE></HEAD>
<BODY>
<H1>File not found</H1>
#{filepath}
<HR>
<ADDRESS>
Hyde on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})
</ADDRESS>
</BODY>
</HTML>"
class Server < WEBrick::HTTPServer
def initialize(config={},&setup)
super(config)
@hyde_pathspec = Hyde::Pathspec.new "/", &setup
self.mount_proc '/' do |req,res|
@hyde_pathspec.match(Hyde::Context.new(req.path, req, res))
end
end
end
module ErrorPages
# 404 text
def error404(request, filepath)
request.response.status = 404
if request.handles.include? 404
request.response.body = request.handles[404].call(
filepath
)
else
request.response.body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n <HEAD><TITLE>File not found</TITLE></HEAD>\n <BODY>\n <H1>File not found</H1>\n #{filepath}\n <HR>\n <ADDRESS>\n Hyde on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})\n </ADDRESS>\n </BODY>\n</HTML>"
end
request.response["Content-Type"] = 'text/html'
end
module_function :error404
end
module_function :default404
# Interchangeable pattern matching
module PatternMatching
def prep_path(path,safe_regex: true)
@ -28,12 +36,15 @@ module Hyde
end
def match?(path)
raise Exception, "Not permitted" if @lock_methods
# behvaiour used by "index" method
return true if @path == ""
split_path = path.split("/").filter { |x| x != "" }
if @path.kind_of? Regexp then
# "safe" as in "will not result in path matching anomalies"
return @path.match normalize_input(path) unless @safe_regex
return Regexp.new("^"+@path.to_s+"$").match split_path[0]
else
# algorithm to match path segments until no more left in @path
@path.split("/").filter { |x| x != "" }
.each_with_index { |x,i|
return false if x != split_path[i]
@ -43,11 +54,12 @@ module Hyde
end
def normalize_input(path)
raise Exception, "Not permitted" if @lock_methods
# remove duplicate slashes and trim edge slashes
(path.split "/").filter { |x| x != "" }.join("/")
end
def normalize(path)
raise Exception, "Not permitted" if @lock_methods
# remove multiple slashes in one spot and trim edge slashes
# remove duplicate slashes and trim edge slashes
path = normalize_input(path)
# globbing behaviour simulated with regexp
if path.match /(?<!\\)[\*\?\[]/ then
@ -133,12 +145,7 @@ module Hyde
request.response.body = data
request.response["Content-Type"] = mimetype
rescue Errno::ENOENT
if request.handles.include? 404 then
request.response.body = request.handles[404].call filepath
request.response["Content-Type"] = "text/html"
else
request.response.body = Hyde::default404 filepath
end
Hyde::ErrorPages::error404 filepath
end
end
end
@ -213,6 +220,7 @@ module Hyde
match_path = normalize_input(request.path).match(@path)
next_path = match_path[0]
request.path = cut_path = match_path.post_match
# remap/root method handling
if @root_override then
request.filepath = if @remap then
@root_override+"/"
@ -220,18 +228,14 @@ module Hyde
else
request.filepath = request.filepath+next_path+"/"
end
# error handle overlaying
@handles.each_pair { |k,v| request.handles[k] = v }
# passthrough to the next path object
next_pathspec = @chain.find { |x| x.match? cut_path }
next_pathspec.match request if next_pathspec
# throw 404 if nowhere to go
unless next_pathspec then
if request.handles.include? 404
request.response.body = request.handles[404].call(
request.filepath+"/"+cut_path
)
else
request.response.body = Hyde::default404(request.filepath+cut_path)
end
request.response["Content-Type"] = 'text/html'
Hyde::ErrorPages::error404 request, request.request.path
end
end
end

13
test/index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title> Welcome to Hyde </title>
</head>
<body>
<h1> Welcome to <a href="https://adastra7.net/git/Yessiest/hyde">Hyde</a> </h1>
<p> Hyde is the horrible side of Jekyll, and, consequently, this particular project.</p>
<hr />
<p> Created by Yessiest </p>
</body>
</html>

View File

@ -1,26 +1,18 @@
require 'webrick'
require_relative 'hyde'
server = WEBrick::HTTPServer.new :Port => 8000
trap 'INT' do server.shutdown end
server.mount_proc '/' do |req,res|
Hyde::Pathspec.new '/' do
serve "index.html" do |ctx|
ctx.response.body = "This is a test of webrick+hyde combination
If you're seeing this, this means it's a success"
ctx.response["Content-Type"] = "text/plain"
end
server = Hyde::Server.new Port: 8000 do
root "/home/yessiest/hyde/test/"
serve "index.html"
path "about" do
serve "webrick" do |ctx|
ctx.response.body = "WEBrick is weird and pretty undocumented"
ctx.response["Content-Type"] = "text/plain"
ctx.response.body = "WEBrick is a modular http server stack"
ctx.response['Content-Type'] = "text/plain"
end
serve "hyde" do |ctx|
ctx.response.body = "Hyde was born because i thought Sinatra is cool, but nested paths are even cooler"
ctx.response["Content-Type"] = "text/plain"
ctx.response.body = "Hyde is the disgusting side of Jekyll, and, by extension, the thing that makes WEBrick usable."
ctx.response['Content-Type'] = "text/plain"
end
end
end.match(Hyde::Context.new(req.path, req, res))
end
server.start
server.start