@@ -15,13 +15,13 @@ import (
15
15
"crypto/tls"
16
16
"fmt"
17
17
"io"
18
- "io/ioutil"
19
18
"log"
20
19
"net/http"
21
20
"net/url"
22
21
"time"
23
22
24
23
"cmd/go/internal/cfg"
24
+ "cmd/go/internal/web2"
25
25
"cmd/internal/browser"
26
26
)
27
27
@@ -53,22 +53,9 @@ func (e *HTTPError) Error() string {
53
53
}
54
54
55
55
// 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
72
59
}
73
60
74
61
// GetMaybeInsecure returns the body of either the importPath's
@@ -87,10 +74,16 @@ func GetMaybeInsecure(importPath string, security SecurityMode) (urlStr string,
87
74
if security == Insecure && scheme == "https" { // fail earlier
88
75
res , err = impatientInsecureHTTPClient .Get (urlStr )
89
76
} else {
90
- res , err = httpClient .Get (urlStr )
77
+ res = nil
78
+ err = web2 .Get (urlStr , web2 .Body (& body ))
91
79
}
92
80
return
93
81
}
82
+ closeReader := func (r io.ReadCloser ) {
83
+ if r != nil {
84
+ r .Close ()
85
+ }
86
+ }
94
87
closeBody := func (res * http.Response ) {
95
88
if res != nil {
96
89
res .Body .Close ()
@@ -103,19 +96,23 @@ func GetMaybeInsecure(importPath string, security SecurityMode) (urlStr string,
103
96
}
104
97
if security == Insecure {
105
98
closeBody (res )
99
+ closeReader (body )
106
100
urlStr , res , err = fetch ("http" )
107
101
}
108
102
}
109
103
if err != nil {
110
104
closeBody (res )
105
+ closeReader (body )
111
106
return "" , nil , err
107
+ } else if body == nil {
108
+ body = res .Body
112
109
}
113
110
// Note: accepting a non-200 OK here, so people can serve a
114
111
// meta import in their http 404 page.
115
- if cfg .BuildV {
112
+ if cfg .BuildV && res != nil {
116
113
log .Printf ("Parsing meta tags from %s (status code %d)" , urlStr , res .StatusCode )
117
114
}
118
- return urlStr , res . Body , nil
115
+ return urlStr , body , nil
119
116
}
120
117
121
118
func QueryEscape (s string ) string { return url .QueryEscape (s ) }
0 commit comments