diff --git a/.gitignore b/.gitignore index 20c7387..fb44cb8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ cacheDir/* bin/* internal/ +pkg/* +!pkg/proxy diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index ad03103..f4b91f8 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -55,7 +55,7 @@ func NewProxy(cache string) http.Handler { realMod, err := getQuery(info.Version.Path, info.Version.Version) if err != nil { errLogger.Printf("goproxy: lookup %s@%s get err %s", info.Path, info.Version.Version, err) - ReturnBadRequest(w, err) + ReturnNotFound(w, err) return } if realMod.Path != info.Version.Path { @@ -70,7 +70,7 @@ func NewProxy(cache string) http.Handler { // use Stat instead of InfoFile, because when query-version is master, no infoFile here, maybe bug of go // TODO(hxzhao527): check whether InfoFile have a bug? errLogger.Printf("goproxy: fetch info %s@%s get err %s", info.Path, info.Version.Version, err) - ReturnBadRequest(w, err) + ReturnNotFound(w, err) } else { ReturnJsonData(w, revInfo) } @@ -79,7 +79,7 @@ func NewProxy(cache string) http.Handler { { if modFile, err := modfetch.GoModFile(realMod.Path, realMod.Version); err != nil { errLogger.Printf("goproxy: fetch modfile %s@%s get err %s", info.Path, info.Version.Version, err) - ReturnBadRequest(w, err) + ReturnNotFound(w, err) } else { http.ServeFile(w, r, modFile) } @@ -89,7 +89,7 @@ func NewProxy(cache string) http.Handler { mod := module.Version{Path: realMod.Path, Version: realMod.Version} if zipFile, err := modfetch.DownloadZip(mod); err != nil { errLogger.Printf("goproxy: download zip %s@%s get err %s", info.Path, info.Version.Version, err) - ReturnBadRequest(w, err) + ReturnNotFound(w, err) } else { http.ServeFile(w, r, zipFile) } @@ -101,22 +101,21 @@ func NewProxy(cache string) http.Handler { { repo, err := modfetch.Lookup(info.Path) if err != nil { - errLogger.Printf("goproxy: lookup failed: %v", err) - ReturnInternalServerError(w, err) + ReturnNotFound(w, err) return } switch suf { case "/@v/list": modPath := strings.Trim(strings.TrimSuffix(r.URL.Path, "/@v/list"), "/") modPath, err := module.DecodePath(modPath) - if err != nil { - ReturnInternalServerError(w, err) + if err != nil { + ReturnNotFound(w, err) return } modload.LoadBuildList() mods := modload.ListModules([]string{modPath + "@latest"}, false, true) - + data := []byte(strings.Join(mods[0].Versions, "\n") + "\n") if len(data) == 1 { data = nil @@ -126,7 +125,7 @@ func NewProxy(cache string) http.Handler { case "/@latest": rev, err := repo.Stat("latest") if err != nil { - errLogger.Printf("latest failed: %v", err) + ReturnNotFound(w, err) return } ReturnJsonData(w, rev) diff --git a/pkg/proxy/response.go b/pkg/proxy/response.go index a1548fc..9f30221 100644 --- a/pkg/proxy/response.go +++ b/pkg/proxy/response.go @@ -24,6 +24,13 @@ func ReturnBadRequest(w http.ResponseWriter, err error) { _, _ = w.Write([]byte(msg)) } +func ReturnNotFound(w http.ResponseWriter, err error) { + w.WriteHeader(http.StatusNotFound) + msg := fmt.Sprintf("%v", err) + errLogger.Printf("goproxy: %s\n", msg) + _, _ = w.Write([]byte(msg)) +} + func ReturnSuccess(w http.ResponseWriter, data []byte) { w.WriteHeader(http.StatusOK) _, _ = w.Write(data) @@ -32,7 +39,7 @@ func ReturnSuccess(w http.ResponseWriter, data []byte) { func ReturnJsonData(w http.ResponseWriter, data interface{}) { js, err := json.Marshal(data) if err != nil { - ReturnInternalServerError(w, err) + ReturnNotFound(w, err) } else { ReturnSuccess(w, js) }