Blame


1 5c2e310e 2021-01-22 op #!/bin/sh
2 5c2e310e 2021-01-22 op
3 5c2e310e 2021-01-22 op set -e
4 5c2e310e 2021-01-22 op
5 5c2e310e 2021-01-22 op # usage: config <global config> <stuff for localhost>
6 5c2e310e 2021-01-22 op # generates a configuration file reg.conf
7 5c2e310e 2021-01-22 op config() {
8 5c2e310e 2021-01-22 op cat <<EOF > reg.conf
9 5c2e310e 2021-01-22 op daemon off
10 5c2e310e 2021-01-22 op ipv6 off
11 5c2e310e 2021-01-22 op port 10965
12 5c2e310e 2021-01-22 op $1
13 5c2e310e 2021-01-22 op server "localhost" {
14 5c2e310e 2021-01-22 op cert "cert.pem"
15 5c2e310e 2021-01-22 op key "key.pem"
16 5c2e310e 2021-01-22 op root "testdata"
17 5c2e310e 2021-01-22 op $2
18 5c2e310e 2021-01-22 op }
19 5c2e310e 2021-01-22 op EOF
20 5c2e310e 2021-01-22 op }
21 5c2e310e 2021-01-22 op
22 5c2e310e 2021-01-22 op checkconf() {
23 5c2e310e 2021-01-22 op ./../gmid -n -c reg.conf
24 5c2e310e 2021-01-22 op }
25 5c2e310e 2021-01-22 op
26 5c2e310e 2021-01-22 op # usage: get <path>
27 5c2e310e 2021-01-22 op # return the body of the request on stdout
28 5c2e310e 2021-01-22 op get() {
29 5c2e310e 2021-01-22 op (./gg.py "$1" 10965 | sed 1d) || true
30 5c2e310e 2021-01-22 op }
31 5c2e310e 2021-01-22 op
32 5c2e310e 2021-01-22 op # usage: head <path>
33 5c2e310e 2021-01-22 op # return the meta response line on stdout
34 5c2e310e 2021-01-22 op head() {
35 5c2e310e 2021-01-22 op (./gg.py "$1" 10965 | sed 1q) || true
36 5c2e310e 2021-01-22 op }
37 5c2e310e 2021-01-22 op
38 5c2e310e 2021-01-22 op run() {
39 5c2e310e 2021-01-22 op # filter out logs for GET requests
40 5c2e310e 2021-01-22 op (./../gmid -c reg.conf 2>&1 | grep -v GET) >&2 &
41 5c2e310e 2021-01-22 op pid=$!
42 5c2e310e 2021-01-22 op }
43 5c2e310e 2021-01-22 op
44 5c2e310e 2021-01-22 op # usage: check [exit-message]
45 5c2e310e 2021-01-22 op # check if gmid is still running
46 5c2e310e 2021-01-22 op check() {
47 5c2e310e 2021-01-22 op if ! ps $pid >/dev/null; then
48 5c2e310e 2021-01-22 op echo ${1:-"gmid crashed?"}
49 5c2e310e 2021-01-22 op exit 1
50 5c2e310e 2021-01-22 op fi
51 5c2e310e 2021-01-22 op }
52 5c2e310e 2021-01-22 op
53 5c2e310e 2021-01-22 op # quit gmid
54 5c2e310e 2021-01-22 op quit() {
55 5c2e310e 2021-01-22 op pkill gmid || true
56 5c2e310e 2021-01-22 op wait || true
57 5c2e310e 2021-01-22 op }
58 5c2e310e 2021-01-22 op
59 5c2e310e 2021-01-22 op # usage: eq a b errmsg
60 5c2e310e 2021-01-22 op # if a and b aren't equal strings, exit with errmsg
61 5c2e310e 2021-01-22 op eq() {
62 5c2e310e 2021-01-22 op if ! [ "$1" = "$2" ]; then
63 5c2e310e 2021-01-22 op echo "$3: \"$1\" not equal \"$2\""
64 5c2e310e 2021-01-22 op exit 1
65 5c2e310e 2021-01-22 op fi
66 5c2e310e 2021-01-22 op }
67 5c2e310e 2021-01-22 op
68 5c2e310e 2021-01-22 op onexit() {
69 5c2e310e 2021-01-22 op rm -f bigfile bigfile.sha
70 5c2e310e 2021-01-22 op quit
71 5c2e310e 2021-01-22 op }
72 5c2e310e 2021-01-22 op
73 5c2e310e 2021-01-22 op # tests
74 5c2e310e 2021-01-22 op
75 5c2e310e 2021-01-22 op trap 'onexit' INT TERM EXIT
76 5c2e310e 2021-01-22 op
77 5c2e310e 2021-01-22 op endl=`printf "\r\n"`
78 5c2e310e 2021-01-22 op lf=`echo`
79 5c2e310e 2021-01-22 op
80 5c2e310e 2021-01-22 op config "" ""
81 5c2e310e 2021-01-22 op checkconf
82 5c2e310e 2021-01-22 op run
83 5c2e310e 2021-01-22 op
84 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
85 5c2e310e 2021-01-22 op eq "$(get /)" "# hello world$ln" "Unexpected body for /"
86 5c2e310e 2021-01-22 op echo OK GET /
87 5c2e310e 2021-01-22 op
88 5c2e310e 2021-01-22 op eq "$(head /foo)" "51 not found" "Unexpected head /foo"
89 5c2e310e 2021-01-22 op eq "$(get /foo)" "" "Unexpected body /foo"
90 5c2e310e 2021-01-22 op echo OK GET /foo
91 5c2e310e 2021-01-22 op
92 5c2e310e 2021-01-22 op # should redirect if asked for a directory but without the trailing /
93 5c2e310e 2021-01-22 op eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
94 5c2e310e 2021-01-22 op eq "$(get /dir)" "" "Unexpected body for redirect"
95 5c2e310e 2021-01-22 op echo OK GET /dir
96 5c2e310e 2021-01-22 op
97 5c2e310e 2021-01-22 op # 51 for a directory without index.gmi
98 5c2e310e 2021-01-22 op eq "$(head /dir/)" "51 not found" "Unexpected head for /"
99 5c2e310e 2021-01-22 op eq "$(get /dir/)" "" "Unexpected body for error"
100 5c2e310e 2021-01-22 op echo OK GET /dir/
101 5c2e310e 2021-01-22 op
102 5c2e310e 2021-01-22 op eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
103 5c2e310e 2021-01-22 op eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
104 5c2e310e 2021-01-22 op echo OK GET /dir/foo.gmi
105 5c2e310e 2021-01-22 op
106 5c2e310e 2021-01-22 op # try a big file
107 5c2e310e 2021-01-22 op eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
108 5c2e310e 2021-01-22 op get /bigfile > bigfile
109 5c2e310e 2021-01-22 op ./sha bigfile bigfile.sha
110 5c2e310e 2021-01-22 op eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
111 5c2e310e 2021-01-22 op echo OK GET /bigfile
112 5c2e310e 2021-01-22 op
113 5c2e310e 2021-01-22 op # shouldn't be executing cgi scripts
114 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
115 5c2e310e 2021-01-22 op echo OK GET /hello
116 5c2e310e 2021-01-22 op
117 5c2e310e 2021-01-22 op check "should be running"
118 5c2e310e 2021-01-22 op quit
119 5c2e310e 2021-01-22 op
120 5c2e310e 2021-01-22 op # try with custom mime
121 5c2e310e 2021-01-22 op config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
122 5c2e310e 2021-01-22 op checkconf
123 5c2e310e 2021-01-22 op run
124 5c2e310e 2021-01-22 op
125 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
126 5c2e310e 2021-01-22 op echo OK GET / with custom mime
127 5c2e310e 2021-01-22 op
128 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
129 5c2e310e 2021-01-22 op echo OK GET /hello with custom mime
130 5c2e310e 2021-01-22 op
131 5c2e310e 2021-01-22 op check "should be running"
132 5c2e310e 2021-01-22 op quit
133 5c2e310e 2021-01-22 op
134 5c2e310e 2021-01-22 op # try with custom lang
135 5c2e310e 2021-01-22 op config '' 'lang "it"'
136 5c2e310e 2021-01-22 op checkconf
137 5c2e310e 2021-01-22 op run
138 5c2e310e 2021-01-22 op
139 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
140 5c2e310e 2021-01-22 op echo OK GET / with custom lang
141 5c2e310e 2021-01-22 op
142 5c2e310e 2021-01-22 op check "should be running"
143 5c2e310e 2021-01-22 op quit
144 5c2e310e 2021-01-22 op
145 5c2e310e 2021-01-22 op # finally try with CGI scripts
146 5c2e310e 2021-01-22 op config '' 'cgi ""'
147 5c2e310e 2021-01-22 op checkconf
148 5c2e310e 2021-01-22 op run
149 5c2e310e 2021-01-22 op
150 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
151 5c2e310e 2021-01-22 op eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
152 5c2e310e 2021-01-22 op echo OK GET /hello with cgi
153 5c2e310e 2021-01-22 op
154 5c2e310e 2021-01-22 op eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
155 5c2e310e 2021-01-22 op eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
156 5c2e310e 2021-01-22 op echo OK GET /slow with cgi
157 5c2e310e 2021-01-22 op
158 5c2e310e 2021-01-22 op eq "$(head /err)" "" "Unexpected head for /err"
159 5c2e310e 2021-01-22 op eq "$(get /err)" "" "Unexpected body for /err"
160 5c2e310e 2021-01-22 op echo OK GET /err with cgi
161 5c2e310e 2021-01-22 op
162 3309ef97 2021-01-23 op eq "$(head /invalid | wc -c | xargs)" 2049 "Unexpected body for /invalid"
163 3309ef97 2021-01-23 op eq "$(get /invalid)" "" "Unexpected body for /invalid"
164 3309ef97 2021-01-23 op echo OK GET /invalid with cgi
165 3309ef97 2021-01-23 op
166 5c2e310e 2021-01-22 op check "should be running"
167 5c2e310e 2021-01-22 op quit