diff --git a/.yardoc/checksums b/.yardoc/checksums new file mode 100644 index 0000000..e69de29 diff --git a/.yardoc/complete b/.yardoc/complete new file mode 100644 index 0000000..e69de29 diff --git a/.yardoc/object_types b/.yardoc/object_types new file mode 100644 index 0000000..fdda275 Binary files /dev/null and b/.yardoc/object_types differ diff --git a/.yardoc/objects/root.dat b/.yardoc/objects/root.dat new file mode 100644 index 0000000..72bcda5 Binary files /dev/null and b/.yardoc/objects/root.dat differ diff --git a/.yardoc/proxy_types b/.yardoc/proxy_types new file mode 100644 index 0000000..beefda1 Binary files /dev/null and b/.yardoc/proxy_types differ diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..274e8bd --- /dev/null +++ b/config.ru @@ -0,0 +1,8 @@ +require 'rack' +require_relative 'test_app' +app = Rack::Builder.new do + use Rack::Lint + run TestApp::App.new +end + +run app diff --git a/hyde.rb b/hyde.old.rb similarity index 52% rename from hyde.rb rename to hyde.old.rb index 65587e0..57fa4cd 100644 --- a/hyde.rb +++ b/hyde.old.rb @@ -3,94 +3,73 @@ require 'webrick' require 'uri' require 'pp' +# Primary module module Hyde - # Branding and version - VERSION = "0.5 (alpha)" + # Hyde version + # @type [String] + VERSION = '0.5 (alpha)' attr_reader :VERSION + + # Hyde branding and version (for error templates) + # @type [String] VLINE = "Hyde/#{Hyde::VERSION} on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})\n" attr_reader :VLINE - def error_template(errortext,backtrace) - < - - - #{WEBrick::HTMLUtils.escape(errortext)} - - - -
-

HYDE

-

Source code

-
-
-

#{WEBrick::HTMLUtils.escape(errortext)}

-

-#{WEBrick::HTMLUtils.escape(backtrace) or "\n\n\n"}
-            
-
-

#{WEBrick::HTMLUtils.escape(VLINE)}

-
- - -HTMLEOF + # Generate HTML error template + # @param errortext [String] Error explanation + # @param backtrace [String] Ruby backtrace + def error_template(errortext, backtrace) + <<~HTMLEOF + + + + #{WEBrick::HTMLUtils.escape(errortext)} + + + +
+

HYDE

+

Source code

+
+
+

#{WEBrick::HTMLUtils.escape(errortext)}

+

+                  #{WEBrick::HTMLUtils.escape(backtrace) or "\n\n\n"}
+                  
+
+

#{WEBrick::HTMLUtils.escape(Hyde::VLINE)}

