commit 713a34e1aa71d1251fdaf8288a1201dd9fab0cb0
parent 62bf8cb0f8fc9edb316571cf349fe0a152a2408d
Author: Luxferre <lux@ferre>
Date: Thu, 30 Mar 2023 18:07:00 +0300
Improved CRLF processing according to RFC
Diffstat:
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/bopher-ng.sh b/bopher-ng.sh
@@ -89,7 +89,7 @@ gophetch() { # args: host, port, selector[, input]
gmparse() { # args: line, curhost, curport
local line="$1"
[[ ! "$line" == *[$'\t']* ]] && line="i$line" # treat non-standard plain tabless lines as information lines
- readarray -d $'\t' -t fields < <(printf '%s' "${line%%$'\r'}") # also remove a trailing CR in case the response is CRLF
+ readarray -d $'\t' -t fields < <(printf '%s' "$line")
local rtype="${fields[0]:0:1}" # resource type
local desc="${fields[0]:1}" # resource description
local sel="${fields[1]}" # resource selector
@@ -147,14 +147,17 @@ amclick() { # args: AM line[, forcedl], output: AM line(s)
[[ ! -z "$input" ]] && fname="${fname}%09${input}.search.txt"
gophetch "$rhost" "$rport" "$puresel" "$input" > "$BOPHER_DLDIR/$fname"
printf 'E\tDownloaded %s to %s\n' "$(amtogopher "$1")" "$BOPHER_DLDIR/$fname"
- elif [[ 'I' == "$action" || 'M' == "$action" || 'P' == "$action" ]]; then # text content
- readarray -t lines < <(gophetch "$rhost" "$rport" "$puresel" "$input")
+ elif [[ 'I' == "$action" || 'M' == "$action" ]]; then # Gophermaps must be processed with CRLF delimiter only
+ readarray -t lines -d $'\r' < <(gophetch "$rhost" "$rport" "$puresel" "$input") # split on CR, not LF!
for line in "${lines[@]}"; do # iterate over every fetched line
- if [[ 'P' == "$action" ]]; then # generate a plaintext AM entry
- printf 'E\t%s\n' "${line%%$'\r'}" # remove a trailing CR if it's there
- else # generate a proper Gophermap AM entry
- gmparse "$line" "$rhost" "$rport"
- fi
+ line="${line##$'\n'}" # remove a starting LF if it's there
+ line="${line%%$'\r'}" # remove a trailing CR if it's there
+ gmparse "$line" "$rhost" "$rport"
+ done
+ elif [[ 'P' == "$action" ]]; then # plain text content (can be delimited with both CRLF or LF)
+ readarray -t lines -d $'\n' < <(gophetch "$rhost" "$rport" "$puresel" "$input") # split on LF
+ for line in "${lines[@]}"; do # iterate over every fetched line
+ printf 'E\t%s\n' "${line%%$'\r'}" # remove a trailing CR if it's there
done
fi
}