A.)
Some context:
- Our goal here is to match up to two network connections, so that anything we receive on one is forwarded to the other.
- netcat is a "networking swiss army knife", it's a tool for making network connections
- HTTP and bash pipelines both happen to be "line oriented", meaning they operate one line at a time. Exploiting this happy coincedence is what makes this work
- httpbin.org is a metanym for any website you need.
So:
echo "" > /tmp/buffer
Initialize our buffer to an empty state tail -f /tmp/buffer
Read each line in the buffer, one at a time, and feed them to the next command. Do this forever (-f). netcat httpbin.org 80
Connect to the website httpbin.org. Transmit each line from our buffer to the website. netcat -lp 8080 > /tmp/buffer
Accept a connection. Transmit any data we receive from httpbin.org to this client; write any data we receive from the client to the buffer, so that it can be transmitted to httpbin.org.B.) I don't think there is one! I've never needed to do this again in the, gosh, seven intervening years. Normally bash pipelines operate like a bucket brigade, moving data unidirectionally through various processing steps, and usually dumping it into a file at the end.
They aren't normally for creating long-lived programs that manage bidirectional flows of data. Which is why writing a web server in bash is a fun challenge.