about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xbollux59
1 files changed, 40 insertions, 19 deletions
diff --git a/bollux b/bollux index 0234282..0361545 100755 --- a/bollux +++ b/bollux
@@ -31,6 +31,10 @@ put() { printf '%s\n' "$*"; }
31# lower = more important 31# lower = more important
32log() { # log [LEVEL] [<] MESSAGE 32log() { # log [LEVEL] [<] MESSAGE
33 case "$1" in 33 case "$1" in
34 -)
35 lvl="-1"
36 shift
37 ;;
34 [0-5]) 38 [0-5])
35 lvl="$1" 39 lvl="$1"
36 shift 40 shift
@@ -125,10 +129,8 @@ request() { # request [-s SERVER] URL
125# cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt 129# cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt
126handle() { # handle URL < RESPONSE 130handle() { # handle URL < RESPONSE
127 url="$(_address "$1")" 131 url="$(_address "$1")"
128 resp="$(cat)"
129 head="$(sed 1q <<<"$resp")"
130 body="$(sed 1d <<<"$resp")"
131 132
133 head="$(sed 1q)"
132 code="$(awk '{print $1}' <<<"$head")" 134 code="$(awk '{print $1}' <<<"$head")"
133 meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")" 135 meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")"
134 136
@@ -148,7 +150,7 @@ handle() { # handle URL < RESPONSE
148 21) log 5 "- End of client certificate session" ;; 150 21) log 5 "- End of client certificate session" ;;
149 *) log 2 "- Unknown response code: '$code'." ;; 151 *) log 2 "- Unknown response code: '$code'." ;;
150 esac 152 esac
151 display <<<"$body" 153 display "$meta"
152 ;; 154 ;;
153 3*) # REDIRECT 155 3*) # REDIRECT
154 log 3 "Redirecting" 156 log 3 "Redirecting"
@@ -157,7 +159,7 @@ handle() { # handle URL < RESPONSE
157 31) log 5 "- Permanent" ;; 159 31) log 5 "- Permanent" ;;
158 *) log 2 "- Unknown response code: '$code'." ;; 160 *) log 2 "- Unknown response code: '$code'." ;;
159 esac 161 esac
160 ((RDRS+=1)) 162 ((RDRS += 1))
161 ((RDRS > MAXR)) && die "$code" "Too many redirects!" 163 ((RDRS > MAXR)) && die "$code" "Too many redirects!"
162 bollux "$meta" 164 bollux "$meta"
163 ;; 165 ;;
@@ -202,20 +204,39 @@ handle() { # handle URL < RESPONSE
202} 204}
203 205
204# display the page 206# display the page
205display() { # display <<< STRING 207display() { # display MIMETYPE < DOCUMENT
206 # normalize line endings to "\n" 208 mimetype="$1"
207 awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}' 209 case "$mimetype" in
208 # TODO: use less with linking and stuff 210 text/*)
209 # less -R -p'^=>' +g 211 # normalize line endings to "\n"
210 # lesskey: 212 awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}'
211 # l /=>\n # highlight links 213 # TODO: use less with linking and stuff
212 # o pipe \n open_url # open the link on the top line 214 # less -R -p'^=>' +g
213 # u shell select_url % # shows a selection prompt for all urls (on screen? file?) 215 # lesskey:
214 # Q exit 1 # for one of these, show a selection prompt for urls 216 # l /=>\n # highlight links
215 # q exit 0 # for the other, just quit 217 # o pipe \n open_url # open the link on the top line
216 ### 218 # u shell select_url % # shows a selection prompt for all urls (on screen? file?)
217 # also look into the prompt, the filename, and input preprocessor 219 # Q exit 1 # for one of these, show a selection prompt for urls
218 # ($LESSOPEN, $LESSCLOSE) 220 # q exit 0 # for the other, just quit
221 ###
222 # also look into the prompt, the filename, and input preprocessor
223 # ($LESSOPEN, $LESSCLOSE)
224 ;;
225 *)
226 tn="$(mktemp)"
227 dd status=progress >"$tn"
228 fn="$DLDR/${url##*/}"
229 if [[ -f "$fn" ]]; then
230 log - "Saved '$tn'."
231 else
232 if mv "$tn" "$fn"; then
233 log - "Saved '$fn'."
234 else
235 log 0 "Error saving '$fn'."
236 fi
237 fi
238 ;;
239 esac
219} 240}
220 241
221### main entry point ### 242### main entry point ###