Author: Adam <git@apiote.xyz>
handle errors in find
get.sh | 20 ++++++++++++++++++-- server2.sh | 38 +++++++++++++++++++++++++------------- start.sh | 2 +-
diff --git a/get.sh b/get.sh index d6fcdb6f6a9e9b49c289fe2413ee5ac1acf9b5fd..2058787f266120a60070b785d194e4ecc585a77a 100644 --- a/get.sh +++ b/get.sh @@ -6,11 +6,27 @@ } list_items() { toy=$1 - find "toys/$toy" . -mindepth 1 -maxdepth 1 -type d + if [ ! -d "toys/$toy" ] + then + printf "n;" + else + printf "0;" + find "toys/$toy" . -mindepth 1 -maxdepth 1 -type d + fi } show_item() { toy=$1 item=$2 - cat "toys/$toy/$item.log" + if [ ! -d "toys/$toy/$item" ] + then + printf 'n;' + elif [ -z "$(cat "toys/$toy/$item.exit")" ] + then + printf 'r;' + cat "toys/$toy/$item.log" + else + printf '%s;' "$(cat "toys/$toy/$item.exit")" + cat "toys/$toy/$item.log" + fi } diff --git a/server2.sh b/server2.sh index 8c610376f46362ea79b5f06ed1b407be79956a98..876f32b7a1402a6c600756af8852f1972a9b4e0a 100755 --- a/server2.sh +++ b/server2.sh @@ -57,13 +57,12 @@ sleep 1; echo "end" > "$fifo2" printf "200 %s" "${#page}" } -respond201() { +respond202() { toy=$1 item=$2 - # todo template - message="" - printf "HTTP/1.1 201 Created\r\nContent-Length: %s\r\nLocation: /toys/%s/%s\r\n\r\n%s\r\n" "${#message}" "$name" "$item" "$message" > "$fifo1" - printf "201 -" + message=$3 + printf "HTTP/1.1 202 Accepted\r\nContent-Length: %s\r\nLocation: /toys/%s/%s\r\n\r\n%s\r\n" "${#message}" "$name" "$item" "$message" > "$fifo1" + printf "202 -" } respond404() { @@ -106,17 +105,30 @@ then page=$(list_toys) respond200 "$page" else - page=$(list_items "$toy") - # todo if $toy not found -> 404 - respond200 "$page" + r=$(list_items "$toy") + status=$(echo "$r" | cut -d ';' -f 1) + if [ "$status" = 'n' ] + then + respond404 "$path" + else + respond200 "$r" + fi fi elif [ "$(slashes "$path")" -eq 2 ] then toy=$(field "$path" 3) item=$(field "$path" 4) - page=$(show_item "$toy" "$item") - # todo if $toy or $toy/$item not found -> 404 - respond200 "$page" + r=$(show_item "$toy" "$item") + status=$(echo "$r" | cut -d ';' -f 1) + if [ "$status" = 'n' ] + then + respond404 "$path" + elif [ "$status" = 'r' ] + then + respond202 "$r" + else + respond200 "$r" + fi else respond404 "$path" fi @@ -128,12 +140,12 @@ then respond404 "$path" fi - item=$(post_item "$toy") + item=$(start_item "$toy") if [ $! -gt 0 ] then respond500 "error while creating $toy" else - respond201 "$toy" "$item" + respond202 "$toy" "$item" fi ;; *) diff --git a/start.sh b/start.sh index 269e04292e2e69383c6a6dde41cb48baecb0c6b0..fc7a7e6127d8d52456900f760857db43214fcf59 100644 --- a/start.sh +++ b/start.sh @@ -1,6 +1,6 @@ #!/bin/sh -post_item() { +start_item() { toy=$1 mkdir -p "toys/$toy/"