goproxy/sumdb/handler.go

107 lines
2.6 KiB
Go
Raw Permalink Normal View History

2020-02-18 13:56:04 +00:00
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2020-04-28 02:40:53 +00:00
// Package sumdb implements sumdb handler proxy.
2020-02-18 13:56:04 +00:00
package sumdb
import (
2022-04-14 07:27:12 +00:00
"context"
"errors"
2020-02-18 13:56:04 +00:00
"fmt"
"io"
"net/http"
"net/url"
"strings"
2022-04-14 07:27:12 +00:00
"time"
2020-02-18 13:56:04 +00:00
)
2022-04-14 07:27:12 +00:00
var supportedSumDB = map[string][]string{
"sum.golang.org": {"https://sum.golang.org/", "https://sum.golang.google.cn/"},
"sum.golang.google.cn": {"https://sum.golang.org/", "https://sum.golang.google.cn/"}, // db-name `sum.golang.google.cn` will be replaced in go
"gosum.io": {"https://gosum.io/"},
2020-02-18 13:56:04 +00:00
}
2022-04-14 07:27:12 +00:00
var (
errSumPathInvalid = errors.New("sumdb request path invalid")
)
2020-02-18 13:56:04 +00:00
2022-04-14 07:27:12 +00:00
// Handler handles sumdb request
// goproxy.io not impl a complete sumdb, just proxy to upstream.
2020-02-18 13:56:04 +00:00
func Handler(w http.ResponseWriter, r *http.Request) {
2022-04-14 07:27:12 +00:00
whichDB, realPath, err := parsePath(r.URL.Path)
_, supported := supportedSumDB[whichDB]
if err != nil || !supported {
// if not check the target db,
// curl https://goproxy.io/sumdb/www.google.com will succ
w.WriteHeader(http.StatusGone)
2022-04-14 07:27:12 +00:00
fmt.Fprint(w, "unsupported db")
return
2020-02-18 13:56:04 +00:00
}
2022-04-14 07:27:12 +00:00
// $GOROOT/src/cmd/go/internal/modfetch/sumdb.go@initBase
// > Before accessing any checksum database URL using a proxy, the proxy
// > client should first fetch <proxyURL>/sumdb/<sumdb-name>/supported.
if realPath == "supported" {
w.WriteHeader(http.StatusOK)
return
}
2022-04-14 07:27:12 +00:00
result := make(chan *http.Response)
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) // 2sec maybe enough
defer cancel()
for _, host := range supportedSumDB[whichDB] {
go proxySumdb(ctx, host, realPath, result)
}
select {
case resp := <-result:
{
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
if _, err := io.Copy(w, resp.Body); err != nil {
fmt.Fprint(w, err.Error())
}
}
case <-ctx.Done():
w.WriteHeader(http.StatusGone)
fmt.Fprint(w, ctx.Err().Error())
return
}
}
2020-02-18 13:56:04 +00:00
2022-04-14 07:27:12 +00:00
func parsePath(rawPath string) (whichDB, path string, err error) {
parts := strings.SplitN(rawPath, "/", 4)
if len(parts) < 4 {
return "", "", errSumPathInvalid
}
whichDB = parts[2]
path = parts[3]
return
2021-03-04 07:06:15 +00:00
}
2022-04-14 07:27:12 +00:00
func proxySumdb(ctx context.Context, host, path string, respChan chan<- *http.Response) {
urlPath, err := url.Parse(host)
if err != nil {
return
}
2022-04-14 07:27:12 +00:00
urlPath.Path = path
2022-04-14 07:27:12 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlPath.String(), nil)
if err != nil {
return
}
2022-04-14 07:27:12 +00:00
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
2022-04-14 07:27:12 +00:00
select {
case <-ctx.Done():
resp.Body.Close()
case respChan <- resp:
}
2021-03-04 07:06:15 +00:00
2020-02-18 13:56:04 +00:00
}