forked from saucelabs/the-internet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
316 lines (254 loc) · 6.04 KB
/
server.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
require 'bundler/setup'
require 'sinatra'
require 'sinatra/flash'
require 'zurb-foundation'
require 'compass'
enable :sessions
configure :production do
require 'newrelic_rpm'
end
get '/' do
erb :index
end
get '/upload' do
erb :upload
end
post '/upload' do
file = params['myfile']
File.open('public/uploads/' + file[:filename], 'w') do |f|
f.write(file[:tempfile].read)
end
erb :uploaded, locals: { file: file }
end
get '/download' do
@file_list = Dir.glob("public/uploads/*.*").map { |f| f.split('/').last }
erb :download
end
get '/download/:filename' do |filename|
mime_type = get_mime_type_for(filename)
send_file "public/uploads/#{filename}",
:filename => filename,
:type => mime_type
end
get '/download/jqueryui/menu/:filename' do |filename|
mime_type = get_mime_type_for(filename)
send_file "public/uploads/jqueryui/menu/#{filename}",
:filename => filename,
:type => mime_type
end
get '/download_secure' do
protected!
@file_list = Dir.glob("public/uploads/*.*").map { |f| f.split('/').last }
erb :download_secure
end
get '/download_secure/:filename' do |filename|
protected!
mime_type = get_mime_type_for(filename)
send_file "public/uploads/#{filename}",
:filename => filename,
:type => mime_type
end
def get_mime_type_for(filename)
file_type = filename.split('.').last
case file_type
when 'csv'
'text/csv'
when 'jpg'
'image/jpeg'
when 'pdf'
'application/pdf'
when 'xls'
'application/vnd.ms-excel'
else
'application/octet-stream'
end
end
def load_frame_get_actions
frame_elements = %w(top bottom left right middle)
frame_elements.each do |element|
frame_path = "frame_#{element}".to_sym
get "/frame_#{element}" do
erb frame_path, :layout => false
end
end
end
load_frame_get_actions
get '/frames' do
erb :frames, :layout => false
end
get '/tinymce' do
erb :tinymce
end
get '/jqueryui/menu' do
erb :jqueryui_menu
end
get '/jqueryui' do
erb :jqueryui
end
get '/windows' do
erb :windows
end
get '/windows/new' do
erb :new_window, :layout => false
end
get '/dropdown' do
erb :dropdown
end
def random_notification_message
messages = [
'Action successful',
'Action unsuccesful, please try again'
]
messages[rand(2)]
end
get '/notification_message' do
flash[:notice] = random_notification_message
redirect '/notification_message_rendered'
end
get '/notification_message_rendered' do
erb :notification_message
end
get '/abtest' do
erb :abtest
end
get '/abtest_cookies' do
erb :abtest_cookies
end
get '/abtest_manual' do
erb :abtest_manual
end
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'admin']
end
end
get '/basic_auth' do
protected!
erb :basic_auth
end
get '/basic_auth/' do
protected!
erb :basic_auth
end
get '/status_codes' do
erb :status_codes
end
get '/status_codes/:status_code' do |status_code|
status status_code
@status_code = status_code
erb :status_code, :layout => true
end
get '/javascript_error' do
erb :javascript_error, :layout => false
end
get '/javascript_alerts' do
erb :javascript_alerts
end
get '/redirect' do
redirect '/status_codes'
end
get '/redirector' do
erb :redirector
end
get '/login' do
erb :login
end
post "/authenticate" do
username = 'tomsmith'
password = 'SuperSecretPassword!'
if username == params[:username]
if password == params[:password]
session[:username] = params[:username]
flash[:success] = 'You logged into a secure area!'
redirect '/secure'
else
flash[:error] = 'Your password is invalid!'
end
else
flash[:error] = 'Your username is invalid!'
end
redirect '/login'
end
get '/secure' do
unless session[:username]
flash[:error] = 'You must login to view the secure area!'
redirect '/login'
end
erb :secure
end
get '/logout' do
session[:username] = nil
flash[:success] = 'You logged out of the secure area!'
redirect "/login"
end
get '/dynamic_loading' do
erb :dynamic_loading
end
get '/dynamic_loading/1' do
erb :dynamic_loading_1
end
get '/dynamic_loading/2' do
erb :dynamic_loading_2
end
get '/tables' do
erb :tables
end
get '/geolocation' do
erb :geolocation
end
get '/large' do
erb :large
end
get '/large/:nested_level' do |nested_level|
@nested_level = nested_level.to_i
erb :large
end
get '/drag_and_drop' do
erb :drag_and_drop
end
get '/forgot_password' do
erb :forgot_password
end
set :email_username, ENV['SENDGRID_USERNAME'] || ''
set :email_password, ENV['SENDGRID_PASSWORD'] || ''
set :email_address, 'dhaeffner@gmail.com'
set :email_service, ENV['EMAIL_SERVICE'] || 'gmail.com'
set :email_domain, ENV['SENDGRID_DOMAIN'] || 'localhost.localdomain'
post '/forgot_password' do
require 'pony'
Pony.mail(
from: "no-reply@the-internet.herokuapp.com",
to: params[:email],
subject: "Forgot Password from the-internet",
body: "A forgot password retrieval was initiated from http://the-internet.herokuapp.com/forgot_password to #{params[:email]}.
If this were a real message, you would likely see a link or some relevant text that would help you retrieve a password.
If you want to test login, visit http://the-internet.herokuapp.com/login and use the following credentials:
username: tomsmith
password: SuperSecretPassword!",
via: 'smtp',
via_options: {
address: 'smtp.' + settings.email_service,
port: '587',
enable_starttls_auto: true,
user_name: settings.email_username,
password: settings.email_password,
authentication: :plain,
domain: settings.email_domain
})
redirect '/email_sent'
end
get '/email_sent' do
erb :email_sent
end
get '/checkboxes' do
erb :checkboxes
end
get '/hovers' do
erb :hovers
end