forked from containers/build
/
http.go
122 lines (108 loc) · 2.92 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015 The appc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discovery
import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
)
type InsecureOption int
const (
defaultDialTimeout = 5 * time.Second
)
const (
InsecureNone InsecureOption = 0
InsecureTls InsecureOption = 1 << iota
InsecureHttp
)
var (
// Client is the default http.Client used for discovery requests.
Client *http.Client
ClientInsecureTls *http.Client
// httpDo is the internal object used by discovery to retrieve URLs; it is
// defined here so it can be overridden for testing
httpDo httpDoer
httpDoInsecureTls httpDoer
)
// httpDoer is an interface used to wrap http.Client for real requests and
// allow easy mocking in local tests.
type httpDoer interface {
Do(req *http.Request) (resp *http.Response, err error)
}
func init() {
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: func(n, a string) (net.Conn, error) {
return net.DialTimeout(n, a, defaultDialTimeout)
},
}
Client = &http.Client{
Transport: t,
}
httpDo = Client
// copy for InsecureTls
tInsecureTls := *t
tInsecureTls.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
ClientInsecureTls = &http.Client{
Transport: &tInsecureTls,
}
httpDoInsecureTls = ClientInsecureTls
}
func httpsOrHTTP(name string, hostHeaders map[string]http.Header, insecure InsecureOption) (urlStr string, body io.ReadCloser, err error) {
fetch := func(scheme string) (urlStr string, res *http.Response, err error) {
u, err := url.Parse(scheme + "://" + name)
if err != nil {
return "", nil, err
}
u.RawQuery = "ac-discovery=1"
urlStr = u.String()
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return "", nil, err
}
if hostHeader, ok := hostHeaders[u.Host]; ok {
req.Header = hostHeader
}
if insecure&InsecureTls != 0 {
res, err = httpDoInsecureTls.Do(req)
return
}
res, err = httpDo.Do(req)
return
}
closeBody := func(res *http.Response) {
if res != nil {
res.Body.Close()
}
}
urlStr, res, err := fetch("https")
if err != nil || res.StatusCode != http.StatusOK {
if insecure&InsecureHttp != 0 {
closeBody(res)
urlStr, res, err = fetch("http")
}
}
if res != nil && res.StatusCode != http.StatusOK {
err = fmt.Errorf("expected a 200 OK got %d", res.StatusCode)
}
if err != nil {
closeBody(res)
return "", nil, err
}
return urlStr, res.Body, nil
}