From d9ad948874892f44ff26e85e1336acebe2455ed7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Mon, 9 Dec 2019 00:17:17 +0100 Subject: [PATCH 1/1] Squashed 'cgilite/' content from commit f0bc458 git-subtree-dir: cgilite git-subtree-split: f0bc458f738e3171e60424d300b937fdbec2583f --- cgilite.sh | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++ file.sh | 121 +++++++++++++++++++++++++++++ html-sh.sed | 68 ++++++++++++++++ logging.sh | 32 ++++++++ session.sh | 83 ++++++++++++++++++++ storage.sh | 93 ++++++++++++++++++++++ 6 files changed, 614 insertions(+) create mode 100755 cgilite.sh create mode 100755 file.sh create mode 100755 html-sh.sed create mode 100755 logging.sh create mode 100755 session.sh create mode 100755 storage.sh diff --git a/cgilite.sh b/cgilite.sh new file mode 100755 index 0000000..333334c --- /dev/null +++ b/cgilite.sh @@ -0,0 +1,217 @@ +#!/bin/sh + +# Copyright 2017 - 2018 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 . + +# ksh and zsh workaround +# set -o posix # ksh, not portable +setopt -o OCTAL_ZEROES 2>&- + +CR=" " +BR=' +' +cgilite_timeout=2 + +HEADER(){ + # Read value of header line. Use this instead of + # referencing HTTP_* environment variables. + if [ -n "${cgilite_headers+x}" ]; then + printf %s "$cgilite_headers" \ + | sed -En 's;^'"${1}"': ([^\r]+)\r?$;\1;i; tX; d; :X;p;q;' + else + eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\"" + fi +} + +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 + kill $cgilite_watchdog + PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}")" + QUERY_STRING="${REQUEST_URI#*\?}" + cgilite_headers="$(while read -r hl; do [ "${hl%${CR}}" ] && printf '%s\n' "$hl" || break; done )" + + HTTP_CONTENT_LENGTH="$(HEADER Content-Length |grep -xE '[0-9]+')" + + export REMOTE_ADDR SERVER_NAME SERVER_PORT REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL \ + PATH_INFO QUERY_STRING HTTP_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}"; + . "$0" | while read -r l; do case $l in + Status:*) cgilite_status="${l#Status: }";; + Content-Length:*) cgilite_cl="${l}";; + $CR) printf '%s %s\r\n%s\n%s\n\r\n' \ + 'HTTP/1.1' "${cgilite_status%${CR}}" \ + "$cgilite_response" "${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 +fi + +if [ "$REQUEST_METHOD" = POST -a "${HTTP_CONTENT_LENGTH:=${CONTENT_LENGTH:=0}}" -gt 0 ]; then + cgilite_post="$(head -c "$HTTP_CONTENT_LENGTH")" +fi + +[ -n "${DEBUG+x}" ] && env + +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}=} + 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#*\?}"; } + +COOKIE(){ + HEX_DECODE "$( + HEADER Cookie \ + | grep -oE '(^|; ?)'"$1"'=[^;]*' \ + | sed -En "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}' + )" +} + +HTML(){ + # HTML Entity Coding + # Prints UTF-8 string as decimal Unicode Code Points + # Useful for escaping user input for use in HTML text and attributes + { [ $# -eq 0 ] && cat || printf %s "$*"; } \ + | hexdump -ve '/1 "%03o\n"' \ + | while read n; do + case $n in + # bitbanging octal UTF-8 chains into singular 7 digit octal numbers + [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do + 2??) printf '%s' ${n#2};; # tail fragment, append 6 bit + 3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start + 34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start + 35?) printf '01%s' ${n#35};; # 3 octet chain start, high + 36?) printf '%s' ${n#36};; # 4 octet (21 bit) chain start + esac + done \ + | sed -E 's;.{7};&\n;g;' \ + | while read n; do + printf '&#%d;' $((0$n)) + done +} + +URL(){ + # Code every character in URL escape hex format + # except alphanumeric ascii + + { [ $# -eq 0 ] && cat || printf %s "$*"; } \ + | hexdump -v -e '/1 ",%02X"' \ + | sed 's;,;%;g; s;%2F;/;g;' +} + +PATH(){ + { [ $# -eq 0 ] && cat || printf %s "$*"; } \ + | sed -E 's;^.*$;/&/;; s;/+;/;g; + :X; + s;^/\.\./;/;; s;/\./;/;g; + tX; + s;/[^/]+/\.\./;/;; + tX; + s;^(/.*)/$;\1;' +} + + +SET_COOKIE(){ + 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' "$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 --git a/file.sh b/file.sh new file mode 100755 index 0000000..51ec245 --- /dev/null +++ b/file.sh @@ -0,0 +1,121 @@ +#!/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 . + +[ -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) printf 'video/mp4';; + ogg) printf 'audio/ogg';; + xml) printf 'application/xml';; + *) 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 --git a/html-sh.sed b/html-sh.sed new file mode 100755 index 0000000..976d42e --- /dev/null +++ b/html-sh.sed @@ -0,0 +1,68 @@ +#!/bin/sed -nEf + +:Escapes +s,\\\\,\\,g; s,\\&,\&,g; +s,\\<,\<,g; s,\\>,\>,g; +s,\\",\",g; s,\\',\',g; +s,\\\[,\[,g; s,\\\],\],g; +s,\\\.,\.,g; s,\\#,\#,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;\[!([^]\[]*)\];;g; +s;\[!--([^]\[]*)--\];;g; + +:tags +s;\[([^]\[< \t]+)([^]\[]*)\];<\1>\2;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; +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;(]+ )?type=(radio|"radio"|'radio')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g; +s;(]+ )?type=(checkbox|"checkbox"|'checkbox')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g; +s;(]+)?)>[ \t]*(checked|selected);\1 selected="selected">;g; +s;(]+)?)>[ \t]*multiple;\1 multiple="multiple">;g; +t attribs; + +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>;g; + +s;;