all repos — flounder @ 1a685e147c66e68bc6e5bf9e662a52513db6d616

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