+
+ + + HTMLEOF end module_function :error_template WEBrick::HTTPResponse.class_exec do - attr_accessor :recent_backtrace public - def set_backtrace(backtrace) - @recent_backtrace = backtrace - end + + attr_accessor :recent_backtrace + def create_error_page - @body = Hyde.error_template(@reason_phrase,@recent_backtrace) + @body = Hyde.error_template(@reason_phrase, @recent_backtrace) end end - - class Server < WEBrick::HTTPServer - def initialize(config={},&setup) - super(config) - @hyde_pathspec = Hyde::Pathspec.new "/", &setup - self.mount_proc '/' do |req,res| - context = Hyde::Context.new(req.path, req, res) - begin - while context and (not context.exit_loop) do - context.exit_loop = true - context = catch :controlled_exit do - @hyde_pathspec._match(context) - context - end - while postprocessor = context.queue_postprocess.shift do - postprocessor.call(context) - end - end - while finalizer = context.queue_finalize.shift do - finalizer.call(context) - end - rescue Exception => e - puts e.message - puts e.backtrace - res.set_backtrace "#{e.message} (#{e.class})\n#{e.backtrace.join "\n"}" - res.status = 500 - end - end - end - end - + # Interchangeable glob/regex/string pattern matching module PatternMatching - def _prep_path(path,safe_regexp: true) + def _prep_path(path, safe_regexp: true) @safe_regexp = safe_regexp - @path = _normalize(path) if path.kind_of? String - @path = path if path.kind_of? Regexp + @path = _normalize(path) if path.is_a? String + @path = path if path.is_a? Regexp end - def _match?(path, ctx) + + # @return [Boolean] + def _match?(path, _ctx) # behvaiour used by "index" method - return true if @path == "" - split_path = path.split("/").filter { |x| x != "" } - if @path.kind_of? Regexp then + return true if @path == '' + split_path = path.split('/').filter { |x| x != '' } + if @path.is_a? Regexp # this chunk of fuck is needed to force regexp into 3 rules: # 1) unsafe regexp means match the whole (remaining) line. # 3) safe regexp means match only the part before the next slash @@ -98,38 +77,40 @@ HTMLEOF # this forces the matching to work somewhat consistently test = @path.match _normalize_input(path) unless @safe_regexp test = @path.match split_path[0] if @safe_regexp - if test and (test.pre_match == "") and (test.post_match == "") then - return true + if test and (test.pre_match == '') and (test.post_match == '') + true else - return false + false end else # algorithm to match path segments until no more left in @path - @path.split("/").filter { |x| x != "" } - .each_with_index { |x,i| + @path.split('/').filter { |x| x != '' } + .each_with_index do |x, i| return false if x != split_path[i] - } - return true + end + true end end + def _normalize_input(path) # remove duplicate slashes and trim edge slashes - (path.split "/").filter { |x| x != "" }.join("/") + (path.split '/').filter { |x| x != '' }.join('/') end + def _normalize(path) # remove duplicate slashe s and trim edge slashes path = _normalize_input(path) # globbing behaviour simulated with regexp - if path.match /(?" ( regexp: acts like a named group for /[^/]*/ ) + # - acts like * as defined above + # - result is captured in a hash with as key + # - allows alphanumeric characters and underscores + class Glob + # @param input [String] Glob pattern + def initialize(pattern) + pattern = Hyde::PatternMatching.canonicalize(pattern) + pieces = pattern.split(/(\/\*\*\/|\*\*|\*|\?|\[!?\w-\w\]|:[^\/]+)/) + # @type [Array] + @index = build_index(pieces) + # @type [Regexp] + @glob = Regexp.new(pieces.map do |filter| + case filter + when "/**/" then "/(.*/|)" + when "**" then "(.*)" + when "*" then "([^/]*)" + when "?" then "[^/]" + when /^\[!?\w-\w\]$/ then filter.sub('!', '^') + when /:[\w_]+/ then "[^/]*" + else filter.gsub(/[\^$.|+(){}]/, '\\\\\\0') + end + end.join("").prepend("^/?")) + puts @glob + end + + # Match the string and assign matches to parameters + # Returns: + # - Unmatched part of a string + # - Unnamed parameters + # - Named parameters + # @param input [String] String to match + # @return [Array(String,Array,Hash)] + def match(input) + input = Hyde::PatternMatching.canonicalize(input) + result = input.match(@glob) + input = result.post_match + named_params, params = assign_by_index(@index, result.captures) + [input, params, named_params] + end + + # Test if a string can be matched + # Lighter version of match that doesn't assign any variables + # @param input [String] String to match + # @return [Boolean] + def match?(input) + input = Hyde::PatternMatching.canonicalize(input) + input.match? @glob + end + + # Test if input is convertible to a Glob and if there is any reason to + # @param input + # @return [Boolean] Input can be safely converted to Glob + def self.can_convert?(input) + input.kinf_of? String and + input.match?(/(?" ( regexp: acts like a named group for /[^/]*/ ) + # - acts like * as defined above + # - result is captured in a hash with as key + # - allows alphanumeric characters and underscores + class Glob + # @param input [String] Glob pattern + def initialize(pattern) + pattern = Hyde::PatternMatching.canonicalize(pattern) + pieces = pattern.split(/(\/\*\*\/|\*\*|\*|\?|\[!?\w-\w\]|:[^\/]+)/) + # @type [Array] + @index = build_index(pieces) + # @type [Regexp] + @glob = Regexp.new(pieces.map do |filter| + case filter + when "/**/" then "/(.*/|)" + when "**" then "(.*)" + when "*" then "([^/]*)" + when "?" then "([^/])" + when /^\[!?\w-\w\]$/ then "(#{filter.sub('!', '^')})" + when /:[\w_]+/ then "([^/]*)" + else filter.gsub(/[\^$.|+(){}]/, '\\\\\\0') + end + end.join("").prepend("^/?")) + end + + # Match the string and assign matches to parameters + # Returns: + # - Unmatched part of a string + # - Unnamed parameters + # - Named parameters + # @param input [String] String to match + # @return [Array(String,Array,Hash)] + def match(input) + input = Hyde::PatternMatching.canonicalize(input) + result = input.match(@glob) + input = result.post_match + named_params, params = assign_by_index(@index, result.captures) + [input, params, named_params] + end + + # Test if a string can be matched + # Lighter version of match that doesn't assign any variables + # @param input [String] String to match + # @return [Boolean] + def match?(input) + input = Hyde::PatternMatching.canonicalize(input) + input.match? @glob + end + + # Test if input is convertible to a Glob and if there is any reason to + # @param input + # @return [Boolean] Input can be safely converted to Glob + def self.can_convert?(input) + input.kinf_of? String and + input.match?(/(? - - - Welcome to Hyde - - -

Welcome to Hyde

-

Hyde is the horrible side of Jekyll, and, consequently, this particular project.

- -
-

Created by Yessiest

