adding webrick functionality
This commit is contained in:
parent
c41c29a901
commit
f50ea11b17
86
hyde.rb
86
hyde.rb
|
@ -1,18 +1,22 @@
|
||||||
|
require 'mime-types'
|
||||||
|
|
||||||
module Hyde
|
module Hyde
|
||||||
# Interchangeable pattern matching
|
# Interchangeable pattern matching
|
||||||
module PatternMatching
|
module PatternMatching
|
||||||
def prep_path(path)
|
def prep_path(path,safe_regex: true)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
raise Exception, "Not permitted" if @lock_methods
|
||||||
|
@safe_regex = safe_regex
|
||||||
@path = normalize(path) if path.kind_of? String
|
@path = normalize(path) if path.kind_of? String
|
||||||
@path = path if path.kind_of? Regexp
|
@path = path if path.kind_of? Regexp
|
||||||
end
|
end
|
||||||
def match?(path)
|
def match?(path)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
raise Exception, "Not permitted" if @lock_methods
|
||||||
return true if @path == ""
|
return true if @path == ""
|
||||||
|
split_path = path.split("/").filter { |x| x != "" }
|
||||||
if @path.kind_of? Regexp then
|
if @path.kind_of? Regexp then
|
||||||
return Regexp.new("^"+@path.to_s+"$").match normalize_input(path)
|
return @path.match normalize_input(path) unless @safe_regex
|
||||||
|
return Regexp.new("^"+@path.to_s+"$").match split_path[0]
|
||||||
else
|
else
|
||||||
split_path = path.split("/").filter { |x| x != "" }
|
|
||||||
@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]
|
||||||
|
@ -42,6 +46,14 @@ module Hyde
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Methods to control requests, accessible from within blocks
|
||||||
|
module PublicRequestControlMethods
|
||||||
|
def redirect(url)
|
||||||
|
puts "Unimplemented method 'redirect' called"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Request control class
|
# Request control class
|
||||||
class Request
|
class Request
|
||||||
def initialize(path,request,response)
|
def initialize(path,request,response)
|
||||||
|
@ -71,19 +83,35 @@ module Hyde
|
||||||
# Handler classes
|
# Handler classes
|
||||||
class Probe
|
class Probe
|
||||||
include Hyde::PatternMatching
|
include Hyde::PatternMatching
|
||||||
def initialize (path, &block_optional)
|
include Hyde::PublicRequestControlMethods
|
||||||
prep_path path
|
def initialize (path, safe_regex: true, &block_optional)
|
||||||
|
prep_path path, safe_regex: safe_regex
|
||||||
@block = block_optional
|
@block = block_optional
|
||||||
end
|
end
|
||||||
def match(request)
|
def match(request)
|
||||||
if match? request.path then
|
if @block and (match? request.path) then
|
||||||
p_request = Hyde::ProtectedRequest.new(request)
|
@current_request = Hyde::ProtectedRequest.new(request)
|
||||||
@lock_methods = true
|
@lock_methods = true
|
||||||
self.instance_exec p_request, &@block if @block
|
return_later = self.instance_exec @current_request, &@block
|
||||||
@lock_methods = false
|
@lock_methods = false
|
||||||
|
return return_later
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Serve < Hyde::Probe
|
||||||
|
def match(request)
|
||||||
|
return super if @block
|
||||||
|
if match? request.path then
|
||||||
|
match_path = normalize_input(request.path).match(@path)[0]
|
||||||
|
filepath = request.filepath+match_path
|
||||||
|
mimetype = MIME::Types.type_for(filepath)
|
||||||
|
file = File.new filepath, "r"
|
||||||
|
data = file.read()
|
||||||
|
# TODO: Finish this
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class PrintProbe < Hyde::Probe
|
class PrintProbe < Hyde::Probe
|
||||||
def match(request)
|
def match(request)
|
||||||
if match? request.path then
|
if match? request.path then
|
||||||
|
@ -94,19 +122,27 @@ module Hyde
|
||||||
|
|
||||||
# Handler invocation functions
|
# Handler invocation functions
|
||||||
module Handlers
|
module Handlers
|
||||||
def probe(*a, **b, &block)
|
{
|
||||||
@chain.append Hyde::Probe.new *a, **b, &block
|
probe: Hyde::Probe,
|
||||||
end
|
printProbe: Hyde::PrintProbe
|
||||||
def printProbe(*a, **b, &block)
|
}.each_pair { |name, newclass|
|
||||||
@chain.append Hyde::PrintProbe.new *a, **b, &block
|
define_method name do |path, *a, **b, &block|
|
||||||
end
|
if path.kind_of? Array then
|
||||||
|
path.each do |x|
|
||||||
|
@chain.append newclass.new x, *a, **b, &block
|
||||||
|
end
|
||||||
|
else
|
||||||
|
@chain.append newclass.new path, *a, **b, &block
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
class Pathspec
|
class Pathspec
|
||||||
include Hyde::PatternMatching
|
include Hyde::PatternMatching
|
||||||
include Hyde::Handlers
|
include Hyde::Handlers
|
||||||
def initialize (path, root_path: nil, &block)
|
def initialize (path, root_path: nil, safe_regex: true, &block)
|
||||||
prep_path path
|
prep_path path, safe_regex: safe_regex
|
||||||
@chain = []
|
@chain = []
|
||||||
@root_override = root_path
|
@root_override = root_path
|
||||||
@remap = false
|
@remap = false
|
||||||
|
@ -114,8 +150,14 @@ module Hyde
|
||||||
self.instance_exec &block
|
self.instance_exec &block
|
||||||
@lock_methods = false
|
@lock_methods = false
|
||||||
end
|
end
|
||||||
def path(*a, **b, &block)
|
def path(path, *a, **b, &block)
|
||||||
@chain.append Hyde::Pathspec.new *a, **b, &block
|
if path.kind_of? Array then
|
||||||
|
path.each do |x|
|
||||||
|
@chain.append Hyde::Pathspec.new x, *a, **b, &block
|
||||||
|
end
|
||||||
|
else
|
||||||
|
@chain.append Hyde::Pathspec.new path, *a, **b, &block
|
||||||
|
end
|
||||||
end
|
end
|
||||||
def root(path)
|
def root(path)
|
||||||
@lock_methods = false
|
@lock_methods = false
|
||||||
|
@ -131,13 +173,13 @@ module Hyde
|
||||||
def match(request)
|
def match(request)
|
||||||
raise Exception, "Not permitted" if @lock_methods
|
raise Exception, "Not permitted" if @lock_methods
|
||||||
if match? request.path then
|
if match? request.path then
|
||||||
cut_path = normalize_input(request.path).sub(@path, "")
|
match_path = normalize_input(request.path).match(@path)
|
||||||
next_path = normalize_input(request.path).match(@path)[0]
|
next_path = match_path[0]
|
||||||
request.path = cut_path
|
request.path = cut_path = match_path.post_match
|
||||||
if @root_override then
|
if @root_override then
|
||||||
request.filepath = if @remap then
|
request.filepath = if @remap then
|
||||||
@root_override+"/"
|
@root_override+"/"
|
||||||
else @root_override+next_path+"/" end
|
else @root_override+"/"+next_path+"/" end
|
||||||
else
|
else
|
||||||
request.filepath = request.filepath+next_path+"/"
|
request.filepath = request.filepath+next_path+"/"
|
||||||
end
|
end
|
||||||
|
|
52
test_hyde.rb
52
test_hyde.rb
|
@ -10,10 +10,46 @@ path = Hyde::Pathspec.new "/" do
|
||||||
printProbe "test"
|
printProbe "test"
|
||||||
printProbe "test_*"
|
printProbe "test_*"
|
||||||
probe "speen" do |request|
|
probe "speen" do |request|
|
||||||
puts "Vinny pls say funny word"
|
puts "maurice spinning"
|
||||||
|
redirect "https://www.youtube.com/watch?v=KeNyN_rVL_c"
|
||||||
pp request
|
pp request
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
path "cell_1337" do
|
||||||
|
root "/var/www/cells"
|
||||||
|
path "control" do
|
||||||
|
probe "close" do |request|
|
||||||
|
puts "Permissions level 4 required to control this cell"
|
||||||
|
pp request
|
||||||
|
end
|
||||||
|
probe "open" do |request|
|
||||||
|
puts "Permissions level 4 required to control this cell"
|
||||||
|
pp request
|
||||||
|
end
|
||||||
|
end
|
||||||
|
printProbe "info"
|
||||||
|
end
|
||||||
|
path (/cell_[^\/]*/) do
|
||||||
|
root "/var/www/cells"
|
||||||
|
path "control" do
|
||||||
|
probe "close" do |request|
|
||||||
|
puts "Closing cell #{request.filepath.match /cell_[^\/]*/}"
|
||||||
|
pp request
|
||||||
|
end
|
||||||
|
probe "open" do |request|
|
||||||
|
puts "Opening cell #{request.filepath.match /cell_[^\/]*/}"
|
||||||
|
pp request
|
||||||
|
end
|
||||||
|
end
|
||||||
|
printProbe "dura"
|
||||||
|
printProbe "info"
|
||||||
|
end
|
||||||
|
path "bad_?" do
|
||||||
|
printProbe "path"
|
||||||
|
end
|
||||||
|
probe ["info","hyde"] do
|
||||||
|
puts "this is the most disgusting and visceral thing i've written yet, and i love it"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -23,8 +59,14 @@ end
|
||||||
Hyde::Request.new("/docs/speen",nil,nil),
|
Hyde::Request.new("/docs/speen",nil,nil),
|
||||||
Hyde::Request.new("/docs/test",nil,nil),
|
Hyde::Request.new("/docs/test",nil,nil),
|
||||||
Hyde::Request.new("/docs/test_3",nil,nil),
|
Hyde::Request.new("/docs/test_3",nil,nil),
|
||||||
|
Hyde::Request.new("/cell_41/control/open",nil,nil),
|
||||||
|
Hyde::Request.new("/cell_21/control/close",nil,nil),
|
||||||
|
Hyde::Request.new("/cell_19283/info",nil,nil),
|
||||||
|
Hyde::Request.new("/cell_1337/control/close",nil,nil),
|
||||||
|
Hyde::Request.new("/duracell_129/control/open",nil,nil),
|
||||||
|
Hyde::Request.new("/duracell_1447/control/close",nil,nil),
|
||||||
|
Hyde::Request.new("/bad_2path",nil,nil),
|
||||||
|
Hyde::Request.new("/info",nil,nil),
|
||||||
|
Hyde::Request.new("/hyde",nil,nil)
|
||||||
].each { |x| path.match(x) }
|
].each { |x| path.match(x) }
|
||||||
|
# what a load of fuck this is
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
require 'webrick'
|
require 'webrick'
|
||||||
|
|
||||||
root = File.expand_path '~/hyde/'
|
server = WEBrick::HTTPServer.new :Port => 8000
|
||||||
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
|
|
||||||
|
|
||||||
trap 'INT' do server.shutdown end
|
trap 'INT' do server.shutdown end
|
||||||
server.mount_proc '/' do |req, res|
|
server.mount_proc '/' do |req, res|
|
||||||
res.body = 'Hello, world!'
|
pp res
|
||||||
|
pp req
|
||||||
|
res['Content-Type'] = "text/plain"
|
||||||
|
res.body = 'A'*65536+"Hello world"
|
||||||
end
|
end
|
||||||
server.start
|
server.start
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue