VRSN=0.1
# State
REDIRECTS=0
+# Bash options
+# shopt -s extglob
bollux_usage() {
cat <<END
exit "$ec"
}
-trim() { sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; }
+# pure bash bible trim_string
+trim() {
+ : "${1#"${1%%[![:space:]]*}"}"
+ : "${_%"${_##*[![:space:]]}"}"
+ printf '%s\n' "$_"
+}
log() {
[[ "$BOLLUX_LOGLEVEL" == QUIET ]] && return
URL="$(run transform_resource "$BOLLUX_URL" "$1")"
fi
[[ "$URL" != *://* ]] && URL="$BOLLUX_PROTO://$URL"
- URL="$(trim <<<"$URL")"
+ URL="$(trim "$URL")"
server="${URL#*://}"
server="${server%%/*}"
+ log d "URL='$URL' server='$server'"
+
run request_url "$server" "$BOLLUX_PORT" "$URL" |
run handle_response "$URL"
}
transform_resource() { # transform_resource BASE_URL REFERENCE_URL
declare -A R B T # reference, base url, target
- eval "$(parse_url B "$1")"
- eval "$(parse_url R "$2")"
+ eval "$(run parse_url B "$1")"
+ eval "$(run parse_url R "$2")"
# A non-strict parser may ignore a scheme in the reference
# if it is identical to the base URI's scheme.
if ! "${STRICT:-true}" && [[ "${R[scheme]}" == "${B[scheme]}" ]]; then
T[scheme]="${R[scheme]}"
isdefined "R[authority]" && T[authority]="${R[authority]}"
isdefined R[path] &&
- T[path]="$(remove_dot_segments "${R[path]}")"
+ T[path]="$(run remove_dot_segments "${R[path]}")"
isdefined "R[query]" && T[query]="${R[query]}"
else
if isdefined "R[authority]"; then
parse_url() { # eval "$(split_url NAME STRING)" => NAME[...]
local name="$1"
local string="$2"
+ # shopt -u extglob # TODO port re ^ to extglob syntax
local re='^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?'
[[ $string =~ $re ]] || return $?
+ # shopt -s extglob
local scheme="${BASH_REMATCH[2]}"
local authority="${BASH_REMATCH[4]}"
for c in scheme authority query fragment; do
[[ "${!c}" ]] &&
- printf '%s[%s]=%q\n' "$name" "$c" "${!c}"
+ run printf '%s[%s]=%q\n' "$name" "$c" "${!c}"
done
# unclear if the path is always set even if empty but it looks that way
- printf '%s[path]=%q\n' "$name" "$path"
+ run printf '%s[path]=%q\n' "$name" "$path"
}
# is a NAME defined ('set' in bash)?
REDIRECTS=0
BOLLUX_URL="$URL"
run prompt "$meta" QUERY
+ # shellcheck disable=2153
run blastoff "?$QUERY"
;;
2*)
display() {
case "$1" in
*\;*)
- mime="$(cut -d\; -f1 <<<"$1" | trim)"
- charset="$(cut -d\; -f2 <<<"$1" | trim)"
+ mime="${1%;*}"
+ charset="${1#*;}"
+ trim "$mime"
+ trim "$charset"
+ log d "$mime $charset"
;;
- *) mime="$(trim <<<"$1")" ;;
+ *) mime="$(trim "$1")" ;;
esac
[[ -z "$mime" ]] && mime="text/gemini"
}
normalize_crlf() {
- gawk 'BEGIN{RS="\n\n"}{gsub(/\r\n?/,"\n");print;print ""}'
+ while read -r line; do
+ printf '%s\n' "${line//$'\r'?($'\n')/}"
+ done
}
typeset_gemini() {