fix: support cache 302 status code

pull/163/head v2.0.5
ping 2020-08-31 14:53:56 +08:00 committed by kun
parent 2c71c26191
commit a4e417a2ce
2 changed files with 49 additions and 0 deletions

View File

@ -72,6 +72,7 @@ func init() {
os.Setenv("GOSUMDB", "off") os.Setenv("GOSUMDB", "off")
downloadRoot = getDownloadRoot() downloadRoot = getDownloadRoot()
os.Setenv("GOMODCACHE", downloadRoot)
} }
func main() { func main() {

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/tls" "crypto/tls"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -96,6 +97,53 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
} }
} }
} }
// support 302 status code.
if r.StatusCode == http.StatusFound {
loc := r.Header.Get("Location")
if loc == "" {
return fmt.Errorf("%d response missing Location header", r.StatusCode)
}
// TODO: location is relative.
_, err := url.Parse(loc)
if err != nil {
return fmt.Errorf("failed to parse Location header %q: %v", loc, err)
}
resp, err := http.Get(loc)
if err != nil {
return err
}
defer resp.Body.Close()
var buf []byte
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
gr, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer gr.Close()
buf, err = ioutil.ReadAll(gr)
if err != nil {
return err
}
resp.Header.Del("Content-Encoding")
} else {
buf, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
}
resp.Body = ioutil.NopCloser(bytes.NewReader(buf))
if buf != nil {
file := filepath.Join(opts.DownloadRoot, r.Request.URL.Path)
os.MkdirAll(path.Dir(file), os.ModePerm)
err = renameio.WriteFile(file, buf, 0666)
if err != nil {
return err
}
}
}
return nil return nil
} }
rt.pattern = opts.Pattern rt.pattern = opts.Pattern