Fixed jumps and create an example of using them
This commit is contained in:
parent
8c54fa3bc6
commit
dd745ca123
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue