# lower = more important
log() { # log [LEVEL] [<] MESSAGE
case "$1" in
+ -)
+ lvl="-1"
+ shift
+ ;;
[0-5])
lvl="$1"
shift
# cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt
handle() { # handle URL < RESPONSE
url="$(_address "$1")"
- resp="$(cat)"
- head="$(sed 1q <<<"$resp")"
- body="$(sed 1d <<<"$resp")"
+ head="$(sed 1q)"
code="$(awk '{print $1}' <<<"$head")"
meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")"
21) log 5 "- End of client certificate session" ;;
*) log 2 "- Unknown response code: '$code'." ;;
esac
- display <<<"$body"
+ display "$meta"
;;
3*) # REDIRECT
log 3 "Redirecting"
31) log 5 "- Permanent" ;;
*) log 2 "- Unknown response code: '$code'." ;;
esac
- ((RDRS+=1))
+ ((RDRS += 1))
((RDRS > MAXR)) && die "$code" "Too many redirects!"
bollux "$meta"
;;
}
# display the page
-display() { # display <<< STRING
- # normalize line endings to "\n"
- awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}'
- # TODO: use less with linking and stuff
- # less -R -p'^=>' +g
- # lesskey:
- # l /=>\n # highlight links
- # o pipe \n open_url # open the link on the top line
- # u shell select_url % # shows a selection prompt for all urls (on screen? file?)
- # Q exit 1 # for one of these, show a selection prompt for urls
- # q exit 0 # for the other, just quit
- ###
- # also look into the prompt, the filename, and input preprocessor
- # ($LESSOPEN, $LESSCLOSE)
+display() { # display MIMETYPE < DOCUMENT
+ mimetype="$1"
+ case "$mimetype" in
+ text/*)
+ # normalize line endings to "\n"
+ awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}'
+ # TODO: use less with linking and stuff
+ # less -R -p'^=>' +g
+ # lesskey:
+ # l /=>\n # highlight links
+ # o pipe \n open_url # open the link on the top line
+ # u shell select_url % # shows a selection prompt for all urls (on screen? file?)
+ # Q exit 1 # for one of these, show a selection prompt for urls
+ # q exit 0 # for the other, just quit
+ ###
+ # also look into the prompt, the filename, and input preprocessor
+ # ($LESSOPEN, $LESSCLOSE)
+ ;;
+ *)
+ tn="$(mktemp)"
+ dd status=progress >"$tn"
+ fn="$DLDR/${url##*/}"
+ if [[ -f "$fn" ]]; then
+ log - "Saved '$tn'."
+ else
+ if mv "$tn" "$fn"; then
+ log - "Saved '$fn'."
+ else
+ log 0 "Error saving '$fn'."
+ fi
+ fi
+ ;;
+ esac
}
### main entry point ###