Last active
June 6, 2019 22:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using
printf
instead ofecho
for portability.Tested in an Alpine docker container.