#! /bin/bash
FQDN=zcrayfish.dyndns.org
#Get the HTTP command, filename, protocol version . . .
read -r CMD OBJ PROTO BADCLIENT
filename="$(echo -ne "$OBJ" | sed -e 's/%2c/,/I' -e 's/%20/ /I')"
# get HTTP extra headers
while read -r KEY VALUE FOO
do
test -z "$KEY" && break;
[ "$KEY" == $'\r' ] && break;
done
date="$(date -u +'%a, %d %b %Y %R:%S GMT')"
error400 () {
echo -e "HTTP/1.0 400 Bad Request\r"
echo -e "Content-Type: text/plain charset=us-ascii\r"
echo -e "Allow: GET\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN"
echo -e "Connection: close"
echo -e "\r\n"
echo -e "HTTP/1.0 400 Bad Request\r"
echo -e "Request could not be understood - bad syntax\r"
echo -e "\r"
echo -e "gopher to http gateway at $FQDN"
exit
}
error404 () {
echo -e "HTTP/1.0 404 Not Found\r"
echo -e "Content-Type: text/plain charset=us-ascii\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN"
echo -e "Connection: close"
echo -e "\r\n"
echo -e "HTTP/1.0 404 Not Found\r"
echo -e "'$OBJ' doesn't exist!\r"
echo -e "This resource cannot be located.\r"
echo -e "\r"
echo -e "gopher to http gateway at $FQDN"
}
error501 () {
echo -e "HTTP/1.0 501 Not Implemented\r"
echo -e "Content-Type: text/plain charset=us-ascii\r"
echo -e "Allow: GET\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN"
echo -e "Connection: close"
echo -e "\r\n"
echo -e "HTTP/1.0 501 Not Implemented\r"
echo -e "This error has accoured for one of the following reasons:\r"
echo -e " *This server only implements the GET method (e.g. your browser sent a HEAD or PUT)\r"
echo -e " *This server, while acting as a gateway or proxy, does not support CSO queries.\r"
echo -e "\r"
echo -e "gopher to http gateway at $FQDN"
exit
}
# See if request is a GET, if not, throw it away
test "$CMD" != 'GET' && error501
# Likewise ignore invalid gets too.
test ! -z "$BADCLIENT" && error400 && exit
#Step4
case "$filename" in
/favicon.ico)
error404
;;
/sitemap*|/robots.txt)
echo -e "HTTP/1.0 200 OK\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN\r"
echo -e "Connection: close\r"
echo -e "\r"
/usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70/0/$filename
;;
/2*)
error501
;;
/7*httpsearch*)
echo -e "HTTP/1.0 200 OK\r"
echo -e "Content-Type: text/html; level=1\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN"
echo -e "Connection: close\r"
echo -e "\r"
echo -e '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN">'
searchytwothousand="$(echo $filename | sed 's/httpsearch=//g')"
/usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70$searchytwothousand | sed 's/<A HREF="\/\/$FQDN:70/<A HREF="http:\/\/$FQDN:80/g' | sed 's/<A HREF="\/\//<A HREF="gopher:\/\//g'
exit
;;
/7*)
echo -e "HTTP/1.0 200 OK\r"
echo -e "Content-Type: text/html; charset=us-ascii\r"
echo -e "Date: $date\r"
echo -e "Server: gopher to http gateway at $FQDN"
echo -e "\r"
echo -e '<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">'
echo -e '<title></title>'
echo -e "<form action='$FQDN$filename' method='get'>"
echo -e '<p>This is a searchable gopher index. Enter search keywords:<input type="text" name="httpsearch"></p></form>'
;;