toymaker.git

commit 6bb9ba8c5625d2c47bf2ad03ade2a8c02c1a0362

Author: Adam <git@apiote.xyz>

serve built artifact

 get.sh | 13 +++++++++++++
 server2.sh | 32 +++++++++++++++++++++++++++++++-


diff --git a/get.sh b/get.sh
index 2058787f266120a60070b785d194e4ecc585a77a..52f9bf395181f1d3f8dee21d02b85ceb9e9592f3 100644
--- a/get.sh
+++ b/get.sh
@@ -30,3 +30,16 @@ 		printf '%s;' "$(cat "toys/$toy/$item.exit")"
 		cat "toys/$toy/$item.log"
 	fi
 }
+
+get_artifact() {
+	toy=$1
+	item=$2
+	artifactName=$3
+	if [ ! -d "toys/$toy/$item/$artifactName" ]
+	then
+		printf 'n;'
+	else
+		contentType=$(file -b -i "toys/$toy/$item/$artifactName")
+		printf '0;%s;%s' "$contentType" "toys/$toy/$item/$artifactName"
+	fi
+}




diff --git a/server2.sh b/server2.sh
index a1c7d11d6055660f24eafd1f1307517eca234eee..2f6578f125c8bfbd8d52c0f07e04305fdbb98917 100755
--- a/server2.sh
+++ b/server2.sh
@@ -1,6 +1,7 @@
 #!/bin/sh
 #
 # Based on server by Upper Stream (c) 2017, MIT license
+# (https://gist.github.com/upperstream/b9b77429bc024541b7605aea76086ad8)
 
 . start.sh
 . get.sh
@@ -74,11 +75,25 @@ 	page="$1"
 
 	# { cat "$fifo2" > /dev/null; printf ""; } > "$fifo1" &
 
-	printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: text/html\r\n\r\n%s\r\n" "${#page}" "$page" >> "$fifo1"
+	printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: text/html\r\n\r\n%s\r\n" "${#page}" "$page" > "$fifo1"
 
 	# printf "%s\r\n" "$page" >> "$fifo1"
 
 	# sleep 1; echo "end" > "$fifo2"
+	printf "200 %s" "${#page}"
+}
+
+respond200file() {
+	contentType="$1"
+	filePath="$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" "${#page}" "$contentType" >> "$fifo1"
+
+	cat "$filePath" >> "$fifo1"
+
+	sleep 1; echo "end" > "$fifo2"
 	printf "200 %s" "${#page}"
 }
 
@@ -205,6 +220,21 @@ 					respond200 "$page"
 				fi
 			else
 				respond406 "$accept"
+			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 "$path"
+			else
+				contentType=$(echo "$r" | cut -d ';' -f 2)
+				filePath=$(echo "$r" | cut -d ';' -f 3)
+				respond200file "$contentType" "$filePath"
 			fi
 		else
 			respond404 "$path"