bash-httpd.sh
· 4.0 KiB · Bash
Raw
#!/bin/bash
# Configurable variables
PATH=/bin:/usr/bin:/usr/local/bin
DOC_ROOT=~/temp/www
DEF_HTML=index.html
DEF_DIR=www
LOG_FACILITY=local1
# End of configurables
HTTP_VERSION="HTTP/1.0"
SERVER="bash-httpd/0.04"
CR=`printf "\015"`
program="${0##/*/}"
response_code(){
num="$1"
case "$num" in
200) err="OK";;
403) err="Forbidden";;
404) err="Not Found";;
500) err="Internal Server Error";;
501) err="Not Implemented";;
*) err="Internal Server Error"
log err "Unknown response: $num"
num=500
;;
esac
log notice "response: $num"
echo "$HTTP_VERSION $num $err"
}
error(){
response_code $1
if [ "$2" ]; then problem="$2"; else problem="$err"; fi
cat <<EOF
Content-Type: text/html
<!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>
EOF
log err "$problem"
exit 1
}
log(){
level="$1"; message="$2"
#logger -p $LOG_FACILITY."$level" "$program[$$]: $method $url $version: $message"
echo "$level" "$program[$$]: $method $url $version: $message">&2
}
read method url version
method="${method%$CR}";
url="${url%$CR}";
version="${version%$CR}";
case "$version" in
""|http/0.9|HTTP/0.9)
readmore=0;;
http/1.0|HTTP/1.0|http/1.1|HTTP/1.1)
readmore=1;;
*)
log notice "$method $url $version"
error 501 "Unknown version";;
esac
if [ "$readmore" != 0 ]; then
read foo; while [ "$foo" != "$CR" -a "$foo" != "" ]; do read foo; done
fi
case "$method" in
GET|get)
what=get;;
HEAD|head)
what=head;;
*)
echo "$method"
error 501 "Unimplemented method";;
esac
echo "URL: $url" >&2
case "$url" in
"/envhi.txt")
type=envfun; file="$DOC_ROOT/env.txt"
;;
"/ipimg.jpg")
type=ipimg; file="$DOC_ROOT/ipimg.jpg"
;;
"/usenet/"*)
type=usenetsearch; file="$(echo "$url"|cut -d/ -f3-)"
;;
*/../*|*[^/A-Za-z0-9~.-]*)
error 403 "Illegal attempt to access resource"
;;
#/~*)
#user="${url#/~}";
#user="${user%%/*}";
#user=`eval echo ~$user`; # we had *better* have cleaned up $url
#rest="${url#/~*/}"; rest="${rest##/~*}";
#type=file; file="$user/$DEF_DIR/$rest"
#;;
""|/*)
type=file; file="$DOC_ROOT/$url"
;;
*)
error 501 "Unknown request type"
;;
esac
case $type in
file)
if [ -d "$file" ]; then file="$file/$DEF_HTML"; fi
if [ ! -e "$file" ]; then error 404; fi
if [ ! -r "$file" ]; then error 403; fi
response_code 200
if [ "$what" = "head" ]; then echo; exit 0; fi
case "$file" in
*.html | *.htm) mime=text/html;;
*.jpg|*.jpeg) mime=image/jpeg;;
*.gif) mime=image/gif;;
*.gz|*.tgz) mime=application/binary;;
*.txt|*.text) mime=text/plain;;
*.css) mime=text/css;;
*.js) mime=text/javascript;;
*.json) mime=application/json;;
*) mime=application/binary;;
esac
echo Content-Type: $mime; echo; cat $file
;;
usenetsearch)
ubase="/home/julian/.slrn/spools/news.tilde.green/news/"
response_code 200
echo 'Content-Type: text/plain'; echo
[ "$file" = "" ] && { echo "Use: /usenet/search_term or /usenet/file.path.to.article"; exit; }
echo "$file" | grep -q '\.'
[ "$?" = "1" ] && search=yes || search=no
[ "$search" = "yes" ] && file="$(echo "$file"|sed -e 's;"; ;g' -e 's;%;.;g')"
[ "$search" = "yes" ] && grep -R -- "$file" "${ubase}campaignwiki" "${ubase}tilde"|sed -e 's;'"$ubase"';;g'
[ "$search" = "no" ] && file="$(echo "$file"|sed 's;\.;/;g')"
[ "$search" = "no" ] && [ -f "${ubase}$file" ] && cat "${ubase}${file}"
;;
ipimg)
response_code 200
echo 'Content-Type: image/jpeg'; echo
[ ! -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
cat $DOC_ROOT/$SOCAT_PEERADDR.jpg
;;
envfun)
response_code 200
echo 'Content-Type: text/plain'; echo
echo "ENV: <END"
#env
echo "Feature Disabled"
echo "END"
;;
*)
error 501 "Messed up internal type"
;;
esac
1 | #!/bin/bash |
2 | # Configurable variables |
3 | PATH=/bin:/usr/bin:/usr/local/bin |
4 | DOC_ROOT=~/temp/www |
5 | DEF_HTML=index.html |
6 | DEF_DIR=www |
7 | LOG_FACILITY=local1 |
8 | # End of configurables |
9 | HTTP_VERSION="HTTP/1.0" |
10 | SERVER="bash-httpd/0.04" |
11 | |
12 | CR=`printf "\015"` |
13 | program="${0##/*/}" |
14 | |
15 | response_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 | |
32 | error(){ |
33 | response_code $1 |
34 | if [ "$2" ]; then problem="$2"; else problem="$err"; fi |
35 | cat <<EOF |
36 | Content-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> |
38 | EOF |
39 | log err "$problem" |
40 | exit 1 |
41 | } |
42 | |
43 | log(){ |
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 | |
49 | read method url version |
50 | |
51 | method="${method%$CR}"; |
52 | url="${url%$CR}"; |
53 | version="${version%$CR}"; |
54 | |
55 | case "$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";; |
63 | esac |
64 | |
65 | if [ "$readmore" != 0 ]; then |
66 | read foo; while [ "$foo" != "$CR" -a "$foo" != "" ]; do read foo; done |
67 | fi |
68 | |
69 | case "$method" in |
70 | GET|get) |
71 | what=get;; |
72 | HEAD|head) |
73 | what=head;; |
74 | *) |
75 | echo "$method" |
76 | error 501 "Unimplemented method";; |
77 | esac |
78 | |
79 | echo "URL: $url" >&2 |
80 | case "$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 | ;; |
106 | esac |
107 | |
108 | case $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 | ;; |
157 | esac |
158 |
index.html
· 80 B · HTML
Raw
<html>
<head><title>Funnyness</title></head>
<body>See index.txt</body>
</html>
1 | <html> |
2 | <head><title>Funnyness</title></head> |
3 | <body>See index.txt</body> |
4 | </html> |
5 |
1 | #!/bin/sh |
2 | |
3 | echo "$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 |
8 | exit |
9 |