Author: Adam <git@apiote.xyz>
template pages
server2.sh | 61 ++++++++++++++++++++++++++++++++++++++++++----- templates/item.txt | 5 +++ todo | 2
diff --git a/server2.sh b/server2.sh index 6d0868416b7f12078962db96570cafe13d4fb837..c00882cd4991892a7c759b060d895e9b89556a82 100755 --- a/server2.sh +++ b/server2.sh @@ -44,6 +44,29 @@ name="$1" mkfifo -m 0600 "/tmp/$name.fifo" && echo "/tmp/$name.fifo" } +templateMessage() { + message=$1 + templateName=$2 + contentType=$3 + case $contentType in + text/plain) + ext='txt' + ;; + text/html|\*|\*/\*|'') + ext='html' + ;; + image/\*|image/svg) + ext='svg' + ;; + *) + return 1 + ;; + esac + [ -f "templates/$templateName.$ext" ] || return 1 + #shellcheck disable=SC1090 + . "templates/$templateName.$ext" + template "$message" +} respond200() { page="$1" @@ -90,6 +113,14 @@ respond405() { message="$1 unsupported" 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 + # todo html + 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() { @@ -118,6 +149,7 @@ execute() { method="$1" path="$2" authorization="$3" + accept="$4" toys=$(field "$path" 2) if [ "$toys" != 'toys' ] then @@ -131,16 +163,23 @@ then toy=$(field "$path" 3) if [ "$toy" = '' ] then - page=$(list_toys) - respond200 "$page" + if page=$(templateMessage "$(list_toys)" 'toys' 'text/html') + then + respond200 "$page" + else + respond406 "$accept" + fi else r=$(list_items "$toy") status=$(echo "$r" | cut -d ';' -f 1) if [ "$status" = 'n' ] then respond404 "$path" + elif page=$(templateMessage "$(list_toys)" 'toys' 'text/html') + then + respond200 "$page" else - respond200 "$r" + respond406 "$accept" fi fi elif [ "$(slashes "$path")" -eq 2 ] @@ -152,11 +191,16 @@ status=$(echo "$r" | cut -d ';' -f 1) if [ "$status" = 'n' ] then respond404 "$path" - elif [ "$status" = 'r' ] + elif page=$(templateMessage "$(list_toys)" 'toys' 'text/html') then - respond202 "$r" + if [ "$status" = 'r' ] + then + respond202 "$page" + else + respond200 "$page" + fi else - respond200 "$r" + respond406 "$accept" fi else respond404 "$path" @@ -224,6 +268,9 @@ ;; Host:*) host=${line#Host: } ;; + Accept:*) + accept=${line#Accept: } + ;; Authorization:*) authorization=${line#Authorization: } ;; @@ -231,7 +278,7 @@ *:*) ;; "") date=$(date +'%d/%m/%Y:%H:%M:%S %z') - status_length=$(execute "$method" "$path" "$authorization") + status_length=$(execute "$method" "$path" "$authorization" "$accept") printf "%s %s \"http://%s%s\" %s\n" "$date" "$method" "$host" "$path" "$status_length" trap - INT exit diff --git a/templates/item.txt b/templates/item.txt new file mode 100644 index 0000000000000000000000000000000000000000..7f9f4504afbbf23cd62be307d269ece42bed94d8 --- /dev/null +++ b/templates/item.txt @@ -0,0 +1,5 @@ +#!/bin/sh + +template() { + printf '%s' "$1" +} diff --git a/todo b/todo index 4a931d746455491497d6ea3b90a8f9246b004a6a..7ac3495f0ad0fe3b8ac305e72692f4254c529d77 100644 --- a/todo +++ b/todo @@ -4,7 +4,7 @@ GET /toys/ => html list of toys GET /toys/$name => html list of toy's items -GET /toys/$name/$id +GET /toys/$name/$id #Accept: (text/html | image/png |image/*webp | image/avif | text/plain) => logs of item container gets ARG $number and must put artifact in ~/toys/$name/$number/