1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // httpClient is the default HTTP client, but a variable so it can be
17 // changed by tests, without modifying http.DefaultClient.
18 var httpClient = http.DefaultClient
20 // httpGET returns the data from an HTTP GET request for the given URL.
21 func httpGET(url string) ([]byte, error) {
22 resp, err := httpClient.Get(url)
26 defer resp.Body.Close()
27 if resp.StatusCode != 200 {
28 return nil, fmt.Errorf("%s: %s", url, resp.Status)
30 b, err := ioutil.ReadAll(resp.Body)
32 return nil, fmt.Errorf("%s: %v", url, err)
37 // httpsOrHTTP returns the body of either the importPath's
38 // https resource or, if unavailable, the http resource.
39 func httpsOrHTTP(importPath string) (urlStr string, body io.ReadCloser, err error) {
40 fetch := func(scheme string) (urlStr string, res *http.Response, err error) {
41 u, err := url.Parse(scheme + "://" + importPath)
45 u.RawQuery = "go-get=1"
48 log.Printf("Fetching %s", urlStr)
50 res, err = httpClient.Get(urlStr)
53 closeBody := func(res *http.Response) {
58 urlStr, res, err := fetch("https")
59 if err != nil || res.StatusCode != 200 {
62 log.Printf("https fetch failed.")
64 log.Printf("ignoring https fetch with status code %d", res.StatusCode)
68 urlStr, res, err = fetch("http")
74 // Note: accepting a non-200 OK here, so people can serve a
75 // meta import in their http 404 page.
77 log.Printf("Parsing meta tags from %s (status code %d)", urlStr, res.StatusCode)
79 return urlStr, res.Body, nil