#!/usr/bin/gawk -f function say(content) { printf "%s", content |& Service } function synthetic(status, response, msg) { say("HTTP/1.1 " status " " response "\n"); say("Connection: close\n"); say("\n"); say(msg); } function reply(url) { say("HTTP/1.1 200 OK\n"); say("Connection: close"); say(cache[url] "\n"); } function get(url) { print "GET " url " HTTP/1.1\n" |& Backend print "Connection: close\n\n" |& Backend Backend |& getline if ($2 != "200") { synthetic($2, "Bad backend", "Bad backend? Got: " $0) } else { cache[url] = "" while ((Backend |& getline c)>0) cache[url] = cache[url] "\n" c reply(url) } } function handle_request() { Service |& getline url=$2 request=$1 if (request != "GET") { synthetic(413,"Only support GET","We only like GET"); return; } if (cache[url]) { reply(url); print "Cache hit: " url "\n"; } else { print "Cache miss: " url "\n"; get(url); } } BEGIN { LINT=1 port = "8080" backend = "kly.no" Service = "/inet/tcp/" port "/0/0" Backend = "/inet/tcp/0/" backend "/80" do { handle_request() close(Service) close(Backend) } while(1) }