-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.awk
65 lines (54 loc) · 1.55 KB
/
http.awk
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
BEGIN {
FILE_DIRECTORY = "/home/swiltink/httpawk/public/";
RS = ORS = "\r\n";
HttpService = "/inet/tcp/80/0/0";
while (1) {
print "handling request";
while ((HttpService |& getline line) > 0 ) {
split(line, parts, " ");
if (parts[1] == "GET") {
path = parts[2];
path = urlDecode(path);
path = substr(path, 2);
fullpath = FILE_DIRECTORY path;
fullpath = trim(fullpath);
"realpath " fullpath | getline realpath;
realpath = trim(realpath);
if (realpath != fullpath) {
print "not a real path: " fullpath;
print "HTTP/1.0 404 FILENOTFOUND" |& HttpService;
break;
}
if (system("test -f " fullpath) != 0) {
print "file not found: " fullpath;
print "HTTP/1.0 404 FILENOTFOUND" |& HttpService;
break;
}
totalfile = "";
while ((getline fileline < fullpath) > 0) {
totalfile = totalfile fileline;
}
close(fullpath);
Len = length(totalfile) + length(ORS);
print "HTTP/1.0 200 OK " |& HttpService;
print "Content-Length: " Len ORS |& HttpService;
print totalfile ORS |& HttpService;
}
}
close(HttpService);
}
}
function urlDecode(url) {
for (i = 0x20; i < 0x40; ++i) {
repl = sprintf("%c", i);
if ((repl == "&") || (repl == "\\")) {
repl = "\\" repl;
}
url = gensub(sprintf("%%%02X", i), repl, "g", url);
url = gensub(sprintf("%%%02X", i), repl, "g", url);
}
return url;
}
function ltrim(s) {sub(/^[ \t\r\n]+/, "", s); return s;}
function rtrim(s) {sub(/[ \t\r\n]+$/, "", s); return s;}
function trim(s) {return rtrim(ltrim(s));}