git ssb

0+

cel / dillo-gemini



Tree: 02044cb4be75b9a46439f79179efc828aceb7d50

Files: 02044cb4be75b9a46439f79179efc828aceb7d50 / gemini.filter.dpi

3294 bytesRaw
1#!/bin/bash
2# dillo-gemini
3# © 2019 cel @f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519
4# Copying and distribution of this file, with or without modification,
5# are permitted in any medium without royalty provided the copyright
6# notice and this notice are preserved. This file is offered as-is,
7# without any warranty.
8
9read -d '>' auth
10read -d '>' cmd
11case "$cmd" in
12 "<cmd='open_url' url='"*);;
13 *) echo $cmd; exit;;
14esac
15url=${cmd#"<cmd='open_url' url='"}
16url=${url%"' '"}
17
18serve_404() {
19 printf "<cmd='start_send_page' url='' '>\n"
20 printf "Content-type: text/plain\r\n\r\n"
21 echo Not found
22}
23
24render_gemini() {
25 printf "Content-type: text/html\r\n\r\n"
26 if which ansi2html 2>&1 >/dev/null
27 then
28 ansi2html | sed 's/^\(=&gt;\s*\)\(\S*\)*\(.*\)/\1<a href="\2">\2<\/a>\3/'
29 else
30 printf "<!doctype html><pre>"
31 sed 's/^\(=>\s*\)\(\S*\)*\(.*\)/\1<a href="\2">\2<\/a>\3/'
32 printf "%s" "</pre>"
33 fi
34}
35
36send_status_msg() {
37 printf "<cmd='send_status_message' msg='%s' '>" "$*"
38}
39
40serve_status_not_supported() {
41 printf "<cmd='start_send_page' url='' '>\n"
42 printf "Content-type: text/plain\r\n\r\n"
43 echo Status not implemented: $1
44 echo $2
45}
46
47serve_missing_status() {
48 printf "<cmd='start_send_page' url='' '>\n"
49 printf "Content-type: text/plain\r\n\r\n"
50 echo Empty status response. $2
51}
52
53serve_success() {
54 printf "<cmd='start_send_page' url='' '>\n"
55 type=$1
56 case "$type" in
57 text/gemini*) render_gemini;;
58 *) printf "Content-type: $type\r\n\r\n"; cat;;
59 esac
60}
61
62serve_redirect() {
63 url=$1
64 send_status_msg "Redirected"
65 printf "<cmd='start_send_page' url='' '>\n"
66 printf "Content-type: text/html\r\n\r\n"
67 # TODO: html-escape url
68 cat <<-EOF
69 <!doctype html>
70 <html>
71 <head>
72 <title>Redirect to $url</title>
73 </head>
74 <body>
75 <h3>Redirect to <a href="$url">$url</a></3>
76 </body>
77 </html>
78 EOF
79}
80
81serve_error() {
82 status=$1
83 meta=$2
84 send_status_msg "Request failed"
85 printf "<cmd='start_send_page' url='' '>\n"
86 printf "Content-type: text/html\r\n\r\n"
87 cat <<-EOF
88 <!doctype html>
89 <html>
90 <head>
91 <title>Request failed</title>
92 </head>
93 <body>
94 <h3>Request failed: $status</h3>
95 <code>$meta</code>
96 </body>
97 </html>
98 EOF
99}
100
101serve_fail() {
102 meta="$1"
103 send_status_msg "Client certificate required"
104 printf "<cmd='start_send_page' url='' '>\n"
105 printf "Content-type: text/html\r\n\r\n"
106 cat <<-EOF
107 <!doctype html>
108 <html>
109 <head>
110 <title>Client certificate required</title>
111 </head>
112 <body>
113 <h3>Client certificate required</h3>
114 <p>Not implemented!</p>
115 <code>$meta</code>
116 </body>
117 </html>
118 EOF
119}
120
121serve_gemini() {
122 url=$1
123 url1=${url#gemini://}
124 hostname=${url1%%/*}
125 host=${hostname%%:*}
126 port=${hostname##*:}
127 if [ "$host" = "$port" ]; then port=1965; fi
128 send_status_msg "Sending request..."
129 printf "%s\r\n" "$url" | openssl s_client -verify_quiet -quiet "$host:$port" | {
130 read status meta
131 send_status_msg "Status: $status"
132 meta=$(echo "$meta" | sed 's/\s*$//')
133 case "$status" in
134 1*) serve_input "$meta";;
135 2*) serve_success "$meta";;
136 3*) serve_redirect "$meta";;
137 4*) serve_error "$status" "$meta";;
138 5*) serve_error "$meta";;
139 6*) serve_client_cert_required "$meta";;
140 '') serve_missing_status "$meta";;
141 *) serve_status_not_supported "$status" "$meta";;
142 esac
143 }
144}
145
146case "$url" in
147 gemini:*) serve_gemini "$url";;
148 *) serve_404;;
149esac
150

Built with git-ssb-web