git ssb

0+

cel / dillo-gemini



Tree: 3eaae0075f7e0bd94595e6486528262ad962274c

Files: 3eaae0075f7e0bd94595e6486528262ad962274c / gemini.filter.dpi

3127 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<!doctype html><pre>"
26 sed 's/^\(=>\s*\)\(\S*\)*\(.*\)/\1<a href="\2">\2<\/a>\3/'
27 printf "%s" "</pre>"
28}
29
30send_status_msg() {
31 printf "<cmd='send_status_message' msg='%s' '>" "$*"
32}
33
34serve_status_not_supported() {
35 printf "<cmd='start_send_page' url='' '>\n"
36 printf "Content-type: text/plain\r\n\r\n"
37 echo Status not implemented: $1
38 echo $2
39}
40
41serve_missing_status() {
42 printf "<cmd='start_send_page' url='' '>\n"
43 printf "Content-type: text/plain\r\n\r\n"
44 echo Empty status response. $2
45}
46
47serve_success() {
48 printf "<cmd='start_send_page' url='' '>\n"
49 type=$1
50 case "$type" in
51 text/gemini*) render_gemini;;
52 *) printf "Content-type: $type\r\n\r\n"; cat;;
53 esac
54}
55
56serve_redirect() {
57 url=$1
58 send_status_msg "Redirected"
59 printf "<cmd='start_send_page' url='' '>\n"
60 printf "Content-type: text/html\r\n\r\n"
61 # TODO: html-escape url
62 cat <<-EOF
63 <!doctype html>
64 <html>
65 <head>
66 <title>Redirect to $url</title>
67 </head>
68 <body>
69 <h3>Redirect to <a href="$url">$url</a></3>
70 </body>
71 </html>
72 EOF
73}
74
75serve_error() {
76 status=$1
77 meta=$2
78 send_status_msg "Request failed"
79 printf "<cmd='start_send_page' url='' '>\n"
80 printf "Content-type: text/html\r\n\r\n"
81 cat <<-EOF
82 <!doctype html>
83 <html>
84 <head>
85 <title>Request failed</title>
86 </head>
87 <body>
88 <h3>Request failed: $status</h3>
89 <code>$meta</code>
90 </body>
91 </html>
92 EOF
93}
94
95serve_fail() {
96 meta="$1"
97 send_status_msg "Client certificate required"
98 printf "<cmd='start_send_page' url='' '>\n"
99 printf "Content-type: text/html\r\n\r\n"
100 cat <<-EOF
101 <!doctype html>
102 <html>
103 <head>
104 <title>Client certificate required</title>
105 </head>
106 <body>
107 <h3>Client certificate required</h3>
108 <p>Not implemented!</p>
109 <code>$meta</code>
110 </body>
111 </html>
112 EOF
113}
114
115serve_gemini() {
116 url=$1
117 url1=${url#gemini://}
118 hostname=${url1%%/*}
119 host=${hostname%%:*}
120 port=${hostname##*:}
121 if [ "$host" = "$port" ]; then port=1965; fi
122 send_status_msg "Sending request..."
123 printf "%s\r\n" "$url" | ncat --ssl "$host" "$port" | {
124 read status meta
125 send_status_msg "Status: $status"
126 meta=$(echo "$meta" | sed 's/\s*$//')
127 case "$status" in
128 1*) serve_input "$meta";;
129 2*) serve_success "$meta";;
130 3*) serve_redirect "$meta";;
131 4*) serve_error "$status" "$meta";;
132 5*) serve_error "$meta";;
133 6*) serve_client_cert_required "$meta";;
134 '') serve_missing_status "$meta";;
135 *) serve_status_not_supported "$status" "$meta";;
136 esac
137 }
138}
139
140case "$url" in
141 gemini:*) serve_gemini "$url";;
142 *) serve_404;;
143esac
144

Built with git-ssb-web