github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/go/http.go (about) 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. 4 5 // +build !cmd_go_bootstrap 6 7 // This code is compiled into the real 'go' binary, but it is not 8 // compiled into the binary that is built during all.bash, so as 9 // to avoid needing to build net (and thus use cgo) during the 10 // bootstrap process. 11 12 package main 13 14 import ( 15 "cmd/internal/browser" 16 "crypto/tls" 17 "fmt" 18 "io" 19 "io/ioutil" 20 "log" 21 "net/http" 22 "net/url" 23 "time" 24 ) 25 26 // httpClient is the default HTTP client, but a variable so it can be 27 // changed by tests, without modifying http.DefaultClient. 28 var httpClient = http.DefaultClient 29 30 // impatientInsecureHTTPClient is used in -insecure mode, 31 // when we're connecting to https servers that might not be there 32 // or might be using self-signed certificates. 33 var impatientInsecureHTTPClient = &http.Client{ 34 Timeout: 5 * time.Second, 35 Transport: &http.Transport{ 36 TLSClientConfig: &tls.Config{ 37 InsecureSkipVerify: true, 38 }, 39 }, 40 } 41 42 type httpError struct { 43 status string 44 statusCode int 45 url string 46 } 47 48 func (e *httpError) Error() string { 49 return fmt.Sprintf("%s: %s", e.url, e.status) 50 } 51 52 // httpGET returns the data from an HTTP GET request for the given URL. 53 func httpGET(url string) ([]byte, error) { 54 resp, err := httpClient.Get(url) 55 if err != nil { 56 return nil, err 57 } 58 defer resp.Body.Close() 59 if resp.StatusCode != 200 { 60 err := &httpError{status: resp.Status, statusCode: resp.StatusCode, url: url} 61 62 return nil, err 63 } 64 b, err := ioutil.ReadAll(resp.Body) 65 if err != nil { 66 return nil, fmt.Errorf("%s: %v", url, err) 67 } 68 return b, nil 69 } 70 71 // httpsOrHTTP returns the body of either the importPath's 72 // https resource or, if unavailable, the http resource. 73 func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body io.ReadCloser, err error) { 74 fetch := func(scheme string) (urlStr string, res *http.Response, err error) { 75 u, err := url.Parse(scheme + "://" + importPath) 76 if err != nil { 77 return "", nil, err 78 } 79 u.RawQuery = "go-get=1" 80 urlStr = u.String() 81 if buildV { 82 log.Printf("Fetching %s", urlStr) 83 } 84 if security == insecure && scheme == "https" { // fail earlier 85 res, err = impatientInsecureHTTPClient.Get(urlStr) 86 } else { 87 res, err = httpClient.Get(urlStr) 88 } 89 return 90 } 91 closeBody := func(res *http.Response) { 92 if res != nil { 93 res.Body.Close() 94 } 95 } 96 urlStr, res, err := fetch("https") 97 if err != nil { 98 if buildV { 99 log.Printf("https fetch failed: %v", err) 100 } 101 if security == insecure { 102 closeBody(res) 103 urlStr, res, err = fetch("http") 104 } 105 } 106 if err != nil { 107 closeBody(res) 108 return "", nil, err 109 } 110 // Note: accepting a non-200 OK here, so people can serve a 111 // meta import in their http 404 page. 112 if buildV { 113 log.Printf("Parsing meta tags from %s (status code %d)", urlStr, res.StatusCode) 114 } 115 return urlStr, res.Body, nil 116 } 117 118 func queryEscape(s string) string { return url.QueryEscape(s) } 119 func openBrowser(url string) bool { return browser.Open(url) }