From 4d77f362ea2e12dbfe586cb8423de1d8ebb40444 Mon Sep 17 00:00:00 2001 From: jonbetti Date: Mon, 4 Jun 2018 23:28:03 +0000 Subject: [PATCH] Don't check URLs I'm going to pump an http.ServeMux to this and that can do my checks for me. git-svn-id: file:///srv/svn/repo/toyohime/trunk@75 922d331f-388e-da47-97a9-ad700dc0b8b9 --- vanity.go | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/vanity.go b/vanity.go index d5e88f5..966bce8 100644 --- a/vanity.go +++ b/vanity.go @@ -5,7 +5,6 @@ import ( "html/template" "io" "net/http" - "strings" ) type importData struct { @@ -27,32 +26,17 @@ var goImportTmpl = template.Must(template.New("main").Parse(` func ImportTag(vcs, importPath, repoRoot string) tag { return func(r *http.Request) (io.Reader, error) { - var path string - if strings.HasPrefix(r.URL.Path, "/cmd/") { - path = r.URL.Path[4:] - } else { - path = r.URL.Path - } - - // redirect github.com/kare/pkg/sub -> github.com/kare/pkg - vcsroot := repoRoot - f := func(c rune) bool { return c == '/' } - shortPath := strings.FieldsFunc(path, f) - if len(shortPath) > 0 { - vcsroot = repoRoot + "/" + shortPath[0] - } - d := &importData{ ImportRoot: r.Host + r.URL.Path, VCS: vcs, - VCSRoot: vcsroot, + VCSRoot: repoRoot, } var buf bytes.Buffer return &buf, goImportTmpl.Execute(&buf, d) } } -func Handle(importPath string, t tag) http.Handler { +func Handle(t tag) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Redirect to https. if r.URL.Scheme == "http" { @@ -75,11 +59,6 @@ func Handle(importPath string, t tag) http.Handler { return } - if !strings.HasPrefix(strings.TrimSuffix(r.Host+r.URL.Path, "/"), importPath+"/") { - http.NotFound(w, r) - return - } - body, err := t(r) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -94,5 +73,5 @@ func Handle(importPath string, t tag) http.Handler { // Redirect is a HTTP middleware that redirects browsers to godoc.org or // Go tool to VCS repository. func Redirect(vcs, importPath, repoRoot string) http.Handler { - return Handle(importPath, ImportTag(vcs, importPath, repoRoot)) + return Handle(ImportTag(vcs, importPath, repoRoot)) }