From 3b8632f8a75fc72766f497a88af8760245354ff9 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Wed, 19 Apr 2023 11:30:24 +0400 Subject: [PATCH] removed the crippling cocaine addiction from regexp pattern matching --- hyde.rb | 63 ++++++++++++++++++++++++------- index.html | 10 +++++ test/uploads/01-blog/index.html | 11 ++++++ test/uploads/01-blog/test.md | 7 ++++ test/uploads/02-rules/index.html | 11 ++++++ test/uploads/02-rules/megafuck.md | 1 + test/uploads/index.html | 15 ++++++++ test_combined.rb | 6 +++ 8 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 index.html create mode 100644 test/uploads/01-blog/index.html create mode 100644 test/uploads/01-blog/test.md create mode 100644 test/uploads/02-rules/index.html create mode 100644 test/uploads/02-rules/megafuck.md create mode 100644 test/uploads/index.html diff --git a/hyde.rb b/hyde.rb index 75d4628..ff7c2f0 100644 --- a/hyde.rb +++ b/hyde.rb @@ -2,6 +2,12 @@ require 'mime-types' require 'webrick' module Hyde + # Branding and version + VERSION = "0.1" + attr_reader :VERSION + VLINE = "
\n Hyde/#{Hyde::VERSION} on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})\n
" + attr_reader :VLINE + class Server < WEBrick::HTTPServer def initialize(config={},&setup) super(config) @@ -11,6 +17,7 @@ module Hyde end end end + module ErrorPages # 404 text def error404(request, filepath) @@ -20,17 +27,18 @@ module Hyde filepath ) else - request.response.body = "\n\n File not found\n \n

File not found

\n #{filepath}\n
\n
\n Hyde on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE})\n
\n \n" + request.response.body = "\n\n File not found\n \n

File not found

\n #{filepath}\n
\n #{Hyde::VLINE}\n \n" end request.response["Content-Type"] = 'text/html' end module_function :error404 end - # Interchangeable pattern matching + + # Interchangeable glob/regex/string pattern matching module PatternMatching - def prep_path(path,safe_regex: true) + def prep_path(path,safe_regexp: true) raise Exception, "Not permitted" if @lock_methods - @safe_regex = safe_regex + @safe_regexp = safe_regexp @path = normalize(path) if path.kind_of? String @path = path if path.kind_of? Regexp end @@ -40,9 +48,18 @@ module Hyde 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] + # 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 + # 2) a .match? only returns when there are no leftovers + # 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 + else + return false + end else # algorithm to match path segments until no more left in @path @path.split("/").filter { |x| x != "" } @@ -91,12 +108,14 @@ module Hyde @request = request @response = response @handles = {} + @indexlist = [] end attr_reader :request attr_reader :response attr_accessor :filepath attr_accessor :path attr_accessor :handles + attr_accessor :indexlist end # Context object with safe path encapsulation @@ -111,14 +130,15 @@ module Hyde undef :path= undef :filepath= undef :handles= + undef :indexlist= end # Handler classes class Probe include Hyde::PatternMatching include Hyde::PublicContextControlMethods - def initialize (path, safe_regex: true, &block_optional) - prep_path path, safe_regex: safe_regex + def initialize (path, safe_regexp: true, &block_optional) + prep_path path, safe_regexp: safe_regexp @block = block_optional end def match(request) @@ -145,7 +165,7 @@ module Hyde request.response.body = data request.response["Content-Type"] = mimetype rescue Errno::ENOENT - Hyde::ErrorPages::error404 filepath + Hyde::ErrorPages::error404 request, request.request.path end end end @@ -181,8 +201,8 @@ module Hyde class Pathspec include Hyde::PatternMatching include Hyde::Handlers - def initialize (path, root_path: nil, safe_regex: true, &block) - prep_path path, safe_regex: safe_regex + def initialize (path, root_path: nil, safe_regexp: true, &block) + prep_path path, safe_regexp: safe_regexp @chain = [] @handles = {} @root_override = root_path @@ -211,6 +231,9 @@ module Hyde @lock_methods = true @remap = true end + def index(list) + @indexlist = list if list.kind_of? Array + end def handle(code, &block) @handles[code] = block end @@ -228,13 +251,25 @@ module Hyde else request.filepath = request.filepath+next_path+"/" end - # error handle overlaying + # parameter overrides overlaying @handles.each_pair { |k,v| request.handles[k] = v } + request.indexlist = @indexlist if @indexlist + # do directory indexing + if cut_path.match /^\/?$/ then + request.indexlist.each do |x| + try_index = @chain.find { |y| y.match? x } + if try_index then + request.path = x + return try_index.match request + end + end + end # passthrough to the next path object next_pathspec = @chain.find { |x| x.match? cut_path } + puts next_pathspec.inspect next_pathspec.match request if next_pathspec - # throw 404 if nowhere to go unless next_pathspec then + # throw 404 if nowhere to go Hyde::ErrorPages::error404 request, request.request.path end end diff --git a/index.html b/index.html new file mode 100644 index 0000000..6b1e77d --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + Unforseen consequences + + +

Cleverly done, mister Freeman, but you are not supposed to be here

+

Get back where you belong

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

This is a test

+
+
yes
+ + diff --git a/test/uploads/01-blog/test.md b/test/uploads/01-blog/test.md new file mode 100644 index 0000000..c41674e --- /dev/null +++ b/test/uploads/01-blog/test.md @@ -0,0 +1,7 @@ +# 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 new file mode 100644 index 0000000..590e381 --- /dev/null +++ b/test/uploads/02-rules/index.html @@ -0,0 +1,11 @@ + + + + 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 new file mode 100644 index 0000000..3702e9b --- /dev/null +++ b/test/uploads/02-rules/megafuck.md @@ -0,0 +1 @@ +# YES diff --git a/test/uploads/index.html b/test/uploads/index.html new file mode 100644 index 0000000..b7385ee --- /dev/null +++ b/test/uploads/index.html @@ -0,0 +1,15 @@ + + + File listing + +

File listing:

+ +
+
+ welcum to this crap +
+ + diff --git a/test_combined.rb b/test_combined.rb index fb7c073..9138770 100644 --- a/test_combined.rb +++ b/test_combined.rb @@ -3,6 +3,7 @@ require_relative 'hyde' server = Hyde::Server.new Port: 8000 do root "/home/yessiest/hyde/test/" serve "index.html" + index ["index.html"] path "about" do serve "webrick" do |ctx| ctx.response.body = "WEBrick is a modular http server stack" @@ -13,6 +14,11 @@ server = Hyde::Server.new Port: 8000 do ctx.response['Content-Type'] = "text/plain" end end + path "uploads" do + index ["index.html"] + serve "**/*.md", safe_regex: false + serve ["*.html","**/*.html"], safe_regex: false + end end server.start