toymaker.git

ref: master

./toymaker.sh


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/sh
#
# Based on server by Upper Stream (c) 2017, MIT license
# (https://gist.github.com/upperstream/b9b77429bc024541b7605aea76086ad8)

. start.sh
. get.sh
. password.sh

set -e

exec >>~/toymaker.log
exec 2>&1

program=${0##*/}

usage() {
	cat <<-EOF
	Usage:

	$program [-p port] [docroot]
	$program -h

	-p port : specify listening port number; defaults to 1313
	-h      : print this help and quit
	docroot : specify document root directory; defaults to the current directory
EOF
}

# default listening port
port=1313

nc=$(command -v nc) || true

pid_file=/tmp/$program.pid

slashes() {
	printf "%s" "$1" | sed 's|[^/]||g' | wc -m
}

field() {
	echo "$1" | cut -d '/' -f "$2"
}
makefifo() {
	name="$1"
	mkfifo -m 0600 "/tmp/$name.fifo" && echo "/tmp/$name.fifo"
}

templateMessage() {
	message="$1"
	templateName=$2
	contentTypeHeader=$3
	while [ -n "$contentTypeHeader" ]
	do
		contentType=$(echo "$contentTypeHeader" | cut -d ',' -f1)
		contentTypeHeader=$(echo "$contentTypeHeader" | cut -d ',' -f2-)
		contentType=$(echo "$contentType" | cut -d ';' -f1)
		case $contentType in
		text/plain)
			ext='txt'
			;;
		text/html|\*|\*/\*|'')
			ext='html'
			;;
		image/\*|image/svg)
			ext='svg'
			;;
		*)
			continue
			;;
		esac
		[ -f "templates/$templateName.$ext" ] && break
		ext=''
	done
	[ -n "$ext" ] || return 1
	#shellcheck disable=SC1090
	. "templates/$templateName.$ext"
	printf '%s\n' "$contentType"
	template "$message"
}

respond200() {
	page="$1"
	contentType="$2"
	[ -z "$contentType" ] && contentType='text/html'

	printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: %s\r\n\r\n%s\r\n" "${#page}" "$contentType" "$page" > "$fifo1"

	printf "200 %s" "${#page}"
}

respond200file() {
	filePath="$1"
	contentType="$2"

	{ cat "$fifo2" > /dev/null; printf ""; } > "$fifo1" &

	printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: %s\r\n\r\n" "$(wc -c "$filePath" | cut -d ' ' -f1)"  "$contentType" >> "$fifo1"

	cat "$filePath" >> "$fifo1"

	sleep 1; echo "end" > "$fifo2"
	printf "200 %s" "$(wc -c "$filePath" | cut -d ' ' -f1)"
}

respond202() {
	toy=$1
	item=$2
	message=$3
	contentType="$4"
	[ -z "$contentType" ] && contentType='text/html'
	printf "HTTP/1.1 202 Accepted\r\nContent-Length: %s\r\nLocation: /toys/%s/%s\r\n\r\n%s\r\n" "${#message}" "$toy" "$item" "$message" > "$fifo1"
	printf "202 -"
}

respond401() {
	message=""
	printf "HTTP/1.1 401 Unauthorized\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "401 -"
}

respond403() {
	message=""
	printf "HTTP/1.1 403 Forbidden\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "403 -"
}

respond404() {
	path=$1
	message=$(templateMessage "$path not found" 'message' 'text/html' | tail -n+2)
	printf "HTTP/1.1 404 Not Found\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "404 -"
}

respond405() {
	method=$1
	message=$(templateMessage "$method unsupported" 'message' 'text/html' | tail -n+2)
	printf "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "405 -"
}

respond406() {
	contentType=$1
	message="$contentType unsupported"
	printf "HTTP/1.1 406 Not Acceptable\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "406 -"
}

respond500() {
	message=$(templateMessage "Server error: $1" 'message' 'text/html' | tail -n+2)
	printf "HTTP/1.1 500 Internal Server Error\r\nContent-Length: %s\r\n\r\n%s\r\n" "${#message}" "$message" > "$fifo1"
	printf "500 -"
}

check_auth() {
	authorization=$1
	stored=$(cat password)
	if [ -z "$stored" ] || [ -z "$authorization" ]
	then
		respond401
		return 1
	fi
	if ! printf '%s' "$authorization" | verify "$stored"
	then
		respond403
		return 1
	fi
}

execute() {
	method="$1"
	path="$2"
	authorization="$3"
	accept="$4"
	_toys=$(field "$path" 2)
	if [ "$_toys" != 'toys' ]
	then
		respond404 "$path"
		return 1
	fi

	case $method in
	GET)
		if [ "$(slashes "$path")" -eq 2 ]
		then
			toy=$(field "$path" 3)
			if [ "$toy" = '' ]
			then
				if page=$(templateMessage "$(list_toys)" 'toys' 'text/html')
				then
					respond200 "$(echo "$page" | tail -n+2)"
				else
					respond406 "$accept"
					return 1
				fi
			else
				r=$(list_items "$toy")
				status=$(echo "$r" | cut -d ';' -f1)
				content=$(echo "$r" | cut -d ';' -f2)
				if [ "$status" = 'n' ]
				then
					respond404 "toy $toy"
					return 1
				elif page=$(templateMessage "$toy;$content" 'toy' 'text/html')
				then
					respond200 "$(echo "$page" | tail -n+2)"
				else
					respond406 "$accept"
					return 1
				fi
			fi
		elif [ "$(slashes "$path")" -eq 3 ]
		then
			toy=$(field "$path" 3)
			item=$(field "$path" 4)
			if [ -z "$item" ]
			then
				respond404 "$path"
				return 1
			fi
			r=$(show_item "$toy" "$item")
			artifacts=$(item_artifacts "$toy" "$item")
			status=$(echo "$r" | head -n1 | cut -d ';' -f1)
			if [ "$status" = 'n' ]
			then
				respond404 "item $toy/$item"
				return 1
			elif page=$(templateMessage "$artifacts;$toy;$item;$r" 'item' "$accept")
			then
				body=$(echo "$page" | tail -n+2)
				contentType=$(echo "$page" | head -n1)
				if [ "$status" = 'r' ]
				then
					respond202 "$toy" "$item" "$body" "$contentType"
				else
					respond200 "$body" "$contentType"
				fi
			else
				respond406 "$accept"
				return 1
			fi
		elif [ "$(slashes "$path")" -eq 4 ]
		then
			toy=$(field "$path" 3)
			item=$(field "$path" 4)
			artifactName=$(field "$path" 5)
			r=$(get_artifact "$toy" "$item" "$artifactName")
			status=$(echo "$r" | cut -d ';' -f 1)
			if [ "$status" = 'n' ]
			then
				respond404 "artifact $toy/$item/$artifactName"
				return 1
			else
				contentType=$(echo "$r" | cut -d ';' -f 2)
				filePath=$(echo "$r" | cut -d ';' -f 3)
				respond200file "$filePath" "$contentType"
			fi
		else
			respond404 "$path"
			return 1
		fi
		;;
	POST)
		check_auth "$authorization" || return 1

		toy=$(field "$path" 3)
		if [ "$(slashes "$path")" -ne 3 ] || [ -n "$(field "$path" 4)" ]
		then
			respond404 "$path"
			return 1
		fi

		if ! (podman image ls --format "{{.Repository}}" | grep -x "localhost/toy_$toy" -q)
		then
			respond404 "toy $toy"
			return 1
		fi

		if item=$(start_item "$toy")
		then
			respond202 "$toy" "$item"
		else
			respond500 "error while creating $toy"
			return 1
		fi

		;;
	DELETE)
		check_auth "$authorization" || return 1

		if [ "$(slashes "$path")" -eq 3 ]
		then
			toy=$(field "$path" 3)
			item=$(field "$path" 4)
			if [ -z "$toy" ] || [ -z "$item" ]
			then
				respond404 "$path"
				return 1
			fi

			if stop_item "$toy" "$item"
			then
				respond200 ""
			else
				respond500 "error while stopping $toy/$item"
				return 1
			fi
		else
			respond404 "$path"
			return 1
		fi
		;;
	*)
		respond405 "$method"
		return 1
		;;
	esac
}

