From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <015d01c22ed3$fcc596e0$6501a8c0@KIKE> From: "matt" To: <9fans@cse.psu.edu> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_015A_01C22EDC.5D6BE4B0" Subject: [9fans] httpd utilities / scripts Date: Fri, 19 Jul 2002 04:25:51 +0100 Topicbox-Message-UUID: d1bbe802-eaca-11e9-9e20-41e7f4b1d025 This is a multi-part message in MIME format. ------=_NextPart_000_015A_01C22EDC.5D6BE4B0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit I've been playing with httpd this evening and done what I think is a useful script. What it does is take the command line options and HTTP headers and put them all in environment variables. I did this to facilitate shell scripted web pages. It works by adding : rfork E /bin/ip/httpd/populate_environment.rc $* to the top of any web serving scripts and then the HTTP Headers etc. can be accessed through /env the example script uses html_encode.sed, which is a very low quality cut and pasted html entity encoder (supplied) I'm also including url_encode.awk which is nothing to do with this but useful all the same For as long as I am on this IP address you can see it in action at http://pc1-nott2-3-cust35.not.cable.ntl.com/magic/http_info.rc suggestions / criticisms welcome Matt ------=_NextPart_000_015A_01C22EDC.5D6BE4B0 Content-Type: multipart/mixed; boundary="upas-hfrzxfyxbvubwzdqnztdapxqaa" Content-Disposition: inline This is a multi-part message in MIME format. --upas-hfrzxfyxbvubwzdqnztdapxqaa Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit The following attachment had content that we can't prove to be harmless. To avoid possible automatic execution, we changed the content headers. The original header was: Content-Type: application/octet-stream; name="html_encode.sed" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="html_encode.sed" --upas-hfrzxfyxbvubwzdqnztdapxqaa Content-Type: application/octet-stream Content-Disposition: attachment; filename="html_encode.sed.suspect" Content-Transfer-Encoding: quoted-printable s/&/&/g=0A= s/"/"/g=0A= s//\>/g=0A= s/=E2=80=9E/\ä/g=0A= s/=C5=BD/\Ä/g=0A= s/=E2=80=9D/\ö/g=0A= s/=E2=84=A2/\Ö/g=0A= s/=C2=81/\ü/g=0A= s/=C5=A1/\Ü/g=0A= s/=C3=A1/\ß/g=0A= --upas-hfrzxfyxbvubwzdqnztdapxqaa-- ------=_NextPart_000_015A_01C22EDC.5D6BE4B0 Content-Type: application/octet-stream; name="http_info.rc" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="http_info.rc" #!/bin/rc=0A= # this is a demo of populate_environment.rc=0A= # both files need to go in wherever your httpd/magic directory is=0A= #=0A= # DEPENDENCY : to make the output nicer I've used html_encode.sed=0A= # which is a pretty lame sed script for turning ascii into html entities=0A= # binary data still gets through, it only subs about 6 elements, I need = a better one=0A= # You can happily remove it, just watch out for a < in your POST requests=0A= #=0A= # matt@proweb.co.uk 19 July 2002=0A= =0A= # IMPORTANT, without the rfork the environment would be shared by all = httpd threads=0A= # if that ruins your day you need to need to do some proper programming=0A= =0A= echo HTTP/1.1 200 FOUND=0A= echo Date: `{date}=0A= echo Server: plan9=0A= echo Content-Type: text/html=0A= echo =0A= =0A= rfork E=0A= /bin/ip/httpd/populate_environment.rc $*=0A= # that's it=0A= # the rest of this code just outputs an example=0A= =0A= echo 'EnviroThe environment variables set by = this request :'=0A= for (e in `{ls /env/HTTP_*}) {=0A= echo -n ''=0A= }=0A= echo '
' $e ''=0A= cat $e | sed -f /bin/ip/httpd/html_encode.sed | awk ' { print $0 = "
\n" } '=0A= echo '
'=0A= =0A= ------=_NextPart_000_015A_01C22EDC.5D6BE4B0 Content-Type: application/octet-stream; name="populate_environment.rc" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="populate_environment.rc" #!/bin/rc=0A= =0A= # this script loads the environment with the parameters of the HTTP = request=0A= # to facilitate shell scripted web serving=0A= # matt@proweb.co.uk 19 July 2002=0A= #=0A= while(~ $1 -*) {=0A= switch($1) {=0A= case -d=0A= shift=0A= echo $1 > /env/HTTP_Domain=0A= case -w=0A= shift=0A= echo $1 > /env/HTTP_Document_Root=0A= case -r=0A= shift=0A= echo $1> /env/HTTP_Remote_IP=0A= case -N=0A= shift=0A= echo $1 > /env/HTTP_Netdir=0A= case -b=0A= shift=0A= echo $1 | sed 's/^r //' > /env/HTTP_Raw=0A= case -R=0A= shift=0A= echo $1 | awk '{print $1}' > /env/HTTP_Method=0A= echo $1 | awk '{print $2}' > /env/HTTP_Query_String=0A= echo $1 | awk '{print $3}' > /env/HTTP_Version=0A= }=0A= shift=0A= }=0A= =0A= eval `{awk '=0A= BEGIN { =0A= body=3D0;=0A= }=0A= =0A= { =0A= i =3D index($0, ": ");=0A= if (i > 0) {=0A= env =3D substr($0, 1, i-1);=0A= val =3D substr($0, i+2);=0A= val =3D substr(val, 1, length(val) -1);=0A= gsub("''", "''''", val);=0A= printf("echo ''%s'' > /env/HTTP_%s;", val , env);=0A= } else {=0A= exit=0A= }=0A= =0A= }=0A= =0A= ' /env/HTTP_Raw}=0A= =0A= ------=_NextPart_000_015A_01C22EDC.5D6BE4B0 Content-Type: application/octet-stream; name="url_encode.awk" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="url_encode.awk" BEGIN {=0A= # We assume an awk implementation that is just plain dumb.=0A= # We will convert an character to its ASCII value with the=0A= # table ord[], and produce two-digit hexadecimal output=0A= # without the printf("%02X") feature.=0A= =0A= urlencode_EOL =3D "%0A" # "end of line" string (encoded)=0A= split ("1 2 3 4 5 6 7 8 9 A B C D E F", urlencode_hextab, " ")=0A= urlencode_hextab [0] =3D 0=0A= for ( urlencode_i=3D1; urlencode_i<=3D255; ++urlencode_i ) ord [ = sprintf ("%c", urlencode_i) "" ] =3Durlencode_i + 0=0A= urlencode_EncodeEOL =3D 1=0A= urlencode_encoded_line =3D ""=0A= }=0A= =0A= function url_encode_string(s) {=0A= encoded_string =3D ""=0A= for ( i=3D1; i<=3Dlength (s); ++i ) {=0A= encoded_string =3D encoded_string url_encode_char(substr ($0, i, 1))=0A= }=0A= return encoded_string=0A= }=0A= =0A= function url_encode_char(c) {=0A= if ( c ~ /[a-zA-Z0-9.-]/ ) { # safe character=0A= return c=0A= }=0A= if ( c =3D=3D " " ) {=0A= return "+" # special handling=0A= }=0A= # unsafe character, encode it as a two-digit hex-number=0A= lo =3D ord [c] % 16=0A= hi =3D int (ord [c] / 16);=0A= return "%" urlencode_hextab [hi] urlencode_hextab [lo]=0A= }=0A= =0A= {=0A= print url_encode_string($0)=0A= } ------=_NextPart_000_015A_01C22EDC.5D6BE4B0--