]> git.plutz.net Git - confetti/commitdiff
Merge commit '36d5feed172cb12ccd469abddebf04eaf9a7c9ae' as 'cgilite'
authorPaul Hänsch <paul@plutz.net>
Sat, 2 Jan 2021 09:20:13 +0000 (10:20 +0100)
committerPaul Hänsch <paul@plutz.net>
Sat, 2 Jan 2021 09:20:13 +0000 (10:20 +0100)
1  2 
cgilite/cgilite.sh
cgilite/file.sh
cgilite/html-sh.sed
cgilite/logging.sh
cgilite/session.sh
cgilite/storage.sh

diff --combined cgilite/cgilite.sh
index 0000000000000000000000000000000000000000,f766ee2a425591245926952a5b961dde86cac4ee..f766ee2a425591245926952a5b961dde86cac4ee
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,268 +1,268 @@@
+ #!/bin/sh
+ # Copyright 2017 - 2020 Paul Hänsch
+ #
+ # This is CGIlite.
+ # A collection of posix shell functions for writing CGI scripts.
+ # 
+ # CGIlite is free software: you can redistribute it and/or modify
+ # it under the terms of the GNU Affero General Public License as published by
+ # the Free Software Foundation, either version 3 of the License, or
+ # (at your option) any later version.
+ # 
+ # CGIlite is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU Affero General Public License for more details.
+ # 
+ # You should have received a copy of the GNU Affero General Public License
+ # along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
+ [ -n "$include_cgilite" ] && return 0
+ # guard set after webserver part
+ # ksh and zsh workaround
+ # set -o posix # ksh, not portable
+ setopt -o OCTAL_ZEROES 2>&-
+ CR="\r"
+ BR='
+ '
+ cgilite_timeout=2
+ PATH(){ 
+   local str seg out
+   [ $# -eq 0 ] && str="$(cat)" || str="$*"
+   while [ "$str" ]; do
+     seg=${str%%/*}; str="${str#*/}"
+     case $seg in
+       ..) out="${out%/}"; out="${out%/*}/";;
+     .|'') out="${out%/}/";;
+        *) out="${out%/}/${seg}";;
+     esac;
+     [ "$seg" = "$str" ] && break
+   done
+   [ "${str}" -a "${out}" ] && printf %s "$out" || printf %s/ "${out%/}"
+ }
+ HEX_DECODE='
+   s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X;
+   # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
+   s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
+   s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
+   s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
+   s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
+   s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
+ '
+ HEX_DECODE(){
+   printf -- "$(printf %s "$1" |sed -E "$HEX_DECODE")"
+ }
+ if [ -z "$REQUEST_METHOD" ]; then
+   # no webserver variables means we are running via inetd / ncat
+   # so use builtin web server
+   # Use env from inetd as webserver variables
+   REMOTE_ADDR="${TCPREMOTEIP}"
+   SERVER_NAME="${TCPLOCALIP}"
+   SERVER_PORT="${TCPLOCALPORT}"
+   # Wait 2 seconds for request or kill connection through watchdog.
+   # Once Request is received the watchdog will be suspended (killed).
+   # At the end of the loop the watchdog will be restarted to enable
+   # timeout for the subsequent request.
+   (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
+   while read REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL; do
+     [ "${SERVER_PROTOCOL#HTTP/1.[01]${CR}}" ] && break
+     kill $cgilite_watchdog
+     SERVER_PROTOCOL="${SERVER_PROTOCOL%${CR}}"
+     PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}" |PATH)"
+     [ "${REQUEST_URI}" = "${REQUEST_URI#*\?}" ] \
+     && QUERY_STRING='' \
+     || QUERY_STRING="${REQUEST_URI#*\?}"
+     cgilite_headers=''; while read -r hl; do
+       hl="${hl%${CR}}"; [ "$hl" ] || break
+       case $hl in
+         'Content-Length: '*) CONTENT_LENGTH="${hl#*: }";;
+           'Content-Type: '*)   CONTENT_TYPE="${hl#*: }";;
+       esac
+       cgilite_headers="${cgilite_headers}${hl}${BR}"
+     done
+     export REMOTE_ADDR SERVER_NAME SERVER_PORT REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL \
+            PATH_INFO QUERY_STRING CONTENT_TYPE CONTENT_LENGTH
+     # Try to serve multiple requests, provided that script serves a
+     # Content-Length header.
+     # Without Content-Length header, connection will terminate after
+     # script.
+     cgilite_status='200 OK'; cgilite_response=''; cgilite_cl="Connection: close${CR}${BR}";
+     . "$0" | while read -r l; do case $l in
+       Status:*)
+         cgilite_status="${l#Status: }";;
+       Content-Length:*)
+         cgilite_cl=""
+         cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
+       Connection:*)
+         cgilite_cl="${l}${BR}";;
+       $CR) printf '%s %s\r\n%s%s\r\n' \
+              'HTTP/1.1' "${cgilite_status%${CR}}" \
+              "${cgilite_response}${cgilite_response:+${BR}}" "${cgilite_cl}"
+            cat || kill $$
+            [ "${cgilite_cl#Connection}" = "${cgilite_cl}" ]; exit;;
+       *) cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
+     esac; done || exit 0;
+     (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
+   done
+   kill $cgilite_watchdog
+   exit 0
+ fi
+ include_cgilite="$0"
+ if [ "${REQUEST_METHOD}" = POST -a "${CONTENT_LENGTH:-0}" -gt 0 -a \
+      "${CONTENT_TYPE}" = "application/x-www-form-urlencoded" ]; then
+   cgilite_post="$(head -c "$CONTENT_LENGTH")"
+ fi
+ debug(){ [ $# -gt 0 ] && printf '%s\n' "$@" >&2 || tee -a /dev/stderr; }
+ [ "${DEBUG+x}" ] && env >&2
+ cgilite_count(){
+   printf %s "&$1" \
+   | grep -oE '&'"$2"'=[^&]*' \
+   | wc -l
+ }
+ cgilite_value(){
+   local str="&$1" name="$2" cnt="${3:-1}"
+   while [ $cnt -gt 0 ]; do
+     [ "${str}" = "${str#*&${name}=}" ] && return 1
+     str="${str#*&${name}=}"
+     cnt=$((cnt - 1))
+   done
+   printf -- "$(printf %s "${str%%&*}" |sed -E 's;\+; ;g;'"$HEX_DECODE")"
+ }
+ cgilite_keys(){
+   local str="&$1"
+   while [ "${str#*&}" != "${str}" ]; do
+     str="${str#*&}"
+     printf '%s\n' "${str%%=*}"
+   done \
+   | sort -u
+ }
+ GET(){ cgilite_value "${QUERY_STRING}" $@; }
+ GET_COUNT(){ cgilite_count "${QUERY_STRING}" $1; }
+ GET_KEYS(){ cgilite_keys "${QUERY_STRING}"; }
+ POST(){ cgilite_value "${cgilite_post}" $@; }
+ POST_COUNT(){ cgilite_count "${cgilite_post}" $1; }
+ POST_KEYS(){ cgilite_keys "${cgilite_post}"; }
+ REF(){ cgilite_value "${HTTP_REFERER#*\?}" $@; }
+ REF_COUNT(){ cgilite_count "${HTTP_REFERER#*\?}" $1; }
+ REF_KEYS(){ cgilite_keys "${HTTP_REFERER#*\?}"; }
+ HEADER(){
+   # Read value of header line. Use this instead of
+   # referencing HTTP_* environment variables.
+   if [ -n "${cgilite_headers+x}" ]; then
+     local str="${BR}${cgilite_headers}"
+     [ "${str}" = "${str#*${BR}${1}: }" ] && return 1
+     str="${str#*${BR}${1}: }"
+     printf %s "${str%%${BR}*}"
+   else
+     local var="HTTP_$(printf %s "$1" |tr a-z- A-Z-)"
+     eval "[ \"\$$var\" ] && printf %s \"\$$var\" || return 1"
+     # eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\""
+   fi
+ }
+ COOKIE(){
+   HEX_DECODE "$(
+     HEADER Cookie \
+     | grep -oE '(^|; ?)'"$1"'=[^;]*' \
+     | sed -En "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
+   )"
+ }
+ HTML(){
+   # Escape HTML cahracters
+   # Also escape [, ], and \n for use in html-sh
+   local str out
+   [ $# -eq 0 ] && str="$(cat)" || str="$*"
+   while [ "$str" ]; do
+     case $str in
+       \&*) out="${out}&amp;";;
+       \<*) out="${out}&lt;";;
+       \>*) out="${out}&gt;";;
+       \"*) out="${out}&quot;";;
+       \'*) out="${out}&#x27;";;
+       \[*) out="${out}&#x5B;";;
+       \]*) out="${out}&#x5D;";;
+       "${CR}"*) out="${out}&#x0D;";;
+       "${BR}"*) out="${out}&#x0A;";;
+       *) out="${out}${str%"${str#?}"}";;
+     esac
+     str="${str#?}"
+   done
+   printf %s "$out"
+ }
+ URL(){
+   # Escape pathes, so they can be used in link tags and HTTP Headers
+   local str out
+   [ $# -eq 0 ] && str="$(cat)" || str="$*"
+   while [ "$str" ]; do
+     case $str in
+       \&*) out="${out}%26";;
+       \"*) out="${out}%22";;
+       \'*) out="${out}%27";;
+       \?*) out="${out}%3F";;
+       \#*) out="${out}%23";;
+       \[*) out="${out}%5B";;
+       \]*) out="${out}%5D";;
+       \ *) out="${out}%20";;
+       "       "*) out="${out}%09";;
+       "${CR}"*) out="${out}%0D";;
+       "${BR}"*) out="${out}%0A";;
+       %*) out="${out}%25";;
+       *) out="${out}${str%"${str#?}"}";;
+     esac
+     str="${str#?}"
+   done
+   printf %s "$out"
+ }
+ SET_COOKIE(){
+   # Param: session | +seconds | [date]
+   # Param: name=value
+   # Param: Path= | Domain= | Secure
+   local expire cookie
+   case "$1" in
+     ''|0|session) expire='';;
+     [+-][0-9]*)   expire="$(date -R -d @$(($(date +%s) + $1)))";;
+     *)            expire="$(date -R -d "$1")";;
+   esac
+   cookie="$2"
+   printf 'Set-Cookie: %s; HttpOnly; SameSite=Lax' "$cookie"
+   [ -n "$expire" ] && printf '; Expires=%s' "${expire%+????}${expire:+GMT}"
+   [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
+   printf '\r\n'
+ }
+ REDIRECT(){
+   printf '%s: %s\r\n' \
+     Status "303 See Other" \
+     Content-Length 0 \
+     Location "$*"
+   printf '\r\n'
+   exit 0
+ }
diff --combined cgilite/file.sh
index 0000000000000000000000000000000000000000,04a8ef617c9f755a4dcb7c3cf3adeeca69683f27..04a8ef617c9f755a4dcb7c3cf3adeeca69683f27
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,126 +1,126 @@@
+ #!/bin/sh
+ # Copyright 2016 - 2019 Paul Hänsch
+ #
+ # This file is part of cgilite.
+ # 
+ # cgilite is free software: you can redistribute it and/or modify
+ # it under the terms of the GNU Affero General Public License as published by
+ # the Free Software Foundation, either version 3 of the License, or
+ # (at your option) any later version.
+ # 
+ # cgilite is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU Affero General Public License for more details.
+ # 
+ # You should have received a copy of the GNU Affero General Public License
+ # along with cgilite.  If not, see <http://www.gnu.org/licenses/>. 
+ [ -n "$include_fileserve" ] && return 0
+ include_fileserve="$0"
+ file_type(){
+   case ${1##*.} in
+     html|html) printf 'text/html';;
+     css)       printf 'text/css';;
+     js)        printf 'text/javascript';;
+     txt)       printf 'text/plain';;
+     sh)        printf 'text/shellscript';;
+     jpg|jpeg)  printf 'image/jpeg';;
+     png)       printf 'image/png';;
+     svg)       printf 'image/svg+xml';;
+     gif)       printf 'image/gif';;
+     webm)      printf 'video/webm';;
+     mp4|m4v)   printf 'video/mp4';;
+     m4a)       printf 'audio/mp4';;
+     ogg)       printf 'audio/ogg';;
+     xml)       printf 'application/xml';;
+     m3u8)      printf 'application/x-mpegURL';;
+     ts)        printf 'video/MP2T';;
+     mpd)       printf 'application/dash+xml';;
+     m4s)       printf 'video/iso.segment';;
+     *)         printf 'application/octet-stream';;
+   esac
+ }
+ FILE(){
+   local file file_size file_date http_date cachedate range mime
+   file="$1" mime="$2"
+   if ! [ -f "$file" ]; then
+     printf 'Content-Length: 0\r\nStatus: 404 Not Found\r\n\r\n'
+     exit 0
+   elif ! [ -r "$file" ]; then
+     printf 'Content-Length: 0\r\nStatus: 403 Forbidden\r\n\r\n'
+     exit 0
+   fi
+   file_size="$(stat -Lc %s "$file")"
+   file_date="$(stat -Lc %Y "$file")"
+   http_date="$(date -uRd @$file_date)"
+   http_date="${http_date%+0000}GMT"
+   cachedate="$(
+     # Parse the allowable date formats from Section 3.3.1 of
+     # https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
+     HEADER If-Modified-Since \
+     | sed -E 's;^[^ ]+, ([0-9]{2}) (...) ([0-9]{4}) (..:..:..) GMT$;\3-\2-\1 \4;;
+               s;^[^ ]+, ([0-9]{2})-(...)-([789][0-9]) (..:..:..) GMT$;19\3-\2-\1 \4;;
+               s;^[^ ]+, ([0-9]{2})-(...)-([0-6][0-9]) (..:..:..) GMT$;20\3-\2-\1 \4;;
+               s;^[^ ]+ (...) ([0-9]{2}) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
+               s;^[^ ]+ (...)  ([0-9]) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
+               s;Jan;01;; s;Feb;02;; s;Mar;03;; s;Apr;04;; s;May;05;; s;Jun;06;;
+               s;Jul;07;; s;Aug;08;; s;Sep;09;; s;Oct;10;; s;Nov;11;; s;Dec;12;;' \
+     | xargs -r0 date +%s -ud 2>&-
+   )"
+   range="$(HEADER Range |sed -nE 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
+   case "$range" in
+     *-) range="${range}$((file_size - 1))";;
+     -*) [ ${range#-} -le $file_size ] \
+         && range="$((file_size - ${range#-}))-$((file_size - 1))" \
+         || range="0-$((file_size - 1))";;
+     *-*) [ ${range#*-} -ge $file_size ] \
+          && range="${range%-*}-$((file_size - 1))";;
+   esac
+   if [ "$file_date" -lt "$cachedate" ] 2>&-; then
+     printf '%s: %s\r\n' \
+       Status '304 Not Modified' \
+       Content-Length 0 \
+       Last-Modified "$http_date"
+     printf '\r\n'
+   
+   elif [ -z "$range" ]; then
+     printf '%s: %s\r\n' \
+       Status "200 OK" \
+       Accept-Ranges bytes \
+       Last-Modified "$http_date" \
+       Content-Type "${mime:-$(file_type "$file")}" \
+       Content-Length $file_size
+     printf '\r\n'
+   
+     [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
+   elif [ "${range%-*}" -le "${range#*-}" ]; then
+     printf '%s: %s\r\n' \
+       Status "206 Partial Content" \
+       Accept-Ranges bytes \
+       Last-Modified "$http_date" \
+       Content-Type "${mime:-$(file_type "$file")}" \
+       Content-Range "bytes ${range}/${file_size}" \
+       Content-Length "$((${range#*-} - ${range%-*} + 1))"
+     printf '\r\n'
+   
+     [ "$REQUEST_METHOD" != HEAD ] \
+     && tail -c+$((${range%-*} + 1)) "$file" \
+      | head -c "$((${range#*-} - ${range%-*} + 1))"
+   elif [ "${range%-*}" -gt "${range#*-}" ]; then
+     printf '%s: %s\r\n' \
+       Status "216 Range Not Satisfiable" \
+       Content-Length 0 \
+       Content-Range \*/${file_size}
+     printf '\r\n'
+   fi
+ }
diff --combined cgilite/html-sh.sed
index 0000000000000000000000000000000000000000,8d7b61c076b93d3de8b59683e67d9b161ffc8075..8d7b61c076b93d3de8b59683e67d9b161ffc8075
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,69 +1,69 @@@
+ #!/bin/sed -nEf
+ :Escapes
+ s,\\\\,\&#92;,g; s,\\&,\&amp;,g;
+ s,\\<,\&lt;,g; s,\\>,\&gt;,g;
+ s,\\",\&quot;,g; s,\\',\&apos;,g;
+ s,\\\[,\&#91;,g; s,\\\],\&#93;,g;
+ s,\\\.,\&#46;,g; s,\\#,\&#35;,g;
+ s,\\,,g;
+ :CommentHandle
+ x; /^<\/!-->/{
+   x; /--]/{
+     H; s;^(.*)--].*$;\1-->;p;
+     g; s;^.*--]([^\n]*)$;\1;
+     x; s;^</!-->\n(.*)\n[^\n]*$;\1;; x;
+     bCommentEnd
+   }
+   p; b;
+ }
+ x;
+ :CommentEnd
+ :shortcuts
+ s;\[hidden[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="hidden" name="\1" value="\2";g;
+ s;\[checkbox[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="checkbox" name="\1" value="\2";g;
+ s;\[radio[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="radio" name="\1" value="\2";g;
+ s;\[submit[ \t]+"([^"]*)"[ \t]+"([^"]*)";[button type="submit" name="\1" value="\2";g;
+ s;\[a[ \t]+"([^"]*)";[a href="\1";g;
+ s;\[img[ \t]+"([^"]*)"[ \t]+"([^"]*)";[img src="\1" alt="\2";g;
+ s;\[!([^]\[]*)\];<!\1>;g;
+ s;\[!--([^]\[]*)--\];<!--\1-->;g;
+ :tags
+ s;\[([^]\[< \t]+)([^]\[]*)\];<\1>\2</\1>;g;
+ t tags;
+ G;
+ :tagclose
+ s;^([^]\n]*)\]([^\n]*)\n([^\n]+);\1\3\2;
+ t tagclose;
+ h; s;^([^\n]*)\n;;; x; s;\n.*$;;;
+ :tagopen
+ s;^([^\[\n]*)\[([^]\[< \t\n]+)([^\n]*);\1<\2>\3\n</\2>;
+ t tagopen;
+ G; h; s;^[^\n]*\n+;;; x; s;\n.*$;;;
+ :attribs
+ s;class="([^>]+)>[ \t]*\.([^< \t]+);class="\2 \1>;g; t attribs;
+ s;(<[^/][^>]*)>[ \t]*\.([^< \t]+);\1 class="\2">;g;
+ s;(<[^/][^>]*)>[ \t]*#([^< \t]+);\1 id="\2">;g;
+ s;(<[^/][^>]*)>[ \t]*([^ \t=<]+=("[^"]*"|'[^']*'|[^< \t]*));\1 \2>;g;
+ t attribs;
+ s;(<input ([^>]+ )?type=(radio|"radio"|'radio')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g;
+ s;(<input ([^>]+ )?type=(checkbox|"checkbox"|'checkbox')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g;
+ s;(<option( [^>]+)?)>[ \t]*(checked|selected);\1 selected="selected">;g;
+ s;(<select( [^>]+)?)>[ \t]*multiple;\1 multiple="multiple">;g;
+ t attribs;
+ s;(<[^/][^>]*>)[ \t]*\.[ \t];\1;g;
+ s;(<[^/][^>]*>)[ \t]*;\1;g;
+ # s;(<[^/][^>]*)>[ \t]*</[^>]+>;\1/>;g;
+ s;(<(br|hr|img|input|link|meta|area|base|col|command|embed|keygen|param|source|track|wbr)[^>]*)>[ \t]*</\1>;\1>;g;
+ s;<!-->;<!--;;
+ p; ${g; s;\n+;;g; p;}
diff --combined cgilite/logging.sh
index 0000000000000000000000000000000000000000,31bb24d3c4d3b9abbcb7bb175bcd455d5a078d2c..31bb24d3c4d3b9abbcb7bb175bcd455d5a078d2c
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,32 +1,32 @@@
+ #!/bin/sh
+ # LOGLEVEL 1: Crash condition
+ # LOGLEVEL 2: Unexpected condition
+ # LOGLEVEL 3: Failed action (i.e. due to config error)
+ # LOGLEVEL 4: Debug
+ [ -n "$include_logging" ] && return 0
+ include_logging="$0"
+ LOGLEVEL="${LOGLEVEL:-3}"
+ LOGFILE="${LOGFILE:-/dev/stderr}"
+ logmsg(){
+   local ll="${1:-3}"
+   shift 1
+   if [ "$ll" -le "$LOGLEVEL" -a "$#" -gt 0 ]; then
+     printf %s\\n "$*" >>"${LOGFILE}"
+   elif [ "$ll" -le "$LOGLEVEL" ]; then
+     tee -a "${LOGFILE}"
+   elif [ ! "$#" -gt 0 ]; then
+     cat
+   fi
+ }
+ die(){
+   [ "$#" -gt 0 ] && logmsg 1 "$@"
+   exit 1
+ }
+ panic(){ logmsg 2 "$@"; }
+ error(){ logmsg 3 "$@"; }
+ debug(){ logmsg 4 "$@"; }
diff --combined cgilite/session.sh
index 0000000000000000000000000000000000000000,12788d3eb534292349b8146ac1c50db80118398b..12788d3eb534292349b8146ac1c50db80118398b
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,83 +1,83 @@@
+ #!/bin/sh
+ [ -n "$include_session" ] && return 0
+ include_session="$0"
+ _DATE="$(date +%s)"
+ SESSION_TIMEOUT="${SESSION_TIMEOUT:-7200}"
+ server_key(){
+   IDFILE="${IDFILE:-${_DATA:-.}/serverkey}"
+   if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then
+     dd count=1 bs=512 if=/dev/urandom \
+     | tee "$IDFILE"
+   fi 2>&-
+ }
+ slopecode(){
+   # 6-Bit Code that retains sort order of input data, while beeing safe to use
+   # in ascii transmissions, unix file names, HTTP URLs, and HTML attributes
+   uuencode -m - | sed '
+     1d;$d; 
+     y;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/;0123456789:=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;
+   '
+ }
+ randomid(){
+   dd bs=12 count=1 if=/dev/urandom 2>&- \
+   | slopecode
+ }
+ timeid(){
+   d=$(($_DATE % 4294967296))
+   { printf "$(
+       printf \\%o \
+         $((d / 16777216 % 256)) \
+         $((d / 65536 % 256)) \
+         $((d / 256 % 256)) \
+         $((d % 256))
+     )"
+     dd bs=8 count=1 if=/dev/urandom 2>&-
+   } | slopecode
+ }
+ checkid(){ grep -m 1 -xE '[0-9a-zA-Z:=]{16}'; }
+ transid(){
+   # transaction ID to modify a given file
+   local file="$1"
+   { stat -c %F%i%n%N%s%Y "$file" 2>&-
+     printf %s "$SESSION_ID"
+     server_key
+   } | sha256sum | cut -d\  -f1
+ }
+ update_session(){
+   local session sid time sig serverkey checksig
+   IFS=- read -r sid time sig <<-END
+       $(POST session_key || COOKIE session)
+       END
+   serverkey="$(server_key)"
+   
+   checksig="$(printf %s "$sid" "$time" "$serverkey" | sha256sum)"
+   checksig="${checksig%% *}"
+   
+   if ! [ "$checksig" = "$sig" \
+     -a "$time" -ge "$_DATE" \
+     -a "$(printf %s "$sid" |checkid)" ] 2>&-
+   then
+     debug "Setting up new session"
+     sid="$(randomid)"
+   fi
+   time=$(( $_DATE + $SESSION_TIMEOUT ))
+   sig="$(printf %s "$sid" "$time" "$serverkey" |sha256sum)"
+   sig="${sig%% *}"
+   printf %s\\n "${sid}-${time}-${sig}"
+ }
+ SESSION_KEY="$(update_session)"
+ SET_COOKIE 0 session="$SESSION_KEY" Path=/ SameSite=Strict HttpOnly
+ SESSION_ID="${SESSION_KEY%%-*}"
diff --combined cgilite/storage.sh
index 0000000000000000000000000000000000000000,7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6..7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,129 +1,129 @@@
+ #!/bin/sh
+ # Copyright 2018, 2019 Paul Hänsch
+ #
+ # This is a file format helper, part of CGIlite.
+ # 
+ # CGIlite is free software: you can redistribute it and/or modify
+ # it under the terms of the GNU Affero General Public License as published by
+ # the Free Software Foundation, either version 3 of the License, or
+ # (at your option) any later version.
+ # 
+ # CGIlite is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU Affero General Public License for more details.
+ # 
+ # You should have received a copy of the GNU Affero General Public License
+ # along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
+ [ -n "$include_storage" ] && return 0
+ include_storage="$0"
+ CR="\r"
+ BR='
+ '
+ LOCK(){
+   local lock timeout block
+   lock="${1}.lock"
+   timeout="${2-20}"
+   if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
+     printf 'Impossible to get lock: %s\n' "$lock" >&2
+     return 1
+   fi
+   while ! mkdir "$lock" 2>&-; do
+     block="$(cat "$lock/pid" || printf 1)"
+     if ! { ps -eo pid |grep -qwF "$block"; }; then
+       printf 'Overriding stale lock: %s\n' "$lock" >&2
+       break
+     fi
+     if [ $timeout -le 0 ]; then
+       printf 'Timeout while trying to get lock: %s\n' "$lock" >&2
+       return 1
+     fi
+     timeout=$((timeout - 1))
+     sleep 1
+   done
+   printf '%i\n' $$ >"${lock}/pid"
+   return 0
+ }
+ RELEASE(){
+   local lock
+   lock="${1}.lock"
+   if [ "$(cat "$lock/pid")" = "$$" ]; then
+     rm "$lock/pid"
+     if ! rmdir "$lock"; then
+       printf 'Cannot remove tainted lock: %s\n' "$lock" >&2
+       printf '%i\n' $$ >"${lock}/pid"
+       return 1
+     fi
+     return 0
+   else
+     printf 'Refusing to release foreign lock: %s\n' "$lock" >&2
+     return 1
+   fi
+ }
+ STRING='
+   s;\\;\\\\;g;
+   s;\n;\\n;g;
+   s;\t;\\t;g;
+   s;\r;\\r;g;
+   s;\+;\\+;g;
+   s; ;+;g;
+ '
+ STRING_OLD(){
+   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+   | sed -E ':X; $!{N;bX;}'"$STRING"
+ }
+ STRING(){
+   local in out=''
+   [ $# -gt 0 ] && in="$*" || in="$(cat)"
+   while [ "$in" ]; do case $in in
+     \\*) out="${out}\\\\"; in="${in#\\}" ;;
+     "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
+     "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
+     " "*) out="${out}\\t"; in="${in#  }" ;;
+     +*) out="${out}\\+"; in="${in#+}" ;;
+     " "*) out="${out}+"; in="${in# }" ;;
+     *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR}   + ]*}"}" ;;
+   esac; done
+   printf '%s' "$out"
+ }
+ UNSTRING='
+   :UNSTRING_X
+   s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
+   s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
+   s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
+   s;((^|[^\\])(\\\\)*)\+;\1 ;g;
+   tUNSTRING_X;
+   s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
+   s;\\\\;\\;g;
+ '
+ UNSTRING_OLD(){
+   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+   | sed -E "$UNSTRING"
+ }
+ UNSTRING(){
+   local in out=''
+   [ $# -gt 0 ] && in="$*" || in="$(cat)"
+   while [ "$in" ]; do case $in in
+     \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
+     \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
+     \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
+     \\t*) out="${out} "; in="${in#\\t}" ;;
+     \\+) out="${out}+"; in="${in#\\+}" ;;
+     +*) out="${out} "; in="${in#+}" ;;
+     \\*) in="${in#\\}" ;;
+     *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
+   esac; done
+   printf '%s' "$out"
+ }