Update go-gemini dependency

This commit is contained in:
Adnan Maolood 2020-11-05 17:51:26 -05:00 committed by Drew DeVault
parent 1074d434d2
commit 030a76c270
3 changed files with 41 additions and 39 deletions

2
go.mod
View file

@ -3,6 +3,6 @@ module git.sr.ht/~sircmpwn/kineto
go 1.15 go 1.15
require ( require (
git.sr.ht/~adnano/go-gemini v0.1.0 // indirect git.sr.ht/~adnano/go-gemini v0.1.6
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3
) )

7
go.sum
View file

@ -1,8 +1,11 @@
git.sr.ht/~adnano/go-gemini v0.1.0 h1:UqRT90kR+/8q5s/JemmDPH6A9VocBezIuWbOpZYBypU= git.sr.ht/~adnano/go-gemini v0.1.6 h1:nc13BMKnrHSWnR55DkaffsAe6Ik7nqwrq31Be1/0T0k=
git.sr.ht/~adnano/go-gemini v0.1.0/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= git.sr.ht/~adnano/go-gemini v0.1.6/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

65
main.go
View file

@ -1,16 +1,16 @@
package main package main
import ( import (
"bytes"
"crypto/x509"
"fmt" "fmt"
"html/template" "html/template"
"io"
"log" "log"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time"
"git.sr.ht/~adnano/go-gemini" "git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~sircmpwn/getopt" "git.sr.ht/~sircmpwn/getopt"
@ -50,7 +50,7 @@ var gemtextPage = template.Must(template.
"pre_toggle_on": func(ctx *GemtextContext, line gemini.Line) *gemini.LinePreformattingToggle { "pre_toggle_on": func(ctx *GemtextContext, line gemini.Line) *gemini.LinePreformattingToggle {
switch l := line.(type) { switch l := line.(type) {
case gemini.LinePreformattingToggle: case gemini.LinePreformattingToggle:
if ctx.Pre % 4 == 0 { if ctx.Pre%4 == 0 {
ctx.Pre += 1 ctx.Pre += 1
return &l return &l
} }
@ -63,7 +63,7 @@ var gemtextPage = template.Must(template.
"pre_toggle_off": func(ctx *GemtextContext, line gemini.Line) *gemini.LinePreformattingToggle { "pre_toggle_off": func(ctx *GemtextContext, line gemini.Line) *gemini.LinePreformattingToggle {
switch l := line.(type) { switch l := line.(type) {
case gemini.LinePreformattingToggle: case gemini.LinePreformattingToggle:
if ctx.Pre % 4 == 3 { if ctx.Pre%4 == 3 {
ctx.Pre += 1 ctx.Pre += 1
return &l return &l
} }
@ -102,19 +102,16 @@ var gemtextPage = template.Must(template.
if err != nil { if err != nil {
return template.URL("error") return template.URL("error")
} }
if u.Scheme == "" { u = ctx.URL.ResolveReference(u)
return template.URL(s)
} if u.Scheme == "" || u.Scheme == "gemini" {
if u.Scheme == "gemini" { if u.Host != ctx.Root.Host {
if u.Host != ctx.URL.Host {
u.Path = fmt.Sprintf("/x/%s%s", u.Host, u.Path) u.Path = fmt.Sprintf("/x/%s%s", u.Host, u.Path)
u.Host = ""
} }
u.Scheme = "" u.Scheme = ""
u.Host = "" u.Host = ""
return template.URL(u.String())
} }
return template.URL(s) return template.URL(u.String())
}, },
"safeCSS": func(s string) template.CSS { "safeCSS": func(s string) template.CSS {
return template.CSS(s) return template.CSS(s)
@ -319,6 +316,7 @@ type GemtextContext struct {
Resp *gemini.Response Resp *gemini.Response
Title string Title string
URL *url.URL URL *url.URL
Root *url.URL
} }
type GemtextHeading struct { type GemtextHeading struct {
@ -326,28 +324,24 @@ type GemtextHeading struct {
Text string Text string
} }
func proxyGemini(req gemini.Request, external bool, func proxyGemini(req gemini.Request, external bool, root *url.URL,
w http.ResponseWriter, r *http.Request) { w http.ResponseWriter, r *http.Request) {
client := gemini.Client{ client := gemini.Client{
TrustCertificate: func(_ string, _ *x509.Certificate, _ *gemini.KnownHosts) error { Timeout: 30 * time.Second,
return nil InsecureSkipTrust: true,
},
}
u := &url.URL{}
*u = *req.URL
if !strings.Contains(req.URL.Host, ":") {
req.URL.Host = req.URL.Host + ":1965"
}
if !strings.Contains(req.Host, ":") {
req.Host = req.Host + ":1965"
} }
resp, err := client.Send(&req) if h := (url.URL{Host: req.Host}); h.Port() == "" {
req.Host += ":1965"
}
resp, err := client.Do(&req)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadGateway) w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(fmt.Sprintf("Gateway error: %v", err))) w.Write([]byte(fmt.Sprintf("Gateway error: %v", err)))
return return
} }
defer resp.Body.Close()
switch resp.Status { switch resp.Status {
case 20: case 20:
@ -392,26 +386,31 @@ func proxyGemini(req gemini.Request, external bool,
if m != "text/gemini" { if m != "text/gemini" {
w.Header().Add("Content-Type", resp.Meta) w.Header().Add("Content-Type", resp.Meta)
w.Write(resp.Body) io.Copy(w, resp.Body)
return return
} }
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
text := gemini.Parse(bytes.NewReader(resp.Body))
ctx := &GemtextContext{ ctx := &GemtextContext{
CSS: defaultCSS, CSS: defaultCSS,
External: external, External: external,
Lines: []gemini.Line(text),
Resp: resp, Resp: resp,
Title: req.URL.Host + " " + req.URL.Path, Title: req.URL.Host + " " + req.URL.Path,
URL: u, URL: req.URL,
Root: root,
} }
for _, line := range text {
var title bool
gemini.ParseLines(resp.Body, func(line gemini.Line) {
ctx.Lines = append(ctx.Lines, line)
if !title {
if h, ok := line.(gemini.LineHeading1); ok { if h, ok := line.(gemini.LineHeading1); ok {
ctx.Title = string(h) ctx.Title = string(h)
break title = true
} }
} }
})
err = gemtextPage.Execute(w, ctx) err = gemtextPage.Execute(w, ctx)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -465,7 +464,7 @@ func main() {
req.URL.Host = root.Host req.URL.Host = root.Host
req.URL.Path = r.URL.Path req.URL.Path = r.URL.Path
req.Host = root.Host req.Host = root.Host
proxyGemini(req, false, w, r) proxyGemini(req, false, root, w, r)
})) }))
http.Handle("/x/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Handle("/x/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -488,7 +487,7 @@ func main() {
} }
req.Host = path[2] req.Host = path[2]
log.Printf("%s (external) %s%s", r.Method, path[2], path[3]) log.Printf("%s (external) %s%s", r.Method, path[2], path[3])
proxyGemini(req, true, w, r) proxyGemini(req, true, root, w, r)
})) }))
log.Printf("HTTP server listening on %s", bind) log.Printf("HTTP server listening on %s", bind)