Created
January 4, 2022 16:01
-
-
Save tyingq/4e568425e2e68e6390f3105e588787c6 to your computer and use it in GitHub Desktop.
janky gawk webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@load "filefuncs" | |
@load "readfile" | |
BEGIN { | |
if (ARGV[1] != "") { | |
if (chdir(ARGV[1])) { | |
print "Failed to chdir to " ARGV[1] | |
exit | |
} | |
ARGC = 1 | |
} | |
RS = ORS = "\r\n" | |
while (1) { | |
S = "/inet/tcp/8080/0/0" | |
while ((S |& getline l) > 0) { | |
split(l, f, " ") | |
if (f[1] == "GET") { | |
p = substr(f[2], 2) | |
} | |
if (p == "") { | |
p = "index.html" | |
} | |
stat(p, s) | |
if (cf(p) && s["type"] == "file") { | |
m = mt(p) | |
o = readfile(p) | |
send(200, "OK", o, m, s["size"]) | |
break | |
} | |
n = "<html>Not Found</html>" | |
send(404, "Not Found", n, "text/html" RS, length(n)) | |
break | |
} | |
} | |
} | |
function cf(x) | |
{ | |
split(x, y, "/") | |
for (z in y) { | |
print "FOUND " y[z] | |
if (y[z] == "..") { | |
return 0 | |
} | |
} | |
return 1 | |
} | |
function mt(f) | |
{ | |
c = "file -b --mime-type " f | |
r = "" | |
while ((c | getline z) > 0) { | |
r = r z | |
} | |
close(c) | |
return r | |
} | |
function send(s, e, d, t, b) | |
{ | |
print("HTTP/1.0 " s " " e) |& S | |
print("Content-Length: " b) |& S | |
print("Content-Type: " t) |& S | |
print(d) |& S | |
close(S) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This probably has serious security issues...don't use it for anything real.