cleanup() {
	rm "$fifo1" "$fifo2" "$pid_file"
}

interrupt() {
	cleanup
	exit 0
}

parse() {
	cr=$(printf "\r")
	while read -r line; do
		line="${line%"$cr"}"
		case "$line" in
		GET*|POST*|PUT*|DELETE*|HEAD*|OPTIONS*|TRACE*)
			# shellcheck disable=SC2086
			set -- $line
			method=$1
			path=$2
			;;
		Host:*)
			host=${line#Host: }
			;;
		Accept:*)
			accept=${line#Accept: }
			;;
		accept:*)
			accept=${line#accept: }
			;;
		Authorization:*)
			authorization=${line#Authorization: }
			;;
		authorization:*)
			authorization=${line#authorization: }
			;;
		*:*)
			;;
		"")
			date=$(date +'%d/%m/%Y:%H:%M:%S %z')
			status_length=$(execute "$method" "$path" "$authorization" "$accept")
			printf "%s %s \"http://%s%s\" %s\n" "$date" "$method" "$host" "$path" "$status_length"
			trap - INT
			exit
			;;
		*)
			;;
		esac
	done
}

# dependency verification
for cmd in nc podman awk
do
	command -v $cmd >/dev/null || { echo "$0: \`$cmd\` not found"; exit 2; }
done

while getopts p:h opt; do
	case $opt in
	p) port=$OPTARG;;
	h) usage; exit 255;;
	*) usage; exit 255;;
	esac
done
shift $((OPTIND-1))

if [ $# -eq 0 ]; then
	docroot=.
else
	docroot="$1"
fi
docroot=$(cd "$docroot"; pwd)

# FIFO to write HTTP response
if ! fifo1=$(makefifo "${0##*/}.$$.1"); then
	echo "$0: creating a named pipe failed. Already running?" 1>&2
	exit 1
fi

# FIFO for internal event notification
if ! fifo2=$(makefifo "${0##*/}.$$.2"); then
	echo "$0: creating a named pipe failed. Already running?" 1>&2
	exit 1
fi

trap interrupt INT

echo $$ > "$pid_file"

cat 1>&2 <<EOF
Simple HTTP Server

Listening at the port number $port.
Type ^C to quit.
EOF

# shellcheck disable=SC2002
while cat "$fifo1" | "$nc" -l -p "$port" | parse; do
	:
done
cleanup
exit