This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
180 lines (157 loc) · 3.9 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
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
# frozen_string_literal: true
require "bundler"
require "bundler/setup"
require "sinatra"
require "json"
SAMPLE_TEXT = <<~HEREDOC
Stand up for what you believe in, even if it means standing alone.
- Andy Biersack
HEREDOC
SAMPLE_XML = <<~HEREDOC
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<message>
<from>Alice</from>
<to>Bob</to>
<body>Hello</body>
</message>
HEREDOC
SAMPLE_HTML = <<~HEREDOC
<!doctype html>
<html lang="en">
<head><meta charset="utf-8" /><title>Hi</title></head>
<body><p>Hello World !</p></body>
</html>
HEREDOC
SAMPLE_JS = <<~HEREDOC
function changeTitle() { document.querySelector("title").innerText = "Plop" };
setTimeout(changeTitle, 2000);
HEREDOC
SAMPLE_HTML_JS_AD = <<~HEREDOC
<!doctype html>
<html lang="en">
<head><meta charset="utf-8" /><title>Hi</title></head>
<script language="javascript" src="/s_code.js"></script>
<body><p>Hello World !</p></body>
</html>
HEREDOC
SAMPLE_JSON = {"life" => 42, "foo" => "bar", "false" => true, "pi" => 13.37}.to_json
KNOWN_HTTP_CODES = {
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
207 => "Multi-Status",
210 => "Content Different",
226 => "IM Used",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Moved Temporarily",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
310 => "Too many Redirects",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authenticat,ion Required",
408 => "Request Time-out",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested range unsatisfiable",
417 => "Expectation failed",
418 => "I’m a teapot",
421 => "Bad mapping / Misdirected Reque",
422 => "Unprocessable entity",
423 => "Locked",
424 => "Method failure",
425 => "Unordered Collection",
426 => "Upgrade Required",
428 => "Precondition Required",
429 => "Too Many Requests",
431 => "Request Header Fields Too La",
449 => "Retry With",
450 => "Blocked by Windows Parental Controls",
451 => "Unavailable For Legal Reasons",
456 => "Unrecoverable Error",
499 => "client has closed connection",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway ou Proxy Error",
503 => "Service Unavailable",
504 => "Gateway Time-out",
505 => "HTTP Version not supported",
506 => "Variant also negociate",
507 => "Insufficient storage",
508 => "Loop detected",
509 => "Bandwidth Limit Exceeded",
510 => "Not extended",
511 => "Network authentication required",
520 => "Web server is returning an unknown error",
}.freeze
get "/" do
redirect to("/html")
end
get "/code/:http_code" do
code = params["http_code"].to_i
code = KNOWN_HTTP_CODES.key?(code) ? code : 200
halt code, KNOWN_HTTP_CODES[code.to_i]
end
get "/json" do
content_type :json
SAMPLE_JSON
end
get "/text" do
content_type :text
SAMPLE_TEXT
end
get "/xml" do
content_type :xml
SAMPLE_XML
end
get "/html" do
content_type :html
SAMPLE_HTML
end
get "/js" do
content_type :js
SAMPLE_JS
end
get "/s_code.js" do
content_type :js
SAMPLE_JS
end
get "/html_js_ad" do
content_type :html
SAMPLE_HTML_JS_AD
end
get "/slow" do
sleep 10
content_type :text
"Hello, tired!"
end
get "/redirection/infinite" do
redirect to("/redirection/infinite")
end
get "/redirection/temporary" do
redirect to("/html"), 301
end
get "/redirection/local" do
redirect to("/html")
end
get "/redirection/other_domain" do
redirect "https://www.perdu.com"
end