very functional prototype
This commit is contained in:
parent
8593315b95
commit
6dcf46f40a
64
hyde.rb
64
hyde.rb
|
@ -2,22 +2,30 @@ require 'mime-types'
|
||||||
require 'webrick'
|
require 'webrick'
|
||||||
|
|
||||||
module Hyde
|
module Hyde
|
||||||
# 404 text
|
class Server < WEBrick::HTTPServer
|
||||||
def default404(filepath)
|
def initialize(config={},&setup)
|
||||||
return "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">
|
super(config)
|
||||||
<HTML>
|
@hyde_pathspec = Hyde::Pathspec.new "/", &setup
|
||||||
<HEAD><TITLE>File not found</TITLE></HEAD>
|
self.mount_proc '/' do |req,res|
|
||||||
<BODY>
|
@hyde_pathspec.match(Hyde::Context.new(req.path, req, res))
|
||||||
<H1>File not found</H1>
|
end
|
||||||
#{filepath}
|
end
|
||||||
<HR>
|
end
|
||||||
<ADDRESS>
|
module ErrorPages
|
||||||
Hyde on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})
|
# 404 text
|
||||||
</ADDRESS>
|
def error404(request, filepath)
|
||||||
</BODY>
|
request.response.status = 404
|
||||||
</HTML>"
|
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
|
end
|
||||||
module_function :default404
|
|
||||||
# Interchangeable pattern matching
|
# Interchangeable pattern matching
|
||||||
module PatternMatching
|
module PatternMatching
|
||||||
def prep_path(path,safe_regex: true)
|
def prep_path(path,safe_regex: true)
|
||||||
|
@ -28,12 +36,15 @@ module Hyde
|
||||||
end
|
end
|
||||||
def match?(path)
|
def match?(path)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
raise Exception, "Not permitted" if @lock_methods
|
||||||
|
# behvaiour used by "index" method
|
||||||
return true if @path == ""
|
return true if @path == ""
|
||||||
split_path = path.split("/").filter { |x| x != "" }
|
split_path = path.split("/").filter { |x| x != "" }
|
||||||
if @path.kind_of? Regexp then
|
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 @path.match normalize_input(path) unless @safe_regex
|
||||||
return Regexp.new("^"+@path.to_s+"$").match split_path[0]
|
return Regexp.new("^"+@path.to_s+"$").match split_path[0]
|
||||||
else
|
else
|
||||||
|
# algorithm to match path segments until no more left in @path
|
||||||
@path.split("/").filter { |x| x != "" }
|
@path.split("/").filter { |x| x != "" }
|
||||||
.each_with_index { |x,i|
|
.each_with_index { |x,i|
|
||||||
return false if x != split_path[i]
|
return false if x != split_path[i]
|
||||||
|
@ -43,11 +54,12 @@ module Hyde
|
||||||
end
|
end
|
||||||
def normalize_input(path)
|
def normalize_input(path)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
raise Exception, "Not permitted" if @lock_methods
|
||||||
|
# remove duplicate slashes and trim edge slashes
|
||||||
(path.split "/").filter { |x| x != "" }.join("/")
|
(path.split "/").filter { |x| x != "" }.join("/")
|
||||||
end
|
end
|
||||||
def normalize(path)
|
def normalize(path)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
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)
|
path = normalize_input(path)
|
||||||
# globbing behaviour simulated with regexp
|
# globbing behaviour simulated with regexp
|
||||||
if path.match /(?<!\\)[\*\?\[]/ then
|
if path.match /(?<!\\)[\*\?\[]/ then
|
||||||
|
@ -133,12 +145,7 @@ module Hyde
|
||||||
request.response.body = data
|
request.response.body = data
|
||||||
request.response["Content-Type"] = mimetype
|
request.response["Content-Type"] = mimetype
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
if request.handles.include? 404 then
|
Hyde::ErrorPages::error404 filepath
|
||||||
request.response.body = request.handles[404].call filepath
|
|
||||||
request.response["Content-Type"] = "text/html"
|
|
||||||
else
|
|
||||||
request.response.body = Hyde::default404 filepath
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -213,6 +220,7 @@ module Hyde
|
||||||
match_path = normalize_input(request.path).match(@path)
|
match_path = normalize_input(request.path).match(@path)
|
||||||
next_path = match_path[0]
|
next_path = match_path[0]
|
||||||
request.path = cut_path = match_path.post_match
|
request.path = cut_path = match_path.post_match
|
||||||
|
# remap/root method handling
|
||||||
if @root_override then
|
if @root_override then
|
||||||
request.filepath = if @remap then
|
request.filepath = if @remap then
|
||||||
@root_override+"/"
|
@root_override+"/"
|
||||||
|
@ -220,18 +228,14 @@ module Hyde
|
||||||
else
|
else
|
||||||
request.filepath = request.filepath+next_path+"/"
|
request.filepath = request.filepath+next_path+"/"
|
||||||
end
|
end
|
||||||
|
# error handle overlaying
|
||||||
@handles.each_pair { |k,v| request.handles[k] = v }
|
@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 = @chain.find { |x| x.match? cut_path }
|
||||||
next_pathspec.match request if next_pathspec
|
next_pathspec.match request if next_pathspec
|
||||||
|
# throw 404 if nowhere to go
|
||||||
unless next_pathspec then
|
unless next_pathspec then
|
||||||
if request.handles.include? 404
|
Hyde::ErrorPages::error404 request, request.request.path
|
||||||
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'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -1,26 +1,18 @@
|
||||||
require 'webrick'
|
|
||||||
require_relative 'hyde'
|
require_relative 'hyde'
|
||||||
|
|
||||||
server = WEBrick::HTTPServer.new :Port => 8000
|
server = Hyde::Server.new Port: 8000 do
|
||||||
trap 'INT' do server.shutdown end
|
root "/home/yessiest/hyde/test/"
|
||||||
server.mount_proc '/' do |req,res|
|
serve "index.html"
|
||||||
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
|
|
||||||
path "about" do
|
path "about" do
|
||||||
serve "webrick" do |ctx|
|
serve "webrick" do |ctx|
|
||||||
ctx.response.body = "WEBrick is weird and pretty undocumented"
|
ctx.response.body = "WEBrick is a modular http server stack"
|
||||||
ctx.response["Content-Type"] = "text/plain"
|
ctx.response['Content-Type'] = "text/plain"
|
||||||
end
|
end
|
||||||
serve "hyde" do |ctx|
|
serve "hyde" do |ctx|
|
||||||
ctx.response.body = "Hyde was born because i thought Sinatra is cool, but nested paths are even cooler"
|
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"
|
ctx.response['Content-Type'] = "text/plain"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end.match(Hyde::Context.new(req.path, req, res))
|
|
||||||
end
|
end
|
||||||
server.start
|
|
||||||
|
|
||||||
|
server.start
|
||||||
|
|
Loading…
Reference in New Issue