adding webrick functionality
This commit is contained in:
parent
c41c29a901
commit
f50ea11b17
88
hyde.rb
88
hyde.rb
|
@ -1,18 +1,22 @@
|
|||
require 'mime-types'
|
||||
|
||||
module Hyde
|
||||
# Interchangeable pattern matching
|
||||
module PatternMatching
|
||||
def prep_path(path)
|
||||
def prep_path(path,safe_regex: true)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
@safe_regex = safe_regex
|
||||
@path = normalize(path) if path.kind_of? String
|
||||
@path = path if path.kind_of? Regexp
|
||||
end
|
||||
def match?(path)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
return true if @path == ""
|
||||
split_path = path.split("/").filter { |x| x != "" }
|
||||
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
|
||||
split_path = path.split("/").filter { |x| x != "" }
|
||||
@path.split("/").filter { |x| x != "" }
|
||||
.each_with_index { |x,i|
|
||||
return false if x != split_path[i]
|
||||
|
@ -42,6 +46,14 @@ module Hyde
|
|||
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
|
||||
class Request
|
||||
def initialize(path,request,response)
|
||||
|
@ -70,20 +82,36 @@ module Hyde
|
|||
|
||||
# Handler classes
|
||||
class Probe
|
||||
include Hyde::PatternMatching
|
||||
def initialize (path, &block_optional)
|
||||
prep_path path
|
||||
include Hyde::PatternMatching
|
||||
include Hyde::PublicRequestControlMethods
|
||||
def initialize (path, safe_regex: true, &block_optional)
|
||||
prep_path path, safe_regex: safe_regex
|
||||
@block = block_optional
|
||||
end
|
||||
def match(request)
|
||||
if match? request.path then
|
||||
p_request = Hyde::ProtectedRequest.new(request)
|
||||
if @block and (match? request.path) then
|
||||
@current_request = Hyde::ProtectedRequest.new(request)
|
||||
@lock_methods = true
|
||||
self.instance_exec p_request, &@block if @block
|
||||
return_later = self.instance_exec @current_request, &@block
|
||||
@lock_methods = false
|
||||
return return_later
|
||||
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
|
||||
def match(request)
|
||||
if match? request.path then
|
||||
|
@ -94,19 +122,27 @@ module Hyde
|
|||
|
||||
# Handler invocation functions
|
||||
module Handlers
|
||||
def probe(*a, **b, &block)
|
||||
@chain.append Hyde::Probe.new *a, **b, &block
|
||||
end
|
||||
def printProbe(*a, **b, &block)
|
||||
@chain.append Hyde::PrintProbe.new *a, **b, &block
|
||||
end
|
||||
{
|
||||
probe: Hyde::Probe,
|
||||
printProbe: Hyde::PrintProbe
|
||||
}.each_pair { |name, newclass|
|
||||
define_method name do |path, *a, **b, &block|
|
||||
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
|
||||
|
||||
class Pathspec
|
||||
include Hyde::PatternMatching
|
||||
include Hyde::Handlers
|
||||
def initialize (path, root_path: nil, &block)
|
||||
prep_path path
|
||||
def initialize (path, root_path: nil, safe_regex: true, &block)
|
||||
prep_path path, safe_regex: safe_regex
|
||||
@chain = []
|
||||
@root_override = root_path
|
||||
@remap = false
|
||||
|
@ -114,8 +150,14 @@ module Hyde
|
|||
self.instance_exec &block
|
||||
@lock_methods = false
|
||||
end
|
||||
def path(*a, **b, &block)
|
||||
@chain.append Hyde::Pathspec.new *a, **b, &block
|
||||
def path(path, *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
|
||||
def root(path)
|
||||
@lock_methods = false
|
||||
|
@ -131,13 +173,13 @@ module Hyde
|
|||
def match(request)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
if match? request.path then
|
||||
cut_path = normalize_input(request.path).sub(@path, "")
|
||||
next_path = normalize_input(request.path).match(@path)[0]
|
||||
request.path = cut_path
|
||||
match_path = normalize_input(request.path).match(@path)
|
||||
next_path = match_path[0]
|
||||
request.path = cut_path = match_path.post_match
|
||||
if @root_override then
|
||||
request.filepath = if @remap then
|
||||
@root_override+"/"
|
||||
else @root_override+next_path+"/" end
|
||||
else @root_override+"/"+next_path+"/" end
|
||||
else
|
||||
request.filepath = request.filepath+next_path+"/"
|
||||
end
|
||||
|
|
52
test_hyde.rb
52
test_hyde.rb
|
@ -10,10 +10,46 @@ path = Hyde::Pathspec.new "/" do
|
|||
printProbe "test"
|
||||
printProbe "test_*"
|
||||
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
|
||||
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
|
||||
|
||||
[
|
||||
|
@ -23,8 +59,14 @@ end
|
|||
Hyde::Request.new("/docs/speen",nil,nil),
|
||||
Hyde::Request.new("/docs/test",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) }
|
||||
|
||||
|
||||
|
||||
|
||||
# what a load of fuck this is
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
require 'webrick'
|
||||
|
||||
root = File.expand_path '~/hyde/'
|
||||
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
|
||||
server = WEBrick::HTTPServer.new :Port => 8000
|
||||
|
||||
trap 'INT' do server.shutdown end
|
||||
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
|
||||
server.start
|
||||
|
||||
|
|
Loading…
Reference in New Issue