forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
89 lines (75 loc) · 2.78 KB
/
httpclient.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
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 httpclient provides a thin, but testable, wrapper around http.Client.
// It adds an ECS header to requests and provides an interface
package httpclient
import (
"crypto/tls"
"net"
"net/http"
"time"
"github.com/aws/amazon-ecs-agent/agent/version"
)
const defaultTimeout = 10 * time.Minute
// Taken from the default http.Client behavior
const defaultDialTimeout = 30 * time.Second
const defaultDialKeepalive = 30 * time.Second
//go:generate mockgen.sh net/http RoundTripper mock/$GOFILE
type ecsRoundTripper struct {
insecureSkipVerify bool
transport http.RoundTripper
}
func userAgent() string {
return version.String() + " (+http://aws.amazon.com/ecs/)"
}
func (client *ecsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", userAgent())
return client.transport.RoundTrip(req)
}
func (client *ecsRoundTripper) CancelRequest(req *http.Request) {
if def, ok := client.transport.(*http.Transport); ok {
def.CancelRequest(req)
}
}
// New returns an ECS httpClient with a roundtrip timeout of the given duration
func New(timeout time.Duration, insecureSkipVerify bool) *http.Client {
// Transport is the transport requests will be made over
// Note, these defaults are taken from the golang http library. We do not
// explicitly do not use theirs to avoid changing their behavior.
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: defaultDialTimeout,
KeepAlive: defaultDialKeepalive,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
if insecureSkipVerify {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
client := &http.Client{
Transport: &ecsRoundTripper{insecureSkipVerify, transport},
Timeout: timeout,
}
return client
}
// OverridableTransport is a transport that provides an override for testing purposes.
type OverridableTransport interface {
SetTransport(http.RoundTripper)
}
// SetTransport allows you to set the transport. It is exposed so tests may
// override it. It fulfills an interface to allow calling thsi function via
// assertion to the exported interface
func (client *ecsRoundTripper) SetTransport(transport http.RoundTripper) {
client.transport = transport
}