Last active
August 29, 2015 13:58
-
-
Save Inndy/10439345 to your computer and use it in GitHub Desktop.
http server with gzip compression in shell script.
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/bash | |
###################### | |
# AUTHOR: Inndy # | |
# DATE: 2014/04/11 # | |
###################### | |
MIME_DEFAULT="text/html" | |
if [ -z $1 ] || [ -z $2 ]; then | |
echo "Usage: $0 <file> <port> [mime]" | |
echo " Default mime-type is '$MIME_DEFAULT'" | |
exit 1 | |
fi | |
file=$1 | |
port=$2 | |
mime=$3 | |
if [ ! -e $file ]; then | |
echo "File not exists (\"$file\")" | |
exit 2 | |
fi | |
if [ ! -z $(echo $port | sed -e 's/[0-9]//g') ]; then | |
echo "Port must be number" | |
exit 3 | |
fi | |
if [ $port -le 0 ] || [ $port -ge 65536 ]; then | |
echo "Port is not correct" | |
exit 4 | |
fi | |
if [ -z $mime ]; then | |
mime=$MIME_DEFAULT | |
fi | |
size=$(gzip -c $file | wc --bytes | tr -d '\n') | |
echo "Ctrl-C to stop server..." | |
until [ $? -eq 130 ]; do | |
(\ | |
echo -ne "HTTP/1.1 200 OK\r\n";\ | |
echo -ne "Connection: Close\r\n";\ | |
echo -ne "Content-Encoding: gzip\r\n";\ | |
echo -ne "Content-Type: $mime\r\n";\ | |
echo -ne "Content-Size: $size\r\n";\ | |
echo -ne "\r\n";\ | |
gzip -c $file;\ | |
echo -ne "\r\n0\r\n"\ | |
) | nc -lp $port | head -n 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment