about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xbollux39
1 files changed, 24 insertions, 15 deletions
diff --git a/bollux b/bollux index 77f7c11..5ed99ba 100755 --- a/bollux +++ b/bollux
@@ -4,8 +4,6 @@
4# License: MIT 4# License: MIT
5# Version: -0.7 5# Version: -0.7
6 6
7set -euo pipefail
8
9### constants ### 7### constants ###
10PRGN="${0##*/}" # program name 8PRGN="${0##*/}" # program name
11DLDR="${BOLLUX_DOWNDIR:-.}" # where to download 9DLDR="${BOLLUX_DOWNDIR:-.}" # where to download
@@ -25,7 +23,7 @@ RDRS=0 # redirects
25 23
26### utility functions ### 24### utility functions ###
27# a better echo 25# a better echo
28put() { printf '%s\n' "$*"; } 26put() { printf '%s\n' "$*" >&3; }
29 27
30# conditionally log events to stderr 28# conditionally log events to stderr
31# lower = more important 29# lower = more important
@@ -116,20 +114,18 @@ request() { # request [-s SERVER] URL
116 log 5 "serv: $serv" 114 log 5 "serv: $serv"
117 log 5 "addr: $addr" 115 log 5 "addr: $addr"
118 116
119 t="$(mktemp)"
120
121 sslcmd=(openssl s_client -crlf -ign_eof -quiet -connect "$serv") 117 sslcmd=(openssl s_client -crlf -ign_eof -quiet -connect "$serv")
122 log "${sslcmd[@]}" 118 log "${sslcmd[@]}"
123 "${sslcmd[@]}" <<<"$addr" 2>"$t" 119 "${sslcmd[@]}" <<<"$addr" 2>/dev/null
124
125 ((LOGL > 4)) && cat "$t"
126 rm "$t"
127} 120}
128 121
129# handle the response 122# handle the response
130# cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt 123# cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt
131handle() { # handle < RESPONSE ## needs $URL in state 124handle() { # handle URL < RESPONSE
132 head="$(sed 1q)" 125 URL="$1"
126 while read -r head; do
127 break # wait to read the first line
128 done
133 code="$(awk '{print $1}' <<<"$head")" 129 code="$(awk '{print $1}' <<<"$head")"
134 meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")" 130 meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")"
135 131
@@ -241,20 +237,33 @@ display() { # display MIMETYPE < DOCUMENT
241 237
242### main entry point ### 238### main entry point ###
243bollux() { 239bollux() {
240 bollux_setup
241
244 if (($# == 1)); then 242 if (($# == 1)); then
245 URL="$1" 243 URL="$1"
246 else 244 else
247 read -rp "GO> " URL 245 read -r -u 3 -p "GO> " URL
248 fi 246 fi
249 247
250 log 5 "URL : $URL" 248 log 5 "URL : $URL"
251 log 5 "addr: $(_address "$URL")"
252 log 5 "serv: $(_server "$URL")"
253 249
254 request "$URL" | handle 250 request "$URL" >.resource &
251 handle "$URL" <.resource
252
253 bollux_cleanup
254}
255
256bollux_setup() {
257 mkfifo .resource
258 trap bollux_cleanup INT QUIT TERM EXIT
259}
260bollux_cleanup() {
261 echo
262 rm -f .resource
255} 263}
256 264
257if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then 265if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
266 set -euo pipefail # strict mode
258 # requirements here -- so they're only checked once 267 # requirements here -- so they're only checked once
259 require awk 268 require awk
260 require dd 269 require dd