shithub: werc

ref: 9681c09647c3d4ef8bd5ab25df88cf7176c55707
dir: /bin/contrib/rc-httpd/select-handler/

View raw version
#!/bin/rc
# 2016-02-01T19:31:03-0500
rfork n
fn do_error{
	do_log $1
	echo 'HTTP/1.1 '^$1^$cr
	emit_extra_headers
	echo 'Content-type: text/html'^$cr
	echo $cr
	echo '<html>
<head>
<title>'^$1^'</title>
</head>
<body>
<h1>'^$1^'</h1>'
	echo $2
	echo '<p><i>rc-httpd at' $SERVER_NAME '</i>'
	echo '
	</body>
	</html>
	'
}

fn http_redirect {
    if(~ $1 http://* https://*)
        t=$1
    if not if(~ $1 /*)
        t=$"base_url^$1
    if not
        t=$"base_url^$"req_path^$1
    exec /bin/echo 'Status: '^$2^'
Location: '^$t^'

'
    exit
}

fn okstatic{
	full_path=`{echo $"FS_ROOT^$"PATH_INFO | urlencode -d}
	full_path=$"full_path
	if(~ $full_path */)
		error 503
	if(test -d $full_path){
		redirect perm $"location^'/' \
			'URL not quite right, and browser did not accept redirect.'
		exit
	}
	if(! test -e $full_path){
		error 404
		exit
	}
	if(! test -r $full_path){
		error 503
		exit
	}
	do_log 200
	type='text/plain'
	max_age=3600	# 1 hour
	echo 'HTTP/1.1 200 OK'^$cr
	emit_extra_headers
	echo 'Content-type: '^$type^'; charset=utf-8'^$cr
	echo 'Content-length: '^`{ls -l $full_path | awk '{print $6}'}
	echo 'Cache-control: max-age='^$max_age^$cr
	echo $cr
	exec cat $full_path
}

# surprise!
if(~ $HTTP_REFERER *hiphopstan.com/forum* *slax.*/forum*){
	PATH_INFO=$location
	FS_ROOT=/usr/sl/www/werc/sites/hotlink
	exec static-or-index
}
if(~ $HTTP_REFERER 'http://okturing.com/index.rc?start=100' || {~ $SERVER_NAME okturing.com && ~ $location /index.rc} || ~ $location /qemu/plan9.flp.gz){
	do_error '402 PAYMENT REQUIRED'
	exit
}
if(~ $location /*/wp-admin* /wiki/index.php* /wp/wp-admin* /wp-config* /wp-content/* wp-login.php* /admin.php* /index.php/admin* /phpinfo.php* /phpMyAdmin/* /phpMyAdmin-2/* /*xmlrpc.php){
	do_error '402 PAYMENT REQUIRED'
	exit
}

# build environment and namespace
SERVER_NAME=`{echo $SERVER_NAME | sed 's/^www\.//g'}

if(~ $location *~nis*)
	location=`{echo $location | sed 's/\/\~nis/\/_nis/g'}

if(~ $SERVER_NAME 9front.org){
	bind /usr/sl/plan9front /usr/sl/www/werc/sites/9front.org/9front
	bind /usr/sl/www/werc/sites/9front.org/_werc/pub/style.css /usr/sl/www/werc/pub/style/style.css
	bind /usr/sl/www/werc/sites/plan9.stanleylieber.com/src /usr/sl/www/werc/sites/9front.org/extra
	aux/stub -d /usr/sl/www/werc/sites/9front.org/extra/rc
	bind /usr/sl/www/werc/sites/plan9.stanleylieber.com/rc /usr/sl/www/werc/sites/9front.org/extra/rc
	bind /usr/sl/www/werc/sites/bell-labs.co/9front/iso /usr/sl/www/werc/sites/9front.org/iso
	bind /usr/sl/www/werc/sites/plan9.stanleylieber.com/pkg /usr/sl/www/werc/sites/9front.org/pkg
	if(~ $location /who/*){
		for(i in Aram aiju alvaro cinap_lenrek eekee erik joy kenji lf94 martian67 mveety rminnich sl)
		bind /usr/sl/www/werc/sites/9front.org/$i /usr/sl/www/werc/sites/9front.org/who/$i/index.txt
	}
	aux/stub /usr/sl/www/werc/sites/9front.org/9front.torrent
	bind /usr/sl/www/werc/sites/bell-labs.co/9front/iso/9front-5048.e16a172bcae6.iso.bz2.torrent /usr/sl/www/werc/sites/9front.org/9front.torrent
	if(~ $location /list.html)
		location=/lists.html
}
if(~ $SERVER_NAME bugs.9front.org){
	bind /usr/sl/www/werc/sites/bugs.9front.org/_werc/pub/style.css /usr/sl/www/werc/pub/style/style.css
	bind -a /usr/bugs /usr/sl/www/werc/sites/bugs.9front.org
	if(~ $location /closed/[a-z0-9]* /open/[a-z0-9]*){
		d=`{basename -d $location}
		aux/stub /usr/sl/www/werc/sites/bugs.9front.org^$d^/index.md
		bind /usr/bugs^$d^/readme /usr/sl/www/werc/sites/bugs.9front.org^$d^/index.md
	}
}
if(~ $SERVER_NAME fqa.9front.org){
	#location=`{echo $"location | sed 's/^(fqa|appendix).*\.html$//g'}
}
if(~ $SERVER_NAME lists.9front.org){
	for(i in 9atom 9changes 9fans 9front 9front-bugs 9front-commits 9front-sysinfo 9nag acme-sac cat-v cypherpunks dlr harvey harvey-commits harvey-issues inferno nix plan9port-dev sam-fans sierra31 skunk-works tscm-l tuhs werc www-html www-talk www-vrml)
		bind /mail/box/$i/mbox /usr/sl/www/werc/sites/lists.9front.org/$i
}
if(~ $SERVER_NAME wiki.9front.org)
	bind /usr/sl/www/werc/sites/wiki.9front.org/_werc/pub/style.css /usr/sl/www/werc/pub/style/style.css
