Skip to content

Instantly share code, notes, and snippets.

@matheusb-comp
Last active June 6, 2019 22:18
Show Gist options
  • Save matheusb-comp/53de330e909a6473169c9e4344c381b9 to your computer and use it in GitHub Desktop.
Save matheusb-comp/53de330e909a6473169c9e4344c381b9 to your computer and use it in GitHub Desktop.
Simple server that responds an HTTP 204 No Content to anything received
#!/bin/sh
# Command line variables
HOST=${1:-127.0.0.1}
PORT=${2:-4422}
# Test PORT
nc -z -w 5 127.0.0.1 ${PORT}
if [ $? -eq 0 ]; then
echo "Port ${PORT} in use, choose a different one"
exit 1
fi
# Minimal server that always responds 204 No Content
while :; do
DATE=$(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)
RESPONSE="HTTP/1.1 204 No Content\r\nDate: ${DATE}\r\n\r\n"
printf %b "${RESPONSE}" | nc -vl -s ${HOST} -p ${PORT}
# Stop the server on errors
RESULT=$?; if [ $RESULT -ne 0 ]; then exit $RESULT; fi
# Print the sent response
printf %b "\n\nResponse:\n${RESPONSE}"
done
@matheusb-comp
Copy link
Author

matheusb-comp commented Jun 5, 2019

Using printf instead of echo for portability.
Tested in an Alpine docker container.

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