From 6dcf46f40a42f77763aba64939f2d2d495e7a995 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Mon, 17 Apr 2023 23:56:46 +0400 Subject: [PATCH] very functional prototype --- hyde.rb | 64 +++++++++++++++++++++++++----------------------- test/index.html | 13 ++++++++++ test_combined.rb | 32 +++++++++--------------- 3 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 test/index.html diff --git a/hyde.rb b/hyde.rb index 046ab50..75d4628 100644 --- a/hyde.rb +++ b/hyde.rb @@ -2,22 +2,30 @@ require 'mime-types' require 'webrick' module Hyde - # 404 text - def default404(filepath) - return " - - File not found - -

File not found

- #{filepath} -
-
- Hyde on WEBrick/#{WEBrick::VERSION} (Ruby/#{RUBY_VERSION}/#{RUBY_RELEASE_DATE}) -
- - " + class Server < WEBrick::HTTPServer + def initialize(config={},&setup) + super(config) + @hyde_pathspec = Hyde::Pathspec.new "/", &setup + self.mount_proc '/' do |req,res| + @hyde_pathspec.match(Hyde::Context.new(req.path, req, res)) + end + end + end + module ErrorPages + # 404 text + def error404(request, filepath) + request.response.status = 404 + if request.handles.include? 404 + request.response.body = request.handles[404].call( + 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" + end + request.response["Content-Type"] = 'text/html' + end + module_function :error404 end - module_function :default404 # Interchangeable pattern matching module PatternMatching def prep_path(path,safe_regex: true) @@ -28,12 +36,15 @@ module Hyde end def match?(path) raise Exception, "Not permitted" if @lock_methods + # behvaiour used by "index" method 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] else + # algorithm to match path segments until no more left in @path @path.split("/").filter { |x| x != "" } .each_with_index { |x,i| return false if x != split_path[i] @@ -43,11 +54,12 @@ module Hyde end def normalize_input(path) raise Exception, "Not permitted" if @lock_methods + # remove duplicate slashes and trim edge slashes (path.split "/").filter { |x| x != "" }.join("/") end def normalize(path) raise Exception, "Not permitted" if @lock_methods - # remove multiple slashes in one spot and trim edge slashes + # remove duplicate slashes and trim edge slashes path = normalize_input(path) # globbing behaviour simulated with regexp if path.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_combined.rb b/test_combined.rb index 04bab29..fb7c073 100644 --- a/test_combined.rb +++ b/test_combined.rb @@ -1,26 +1,18 @@ -require 'webrick' require_relative 'hyde' -server = WEBrick::HTTPServer.new :Port => 8000 -trap 'INT' do server.shutdown end -server.mount_proc '/' do |req,res| - Hyde::Pathspec.new '/' do - serve "index.html" do |ctx| - ctx.response.body = "This is a test of webrick+hyde combination -If you're seeing this, this means it's a success" - ctx.response["Content-Type"] = "text/plain" +server = Hyde::Server.new Port: 8000 do + root "/home/yessiest/hyde/test/" + serve "index.html" + path "about" do + serve "webrick" do |ctx| + ctx.response.body = "WEBrick is a modular http server stack" + ctx.response['Content-Type'] = "text/plain" end - path "about" do - serve "webrick" do |ctx| - ctx.response.body = "WEBrick is weird and pretty undocumented" - ctx.response["Content-Type"] = "text/plain" - end - serve "hyde" do |ctx| - ctx.response.body = "Hyde was born because i thought Sinatra is cool, but nested paths are even cooler" - ctx.response["Content-Type"] = "text/plain" - end + serve "hyde" do |ctx| + 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 - end.match(Hyde::Context.new(req.path, req, res)) + end end -server.start +server.start