Skip to content

Commit 5a7317e

Browse files
committed
cmd/go: Use web2 to fetch secure import paths
This allows `go get` to use netrc files. - Reimplement web.Get as a forwarded call to web2.Get. - Replace secure half of web.GetMaybeInsecure with web2.Get and return the body through it instead of the HTTP response. Related to golang#29888.
1 parent 05e77d4 commit 5a7317e

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/cmd/go/internal/web/http.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
"crypto/tls"
1616
"fmt"
1717
"io"
18-
"io/ioutil"
1918
"log"
2019
"net/http"
2120
"net/url"
2221
"time"
2322

2423
"cmd/go/internal/cfg"
24+
"cmd/go/internal/web2"
2525
"cmd/internal/browser"
2626
)
2727

@@ -53,22 +53,9 @@ func (e *HTTPError) Error() string {
5353
}
5454

5555
// Get returns the data from an HTTP GET request for the given URL.
56-
func Get(url string) ([]byte, error) {
57-
resp, err := httpClient.Get(url)
58-
if err != nil {
59-
return nil, err
60-
}
61-
defer resp.Body.Close()
62-
if resp.StatusCode != 200 {
63-
err := &HTTPError{status: resp.Status, StatusCode: resp.StatusCode, url: url}
64-
65-
return nil, err
66-
}
67-
b, err := ioutil.ReadAll(resp.Body)
68-
if err != nil {
69-
return nil, fmt.Errorf("%s: %v", url, err)
70-
}
71-
return b, nil
56+
func Get(url string) (body []byte, err error) {
57+
err = web2.Get(url, web2.ReadAllBody(&body))
58+
return body, err
7259
}
7360

7461
// GetMaybeInsecure returns the body of either the importPath's
@@ -87,10 +74,16 @@ func GetMaybeInsecure(importPath string, security SecurityMode) (urlStr string,
8774
if security == Insecure && scheme == "https" { // fail earlier
8875
res, err = impatientInsecureHTTPClient.Get(urlStr)
8976
} else {
90-
res, err = httpClient.Get(urlStr)
77+
res = nil
78+
err = web2.Get(urlStr, web2.Body(&body))
9179
}
9280
return
9381
}
82+
closeReader := func(r io.ReadCloser) {
83+
if r != nil {
84+
r.Close()
85+
}
86+
}
9487
closeBody := func(res *http.Response) {
9588
if res != nil {
9689
res.Body.Close()
@@ -103,19 +96,23 @@ func GetMaybeInsecure(importPath string, security SecurityMode) (urlStr string,
10396
}
10497
if security == Insecure {
10598
closeBody(res)
99+
closeReader(body)
106100
urlStr, res, err = fetch("http")
107101
}
108102
}
109103
if err != nil {
110104
closeBody(res)
105+
closeReader(body)
111106
return "", nil, err
107+
} else if body == nil {
108+
body = res.Body
112109
}
113110
// Note: accepting a non-200 OK here, so people can serve a
114111
// meta import in their http 404 page.
115-
if cfg.BuildV {
112+
if cfg.BuildV && res != nil {
116113
log.Printf("Parsing meta tags from %s (status code %d)", urlStr, res.StatusCode)
117114
}
118-
return urlStr, res.Body, nil
115+
return urlStr, body, nil
119116
}
120117

121118
func QueryEscape(s string) string { return url.QueryEscape(s) }

0 commit comments

Comments
 (0)