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
This commit is contained in:
jonbetti 2018-06-04 23:28:03 +00:00
parent f50d8ea86b
commit 4d77f362ea

View File

@ -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(`<!DOCTYPE html>
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))
}