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