public
Created

bash shell script to serve an http request via inetd

  • Download Gist
inetd_www.bash
Shell
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.