From dd745ca1233cc1ca93ebdf3c04af21cb4a23b6ec Mon Sep 17 00:00:00 2001 From: Yessiest Date: Tue, 7 May 2024 19:31:31 +0400 Subject: [PATCH] Fixed jumps and create an example of using them --- examples/jumps.ru | 49 +++++++++++++++++++++++++++++++ lib/landline/dsl/methods_probe.rb | 6 ++-- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 examples/jumps.ru diff --git a/examples/jumps.ru b/examples/jumps.ru new file mode 100644 index 0000000..b210f2a --- /dev/null +++ b/examples/jumps.ru @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'landline' + +$unlocked = false + +class Application < Landline::App + get "/locked" do + header "content-type", "text/plain" + "Uh oh, looks like the endpoint is locked!" + end + + get "/important" do + redirect "/locked" unless $unlocked + "Important things here" + end + + path "/secret" do + preprocess do + header "content-type", "text/plain" + redirect "/locked" unless $unlocked + end + + get "/bin" do + "Empty" + end + + get "/secret" do + "Very secret data" + end + + get "/jump" do + @allowed = true + jump "/jump" + end + end + + get "/jump" do + redirect "/locked" unless @allowed + "Congratulations, you've just experienced a cross-handler jump" + end + + get "/unlock" do + $unlocked = true + "Endpoints unlocked" + end +end + +run Application.new diff --git a/lib/landline/dsl/methods_probe.rb b/lib/landline/dsl/methods_probe.rb index a13050a..9b77794 100644 --- a/lib/landline/dsl/methods_probe.rb +++ b/lib/landline/dsl/methods_probe.rb @@ -43,21 +43,21 @@ module Landline # @param path [String] def jump(path) @origin.request.path = path - throw(:break, [307, { "x-internal-jump": true }, []]) + throw(:finish, [307, { "x-internal-jump": true }, []]) end # (in Landline::Probe context) # Do clientside request redirection via 302 code # @param path [String] def redirect(path) - throw(:break, [302, { "location": path }, []]) + throw(:finish, [302, { "location": path }, []]) end # (in Landline::Probe context) # Do clientside request redirection via 307 code # @param path [String] def redirect_with_method(path) - throw(:break, [307, { "location": path }, []]) + throw(:finish, [307, { "location": path }, []]) end alias code status