-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
98 lines (78 loc) · 2.18 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require "sinatra"
require "httparty"
require "colorize"
if ENV["FORWARD_TO"].nil?
raise StandardError, "FORWARD_TO enviroment variable must be specified."
else
puts
puts "Requests will be forwarded to #{ENV["FORWARD_TO"]}"
if ENV["FORWARD_HEADERS"]
puts "Extra headers to forward: #{ENV["FORWARD_HEADERS"]}"
end
puts
end
class App < Sinatra::Base
get "/" do
"Hello. This is RequestSink!"
end
def set_target_url
@target_url = [ENV["FORWARD_TO"], request.path_info].join
end
def set_headers
@headers = {
'Content-Type' => request.env["CONTENT_TYPE"]
}
extra_headers = ENV["FORWARD_HEADERS"].to_s.split(' ')
extra_headers.each do |key|
@headers[key] = request.env["HTTP_#{key}"]
end
end
def forward(method, body = nil)
HTTParty.send(
method,
@target_url,
body: body,
headers: @headers
)
end
ENV["GET_PATHS"]&.split(' ')&.each do |path|
get path do
set_target_url
set_headers
resp = forward "get"
status resp.code
content_type resp.content_type
puts <<~LOG
#{request.user_agent.italic.gray}
#{request.request_method.bold} #{@target_url}
#{resp.code.to_s.yellow}
LOG
if resp.headers["www-authenticate"]
response.headers["Www-Authenticate"] = Array(resp.headers["www-authenticate"]).first
end
resp.body if resp.code < 300
end
end
%w(post put patch delete).each do |method|
send method, "/*" do
set_target_url
set_headers
request_body = request.body.read
resp = forward method, request_body
status resp.code
puts <<~LOG
#{request.user_agent.italic.gray}
#{request.request_method.bold} #{@target_url.italic}
#{@headers.to_s.blue}
#{request_body}
#{resp.code.to_s.yellow}
LOG
if resp.headers["www-authenticate"]
response.headers["Www-Authenticate"] = Array(resp.headers["www-authenticate"]).first
end
# If resp succeeded return the body too
# otherwise hide the body, to not leak error backtraces or similar sensitive content.
resp.body if resp.code < 300
end
end
end