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 02be96c6 2021-02-09 op ggflags=
6 02be96c6 2021-02-09 op
7 5c2e310e 2021-01-22 op # usage: config <global config> <stuff for localhost>
8 5c2e310e 2021-01-22 op # generates a configuration file reg.conf
9 5c2e310e 2021-01-22 op config() {
10 5c2e310e 2021-01-22 op cat <<EOF > reg.conf
11 5c2e310e 2021-01-22 op ipv6 off
12 5c2e310e 2021-01-22 op port 10965
13 5c2e310e 2021-01-22 op $1
14 5c2e310e 2021-01-22 op server "localhost" {
15 6ff23c67 2021-02-01 op cert "$PWD/cert.pem"
16 6ff23c67 2021-02-01 op key "$PWD/key.pem"
17 6ff23c67 2021-02-01 op root "$PWD/testdata"
18 5c2e310e 2021-01-22 op $2
19 5c2e310e 2021-01-22 op }
20 5c2e310e 2021-01-22 op EOF
21 5c2e310e 2021-01-22 op }
22 5c2e310e 2021-01-22 op
23 5c2e310e 2021-01-22 op checkconf() {
24 5c2e310e 2021-01-22 op ./../gmid -n -c reg.conf
25 5c2e310e 2021-01-22 op }
26 5c2e310e 2021-01-22 op
27 5c2e310e 2021-01-22 op # usage: get <path>
28 5c2e310e 2021-01-22 op # return the body of the request on stdout
29 5c2e310e 2021-01-22 op get() {
30 02be96c6 2021-02-09 op ./../gg -b $ggflags "gemini://localhost:10965/$1"
31 5c2e310e 2021-01-22 op }
32 5c2e310e 2021-01-22 op
33 5c2e310e 2021-01-22 op # usage: head <path>
34 5c2e310e 2021-01-22 op # return the meta response line on stdout
35 5c2e310e 2021-01-22 op head() {
36 02be96c6 2021-02-09 op ./../gg -h $ggflags "gemini://localhost:10965/$1"
37 5c2e310e 2021-01-22 op }
38 5c2e310e 2021-01-22 op
39 31a4993a 2021-01-23 op # usage: raw <path>
40 31a4993a 2021-01-23 op # return both header and body
41 31a4993a 2021-01-23 op raw() {
42 02be96c6 2021-02-09 op ./../gg $ggflags "gemini://localhost:10965/$1"
43 31a4993a 2021-01-23 op }
44 31a4993a 2021-01-23 op
45 5c2e310e 2021-01-22 op run() {
46 5c2e310e 2021-01-22 op # filter out logs for GET requests
47 46af8c6c 2021-01-27 op (./../gmid -f -c reg.conf 2>&1 | grep -v GET) >&2 &
48 5c2e310e 2021-01-22 op pid=$!
49 31a4993a 2021-01-23 op # give gmid time to bind the port, otherwise we end up
50 31a4993a 2021-01-23 op # executing gg when gmid isn't ready yet.
51 31a4993a 2021-01-23 op sleep 1
52 5c2e310e 2021-01-22 op }
53 5c2e310e 2021-01-22 op
54 5c2e310e 2021-01-22 op # usage: check [exit-message]
55 5c2e310e 2021-01-22 op # check if gmid is still running
56 5c2e310e 2021-01-22 op check() {
57 5c2e310e 2021-01-22 op if ! ps $pid >/dev/null; then
58 5c2e310e 2021-01-22 op echo ${1:-"gmid crashed?"}
59 5c2e310e 2021-01-22 op exit 1
60 5c2e310e 2021-01-22 op fi
61 5c2e310e 2021-01-22 op }
62 5c2e310e 2021-01-22 op
63 afc025ff 2021-02-06 op restart() {
64 afc025ff 2021-02-06 op pkill -SIGHUP gmid
65 afc025ff 2021-02-06 op sleep 1
66 afc025ff 2021-02-06 op }
67 afc025ff 2021-02-06 op
68 5c2e310e 2021-01-22 op # quit gmid
69 5c2e310e 2021-01-22 op quit() {
70 5c2e310e 2021-01-22 op pkill gmid || true
71 5c2e310e 2021-01-22 op wait || true
72 5c2e310e 2021-01-22 op }
73 5c2e310e 2021-01-22 op
74 9f006a21 2021-02-07 op count() {
75 9f006a21 2021-02-07 op wc -l | xargs
76 9f006a21 2021-02-07 op }
77 9f006a21 2021-02-07 op
78 5c2e310e 2021-01-22 op # usage: eq a b errmsg
79 5c2e310e 2021-01-22 op # if a and b aren't equal strings, exit with errmsg
80 5c2e310e 2021-01-22 op eq() {
81 5c2e310e 2021-01-22 op if ! [ "$1" = "$2" ]; then
82 5c2e310e 2021-01-22 op echo "$3: \"$1\" not equal \"$2\""
83 5c2e310e 2021-01-22 op exit 1
84 5c2e310e 2021-01-22 op fi
85 5c2e310e 2021-01-22 op }
86 5c2e310e 2021-01-22 op
87 5c2e310e 2021-01-22 op onexit() {
88 5c2e310e 2021-01-22 op rm -f bigfile bigfile.sha
89 5c2e310e 2021-01-22 op quit
90 5c2e310e 2021-01-22 op }
91 5c2e310e 2021-01-22 op
92 5c2e310e 2021-01-22 op # tests
93 5c2e310e 2021-01-22 op
94 5c2e310e 2021-01-22 op trap 'onexit' INT TERM EXIT
95 5c2e310e 2021-01-22 op
96 5c2e310e 2021-01-22 op endl=`printf "\r\n"`
97 5c2e310e 2021-01-22 op lf=`echo`
98 5c2e310e 2021-01-22 op
99 5c2e310e 2021-01-22 op config "" ""
100 5c2e310e 2021-01-22 op checkconf
101 5c2e310e 2021-01-22 op run
102 5c2e310e 2021-01-22 op
103 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
104 5c2e310e 2021-01-22 op eq "$(get /)" "# hello world$ln" "Unexpected body for /"
105 5c2e310e 2021-01-22 op echo OK GET /
106 5c2e310e 2021-01-22 op
107 5c2e310e 2021-01-22 op eq "$(head /foo)" "51 not found" "Unexpected head /foo"
108 5c2e310e 2021-01-22 op eq "$(get /foo)" "" "Unexpected body /foo"
109 5c2e310e 2021-01-22 op echo OK GET /foo
110 5c2e310e 2021-01-22 op
111 5c2e310e 2021-01-22 op # should redirect if asked for a directory but without the trailing /
112 5c2e310e 2021-01-22 op eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
113 5c2e310e 2021-01-22 op eq "$(get /dir)" "" "Unexpected body for redirect"
114 5c2e310e 2021-01-22 op echo OK GET /dir
115 5c2e310e 2021-01-22 op
116 5c2e310e 2021-01-22 op # 51 for a directory without index.gmi
117 5c2e310e 2021-01-22 op eq "$(head /dir/)" "51 not found" "Unexpected head for /"
118 5c2e310e 2021-01-22 op eq "$(get /dir/)" "" "Unexpected body for error"
119 5c2e310e 2021-01-22 op echo OK GET /dir/
120 5c2e310e 2021-01-22 op
121 5c2e310e 2021-01-22 op eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
122 5c2e310e 2021-01-22 op eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
123 5c2e310e 2021-01-22 op echo OK GET /dir/foo.gmi
124 5c2e310e 2021-01-22 op
125 5c2e310e 2021-01-22 op # try a big file
126 5c2e310e 2021-01-22 op eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
127 5c2e310e 2021-01-22 op get /bigfile > bigfile
128 5c2e310e 2021-01-22 op ./sha bigfile bigfile.sha
129 5c2e310e 2021-01-22 op eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
130 5c2e310e 2021-01-22 op echo OK GET /bigfile
131 5c2e310e 2021-01-22 op
132 5c2e310e 2021-01-22 op # shouldn't be executing cgi scripts
133 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
134 5c2e310e 2021-01-22 op echo OK GET /hello
135 5c2e310e 2021-01-22 op
136 5c2e310e 2021-01-22 op check "should be running"
137 5c2e310e 2021-01-22 op
138 5c2e310e 2021-01-22 op # try with custom mime
139 5c2e310e 2021-01-22 op config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
140 5c2e310e 2021-01-22 op checkconf
141 afc025ff 2021-02-06 op restart
142 5c2e310e 2021-01-22 op
143 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
144 5c2e310e 2021-01-22 op echo OK GET / with custom mime
145 5c2e310e 2021-01-22 op
146 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
147 5c2e310e 2021-01-22 op echo OK GET /hello with custom mime
148 5c2e310e 2021-01-22 op
149 5c2e310e 2021-01-22 op check "should be running"
150 5c2e310e 2021-01-22 op
151 5c2e310e 2021-01-22 op # try with custom lang
152 5c2e310e 2021-01-22 op config '' 'lang "it"'
153 5c2e310e 2021-01-22 op checkconf
154 afc025ff 2021-02-06 op restart
155 5c2e310e 2021-01-22 op
156 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
157 5c2e310e 2021-01-22 op echo OK GET / with custom lang
158 5c2e310e 2021-01-22 op
159 5c2e310e 2021-01-22 op check "should be running"
160 5c2e310e 2021-01-22 op
161 49b73ba1 2021-02-10 op # make sure we can use different lang in different location rules
162 49b73ba1 2021-02-10 op config '' 'lang "it" location "/en/*" { lang "en" } location "/de/*" { lang "de" }'
163 49b73ba1 2021-02-10 op checkconf
164 a4188b78 2021-02-12 op echo OK parse multiple locations correctly
165 49b73ba1 2021-02-10 op restart
166 49b73ba1 2021-02-10 op
167 9f006a21 2021-02-07 op # try with CGI scripts
168 87f2b68b 2021-02-02 op config '' 'cgi "*"'
169 5c2e310e 2021-01-22 op checkconf
170 afc025ff 2021-02-06 op restart
171 5c2e310e 2021-01-22 op
172 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
173 5c2e310e 2021-01-22 op eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
174 5c2e310e 2021-01-22 op echo OK GET /hello with cgi
175 5c2e310e 2021-01-22 op
176 5c2e310e 2021-01-22 op eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
177 5c2e310e 2021-01-22 op eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
178 5c2e310e 2021-01-22 op echo OK GET /slow with cgi
179 5c2e310e 2021-01-22 op
180 35744950 2021-02-01 op eq "$(head /err)" "42 CGI error" "Unexpected head for /err"
181 5c2e310e 2021-01-22 op eq "$(get /err)" "" "Unexpected body for /err"
182 5c2e310e 2021-01-22 op echo OK GET /err with cgi
183 5c2e310e 2021-01-22 op
184 6cdecad8 2021-01-23 op eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
185 3309ef97 2021-01-23 op echo OK GET /invalid with cgi
186 3309ef97 2021-01-23 op
187 7b31a638 2021-01-24 op # try a big file
188 7b31a638 2021-01-24 op eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
189 7b31a638 2021-01-24 op get /bigfile > bigfile
190 7b31a638 2021-01-24 op ./sha bigfile bigfile.sha
191 7b31a638 2021-01-24 op eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
192 7b31a638 2021-01-24 op echo OK GET /serve-bigfile with cgi
193 7b31a638 2021-01-24 op
194 9f006a21 2021-02-07 op # ensure we split the query correctly
195 9f006a21 2021-02-07 op eq "$(get /env | awk /^-/ | count)" 1 "Unexpected number of arguments"
196 9f006a21 2021-02-07 op eq "$(get /env?foo | awk /^-/ | count)" 2 "Unexpected number of arguments"
197 9f006a21 2021-02-07 op eq "$(get /env?foo+bar | awk /^-/ | count)" 3 "Unexpected number of arguments"
198 9f006a21 2021-02-07 op eq "$(get /env?foo+bar=5 | awk /^-/ | count)" 1 "Unexpected number of arguments"
199 9f006a21 2021-02-07 op eq "$(get /env?foo+bar%3d5 | awk /^-/ | count)" 3 "Unexpected number of arguments"
200 9f006a21 2021-02-07 op
201 5c2e310e 2021-01-22 op check "should be running"
202 e7a2a99b 2021-01-24 op
203 e7a2a99b 2021-01-24 op config '' 'index "foo.gmi"'
204 e7a2a99b 2021-01-24 op checkconf
205 afc025ff 2021-02-06 op restart
206 e7a2a99b 2021-01-24 op
207 e7a2a99b 2021-01-24 op eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /"
208 e7a2a99b 2021-01-24 op eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error"
209 e7a2a99b 2021-01-24 op echo OK GET /dir/ with custom index
210 e7a2a99b 2021-01-24 op
211 e7a2a99b 2021-01-24 op check "should be running"
212 c8b74339 2021-01-24 op
213 49b73ba1 2021-02-10 op config '' 'location "/dir/*" { default type "text/plain" index "hello" }'
214 c8b74339 2021-01-24 op checkconf
215 afc025ff 2021-02-06 op restart
216 c8b74339 2021-01-24 op
217 c8b74339 2021-01-24 op eq "$(head /dir/hello)" "20 text/plain" "Unexpected head for /"
218 c8b74339 2021-01-24 op echo OK GET /dir/hello with location and default type
219 c8b74339 2021-01-24 op
220 252908e6 2021-01-24 op eq "$(head /dir/)" "20 text/plain" "Unexpected head for /dir/"
221 c8b74339 2021-01-24 op eq "$(get /dir/|tail -1)" 'echo "# hello world"' "Unexpected body for /dir/"
222 c8b74339 2021-01-24 op echo OK GET /dir/ with location and custom index
223 c8b74339 2021-01-24 op
224 c8b74339 2021-01-24 op check "should be running"
225 252908e6 2021-01-24 op
226 49b73ba1 2021-02-10 op config '' 'location "/dir/*" { auto index on }'
227 252908e6 2021-01-24 op checkconf
228 afc025ff 2021-02-06 op restart
229 252908e6 2021-01-24 op
230 252908e6 2021-01-24 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
231 252908e6 2021-01-24 op eq "$(get /)" "# hello world$ln" "Unexpected body for /"
232 252908e6 2021-01-24 op echo OK GET / with auto index
233 252908e6 2021-01-24 op
234 252908e6 2021-01-24 op eq "$(head /dir)" "30 /dir/" "Unexpected head for /dir"
235 252908e6 2021-01-24 op eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /dir/"
236 5f715ce4 2021-02-02 op eq "$(get /dir/|wc -l|xargs)" "5" "Unexpected body for /dir/"
237 252908e6 2021-01-24 op echo OK GET /dir/ with auto index on
238 252908e6 2021-01-24 op
239 252908e6 2021-01-24 op check "should be running"
240 6abda252 2021-02-06 op
241 6abda252 2021-02-06 op # test block return and strip
242 6abda252 2021-02-06 op
243 6abda252 2021-02-06 op config '' 'location "*" { block }'
244 6abda252 2021-02-06 op checkconf
245 afc025ff 2021-02-06 op restart
246 6abda252 2021-02-06 op
247 6abda252 2021-02-06 op eq "$(head /)" "40 temporary failure" "Unexpected head for /"
248 6abda252 2021-02-06 op eq "$(get /)" "" "Unexpected body for /"
249 6abda252 2021-02-06 op echo OK GET / with block
250 6abda252 2021-02-06 op
251 6abda252 2021-02-06 op eq "$(head /nonexists)" "40 temporary failure" "Unexpected head for /nonexists"
252 6abda252 2021-02-06 op eq "$(get /nonexists)" "" "Unexpected body for /nonexists"
253 6abda252 2021-02-06 op echo OK GET /nonexists with block
254 6abda252 2021-02-06 op
255 6abda252 2021-02-06 op check "should be running"
256 6abda252 2021-02-06 op
257 6abda252 2021-02-06 op config '' '
258 6abda252 2021-02-06 op location "/dir" {
259 6abda252 2021-02-06 op strip 1
260 6abda252 2021-02-06 op block return 40 "%% %p %q %P %N test"
261 6abda252 2021-02-06 op }
262 6abda252 2021-02-06 op location "*" {
263 6abda252 2021-02-06 op strip 99
264 6abda252 2021-02-06 op block return 40 "%% %p %q %P %N test"
265 6abda252 2021-02-06 op }'
266 6abda252 2021-02-06 op checkconf
267 afc025ff 2021-02-06 op restart
268 6abda252 2021-02-06 op
269 6abda252 2021-02-06 op eq "$(head /dir/foo.gmi)" "40 % /foo.gmi 10965 localhost test"
270 6abda252 2021-02-06 op echo OK GET /dir/foo.gmi with strip and block
271 6abda252 2021-02-06 op
272 6abda252 2021-02-06 op eq "$(head /bigfile)" "40 % 10965 localhost test"
273 6abda252 2021-02-06 op echo OK GET /bigfile with strip and block
274 6abda252 2021-02-06 op
275 6abda252 2021-02-06 op check "should be running"
276 e3ddf390 2021-02-06 op
277 e3ddf390 2021-02-06 op # test the entrypoint
278 e3ddf390 2021-02-06 op
279 e3ddf390 2021-02-06 op config '' 'entrypoint "/env"'
280 e3ddf390 2021-02-06 op checkconf
281 e3ddf390 2021-02-06 op restart
282 afc025ff 2021-02-06 op
283 e3ddf390 2021-02-06 op eq "$(head /foo/bar)" "20 text/plain; lang=en" "Unknown head for /foo/bar"
284 e3ddf390 2021-02-06 op eq "$(get /foo/bar|grep PATH_INFO)" "PATH_INFO=/foo/bar" "Unexpected PATH_INFO"
285 e3ddf390 2021-02-06 op echo OK GET /foo/bar with entrypoint
286 e3ddf390 2021-02-06 op
287 02be96c6 2021-02-09 op # test with require ca
288 02be96c6 2021-02-09 op
289 02be96c6 2021-02-09 op config '' 'require client ca "'$PWD'/testca.pem"'
290 02be96c6 2021-02-09 op checkconf
291 02be96c6 2021-02-09 op restart
292 02be96c6 2021-02-09 op
293 02be96c6 2021-02-09 op eq "$(head /)" "60 client certificate required" "Unexpected head for /"
294 02be96c6 2021-02-09 op echo OK GET / without client certificate
295 02be96c6 2021-02-09 op
296 02be96c6 2021-02-09 op ggflags="-C valid.crt -K valid.key"
297 02be96c6 2021-02-09 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
298 02be96c6 2021-02-09 op echo OK GET / with valid client certificate
299 02be96c6 2021-02-09 op
300 02be96c6 2021-02-09 op ggflags="-C invalid.cert.pem -K invalid.key.pem"
301 02be96c6 2021-02-09 op eq "$(head /)" "61 certificate not authorised" "Unexpected head for /"
302 02be96c6 2021-02-09 op echo OK GET / with invalid client certificate
303 02be96c6 2021-02-09 op
304 02be96c6 2021-02-09 op ggflags=''
305 02be96c6 2021-02-09 op
306 6abda252 2021-02-06 op quit