blob: 4a7fe69d2e19a937873155bb7dbeb7b471983cf8 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package transport
16
17import (
18 "net"
19 "net/http"
20 "strings"
21 "time"
22)
23
24type unixTransport struct{ *http.Transport }
25
26func NewTransport(info TLSInfo, dialtimeoutd time.Duration) (*http.Transport, error) {
27 cfg, err := info.ClientConfig()
28 if err != nil {
29 return nil, err
30 }
31
32 t := &http.Transport{
33 Proxy: http.ProxyFromEnvironment,
34 Dial: (&net.Dialer{
35 Timeout: dialtimeoutd,
36 // value taken from http.DefaultTransport
37 KeepAlive: 30 * time.Second,
38 }).Dial,
39 // value taken from http.DefaultTransport
40 TLSHandshakeTimeout: 10 * time.Second,
41 TLSClientConfig: cfg,
42 }
43
44 dialer := (&net.Dialer{
45 Timeout: dialtimeoutd,
46 KeepAlive: 30 * time.Second,
47 })
48 dial := func(net, addr string) (net.Conn, error) {
49 return dialer.Dial("unix", addr)
50 }
51
52 tu := &http.Transport{
53 Proxy: http.ProxyFromEnvironment,
54 Dial: dial,
55 TLSHandshakeTimeout: 10 * time.Second,
56 TLSClientConfig: cfg,
57 }
58 ut := &unixTransport{tu}
59
60 t.RegisterProtocol("unix", ut)
61 t.RegisterProtocol("unixs", ut)
62
63 return t, nil
64}
65
66func (urt *unixTransport) RoundTrip(req *http.Request) (*http.Response, error) {
67 url := *req.URL
68 req.URL = &url
69 req.URL.Scheme = strings.Replace(req.URL.Scheme, "unix", "http", 1)
70 return urt.Transport.RoundTrip(req)
71}