all repos — flounder @ e18e7426101c59d53f35e31377b4d40127bd6900

A small site builder for the Gemini protocol

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