Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
bash shell script to serve an http request via inetd
#!/bin/bash
#
# Shell script usable as web "server"
# Code mostly copied from web, my contribution was to add the sanitation.
#
###
### PARSE THE REQUEST
###
# first line is request
read request
# ignore the header
while /bin/true; do
read header
[ "$header" == $'\r' ] && break
done
# extract url, path, and query from request
url="${request#GET }"
url="${url% HTTP/*}"
path="${url%%\?*}"
query="${url#*\?}"
# make p_variables out of parameter string
for i in $(echo $query | sed 's/\&/\n/g') ; do
key=${i%%=*}
key=$(echo $key | sed 's/[^a-z_]//g') # sanitize, accept lowercase only + underscore
value=${i#*=}
value=$(echo $value | tr -d ';''\001'-'\011''\013''\014''\016'-'\037''\200'-'\377') # sanitize, remove non-printing chars and semicolon
eval "p_$key=$value"
done
###
### DO THE WORK
###
#
# there are plenty of extant examples for serving a file here
# google for "bash web server", etc.
#
exit 0

sarnobat commented Aug 7, 2014

Do you have anything similar for post requests?

aessam commented Sep 16, 2014

I assume you will have to parse the post request your self, it will be little bit tricky as you need to handle things like multipart.

tpham3783 commented Aug 25, 2017

@thanks,

this is nice, but dont use it in services on the internet. Use it only on a protected network!

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