Last active 1729718915

Revision cf0ac3ddd8b6bbcf1bb54f6352811881195ff693

bash-httpd.sh Raw
1#!/bin/bash
2# Configurable variables
3PATH=/bin:/usr/bin:/usr/local/bin
4DOC_ROOT=~/temp/www
5DEF_HTML=index.html
6DEF_DIR=www
7LOG_FACILITY=local1
8# End of configurables
9HTTP_VERSION="HTTP/1.0"
10SERVER="bash-httpd/0.04"
11
12CR=`printf "\015"`
13program="${0##/*/}"
14
15response_code(){
16 num="$1"
17 case "$num" in
18 200) err="OK";;
19 403) err="Forbidden";;
20 404) err="Not Found";;
21 500) err="Internal Server Error";;
22 501) err="Not Implemented";;
23 *) err="Internal Server Error"
24 log err "Unknown response: $num"
25 num=500
26 ;;
27 esac
28 log notice "response: $num"
29 echo "$HTTP_VERSION $num $err"
30}
31
32error(){
33 response_code $1
34 if [ "$2" ]; then problem="$2"; else problem="$err"; fi
35cat <<EOF
36Content-Type: text/html
37<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title> $problem </title></head><body><h1> $num $problem </h1> $problem </body></html>
38EOF
39 log err "$problem"
40 exit 1
41}
42
43log(){
44 level="$1"; message="$2"
45 #logger -p $LOG_FACILITY."$level" "$program[$$]: $method $url $version: $message"
46 echo "$level" "$program[$$]: $method $url $version: $message">&2
47}
48
49read method url version
50
51method="${method%$CR}";
52url="${url%$CR}";
53version="${version%$CR}";
54
55case "$version" in
56 ""|http/0.9|HTTP/0.9)
57 readmore=0;;
58 http/1.0|HTTP/1.0|http/1.1|HTTP/1.1)
59 readmore=1;;
60 *)
61 log notice "$method $url $version"
62 error 501 "Unknown version";;
63esac
64
65if [ "$readmore" != 0 ]; then
66 read foo; while [ "$foo" != "$CR" -a "$foo" != "" ]; do read foo; done
67fi
68
69case "$method" in
70 GET|get)
71 what=get;;
72 HEAD|head)
73 what=head;;
74 *)
75 echo "$method"
76 error 501 "Unimplemented method";;
77esac
78
79echo "URL: $url" >&2
80case "$url" in
81 "/envhi.txt")
82 type=envfun; file="$DOC_ROOT/env.txt"
83 ;;
84 "/ipimg.jpg")
85 type=ipimg; file="$DOC_ROOT/ipimg.jpg"
86 ;;
87 "/usenet/"*)
88 type=usenetsearch; file="$(echo "$url"|cut -d/ -f3-)"
89 ;;
90 */../*|*[^/A-Za-z0-9~.-]*)
91 error 403 "Illegal attempt to access resource"
92 ;;
93 #/~*)
94 #user="${url#/~}";
95 #user="${user%%/*}";
96 #user=`eval echo ~$user`; # we had *better* have cleaned up $url
97 #rest="${url#/~*/}"; rest="${rest##/~*}";
98 #type=file; file="$user/$DEF_DIR/$rest"
99 #;;
100 ""|/*)
101 type=file; file="$DOC_ROOT/$url"
102 ;;
103 *)
104 error 501 "Unknown request type"
105 ;;
106esac
107
108case $type in
109 file)
110 if [ -d "$file" ]; then file="$file/$DEF_HTML"; fi
111 if [ ! -e "$file" ]; then error 404; fi
112 if [ ! -r "$file" ]; then error 403; fi
113 response_code 200
114 if [ "$what" = "head" ]; then echo; exit 0; fi
115 case "$file" in
116 *.html | *.htm) mime=text/html;;
117 *.jpg|*.jpeg) mime=image/jpeg;;
118 *.gif) mime=image/gif;;
119 *.gz|*.tgz) mime=application/binary;;
120 *.txt|*.text) mime=text/plain;;
121 *.css) mime=text/css;;
122 *.js) mime=text/javascript;;
123 *.json) mime=application/json;;
124 *) mime=application/binary;;
125 esac
126 echo Content-Type: $mime; echo; cat $file
127 ;;
128 usenetsearch)
129 ubase="/home/julian/.slrn/spools/news.tilde.green/news/"
130 response_code 200
131 echo 'Content-Type: text/plain'; echo
132 [ "$file" = "" ] && { echo "Use: /usenet/search_term or /usenet/file.path.to.article"; exit; }
133 echo "$file" | grep -q '\.'
134 [ "$?" = "1" ] && search=yes || search=no
135 [ "$search" = "yes" ] && file="$(echo "$file"|sed -e 's;"; ;g' -e 's;%;.;g')"
136 [ "$search" = "yes" ] && grep -R -- "$file" "${ubase}campaignwiki" "${ubase}tilde"|sed -e 's;'"$ubase"';;g'
137 [ "$search" = "no" ] && file="$(echo "$file"|sed 's;\.;/;g')"
138 [ "$search" = "no" ] && [ -f "${ubase}$file" ] && cat "${ubase}${file}"
139 ;;
140 ipimg)
141 response_code 200
142 echo 'Content-Type: image/jpeg'; echo
143 [ ! -f "$DOC_ROOT/$SOCAT_PEERADDR.jpg" ] && magick -gravity center -background black -fill white -size 400x300 -font "/home/julian/.local/share/fonts/Hack Regular Nerd Font Complete.ttf" caption:"Hello, your IP is $SOCAT_PEERADDR" $DOC_ROOT/$SOCAT_PEERADDR.jpg >&2
144 cat $DOC_ROOT/$SOCAT_PEERADDR.jpg
145 ;;
146 envfun)
147 response_code 200
148 echo 'Content-Type: text/plain'; echo
149 echo "ENV: <END"
150 #env
151 echo "Feature Disabled"
152 echo "END"
153 ;;
154 *)
155 error 501 "Messed up internal type"
156 ;;
157esac
158
index.html Raw
1<html>
2<head><title>Funnyness</title></head>
3<body>See index.txt</body>
4</html>
5
test.sh Raw
1#!/bin/sh
2
3echo "$SOCAT_PEERADDR:$SOCAT_PEERPORT" >&2
4#echo "$SOCAT_PEERADDR:$SOCAT_PEERPORT" >>guestbook.txt
5#timeout 5s cat >>guestbook.txt
6#printf 'HTTP/1.1 500 Not implemented\nServer: funserv\n\nThanks\n'
7./bash-httpd.sh
8exit
9