9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] httpd utilities / scripts
@ 2002-07-19  3:25 matt
  2002-07-19  3:39 ` matt
  2002-07-19  7:42 ` [9fans] httpd utilities / scripts William S.
  0 siblings, 2 replies; 9+ messages in thread
From: matt @ 2002-07-19  3:25 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 823 bytes --]

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

[-- Attachment #2.1: Type: text/plain, Size: 351 bytes --]

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"

[-- Attachment #2.2: html_encode.sed.suspect --]
[-- Type: application/octet-stream, Size: 168 bytes --]

[-- Attachment #3: http_info.rc --]
[-- Type: application/octet-stream, Size: 1152 bytes --]

#!/bin/rc
# this is a demo of populate_environment.rc
# both files need to go in wherever your httpd/magic directory is
#
# DEPENDENCY : to make the output nicer I've used html_encode.sed
#	which is a pretty lame sed script for turning ascii into html entities
#	binary data still gets through, it only subs about 6 elements, I need a better one
#	You can happily remove it, just watch out for a < in your POST requests
#
#	matt@proweb.co.uk	19 July 2002

# IMPORTANT, without the rfork the environment would be shared by all httpd threads
# if that ruins your day you need to need to do some proper programming

echo HTTP/1.1 200 FOUND
echo Date: `{date}
echo Server: plan9
echo Content-Type: text/html
echo 

rfork E
/bin/ip/httpd/populate_environment.rc $*
# that's it
# the rest of this code just outputs an example

echo '<HTML><TITLE>Enviro</TITLE><BODY>The environment variables set by this request :<TABLE>'
for (e in `{ls /env/HTTP_*}) {
	echo -n '<TR valign=top><TD>' $e '</TD><TD>'
	cat $e | sed -f /bin/ip/httpd/html_encode.sed | awk ' { print $0 "<BR>\n" } '
	echo '</TD>'
}
echo '</TABLE></BODY></HTML>'


[-- Attachment #4: populate_environment.rc --]
[-- Type: application/octet-stream, Size: 969 bytes --]

#!/bin/rc

# this script loads the environment with the parameters of the HTTP request
# to facilitate shell scripted web serving
# matt@proweb.co.uk 19 July 2002
#
while(~ $1 -*) {
	switch($1) {
	case -d
		shift
		echo $1 > /env/HTTP_Domain
	case -w
		shift
		echo $1 > /env/HTTP_Document_Root
	case -r
		shift
		echo $1> /env/HTTP_Remote_IP
	case -N
		shift
		echo $1 > /env/HTTP_Netdir
	case -b
		shift
		echo $1 | sed 's/^r //' > /env/HTTP_Raw
	case -R
		shift
		echo $1 | awk '{print $1}' > /env/HTTP_Method
		echo $1 | awk '{print $2}' > /env/HTTP_Query_String
		echo $1 | awk '{print $3}' > /env/HTTP_Version
	}
	shift
}

eval `{awk '
BEGIN { 
	body=0;
}

{ 
	i = index($0, ": ");
	if (i > 0) {
		env = substr($0, 1, i-1);
		val = substr($0, i+2);
		val = substr(val, 1, length(val) -1);
		gsub("''", "''''", val);
		printf("echo ''%s'' > /env/HTTP_%s;", val , env);
	} else {
		exit
	}

}

' /env/HTTP_Raw}


[-- Attachment #5: url_encode.awk --]
[-- Type: application/octet-stream, Size: 1113 bytes --]

    BEGIN {
	# We assume an awk implementation that is just plain dumb.
	# We will convert an character to its ASCII value with the
	# table ord[], and produce two-digit hexadecimal output
	# without the printf("%02X") feature.

	urlencode_EOL = "%0A"		# "end of line" string (encoded)
	split ("1 2 3 4 5 6 7 8 9 A B C D E F", urlencode_hextab, " ")
	urlencode_hextab [0] = 0
	for ( urlencode_i=1; urlencode_i<=255; ++urlencode_i ) ord [ sprintf ("%c", urlencode_i) "" ] =urlencode_i + 0
	urlencode_EncodeEOL = 1
	urlencode_encoded_line = ""
    }

function url_encode_string(s) {
	encoded_string = ""
	for ( i=1; i<=length (s); ++i ) {
	    encoded_string = encoded_string url_encode_char(substr ($0, i, 1))
	}
	return encoded_string
}

function url_encode_char(c) {
	if ( c ~ /[a-zA-Z0-9.-]/ ) { # safe character
		return c
	}
	if ( c == " " ) {
		return "+"	# special handling
	}
	# unsafe character, encode it as a two-digit hex-number
	lo = ord [c] % 16
	hi = int (ord [c] / 16);
	return "%" urlencode_hextab [hi] urlencode_hextab [lo]
}

{
	print url_encode_string($0)
}

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-07-29 16:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-19  3:25 [9fans] httpd utilities / scripts matt
2002-07-19  3:39 ` matt
2002-07-19 16:41   ` [9fans] UTF-8 support with Mothra plan9
2002-07-19 17:40     ` [9fans] The "i" browser Howard Trickey
2002-07-29 16:00       ` Ben
2002-07-19  7:42 ` [9fans] httpd utilities / scripts William S.
2002-07-19  9:16   ` matt
2002-07-19  9:42     ` arisawa
2002-07-19 17:08       ` William S.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).