if(~ $SERVER_NAME golang.cat-v.org)
	bind /usr/sl/www/werc/sites/go-lang.cat-v.org /usr/sl/www/werc/sites/golang.cat-v.org
if(~ $SERVER_NAME plan9.stanleylieber.com){
	bind /usr/sl/www/werc/sites/plan9.stanleylieber.com/_werc/pub/style.css /usr/sl/www/werc/pub/style/style.css
	bind /mail/lib /usr/sl/www/werc/sites/plan9.stanleylieber.com/mail/lib
	bind /sys/lib/dist/mail/lib/names.local /usr/sl/www/werc/sites/plan9.stanleylieber.com/mail/lib/names.local
	bind /rc/bin/service.crazy/tcp587 /usr/sl/www/werc/sites/plan9.stanleylieber.com/mail/service/tcp587
	bind /rc/bin/service.crazy/tcp993 /usr/sl/www/werc/sites/plan9.stanleylieber.com/mail/service/tcp993
	bind /rc/bin/rc-httpd/select-handler /usr/sl/www/werc/sites/plan9.stanleylieber.com/rc-httpd/select-handler
	bind /rc/bin/service.crazy/tcp80 /usr/sl/www/werc/sites/plan9.stanleylieber.com/rc-httpd/tcp80
	bind /rc/bin/service.crazy/tcp443 /usr/sl/www/werc/sites/plan9.stanleylieber.com/rc-httpd/tcp443
}

# route requests
if(~ $SERVER_NAME okturing.com && ~ $location /src/*/body){
	PATH_INFO=$location
	FS_ROOT=/usr/sl/www/werc/sites/okturing.com
	okstatic
}
if not if(~ $SERVER_NAME emma.stanleylieber.com fqa.* gl.* iawtp.com ln.* lists.* mold.dk nm.* nsacom.net osuny.co.uk osx.* ph.* pop.* pp.* qualitycountrylyrics.com ragnarok.* tcasey.* tip.9front.org tn.* ur.* volksutils.com weekly.9front.org zeroxpark.com || ~ $location /cbz/* /favicon.ico /img/* /pdf/* /src/* /txt/*){
	PATH_INFO=$location
	FS_ROOT=/usr/sl/www/werc/sites/$SERVER_NAME
	exec static-or-index
}
if not if(~ $SERVER_NAME *bell-labs.co){
	bind -a /usr/sl/www/werc/sites/9front.org/who /usr/sl/www/werc/sites/bell-labs.co/who
	PATH_INFO=$location
	FS_ROOT=/usr/sl/www/werc/sites/bell-labs.co
	exec static-or-index
}
if not if(~ $SERVER_NAME ttr.inri.net){
	PATH_INFO=$location
	FS_ROOT=/usr/sl/t
	exec static-or-index
}
if not if(~ $SERVER_NAME vr.stanleylieber.com){
	if(~ $location / /bin/* /etc/* /*htaccess /*htpasswd /index.rc* /lib/* /stats/*){
		PATH_INFO=$location
		FS_ROOT=/usr/sl/www/werc/sites/vr.stanleylieber.com
		exec cgi /usr/sl/www/werc/sites/vr.stanleylieber.com/index.rc $*
	}
	if not{
		PATH_INFO=$location
		FS_ROOT=/usr/sl/www/werc/sites/vr.stanleylieber.com
		exec static-or-index
	}
}
if not if(~ $SERVER_NAME *){
	PATH_INFO=$location
	FS_ROOT=/usr/sl/www/werc/sites/$SERVER_NAME
	exec static-or-cgi /usr/sl/www/werc/bin/werc.rc
}
if not
	error 503