Skip to content

Commit

Permalink
enable sumdb if server can not access sum.golang.org
Browse files Browse the repository at this point in the history
  • Loading branch information
oiooj committed Feb 26, 2021
1 parent 9e48374 commit 4ca478b
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions sumdb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ func init() {

//Handler handles sumdb request
func Handler(w http.ResponseWriter, r *http.Request) {
if !enableGoogleSumDB {
w.WriteHeader(http.StatusNotFound)
return
}

if strings.HasSuffix(r.URL.Path, "/supported") {
for _, supported := range supportedSumDB {
uri := fmt.Sprintf("/sumdb/%s/supported", supported)
Expand All @@ -49,6 +44,11 @@ func Handler(w http.ResponseWriter, r *http.Request) {
return
}

if !enableGoogleSumDB {
sumViaGoproxy(w, r)
return
}

p := "https://" + strings.TrimPrefix(r.URL.Path, "/sumdb/")
_, err := url.Parse(p)
if err != nil {
Expand All @@ -69,6 +69,29 @@ func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, err.Error())
return
}
return
}

func sumViaGoproxy(w http.ResponseWriter, r *http.Request) {
p := "https://goproxy.io" + r.URL.Path
_, err := url.Parse(p)
if err != nil {
w.WriteHeader(http.StatusGone)
fmt.Fprintf(w, err.Error())
return
}

resp, err := http.Get(p)
if err != nil {
w.WriteHeader(http.StatusGone)
fmt.Fprintf(w, err.Error())
return
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
fmt.Fprintf(w, err.Error())
return
}
return
}

0 comments on commit 4ca478b

Please sign in to comment.