Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

bash shell script to serve an http request via inetd

View inetd_www.bash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/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

Do you have anything similar for post requests?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.