Fixed jumps and create an example of using them

This commit is contained in:
Yessiest 2024-05-07 19:31:31 +04:00
parent 8c54fa3bc6
commit dd745ca123
2 changed files with 52 additions and 3 deletions

49
examples/jumps.ru Normal file
View File

@ -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

View File

@ -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