proxy.go (view raw)
1// Copied from https://git.sr.ht/~sircmpwn/kineto/tree/master/item/main.go
2package main
3
4import (
5 "fmt"
6 "html/template"
7 "io"
8 "mime"
9 "net/http"
10 "net/url"
11 "strings"
12 "time"
13
14 "git.sr.ht/~adnano/go-gemini"
15)
16
17func proxyGemini(w http.ResponseWriter, r *http.Request) {
18 if r.Method != "GET" {
19 w.WriteHeader(http.StatusMethodNotAllowed)
20 w.Write([]byte("404 Not found"))
21 return
22 }
23 path := strings.SplitN(r.URL.Path, "/", 3)
24 req := gemini.Request{}
25 var err error
26 req.Host = path[1]
27 if len(path) > 2 {
28 req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/%s", path[1], path[2]))
29 } else {
30 req.URL, err = url.Parse(fmt.Sprintf("gemini://%s/", path[1]))
31 }
32 client := gemini.Client{
33 Timeout: 60 * time.Second,
34 InsecureSkipTrust: true,
35 }
36
37 if h := (url.URL{Host: req.Host}); h.Port() == "" {
38 req.Host += ":1965"
39 }
40
41 resp, err := client.Do(&req)
42 if err != nil {
43 w.WriteHeader(http.StatusBadGateway)
44 fmt.Fprintf(w, "Gateway error: %v", err)
45 return
46 }
47 defer resp.Body.Close()
48
49 switch resp.Status {
50 case 10, 11:
51 // TODO accept input
52 w.WriteHeader(http.StatusInternalServerError)
53 return
54 case 20:
55 break // OK
56 case 30, 31:
57 to, err := url.Parse(resp.Meta)
58 if err != nil {
59 w.WriteHeader(http.StatusBadGateway)
60 w.Write([]byte(fmt.Sprintf("Gateway error: bad redirect %v", err)))
61 }
62 next := req.URL.ResolveReference(to)
63 if next.Scheme != "gemini" {
64 w.WriteHeader(http.StatusOK)
65 w.Write([]byte(fmt.Sprintf("This page is redirecting you to %s", next.String())))
66 return
67 }
68 next.Host = r.URL.Host
69 next.Scheme = r.URL.Scheme
70 w.Header().Add("Location", next.String())
71 w.WriteHeader(http.StatusFound)
72 w.Write([]byte("Redirecting to " + next.String()))
73 return
74 case 40, 41, 42, 43, 44:
75 w.WriteHeader(http.StatusServiceUnavailable)
76 fmt.Fprintf(w, "The remote server returned %d: %s", resp.Status, resp.Meta)
77 return
78 case 50, 51:
79 w.WriteHeader(http.StatusNotFound)
80 fmt.Fprintf(w, "The remote server returned %d: %s", resp.Status, resp.Meta)
81 return
82 case 52, 53, 59:
83 w.WriteHeader(http.StatusServiceUnavailable)
84 fmt.Fprintf(w, "The remote server returned %d: %s", resp.Status, resp.Meta)
85 return
86 default:
87 w.WriteHeader(http.StatusNotImplemented)
88 fmt.Fprintf(w, "Proxy does not understand Gemini response status %d", resp.Status)
89 return
90 }
91
92 m, _, err := mime.ParseMediaType(resp.Meta)
93 if err != nil {
94 w.WriteHeader(http.StatusBadGateway)
95 w.Write([]byte(fmt.Sprintf("Gateway error: %d %s: %v",
96 resp.Status, resp.Meta, err)))
97 return
98 }
99
100 if m != "text/gemini" {
101 w.Header().Add("Content-Type", resp.Meta)
102 io.Copy(w, resp.Body)
103 return
104 }
105
106 w.Header().Add("Content-Type", "text/html")
107
108 htmlString := textToHTML(req.URL, gemini.ParseText(resp.Body))
109 data := struct {
110 SiteBody template.HTML
111 Favicon string
112 PageTitle string
113 URI *url.URL
114 }{template.HTML(htmlString), "", req.URL.String(), req.URL}
115
116 err = t.ExecuteTemplate(w, "user_page.html", data)
117 if err != nil {
118 w.WriteHeader(http.StatusInternalServerError)
119 fmt.Fprintf(w, "%v", err)
120 return
121 }
122}