Source File
client.go
Belonging Package
golang.org/x/pkgsite/internal/proxy
package proxy
import (
)
const disableFetchHeader = "Disable-Module-Fetch"
func ( *Client) () *Client {
:= *
.disableFetch = true
return &
}
func ( *Client) () bool {
return .disableFetch
}
func ( *Client) () *Client {
:= *
.rememberLastZip = true
.lastZipModulePath = ""
.lastZipVersion = ""
.lastZipReader = nil
return &
}
func ( *Client) ( context.Context, , string) ( *zip.Reader, error) {
defer derrors.WrapStack(&, "proxy.Client.Zip(ctx, %q, %q)", , )
if .lastZipModulePath == && .lastZipVersion == {
return .lastZipReader, nil
}
, := .readBody(, , , "zip")
if != nil {
return nil,
}
, := zip.NewReader(bytes.NewReader(), int64(len()))
if != nil {
return nil, fmt.Errorf("zip.NewReader: %v: %w", , derrors.BadModule)
}
if .rememberLastZip {
.lastZipModulePath =
.lastZipVersion =
.lastZipReader =
}
return , nil
}
func ( *Client) ( context.Context, , string) ( int64, error) {
defer derrors.WrapStack(&, "proxy.Client.ZipSize(ctx, %q, %q)", , )
, := .escapedURL(, , "zip")
if != nil {
return 0,
}
, := ctxhttp.Head(, .httpClient, )
if != nil {
return 0, fmt.Errorf("ctxhttp.Head(ctx, client, %q): %v", , )
}
defer .Body.Close()
if := responseError(, false); != nil {
return 0,
}
if .ContentLength < 0 {
return 0, errors.New("unknown content length")
}
return .ContentLength, nil
}
func ( *Client) (, , string) ( string, error) {
defer derrors.WrapStack(&, "Client.escapedURL(%q, %q, %q)", , , )
if != "info" && != "mod" && != "zip" {
return "", errors.New(`suffix must be "info", "mod" or "zip"`)
}
, := module.EscapePath()
if != nil {
return "", fmt.Errorf("path: %v: %w", , derrors.InvalidArgument)
}
if == internal.LatestVersion {
if != "info" {
return "", fmt.Errorf("cannot ask for latest with suffix %q", )
}
return fmt.Sprintf("%s/%s/@latest", .url, ), nil
}
, := module.EscapeVersion()
if != nil {
return "", fmt.Errorf("version: %v: %w", , derrors.InvalidArgument)
}
return fmt.Sprintf("%s/%s/@v/%s.%s", .url, , , ), nil
}
func ( *Client) ( context.Context, , , string) ( []byte, error) {
defer derrors.WrapStack(&, "Client.readBody(%q, %q, %q)", , , )
, := .escapedURL(, , )
if != nil {
return nil,
}
var []byte
= .executeRequest(, , func( io.Reader) error {
var error
, = ioutil.ReadAll()
return
})
if != nil {
return nil,
}
return , nil
}
func ( *Client) ( context.Context, string) ( []string, error) {
, := module.EscapePath()
if != nil {
return nil, fmt.Errorf("module.EscapePath(%q): %w", , derrors.InvalidArgument)
}
:= fmt.Sprintf("%s/%s/@v/list", .url, )
var []string
:= func( io.Reader) error {
:= bufio.NewScanner()
for .Scan() {
= append(, .Text())
}
return .Err()
}
if := .executeRequest(, , ); != nil {
return nil,
}
return , nil
}
func ( *Client) ( context.Context, string, func( io.Reader) error) ( error) {
defer func() {
if .Err() != nil {
= fmt.Errorf("%v: %w", , derrors.ProxyTimedOut)
}
derrors.WrapStack(&, "executeRequest(ctx, %q)", )
}()
, := http.NewRequest("GET", , nil)
if != nil {
return
}
if .disableFetch {
.Header.Set(disableFetchHeader, "true")
}
, := ctxhttp.Do(, .httpClient, )
if != nil {
return fmt.Errorf("ctxhttp.Do(ctx, client, %q): %v", , )
}
defer .Body.Close()
if := responseError(, .disableFetch); != nil {
return
}
return (.Body)
}
func ( *http.Response, bool) error {
switch {
case 200 <= .StatusCode && .StatusCode < 300:
return nil
case 500 <= .StatusCode:
return derrors.ProxyError
case .StatusCode == http.StatusNotFound,
, := ioutil.ReadAll(.Body)
if != nil {
return fmt.Errorf("ioutil.readall: %v", )
}
:= string()
switch {
case strings.Contains(, "fetch timed out"):
= derrors.ProxyTimedOut
case :
= derrors.NotFetched
default:
= derrors.NotFound
}
return fmt.Errorf("%q: %w", , )
default:
return fmt.Errorf("unexpected status %d %s", .StatusCode, .Status)
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |