-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathapplication.rb
More file actions
203 lines (158 loc) · 4.32 KB
/
application.rb
File metadata and controls
203 lines (158 loc) · 4.32 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# frozen_string_literal: true
require "sinatra/base"
module Ferrum
class Application < Sinatra::Base
configure { set :protection, except: :frame_options }
FERRUM_APP = File.dirname(__FILE__)
FERRUM_VIEWS = "#{FERRUM_APP}/views".freeze
FERRUM_PUBLIC = "#{FERRUM_APP}/public".freeze
set :root, FERRUM_APP
set :static, true
set :raise_errors, true
set :show_exceptions, false
helpers do
def requires_credentials(login, password)
return if authorized?(login, password)
headers["WWW-Authenticate"] = %(Basic realm="Restricted Area")
halt(401, "Not authorized\n")
end
def authorized?(login, password)
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && (@auth.credentials == [login, password])
end
end
get "/test.js" do
content_type :js
File.read("#{FERRUM_PUBLIC}/test.js")
end
get "/jquery.min.js" do
content_type :js
File.read("#{FERRUM_PUBLIC}/jquery-3.7.1.min.js")
end
get "/jquery-ui.min.js" do
content_type :js
File.read("#{FERRUM_PUBLIC}/jquery-ui-1.13.2.min.js")
end
get "/add_style_tag.css" do
content_type :css
File.read("#{FERRUM_PUBLIC}/add_style_tag.css")
end
get "/" do
response.set_cookie("cookie", value: "root cookie", domain: request.host, path: request.path)
%(<link rel="icon" href="data:,">Hello world! <a href="with_html">Relative</a>)
end
get "/set_cookie" do
set_stealth_cookie
end
get "/get_cookie" do
request.cookies["stealth"]
end
get "/custom/get_cookie" do
request.cookies["stealth"]
end
get "/attachment.pdf" do
send_file File.join(FERRUM_APP, "static", "attachment.pdf"),
disposition: :attachment,
filename: "attachment.pdf"
end
get "/foo" do
"Another World"
end
get "/landed" do
"You landed"
end
post "/landed" do
"You post landed: #{params.dig(:form, 'data')}"
end
get "/host" do
"Current host is #{request.scheme}://#{request.host}:#{request.port}"
end
get "/unicode" do
File.read("#{FERRUM_VIEWS}/unicode.html")
end
get "/csp" do
headers["Content-Security-Policy"] = %(default-src "self")
"csp content"
end
get "/show_cookies/set_cookie_slow" do
sleep 1
set_stealth_cookie
end
get "/redirect" do
redirect "/redirect_again"
end
get "/redirect_again" do
redirect "/landed"
end
get "/form/get" do
%(<pre id="results">#{params[:form].to_yaml}</pre>)
end
post "/relative" do
%(<pre id="results">#{params[:form].to_yaml}</pre>)
end
get "/favicon.ico" do
nil
end
post "/redirect" do
redirect "/redirect_again"
end
get "/apple-touch-icon-precomposed.png" do
halt(404)
end
get "/unexist.png" do
halt(404)
end
get "/server_error" do
halt(500)
end
get "/status/:status" do
status params["status"]
render_view "with_different_resources"
end
get "/redirect_to_headers" do
redirect "/headers"
end
get "/slow" do
sleep 0.2
"slow page"
end
get "/really_slow" do
sleep 3
"really slow page"
end
get "/basic_auth" do
requires_credentials("login", "pass")
render_view :basic_auth
end
post "/post_basic_auth" do
requires_credentials("login", "pass")
"Authorized POST request"
end
get "/cacheable" do
cache_control :public, max_age: 60
etag "deadbeef"
%(<link rel="icon" href="data:,">Cacheable request <a href='/cacheable'>click me</a>)
end
get "/arbitrary_path/:status/:remaining_path" do
status params["status"].to_i
params["remaining_path"]
end
post "/ping" do
# Sleeping to simulate a server that does not send a response to PING requests
sleep 5
halt(204)
end
get "/:view" do |view|
render_view view
end
protected
def render_view(view)
erb File.read("#{FERRUM_VIEWS}/#{view}.erb")
end
def set_stealth_cookie
cookie_value = "test_cookie"
response.set_cookie("stealth", cookie_value)
"Cookie set to #{cookie_value}"
end
end
end