about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xbollux39
1 files changed, 28 insertions, 11 deletions
diff --git a/bollux b/bollux index 0686c21..87c35fb 100755 --- a/bollux +++ b/bollux
@@ -9,6 +9,8 @@ PRGN="${0##*/}"
9VRSN=0.1 9VRSN=0.1
10# State 10# State
11REDIRECTS=0 11REDIRECTS=0
12# Bash options
13# shopt -s extglob
12 14
13bollux_usage() { 15bollux_usage() {
14 cat <<END 16 cat <<END
@@ -38,7 +40,12 @@ die() {
38 exit "$ec" 40 exit "$ec"
39} 41}
40 42
41trim() { sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; } 43# pure bash bible trim_string
44trim() {
45 : "${1#"${1%%[![:space:]]*}"}"
46 : "${_%"${_##*[![:space:]]}"}"
47 printf '%s\n' "$_"
48}
42 49
43log() { 50log() {
44 [[ "$BOLLUX_LOGLEVEL" == QUIET ]] && return 51 [[ "$BOLLUX_LOGLEVEL" == QUIET ]] && return
@@ -125,19 +132,21 @@ blastoff() { # load a url
125 URL="$(run transform_resource "$BOLLUX_URL" "$1")" 132 URL="$(run transform_resource "$BOLLUX_URL" "$1")"
126 fi 133 fi
127 [[ "$URL" != *://* ]] && URL="$BOLLUX_PROTO://$URL" 134 [[ "$URL" != *://* ]] && URL="$BOLLUX_PROTO://$URL"
128 URL="$(trim <<<"$URL")" 135 URL="$(trim "$URL")"
129 136
130 server="${URL#*://}" 137 server="${URL#*://}"
131 server="${server%%/*}" 138 server="${server%%/*}"
132 139
140 log d "URL='$URL' server='$server'"
141
133 run request_url "$server" "$BOLLUX_PORT" "$URL" | 142 run request_url "$server" "$BOLLUX_PORT" "$URL" |
134 run handle_response "$URL" 143 run handle_response "$URL"
135} 144}
136 145
137transform_resource() { # transform_resource BASE_URL REFERENCE_URL 146transform_resource() { # transform_resource BASE_URL REFERENCE_URL
138 declare -A R B T # reference, base url, target 147 declare -A R B T # reference, base url, target
139 eval "$(parse_url B "$1")" 148 eval "$(run parse_url B "$1")"
140 eval "$(parse_url R "$2")" 149 eval "$(run parse_url R "$2")"
141 # A non-strict parser may ignore a scheme in the reference 150 # A non-strict parser may ignore a scheme in the reference
142 # if it is identical to the base URI's scheme. 151 # if it is identical to the base URI's scheme.
143 if ! "${STRICT:-true}" && [[ "${R[scheme]}" == "${B[scheme]}" ]]; then 152 if ! "${STRICT:-true}" && [[ "${R[scheme]}" == "${B[scheme]}" ]]; then
@@ -149,7 +158,7 @@ transform_resource() { # transform_resource BASE_URL REFERENCE_URL
149 T[scheme]="${R[scheme]}" 158 T[scheme]="${R[scheme]}"
150 isdefined "R[authority]" && T[authority]="${R[authority]}" 159 isdefined "R[authority]" && T[authority]="${R[authority]}"
151 isdefined R[path] && 160 isdefined R[path] &&
152 T[path]="$(remove_dot_segments "${R[path]}")" 161 T[path]="$(run remove_dot_segments "${R[path]}")"
153 isdefined "R[query]" && T[query]="${R[query]}" 162 isdefined "R[query]" && T[query]="${R[query]}"
154 else 163 else
155 if isdefined "R[authority]"; then 164 if isdefined "R[authority]"; then
@@ -239,8 +248,10 @@ remove_dot_segments() { # 5.2.4
239parse_url() { # eval "$(split_url NAME STRING)" => NAME[...] 248parse_url() { # eval "$(split_url NAME STRING)" => NAME[...]
240 local name="$1" 249 local name="$1"
241 local string="$2" 250 local string="$2"
251 # shopt -u extglob # TODO port re ^ to extglob syntax
242 local re='^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?' 252 local re='^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?'
243 [[ $string =~ $re ]] || return $? 253 [[ $string =~ $re ]] || return $?
254 # shopt -s extglob
244 255
245 local scheme="${BASH_REMATCH[2]}" 256 local scheme="${BASH_REMATCH[2]}"
246 local authority="${BASH_REMATCH[4]}" 257 local authority="${BASH_REMATCH[4]}"
@@ -250,10 +261,10 @@ parse_url() { # eval "$(split_url NAME STRING)" => NAME[...]
250 261
251 for c in scheme authority query fragment; do 262 for c in scheme authority query fragment; do
252 [[ "${!c}" ]] && 263 [[ "${!c}" ]] &&
253 printf '%s[%s]=%q\n' "$name" "$c" "${!c}" 264 run printf '%s[%s]=%q\n' "$name" "$c" "${!c}"
254 done 265 done
255 # unclear if the path is always set even if empty but it looks that way 266 # unclear if the path is always set even if empty but it looks that way
256 printf '%s[path]=%q\n' "$name" "$path" 267 run printf '%s[path]=%q\n' "$name" "$path"
257} 268}
258 269
259# is a NAME defined ('set' in bash)? 270# is a NAME defined ('set' in bash)?
@@ -289,6 +300,7 @@ handle_response() {
289 REDIRECTS=0 300 REDIRECTS=0
290 BOLLUX_URL="$URL" 301 BOLLUX_URL="$URL"
291 run prompt "$meta" QUERY 302 run prompt "$meta" QUERY
303 # shellcheck disable=2153
292 run blastoff "?$QUERY" 304 run blastoff "?$QUERY"
293 ;; 305 ;;
294 2*) 306 2*)
@@ -323,10 +335,13 @@ handle_response() {
323display() { 335display() {
324 case "$1" in 336 case "$1" in
325 *\;*) 337 *\;*)
326 mime="$(cut -d\; -f1 <<<"$1" | trim)" 338 mime="${1%;*}"
327 charset="$(cut -d\; -f2 <<<"$1" | trim)" 339 charset="${1#*;}"
340 trim "$mime"
341 trim "$charset"
342 log d "$mime $charset"
328 ;; 343 ;;
329 *) mime="$(trim <<<"$1")" ;; 344 *) mime="$(trim "$1")" ;;
330 esac 345 esac
331 346
332 [[ -z "$mime" ]] && mime="text/gemini" 347 [[ -z "$mime" ]] && mime="text/gemini"
@@ -384,7 +399,9 @@ mklesskey() {
384} 399}
385 400
386normalize_crlf() { 401normalize_crlf() {
387 gawk 'BEGIN{RS="\n\n"}{gsub(/\r\n?/,"\n");print;print ""}' 402 while read -r line; do
403 printf '%s\n' "${line//$'\r'?($'\n')/}"
404 done
388} 405}
389 406
390typeset_gemini() { 407typeset_gemini() {