-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_content_setup.sh
339 lines (278 loc) · 7.97 KB
/
server_content_setup.sh
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/bash
SERVER_PATH=tests/www
SERVER_LOCALHOST_PATH=$SERVER_PATH/localhost
PYTHON_INTERPRETER=python3
# &> is not portable, and it might be deprecated
if ! type python3 > /dev/null 2>&1
then
PYTHON_INTERPRETER=python
fi
if [ -d $SERVER_LOCALHOST_PATH/media ]
then
rm -rf $SERVER_LOCALHOST_PATH/media
fi
PYTHON_INTERPRETER_PATH=$(command -v $PYTHON_INTERPRETER)
PERL_INTERPRETER_PATH=$(command -v perl)
mkdir -p $SERVER_LOCALHOST_PATH
mkdir -p $SERVER_LOCALHOST_PATH/gallery
mkdir -p $SERVER_LOCALHOST_PATH/media
mkdir -p $SERVER_LOCALHOST_PATH/cgi-bin
cat << EOF > $SERVER_LOCALHOST_PATH/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>INDEX</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<h1>INDEX</h1>
</body>
</html>
EOF
sed s/INDEX/GALLERY/g $SERVER_LOCALHOST_PATH/index.html \
> $SERVER_LOCALHOST_PATH/gallery/gallery.html
sed s/INDEX/NOT_FOUND/g $SERVER_LOCALHOST_PATH/index.html \
> $SERVER_PATH/404.html
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/reply.py
import sys
import os
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
print("Content-Type: text/plain; charset=utf-8")
print("")
print(sys.stdin.read())
for k, v in os.environ.items():
print(k, v)
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/reply.py
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/redir.py
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
print("Location: http://localhost:8080/gallery")
print("")
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/redir.py
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/local_redir.py
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
print("Location: /index.html")
print("")
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/local_redir.py
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/query.pl
use strict;
use warnings;
use CGI;
# Inserting '\' before dollar sign to prevent variable substitution from bash
my \$cgi = CGI->new;
# Array of query keys
my @q_keys;
# Subroutine to add an HTML paragraph
sub addParagraph {
print "<p>";
# Extracts the first item off the list of arguments and prints it
print shift;
print "</p>";
}
# Prints Content-Type: text/html
print \$cgi->header;
# Opens html and body tags and sets the argument as title
print \$cgi->start_html("Query Parameters");
# Inserts query keys into array
@q_keys = \$cgi->param();
# Checks if array is empty
if (@q_keys == 0) {
addParagraph("Include query parameters in the url.");
addParagraph("Example: localhost:9000/query.pl?hola=mundo&hello=world");
}
else {
my \$key;
my \$value;
foreach \$key (@q_keys) {
\$value = \$cgi->param(\$key);
addParagraph("\$key = \$value");
}
}
# Closes html and body tags
print \$cgi->end_html;
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/query.pl
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/form.pl
use strict;
use warnings;
use CGI;
# Inserting '\' before dollar sign to prevent variable substitution from bash
my \$cgi = CGI->new;
# Array of form keys
my @f_keys;
# Subroutine to add an HTML paragraph
sub addParagraph {
print "<p>";
# Extracts the first item off the list of arguments and prints it
print shift;
print "</p>";
}
# Prints Content-Type: text/html
print \$cgi->header;
# Opens html and body tags and sets the argument as title
print \$cgi->start_html("Form Data");
if (\$ENV{REQUEST_METHOD} eq 'GET')
{
# Non-interpolating heredoc
print <<'END_FORM'
<form action="/form.pl" method="post">
<div style="padding: 20px 0">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
</div>
<div style="padding: 20px 0">
<label for="mail">Email:</label>
<input type="email" id="mail" name="email">
</div>
<div style="padding: 20px 0">
<label for="msg">Message:</label>
<textarea id="msg" name="message" style="vertical-align: top"></textarea>
</div>
<div style="padding: 20px 0">
<button type="submit">Submit</button>
</div>
</form>
END_FORM
}
else
{
# Inserts form keys into array
@f_keys = \$cgi->param();
# Checks if array is empty
if (@f_keys == 0) {
addParagraph("Received an empty form.");
}
else {
my \$key;
my \$value;
foreach \$key (@f_keys) {
\$value = \$cgi->param(\$key);
addParagraph("\$key = \$value");
}
}
}
# Closes html and body tags
print \$cgi->end_html;
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/form.pl
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/cookie.pl
use CGI qw/:standard/;
use CGI::Cookie;
# Create new cookies and send them
\$cookie1 = CGI::Cookie->new(-name=>'HIPPIE_COOKIE',-value=>42);
print header(-cookie=>\$cookie1);
# fetch existing cookies
%cookies = CGI::Cookie->fetch;
\$hippieCookie = \$cookies{'HIPPIE_COOKIE'};
if (\$hippieCookie)
{
\$hippieCookieVal = \$cookies{'HIPPIE_COOKIE'}->value;
print "Found hippie cookie that says -> \$hippieCookieVal";
}
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/cookie.pl
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/cookiedos.pl
use CGI qw/:standard/;
use CGI::Cookie;
# Create new cookies and send them
\$cookie1 = CGI::Cookie->new(-name=>'LOGIN_COOKIE',-value=>666,-expires=>'+3m');
print header(-cookie=>\$cookie1);
# fetch existing cookies
%cookies = CGI::Cookie->fetch;
\$hippieCookie = \$cookies{'LOGIN_COOKIE'};
if (\$hippieCookie)
{
\$hippieCookieVal = \$cookies{'LOGIN_COOKIE'}->value;
print "Found login cookie that says -> \$hippieCookieVal";
}
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/cookiedos.pl
cat << EOF > $SERVER_LOCALHOST_PATH/cgi-bin/login_page.pl
use CGI qw/:standard/;
use CGI::Cookie;
print "Content-Type: text/html\n\n";
# Note there is a newline between
# this header and Data
# Simple HTML code follows
print "<html> <head>\n";
print "<title>Login page</title>";
print "<link rel='stylesheet' href='./style.css'>";
print "</head>\n";
print "<body>\n";
# fetch existing cookies
%cookies = CGI::Cookie->fetch;
\$hippieCookie = \$cookies{'LOGIN_COOKIE'};
if (\$hippieCookie)
{
print "<h1>Cookie seteada!</h1>\n";
\$hippieCookieVal = \$cookies{'LOGIN_COOKIE'}->value;
print "<h2>Login cookie contiene -> \$hippieCookieVal</h2>";
print "<div align='center'><img src='https://media.istockphoto.com/vectors/good-job-vector-id1166386955?k=20&m=1166386955&s=612x612&w=0&h=YDNVQ-n0QRR2NMu9Rr99MCus2xQhNnlFf7niG3cD4Tc='></div>"
}
else
{
print "<h1>Cookie no seteada o expirada</h1>\n";
}
print "</body> </html>\n";
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cgi-bin/login_page.pl
FELIZ_JUEVES=$SERVER_LOCALHOST_PATH/feliz-jueves.jpg
if [ -f "$FELIZ_JUEVES" ]; then
echo "feliz-jueves exists."
else
curl -s -o $FELIZ_JUEVES https://pbs.twimg.com/media/FdzwIuIWYAEkdoh?format=jpg&name=medium
fi
cat << EOF > $SERVER_LOCALHOST_PATH/style.css
body {
background-image: url("feliz-jueves.jpg");
background-color: #cccccc;
}
h1 {
color: rgb(6, 6, 9);
background-color: blueviolet;
text-align: center;
}
h2 {
color: rgb(6, 6, 9);
background-color: blueviolet;
text-align: center;
}
EOF
chmod u+x $SERVER_LOCALHOST_PATH/style.css
cat << EOF > $SERVER_LOCALHOST_PATH/cookie.html
<html><head>
<meta content="text/html; charset=windows-1252">
<title>Hello, world!</title>
<body>
<h1>Lanzador de cookies</h1>
<p>
<a href="http://localhost:9000/cookie.pl">
<button>primera cookie de muestra</button>
</a>
</p>
<p>
<a href="http://localhost:9000/cookiedos.pl">
<button>segunda cookie de login</button>
</a>
</p>
</body></html>
EOF
chmod u+x $SERVER_LOCALHOST_PATH/cookie.html
sed s:WEBSERV_PATH:$PWD:g tests/example_config.json \
> tests/tmp_config.json
# sed -i is not portable
sed s:PYTHON_PATH:$PYTHON_INTERPRETER_PATH:g tests/tmp_config.json \
> tests/inter_config.json
# Replace content of tmp_config with that of inter_config
cp -f tests/inter_config.json tests/tmp_config.json
rm tests/inter_config.json
sed s:PERL_PATH:$PERL_INTERPRETER_PATH:g tests/tmp_config.json \
> tests/inter_config.json
cp -f tests/inter_config.json tests/tmp_config.json
rm tests/inter_config.json