made tomcat extension parsing in-line with upcoming release of rust rewrite

This commit is contained in:
Emma Tebibyte 2023-03-22 13:41:32 -04:00
parent 494d9ad140
commit 265a1b9b95
Signed by: emma
GPG Key ID: 6D661C738815E7DD
1 changed files with 49 additions and 37 deletions

View File

@ -20,6 +20,19 @@ set -e
argv0="$0" argv0="$0"
get_urls() {
i="0"
until ! tomcat replace.urls["$i"] "$file" >/dev/null 2>&1
do
URLS="$(printf "%s\n%s" \
"$URLS" \
"$(tomcat replace.urls["$i"] "$file")"
)"
i="$(printf "%s+1\n" "$i" | bc)"
done
}
# check if usage is valid # check if usage is valid
if test -z "$1"; then if test -z "$1"; then
printf "Usage: %s [resource...]\n" "$argv0" 1>&2 printf "Usage: %s [resource...]\n" "$argv0" 1>&2
@ -28,8 +41,7 @@ fi
for dep in \ for dep in \
curl \ curl \
handlr \ handlr
tomcat
do do
if ! command -v "$dep" >/dev/null 2>&1; then if ! command -v "$dep" >/dev/null 2>&1; then
printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2 printf "%s: Missing dependency: %s(1)\n" "$argv0" "$dep" 1>&2
@ -46,9 +58,8 @@ fi
while test -n "$1"; do while test -n "$1"; do
URL="$1" URL="$1"
# use curl(1) to write out the request header's content_type, # use curl(1) to write out the request header's content_type, strip everything
# strip everything after the first semicolon, # after the first semicolon, chop off any weird whitespace remnants
# chop off any weird whitespace remnants
MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \
| xargs echo)" | xargs echo)"
@ -56,39 +67,40 @@ while test -n "$1"; do
MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \
| sed -e 's/\/.*\+//g')" | sed -e 's/\/.*\+//g')"
# run through MIME extensions # only check for extensions if tomcat(1) is installed
for file in \ if command -v tomcat >/dev/null 2>&1; then
"$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ # run through MIME extensions
/usr/share/xdg-sanity/*-mime.toml \ for file in \
/usr/local/share/xdg-sanity/*-mime.toml "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \
do /usr/share/xdg-sanity/*-mime.toml \
if test -e "$file"; then /usr/local/share/xdg-sanity/*-mime.toml
i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l) do
while ! [ "$i" = 0 ]; do if test -e "$file"; then
if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then get_urls
MIME="$(tomcat with.mime "$file")" for url in $URLS; do
fi if [ "$MATCH" = "$url" ]; then
i="$(printf "%s-1\n" "$i" | bc)" MIME="$(tomcat with.mime "$file")"
done fi
fi done
done fi
done
# and the replace extensions # and the replace extensions
for file in \ for file in \
"$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \
/usr/share/xdg-sanity/*-replace.toml \ /usr/share/xdg-sanity/*-replace.toml \
/usr/local/share/xdg-sanity/*-replace.toml /usr/local/share/xdg-sanity/*-replace.toml
do do
if test -e "$file"; then if test -e "$file"; then
i="$(tomcat replace.urls "$file" | sed 's/ /\n/g' | wc -l)" get_urls
while ! [ "$i" = 0 ]; do for url in $URLS; do
if [ "$MATCH" = "$(tomcat replace.urls["$i"] "$file")" ]; then if [ "$MATCH" = "$url" ]; then
URL="$(tomcat with.url "$file")" URL="$(tomcat with.url "$file")"
fi fi
i="$(printf "%s-1\n" "$i" | bc)" done
done fi
fi done
done fi
# these commands may fail; this is intentional # these commands may fail; this is intentional
if [ "$MIME" = "text/html" ]; then if [ "$MIME" = "text/html" ]; then