From 9c9ceaf4f8e479bc24eff9cf42c281276a4ad9dc Mon Sep 17 00:00:00 2001 From: Yessiest Date: Sat, 9 Sep 2023 00:07:43 +0400 Subject: [PATCH] Also actual files for the previous commit (how did i forget to do that) --- lib/hyde/dsl/path_methods.rb | 5 +---- lib/hyde/node.rb | 21 +++++++++++++++++++-- lib/hyde/path.rb | 4 +--- lib/hyde/pattern_matching/glob.rb | 8 ++++---- lib/hyde/probe.rb | 3 +-- lib/hyde/probe/serve_handler.rb | 5 ++++- lib/hyde/request.rb | 2 +- lib/hyde/server.rb | 7 +++---- test/Hyde_PatternMatching_Glob.rb | 21 +++++++++++++++++++++ 9 files changed, 55 insertions(+), 21 deletions(-) diff --git a/lib/hyde/dsl/path_methods.rb b/lib/hyde/dsl/path_methods.rb index 152bcf4..d0b727b 100644 --- a/lib/hyde/dsl/path_methods.rb +++ b/lib/hyde/dsl/path_methods.rb @@ -21,16 +21,13 @@ module Hyde # Set root path (appends matched part of the path). # @param path [String def root(path) - raise StandardError, "path should be a String" unless path.is_a? String - @origin.root = path end # Set root path (without appending matched part). # @param path [String def remap(path) - root(path) - @origin.remap = true + @origin.remap = path end # Add a preprocessor to the path. diff --git a/lib/hyde/node.rb b/lib/hyde/node.rb index 9b757da..1434de8 100644 --- a/lib/hyde/node.rb +++ b/lib/hyde/node.rb @@ -9,12 +9,29 @@ module Hyde # @abstract class Node # @param path [Object] - def initialize(path) + def initialize(path, parent:) @pattern = Pattern.new(path).freeze + @properties = Hyde::Util::Lookup.new(parent&.properties) @root = nil @remap = false end + # Set Node file root (like root in Nginx) + # @param path [String] + def root=(path) + raise StandardError, "path should be a String" unless path.is_a? String + + @properties["path"] = File.expand_path(path) + @root = File.expand_path(path) + end + + # Set Node absolute file path (like alias in Nginx) + # @param path [String] + def remap=(path) + self.root = path + @remap = true + end + # Try to navigate the path. Run method callback in response. # @param [Hyde::Request] # @return [Boolean] @@ -49,7 +66,7 @@ module Hyde true end - attr_accessor :remap, :root + attr_reader :remap, :root private diff --git a/lib/hyde/path.rb b/lib/hyde/path.rb index c53fb12..67798da 100644 --- a/lib/hyde/path.rb +++ b/lib/hyde/path.rb @@ -25,11 +25,9 @@ module Hyde # @param parent [Hyde::Node] Parent object to inherit properties to # @param setup [#call] Setup block def initialize(path, parent:, &setup) - super(path) + super(path, parent: parent) # Child nodes array @children = [] - # Inherited properties array - @properties = Hyde::Util::Lookup.new(parent&.properties) # Arrays of preprocessors, postprocessors and filters @preprocessors = [] @postprocessors = [] diff --git a/lib/hyde/pattern_matching/glob.rb b/lib/hyde/pattern_matching/glob.rb index af9fe1b..2c8a0fe 100644 --- a/lib/hyde/pattern_matching/glob.rb +++ b/lib/hyde/pattern_matching/glob.rb @@ -100,11 +100,12 @@ module Hyde private + # i shall name thee Norxondor Glorbulon + # Regexp pattern to match glob tokens TOKENS = / ( # Glob-specific tokens - \/\*\*\/ | # Freestanding globstar - \*\* | # Attached globstar + (?<=(?:\/|^))\*\*(?:\/|$) | # Freestanding globstar \* | # Regular glob \[!?\w-\w\]\+ | # Character group (?<=\/):[\w_]+(?=(?:\/|$)) | # Named glob @@ -153,8 +154,7 @@ module Hyde def build_regexp(tokens) Regexp.new(tokens.map do |filter| case filter - when "/**/" then "/(?:(.*)\/|)" # FIXME: this may return nil - when "**" then "(.*)" + when /(\/|^)\*\*(\/|$)/ then "(?:(.*)/|)" # FIXME: this may return nil when "*" then "([^/]*)" when /^\([\w\/|_-]+\)$/ then filter.sub('-', '\\-') when /^\[!?\w-\w\]\+$/ then filter.sub('!', '^') diff --git a/lib/hyde/probe.rb b/lib/hyde/probe.rb index 0dd351a..9d91bcc 100644 --- a/lib/hyde/probe.rb +++ b/lib/hyde/probe.rb @@ -21,8 +21,7 @@ module Hyde # @param path [Object] # @param parent [Hyde::Node] def initialize(path, parent:) - super(path) - @properties = Hyde::Util::Lookup.new(parent&.properties) + super(path, parent: parent) end attr_reader :properties diff --git a/lib/hyde/probe/serve_handler.rb b/lib/hyde/probe/serve_handler.rb index 08e4c52..d6f1c8a 100644 --- a/lib/hyde/probe/serve_handler.rb +++ b/lib/hyde/probe/serve_handler.rb @@ -20,7 +20,10 @@ module Hyde # @param request [Hyde::Request] # @return [Boolean] true if file was found def process(request) - File.open(request.filepath.delete_suffix("/")) + path = File.expand_path(request.filepath) + return unless path.start_with? @properties["path"] + + File.open(path.delete_suffix("/")) rescue StandardError false end diff --git a/lib/hyde/request.rb b/lib/hyde/request.rb index f7bd5ee..6b28e32 100644 --- a/lib/hyde/request.rb +++ b/lib/hyde/request.rb @@ -15,7 +15,7 @@ module Hyde @param = {} @splat = [] # Traversal route. Public and writable. - @path = env["PATH_INFO"].dup + @path = URI.decode_www_form_component(env["PATH_INFO"].dup) # File serving path. Public and writable. @filepath = "/" # Encapsulates all rack variables. Should not be public. diff --git a/lib/hyde/server.rb b/lib/hyde/server.rb index 899d669..3b38d63 100644 --- a/lib/hyde/server.rb +++ b/lib/hyde/server.rb @@ -28,10 +28,9 @@ module Hyde "content-type": "text/html" } [headers, page] - end - }.each do |k, v| - @properties[k] = v unless @properties[k] - end + end, + "path" => "/" + }.each { |k, v| @properties[k] = v unless @properties[k] } end # Rack ingress point. diff --git a/test/Hyde_PatternMatching_Glob.rb b/test/Hyde_PatternMatching_Glob.rb index dada028..49f105c 100644 --- a/test/Hyde_PatternMatching_Glob.rb +++ b/test/Hyde_PatternMatching_Glob.rb @@ -69,6 +69,27 @@ class TestGlob < Test::Unit::TestCase puts("Testing: #{test}") assert_equal(result, unit.match?(test)) end + unit = Glob.new("**/*.php") + [ + "archive.php", true, + "assets/thing.js", false, + "assetsthing.js", false, + "parts/thing.php", true, + "partsthing.php", true, + ".php", true, + "parts/extra/test.php", true, + "archive.php", true, + "assets/thing.js", false, + "assetsthing.js", false, + "parts/thing.php", true, + "partsthing.php", true, + ".php", true, + "parts/extra/test.php", true, + "parts/extra/test.php/literally/anything/here", true + ].each_slice(2) do |test, result| + puts("Testing: #{test}") + assert_equal(result, unit.match?(test)) + end puts "Test group: char ranges" unit = Glob.new("/test/[9-z]+") [