- - - diff --git a/test/uploads/01-blog/index.html b/test/uploads/01-blog/index.html deleted file mode 100644 index 7fb3fe4..0000000 --- a/test/uploads/01-blog/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - test - - -

This is a test

-
-
yes
- - diff --git a/test/uploads/01-blog/test.md b/test/uploads/01-blog/test.md deleted file mode 100644 index c41674e..0000000 --- a/test/uploads/01-blog/test.md +++ /dev/null @@ -1,7 +0,0 @@ -# Welcome to the INFINITE HYPERNET - ---- -- THE HYPE IS REAL -- SCENE IS DEAD -- BLOOD OF LAMERS IS FUEL - diff --git a/test/uploads/02-rules/index.html b/test/uploads/02-rules/index.html deleted file mode 100644 index 590e381..0000000 --- a/test/uploads/02-rules/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - very test - - -

This is a very test

-
-
yes 2
- - diff --git a/test/uploads/02-rules/megafuck.md b/test/uploads/02-rules/megafuck.md deleted file mode 100644 index 3702e9b..0000000 --- a/test/uploads/02-rules/megafuck.md +++ /dev/null @@ -1 +0,0 @@ -# YES diff --git a/test/uploads/index.html b/test/uploads/index.html deleted file mode 100644 index b7385ee..0000000 --- a/test/uploads/index.html +++ /dev/null @@ -1,15 +0,0 @@ - - - File listing - -

File listing:

- -
-
- welcum to this crap -
- - diff --git a/test_calculator.rb b/test_calculator.rb deleted file mode 100644 index f3f555f..0000000 --- a/test_calculator.rb +++ /dev/null @@ -1,32 +0,0 @@ -require_relative "hyde" -server = Hyde::Server.new Port: 8000 do - {"add" => -> (a,b) { a + b }, - "sub" => -> (a,b) { a - b }, - "mul" => -> (a,b) { a * b }, - "div" => -> (a,b) { - begin - return a/b - rescue ZeroDivisionError - return "Divided by zero" - end - } - }.each_pair do |k,v| - serve k do |ctx| - req,res = ctx.request,ctx.response - a,b = req.query["a"],req.query["b"] - result = (a and b) ? v.call(a.to_f,b.to_f) : "Invalid parameters" - res.body = " - - - - Calculator API test - - - Result: #{result} - -" - res['Content-Type'] = "text/html" - end - end -end -server.start diff --git a/test_combined.rb b/test_combined.rb deleted file mode 100644 index 68418c3..0000000 --- a/test_combined.rb +++ /dev/null @@ -1,46 +0,0 @@ -require_relative 'hyde' - -server = Hyde::Server.new Port: 8000 do - root "/home/yessiest/Projects/hyde/test/" - serve "index.html" - index ["index.html"] - path "about" do - preprocess do |ctx| - puts "#{ctx} entered fully virtual directory!" - end - postprocess do |ctx| - puts "#{ctx} reached endpoint!" - end - finalize do |ctx| - puts "#{ctx} finished processing!" - end - get "portal" do |ctx| - ctx.vars[:ass] = true - rewrite "/about/hyde" - end - get "webrick" do |ctx| - ctx.response.body = "WEBrick is a modular http server stack" - ctx.response['Content-Type'] = "text/plain" - end - get "en_passant" do |ctx| - puts "holy hell!" - redirect "https://google.com/search?q=en+passant" - end - get "hyde" do |ctx| - puts ctx.vars[:ass] - 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 - post "hyde" do |ctx| - ctx.response.body = "Your message: #{ctx.request.body}" - ctx.response['Content-Type'] = "text/plain" - end - end - path "uploads" do - index ["index.html"] - serve "**/*.md", safe_regexp: false - serve ["*.html","**/*.html"], safe_regexp: false - end -end - -server.start diff --git a/test_hyde.rb b/test_hyde.rb deleted file mode 100644 index 264cd38..0000000 --- a/test_hyde.rb +++ /dev/null @@ -1,72 +0,0 @@ -require_relative "hyde" -path = Hyde::Pathspec.new "/" do - root "/var/www" - path "about" do - printProbe "test" - printProbe "test_*" - end - path "docs" do - remap "/var/www/markdown_compiled/" - printProbe "test" - printProbe "test_*" - probe "speen" do |request| - 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 - -[ - Hyde::Request.new("/about/speen",nil,nil), - Hyde::Request.new("/about/test",nil,nil), - Hyde::Request.new("/about/test_2",nil,nil), - 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 diff --git a/test_webrick.rb b/test_webrick.rb deleted file mode 100644 index be38125..0000000 --- a/test_webrick.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'webrick' - -server = WEBrick::HTTPServer.new :Port => 8000 - -trap 'INT' do server.shutdown end -server.mount_proc '/' do |req, res| - pp res - pp req - res['Content-Type'] = "text/plain" - res.body = 'A'*65536+"Hello world" -end -server.start - -