From edd4fe31f16f9eb9565d2b6a329738ceedea8de9 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Wed, 24 Feb 2021 23:32:39 -0500 Subject: [PATCH] Update go-gemini dependency --- go.mod | 2 +- go.sum | 11 +++++++++-- main.go | 22 +++++++++------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 623c48d..dd7dcaf 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module git.sr.ht/~sircmpwn/kineto go 1.15 require ( - git.sr.ht/~adnano/go-gemini v0.1.13 + git.sr.ht/~adnano/go-gemini v0.1.17 git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 ) diff --git a/go.sum b/go.sum index f37e299..2d2857b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.sr.ht/~adnano/go-gemini v0.1.13 h1:vzKkkVrOzMpfJ1AAeE/PChg0Rw5Zf+9HrnwsgVxXUT4= -git.sr.ht/~adnano/go-gemini v0.1.13/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= +git.sr.ht/~adnano/go-gemini v0.1.17 h1:CJ3tDaqAJQbOdVXmiPC/iXHt4lF/CRu2nxPNOPjHUBk= +git.sr.ht/~adnano/go-gemini v0.1.17/go.mod h1:hQ75Y0i5jSFL+FQ7AzWVAYr5LQsaFC7v3ZviNyj46dY= 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= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= @@ -9,3 +9,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN 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= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go index 00aec8a..e03574f 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "html/template" "io" @@ -383,15 +384,12 @@ type GemtextHeading struct { func proxyGemini(req gemini.Request, external bool, root *url.URL, w http.ResponseWriter, r *http.Request, css string) { - client := gemini.Client{ - Timeout: 30 * time.Second, - } - if h := (url.URL{Host: req.Host}); h.Port() == "" { - req.Host += ":1965" - } + ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second) + defer cancel() - resp, err := client.Do(&req) + client := gemini.Client{} + resp, err := client.Do(ctx, &req) if err != nil { w.WriteHeader(http.StatusBadGateway) fmt.Fprintf(w, "Gateway error: %v", err) @@ -479,7 +477,7 @@ func proxyGemini(req gemini.Request, external bool, root *url.URL, lang := params["lang"] w.Header().Add("Content-Type", "text/html") - ctx := &GemtextContext{ + gemctx := &GemtextContext{ CSS: css, External: external, Resp: resp, @@ -491,16 +489,16 @@ func proxyGemini(req gemini.Request, external bool, root *url.URL, var title bool gemini.ParseLines(resp.Body, func(line gemini.Line) { - ctx.Lines = append(ctx.Lines, line) + gemctx.Lines = append(gemctx.Lines, line) if !title { if h, ok := line.(gemini.LineHeading1); ok { - ctx.Title = string(h) + gemctx.Title = string(h) title = true } } }) - err = gemtextPage.Execute(w, ctx) + err = gemtextPage.Execute(w, gemctx) if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "%v", err) @@ -573,7 +571,6 @@ func main() { req.URL.Scheme = root.Scheme req.URL.Host = root.Host req.URL.Path = r.URL.Path - req.Host = root.Host req.URL.RawQuery = r.URL.RawQuery proxyGemini(req, false, root, w, r, css) })) @@ -607,7 +604,6 @@ func main() { req.URL.Scheme = "gemini" req.URL.Host = path[2] req.URL.Path = "/" + path[3] - req.Host = path[2] req.URL.RawQuery = r.URL.RawQuery log.Printf("%s (external) %s%s", r.Method, r.URL.Host, r.URL.Path) proxyGemini(req, true, root, w, r, css)