Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 68 lines (58 sloc) 2.14 KB
#!/bin/bash
# quickserve - easy and convenient HTTP server that serves directory listings and files.
port=8080
fifo=`mktemp /tmp/XXXXXX`
output_buffer=`mktemp /tmp/XXXXXX`
rm -f "$fifo"
mkfifo "$fifo"
echo "Listening on $port" >&2
trap 'echo -e "\rGoodbye."; rm -f "$fifo"; rm -f "$output_buffer"; exit' 2
netcat_help=$(nc -h 2>&1)
while true; do
if [[ "$netcat_help" =~ 'GNU' ]]; then
nc -l -p "$port"
else
nc -l "localhost" "$port"
fi < "$fifo" |
head -n 1 |
cut -d' ' -f2 | (
read uri
echo "[$(date)] $uri" >&2
if [ -d "$uri" ]; then
escuri="$(echo "$uri" | sed -e "s/'/\&#039;/" | sed -e 's/>/\&gt;/' | sed -e 's/</\&lt;/')"
echo "<h2>Directory listing for $escuri</h2>" > $output_buffer
echo "<ul>" >> $output_buffer
if [[ "$escuri" != */ ]]; then
escuri="$escuri/"
fi
ls -a "$uri" |
tail -n +2 |
sed -e "s/'/\&#039;/" |
sed -e 's/>/\&gt;/' |
sed -e 's/</\&lt;/' |
sed -e "s!\(.*\)!<li><a href='$escuri\1'>\1</a></li>!" >> $output_buffer
echo "</ul>" >> $output_buffer
echo "HTTP/1.1 200 OK"
echo "Connection: close"
echo "Content-Type: text/html"
echo "Content-Length: $(stat $output_buffer | cut -d' ' -f8)"
echo
cat $output_buffer
elif [ -f "$uri" ]; then
mime="$(file --mime-type "$uri" | sed -e 's/^.*: \(.*\)/\1/')"
echo "HTTP/1.1 200 OK"
echo "Connection: close"
echo "Content-Type: $mime"
echo "Content-Length: $(stat "$uri" | cut -d' ' -f8)"
echo
cat "$uri"
else
echo "HTTP/1.1 404 NotFound"
echo "Connection: close"
echo "Content-Type: text/html"
echo "Content-Length: 19"
echo
echo "<h2>Not Found</h2>"
fi
) > "$fifo"
done