From 5aa10edcd3ffea3c727667426e7ccd56cba2e7d9 Mon Sep 17 00:00:00 2001 From: jsign Date: Thu, 29 Aug 2019 18:11:31 -0300 Subject: [PATCH] proxy/router: fix panic & gofmt --- main.go | 4 ++-- proxy/router.go | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index e8f58c9..87f73b6 100644 --- a/main.go +++ b/main.go @@ -92,8 +92,8 @@ func main() { log.Printf("ExcludeHost %s\n", excludeHost) } handle = &logger{proxy.NewRouter(proxy.NewServer(new(ops)), &proxy.RouterOptions{ - Pattern: excludeHost, - Proxy: proxyHost, + Pattern: excludeHost, + Proxy: proxyHost, DownloadRoot: downloadRoot, })} } else { diff --git a/proxy/router.go b/proxy/router.go index 0e53138..c86e7a7 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -20,8 +20,8 @@ const ListExpire = 5 * time.Minute // A RouterOps provides the proxy host and the external pattern type RouterOptions struct { - Pattern string - Proxy string + Pattern string + Proxy string DownloadRoot string } @@ -29,9 +29,9 @@ type RouterOptions struct { // which implements Route Filter to // routing private module or public module . type Router struct { - srv *Server - proxy *httputil.ReverseProxy - pattern string + srv *Server + proxy *httputil.ReverseProxy + pattern string downloadRoot string } @@ -107,6 +107,11 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { var ctype string defer f.Close() i := strings.Index(r.URL.Path, "/@v/") + if i < 0 { + http.Error(w, "no such path", http.StatusNotFound) + return + } + what := r.URL.Path[i+len("/@v/"):] if what == "list" { if time.Since(info.ModTime()) >= ListExpire { @@ -119,15 +124,15 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { } else { ext := path.Ext(what) switch ext { - case ".info": - ctype = "application/json" - case ".mod": - ctype = "text/plain; charset=UTF-8" - case ".zip": - ctype = "application/octet-stream" - default: - http.Error(w, "request not recognized", http.StatusNotFound) - return + case ".info": + ctype = "application/json" + case ".mod": + ctype = "text/plain; charset=UTF-8" + case ".zip": + ctype = "application/octet-stream" + default: + http.Error(w, "request not recognized", http.StatusNotFound) + return } } w.Header().Set("Content-Type", ctype)