Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| #!/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/'/\'/" | sed -e 's/>/\>/' | sed -e 's/</\</')" | |
| 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/'/\'/" | | |
| sed -e 's/>/\>/' | | |
| sed -e 's/</\</' | | |
| 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 |