I asked ChatGPT to explain that and "prettify" it, for anyone interested (everything below from ChatGPT):
This is an AWK script that serves HTTP requests by listening on port 8080.
It defines several functions:
1. `send` function takes in status code, status message, content, content type, and content length and sends an HTTP response with the provided information.
2. `cf` function checks if a path contains `..` and returns 0 if it does, otherwise returns 1.
3. `mt` function determines the MIME type of a file using the `file` command.
The script sets the record separator RS and output record separator ORS to \r\n which is the line ending used in HTTP.
It enters an infinite loop listening for incoming connections on port 8080. When a connection is established, it reads the HTTP request line by line using `getline` and processes the `GET` request. If no path is provided, the script serves `index.html`. It checks if the requested path is safe using the `cf` function, and if it is a file, reads the file and sends an HTTP response using the `send` function. If the path is not valid or the file is not found, it sends a "404 Not Found" response.
gawk '@load "filefuncs"
@load "readfile"
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)
}
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
}
BEGIN {
# Change to the specified directory
if (ARGV[1] != "") {
if (chdir(ARGV[1])) {
print "Failed to chdir to " ARGV[1]
exit
}
ARGC = 1
}
# Set the record separator and output record separator
RS = ORS = "\r\n"
# Listen for incoming connections
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
}
}
}'
I added some comments to explain what each section of the code does. Let me know if you have any questions!
Looks like ChatGPT got it right with the description. Perhaps obvious, but the ".." check is to stop path traversal. Also, if you pass an additional arg, it takes that as a directory for the script to serve over http, defaults to CWD. The rest seems like enough explanation.
You can also pretty print gawk source with -o[filename], using - as the filename to print to stdout, so you can just run the oneliner version, but lead with "gawk -o- " and it will print the pretty version.
Edit: Also, the performance is terrible. Gawk's listen sockets suck because you get no granular control over listen/accept. The socat based one I replied to is probably much better.
https://github.com/crossbowerbt/awk-webserver
I know, just because you can doesn't mean you should.