Skip to content

Instantly share code, notes, and snippets.

@tyingq
Created January 4, 2022 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyingq/4e568425e2e68e6390f3105e588787c6 to your computer and use it in GitHub Desktop.
Save tyingq/4e568425e2e68e6390f3105e588787c6 to your computer and use it in GitHub Desktop.
janky gawk webserver
@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)
}
@tyingq
Copy link
Author

tyingq commented Jan 4, 2022

Note: This probably has serious security issues...don't use it for anything real.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment