Improved CRLF processing according to RFC

This commit is contained in:
Luxferre
2023-03-30 18:07:00 +03:00
parent 62bf8cb0f8
commit 713a34e1aa
+11 -8
View File
@@ -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
}