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 31a4993a 2021-01-23 op ./../gg -b "gemini://localhost:10965/$1"
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 31a4993a 2021-01-23 op ./../gg -h "gemini://localhost:10965/$1"
36 5c2e310e 2021-01-22 op }
37 5c2e310e 2021-01-22 op
38 31a4993a 2021-01-23 op # usage: raw <path>
39 31a4993a 2021-01-23 op # return both header and body
40 31a4993a 2021-01-23 op raw() {
41 31a4993a 2021-01-23 op ./../gg "gemini://localhost:10965/$1"
42 31a4993a 2021-01-23 op }
43 31a4993a 2021-01-23 op
44 5c2e310e 2021-01-22 op run() {
45 5c2e310e 2021-01-22 op # filter out logs for GET requests
46 5c2e310e 2021-01-22 op (./../gmid -c reg.conf 2>&1 | grep -v GET) >&2 &
47 5c2e310e 2021-01-22 op pid=$!
48 31a4993a 2021-01-23 op # give gmid time to bind the port, otherwise we end up
49 31a4993a 2021-01-23 op # executing gg when gmid isn't ready yet.
50 31a4993a 2021-01-23 op sleep 1
51 5c2e310e 2021-01-22 op }
52 5c2e310e 2021-01-22 op
53 5c2e310e 2021-01-22 op # usage: check [exit-message]
54 5c2e310e 2021-01-22 op # check if gmid is still running
55 5c2e310e 2021-01-22 op check() {
56 5c2e310e 2021-01-22 op if ! ps $pid >/dev/null; then
57 5c2e310e 2021-01-22 op echo ${1:-"gmid crashed?"}
58 5c2e310e 2021-01-22 op exit 1
59 5c2e310e 2021-01-22 op fi
60 5c2e310e 2021-01-22 op }
61 5c2e310e 2021-01-22 op
62 5c2e310e 2021-01-22 op # quit gmid
63 5c2e310e 2021-01-22 op quit() {
64 5c2e310e 2021-01-22 op pkill gmid || true
65 5c2e310e 2021-01-22 op wait || true
66 5c2e310e 2021-01-22 op }
67 5c2e310e 2021-01-22 op
68 5c2e310e 2021-01-22 op # usage: eq a b errmsg
69 5c2e310e 2021-01-22 op # if a and b aren't equal strings, exit with errmsg
70 5c2e310e 2021-01-22 op eq() {
71 5c2e310e 2021-01-22 op if ! [ "$1" = "$2" ]; then
72 5c2e310e 2021-01-22 op echo "$3: \"$1\" not equal \"$2\""
73 5c2e310e 2021-01-22 op exit 1
74 5c2e310e 2021-01-22 op fi
75 5c2e310e 2021-01-22 op }
76 5c2e310e 2021-01-22 op
77 5c2e310e 2021-01-22 op onexit() {
78 5c2e310e 2021-01-22 op rm -f bigfile bigfile.sha
79 5c2e310e 2021-01-22 op quit
80 5c2e310e 2021-01-22 op }
81 5c2e310e 2021-01-22 op
82 5c2e310e 2021-01-22 op # tests
83 5c2e310e 2021-01-22 op
84 5c2e310e 2021-01-22 op trap 'onexit' INT TERM EXIT
85 5c2e310e 2021-01-22 op
86 5c2e310e 2021-01-22 op endl=`printf "\r\n"`
87 5c2e310e 2021-01-22 op lf=`echo`
88 5c2e310e 2021-01-22 op
89 5c2e310e 2021-01-22 op config "" ""
90 5c2e310e 2021-01-22 op checkconf
91 5c2e310e 2021-01-22 op run
92 5c2e310e 2021-01-22 op
93 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
94 5c2e310e 2021-01-22 op eq "$(get /)" "# hello world$ln" "Unexpected body for /"
95 5c2e310e 2021-01-22 op echo OK GET /
96 5c2e310e 2021-01-22 op
97 5c2e310e 2021-01-22 op eq "$(head /foo)" "51 not found" "Unexpected head /foo"
98 5c2e310e 2021-01-22 op eq "$(get /foo)" "" "Unexpected body /foo"
99 5c2e310e 2021-01-22 op echo OK GET /foo
100 5c2e310e 2021-01-22 op
101 5c2e310e 2021-01-22 op # should redirect if asked for a directory but without the trailing /
102 5c2e310e 2021-01-22 op eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
103 5c2e310e 2021-01-22 op eq "$(get /dir)" "" "Unexpected body for redirect"
104 5c2e310e 2021-01-22 op echo OK GET /dir
105 5c2e310e 2021-01-22 op
106 5c2e310e 2021-01-22 op # 51 for a directory without index.gmi
107 5c2e310e 2021-01-22 op eq "$(head /dir/)" "51 not found" "Unexpected head for /"
108 5c2e310e 2021-01-22 op eq "$(get /dir/)" "" "Unexpected body for error"
109 5c2e310e 2021-01-22 op echo OK GET /dir/
110 5c2e310e 2021-01-22 op
111 5c2e310e 2021-01-22 op eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
112 5c2e310e 2021-01-22 op eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
113 5c2e310e 2021-01-22 op echo OK GET /dir/foo.gmi
114 5c2e310e 2021-01-22 op
115 5c2e310e 2021-01-22 op # try a big file
116 5c2e310e 2021-01-22 op eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
117 5c2e310e 2021-01-22 op get /bigfile > bigfile
118 5c2e310e 2021-01-22 op ./sha bigfile bigfile.sha
119 5c2e310e 2021-01-22 op eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
120 5c2e310e 2021-01-22 op echo OK GET /bigfile
121 5c2e310e 2021-01-22 op
122 5c2e310e 2021-01-22 op # shouldn't be executing cgi scripts
123 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
124 5c2e310e 2021-01-22 op echo OK GET /hello
125 5c2e310e 2021-01-22 op
126 5c2e310e 2021-01-22 op check "should be running"
127 5c2e310e 2021-01-22 op quit
128 5c2e310e 2021-01-22 op
129 5c2e310e 2021-01-22 op # try with custom mime
130 5c2e310e 2021-01-22 op config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
131 5c2e310e 2021-01-22 op checkconf
132 5c2e310e 2021-01-22 op run
133 5c2e310e 2021-01-22 op
134 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
135 5c2e310e 2021-01-22 op echo OK GET / with custom mime
136 5c2e310e 2021-01-22 op
137 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
138 5c2e310e 2021-01-22 op echo OK GET /hello with custom mime
139 5c2e310e 2021-01-22 op
140 5c2e310e 2021-01-22 op check "should be running"
141 5c2e310e 2021-01-22 op quit
142 5c2e310e 2021-01-22 op
143 5c2e310e 2021-01-22 op # try with custom lang
144 5c2e310e 2021-01-22 op config '' 'lang "it"'
145 5c2e310e 2021-01-22 op checkconf
146 5c2e310e 2021-01-22 op run
147 5c2e310e 2021-01-22 op
148 5c2e310e 2021-01-22 op eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
149 5c2e310e 2021-01-22 op echo OK GET / with custom lang
150 5c2e310e 2021-01-22 op
151 5c2e310e 2021-01-22 op check "should be running"
152 5c2e310e 2021-01-22 op quit
153 5c2e310e 2021-01-22 op
154 5c2e310e 2021-01-22 op # finally try with CGI scripts
155 5c2e310e 2021-01-22 op config '' 'cgi ""'
156 5c2e310e 2021-01-22 op checkconf
157 5c2e310e 2021-01-22 op run
158 5c2e310e 2021-01-22 op
159 5c2e310e 2021-01-22 op eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
160 5c2e310e 2021-01-22 op eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
161 5c2e310e 2021-01-22 op echo OK GET /hello with cgi
162 5c2e310e 2021-01-22 op
163 5c2e310e 2021-01-22 op eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
164 5c2e310e 2021-01-22 op eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
165 5c2e310e 2021-01-22 op echo OK GET /slow with cgi
166 5c2e310e 2021-01-22 op
167 5c2e310e 2021-01-22 op eq "$(head /err)" "" "Unexpected head for /err"
168 5c2e310e 2021-01-22 op eq "$(get /err)" "" "Unexpected body for /err"
169 5c2e310e 2021-01-22 op echo OK GET /err with cgi
170 5c2e310e 2021-01-22 op
171 6cdecad8 2021-01-23 op eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
172 3309ef97 2021-01-23 op echo OK GET /invalid with cgi
173 3309ef97 2021-01-23 op
174 7b31a638 2021-01-24 op # try a big file
175 7b31a638 2021-01-24 op eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
176 7b31a638 2021-01-24 op get /bigfile > bigfile
177 7b31a638 2021-01-24 op ./sha bigfile bigfile.sha
178 7b31a638 2021-01-24 op eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
179 7b31a638 2021-01-24 op echo OK GET /serve-bigfile with cgi
180 7b31a638 2021-01-24 op
181 5c2e310e 2021-01-22 op check "should be running"
182 5c2e310e 2021-01-22 op quit
183 e7a2a99b 2021-01-24 op
184 e7a2a99b 2021-01-24 op config '' 'index "foo.gmi"'
185 e7a2a99b 2021-01-24 op checkconf
186 e7a2a99b 2021-01-24 op run
187 e7a2a99b 2021-01-24 op
188 e7a2a99b 2021-01-24 op eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /"
189 e7a2a99b 2021-01-24 op eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error"
190 e7a2a99b 2021-01-24 op echo OK GET /dir/ with custom index
191 e7a2a99b 2021-01-24 op
192 e7a2a99b 2021-01-24 op check "should be running"
193 e7a2a99b 2021-01-24 op quit
194 c8b74339 2021-01-24 op
195 c8b74339 2021-01-24 op config '' 'location "/dir/" { default type "text/plain" index "hello" }'
196 c8b74339 2021-01-24 op checkconf
197 c8b74339 2021-01-24 op run
198 c8b74339 2021-01-24 op
199 c8b74339 2021-01-24 op eq "$(head /dir/hello)" "20 text/plain" "Unexpected head for /"
200 c8b74339 2021-01-24 op echo OK GET /dir/hello with location and default type
201 c8b74339 2021-01-24 op
202 252908e6 2021-01-24 op eq "$(head /dir/)" "20 text/plain" "Unexpected head for /dir/"
203 c8b74339 2021-01-24 op eq "$(get /dir/|tail -1)" 'echo "# hello world"' "Unexpected body for /dir/"
204 c8b74339 2021-01-24 op echo OK GET /dir/ with location and custom index
205 c8b74339 2021-01-24 op
206 c8b74339 2021-01-24 op check "should be running"
207 c8b74339 2021-01-24 op quit
208 252908e6 2021-01-24 op
209 252908e6 2021-01-24 op config '' 'location "/dir/" { auto index on }'
210 252908e6 2021-01-24 op checkconf
211 252908e6 2021-01-24 op run
212 252908e6 2021-01-24 op
213 252908e6 2021-01-24 op eq "$(head /)" "20 text/gemini" "Unexpected head for /"
214 252908e6 2021-01-24 op eq "$(get /)" "# hello world$ln" "Unexpected body for /"
215 252908e6 2021-01-24 op echo OK GET / with auto index
216 252908e6 2021-01-24 op
217 252908e6 2021-01-24 op eq "$(head /dir)" "30 /dir/" "Unexpected head for /dir"
218 252908e6 2021-01-24 op eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /dir/"
219 252908e6 2021-01-24 op eq "$(get /dir/|wc -l|xargs)" "3" "Unexpected body for /dir/"
220 252908e6 2021-01-24 op echo OK GET /dir/ with auto index on
221 252908e6 2021-01-24 op
222 252908e6 2021-01-24 op check "should be running"
223 252908e6 2021-01-24 op quit