Skip to content
Permalink
Browse files

(gometa): Use ValidQuery to check go-get query

  • Loading branch information
erbesharat committed Aug 17, 2019
1 parent fad7452 commit 41a41c2ba6c00a5515e133f5931e2687971c59bc
Showing with 49 additions and 21 deletions.
  1. +36 −12 gometa.go
  2. +7 −4 gometa_test.go
  3. +6 −5 txtdirect.go
@@ -20,6 +20,22 @@ import (
"strings"
)

type Gometa struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

func NewGometa(w http.ResponseWriter, r *http.Request, rec record, c Config) *Gometa {
return &Gometa{
rw: w,
req: r,
rec: rec,
c: c,
}
}

var tmpl = template.Must(template.New("").Parse(`<!DOCTYPE html>
<html>
<head>
@@ -30,28 +46,36 @@ var tmpl = template.Must(template.New("").Parse(`<!DOCTYPE html>

// gometa executes a template on the given ResponseWriter
// that contains go-import meta tag
func gometa(w http.ResponseWriter, r record, host, path string) error {
if r.Vcs == "" {
r.Vcs = "git"
func (g *Gometa) Serve() error {
if g.rec.Vcs == "" {
g.rec.Vcs = "git"
}
if path == "/" {
path = ""
if g.req.URL.Path == "/" {
g.req.URL.Path = ""
}

gosource := strings.Contains(r.To, "github.com")
gosource := strings.Contains(g.rec.To, "github.com")

RequestsByStatus.WithLabelValues(host, strconv.Itoa(http.StatusFound)).Add(1)
return tmpl.Execute(w, struct {
RequestsByStatus.WithLabelValues(g.req.Host, strconv.Itoa(http.StatusFound)).Add(1)
return tmpl.Execute(g.rw, struct {
Host string
Path string
Vcs string
NewURL string
HasGoSource bool
}{
host,
path,
r.Vcs,
r.To,
g.req.Host,
g.req.URL.Path,
g.rec.Vcs,
g.rec.To,
gosource,
})
}

func (g *Gometa) ValidQuery() bool {
if g.req.URL.Query().Get("go-get") != "1" {
fallback(g.rw, g.req, "website", http.StatusFound, g.c)
return false
}
return true
}
@@ -14,6 +14,7 @@ limitations under the License.
package txtdirect

import (
"fmt"
"io/ioutil"
"net/http/httptest"
"testing"
@@ -86,13 +87,15 @@ func TestGometa(t *testing.T) {
}

for i, test := range tests {
rec := httptest.NewRecorder()
err := gometa(rec, test.record, test.host, test.path)
if err != nil {
req := httptest.NewRequest("GET", fmt.Sprintf("https://%s%s", test.host, test.path), nil)
resp := httptest.NewRecorder()
gometa := NewGometa(resp, req, test.record, Config{})

if err := gometa.Serve(); err != nil {
t.Errorf("Test %d: Unexpected error: %s", i, err)
continue
}
txt, err := ioutil.ReadAll(rec.Body)
txt, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Test %d: Unexpected error: %s", i, err)
continue
@@ -216,8 +216,8 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {

docker := NewDockerv2(w, r, rec, c)

// Fallback gets triggered if the User-Agent isn't valid
if !docker.ValidAgent() {
// Fallback gets triggered if the User-Agent isn't valid
return nil
}

@@ -243,13 +243,14 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
if rec.Type == "gometa" {
RequestsCountBasedOnType.WithLabelValues(host, "gometa").Add(1)

// Trigger fallback when request isn't from `go get`
if r.URL.Query().Get("go-get") != "1" {
fallback(w, r, "website", http.StatusFound, c)
gometa := NewGometa(w, r, rec, c)

// Triggers fallback when request isn't from `go get`
if !gometa.ValidQuery() {
return nil
}

return gometa(w, rec, host, path)
return gometa.Serve()
}

if rec.Type == "gomods" {

0 comments on commit 41a41c2

Please sign in to comment.
You can’t perform that action at this time.