|
| 1 | +/* |
| 2 | + * Copyright The Dragonfly Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package http |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/dragonflyoss/Dragonfly/pkg/errortypes" |
| 27 | + "github.com/dragonflyoss/Dragonfly/pkg/protocol" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + // DefaultTransport is default implementation of http.Transport. |
| 32 | + DefaultTransport = newDefaultTransport() |
| 33 | + |
| 34 | + // DefaultClient is default implementation of Client. |
| 35 | + DefaultClient = &Client{ |
| 36 | + client: &http.Client{Transport: DefaultTransport}, |
| 37 | + transport: DefaultTransport, |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + // http protocol name |
| 43 | + ProtocolHTTPName = "http" |
| 44 | + |
| 45 | + // https protocol name |
| 46 | + ProtocolHTTPSName = "https" |
| 47 | +) |
| 48 | + |
| 49 | +func init() { |
| 50 | + protocol.RegisterProtocol(ProtocolHTTPName, &ClientBuilder{}) |
| 51 | + protocol.RegisterProtocol(ProtocolHTTPSName, &ClientBuilder{supportHTTPS: true}) |
| 52 | +} |
| 53 | + |
| 54 | +const ( |
| 55 | + HTTPTransport = "http.transport" |
| 56 | + TLSConfig = "tls.config" |
| 57 | +) |
| 58 | + |
| 59 | +func newDefaultTransport() *http.Transport { |
| 60 | + // copy from http.DefaultTransport |
| 61 | + return &http.Transport{ |
| 62 | + Proxy: http.ProxyFromEnvironment, |
| 63 | + DialContext: (&net.Dialer{ |
| 64 | + Timeout: 30 * time.Second, |
| 65 | + KeepAlive: 30 * time.Second, |
| 66 | + DualStack: true, |
| 67 | + }).DialContext, |
| 68 | + MaxIdleConns: 100, |
| 69 | + IdleConnTimeout: 90 * time.Second, |
| 70 | + TLSHandshakeTimeout: 10 * time.Second, |
| 71 | + ExpectContinueTimeout: 1 * time.Second, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// ClientOpt is the argument of NewProtocolClient. |
| 76 | +// ClientOpt supports some opt by key, such as "http.transport", "tls.config". |
| 77 | +// if not set, default opt will be used. |
| 78 | +type ClientOpt struct { |
| 79 | + opt map[string]interface{} |
| 80 | +} |
| 81 | + |
| 82 | +func NewClientOpt() *ClientOpt { |
| 83 | + return &ClientOpt{ |
| 84 | + opt: make(map[string]interface{}), |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (opt *ClientOpt) Set(key string, value interface{}) error { |
| 89 | + switch key { |
| 90 | + case HTTPTransport: |
| 91 | + if _, ok := value.(*http.Transport); !ok { |
| 92 | + return errortypes.ErrConvertFailed |
| 93 | + } |
| 94 | + break |
| 95 | + case TLSConfig: |
| 96 | + if _, ok := value.(*tls.Config); !ok { |
| 97 | + return errortypes.ErrConvertFailed |
| 98 | + } |
| 99 | + break |
| 100 | + default: |
| 101 | + return fmt.Errorf("not support") |
| 102 | + } |
| 103 | + |
| 104 | + opt.opt[key] = value |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (opt *ClientOpt) Get(key string) interface{} { |
| 109 | + v, ok := opt.opt[key] |
| 110 | + if !ok { |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + return v |
| 115 | +} |
| 116 | + |
| 117 | +var _ protocol.Client = &Client{} |
| 118 | + |
| 119 | +// Client is an implementation of protocol.Client for http protocol. |
| 120 | +type Client struct { |
| 121 | + client *http.Client |
| 122 | + transport http.RoundTripper |
| 123 | +} |
| 124 | + |
| 125 | +func (cli *Client) GetResource(url string, md protocol.Metadata) protocol.Resource { |
| 126 | + var ( |
| 127 | + hd *Headers |
| 128 | + ) |
| 129 | + |
| 130 | + if md != nil { |
| 131 | + h, ok := md.(*Headers) |
| 132 | + if ok { |
| 133 | + hd = h |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return &Resource{ |
| 138 | + url: url, |
| 139 | + hd: hd, |
| 140 | + client: cli, |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// ClientBuilder is an implementation of protocol.ClientBuilder for http protocol. |
| 145 | +type ClientBuilder struct { |
| 146 | + supportHTTPS bool |
| 147 | +} |
| 148 | + |
| 149 | +func (cb *ClientBuilder) NewProtocolClient(clientOpt interface{}) (protocol.Client, error) { |
| 150 | + var ( |
| 151 | + transport *http.Transport = DefaultTransport |
| 152 | + tlsConfig *tls.Config |
| 153 | + ) |
| 154 | + |
| 155 | + if clientOpt != nil { |
| 156 | + opt, ok := clientOpt.(*ClientOpt) |
| 157 | + if !ok { |
| 158 | + return nil, errortypes.ErrConvertFailed |
| 159 | + } |
| 160 | + |
| 161 | + tran := opt.Get(HTTPTransport) |
| 162 | + if tran != nil { |
| 163 | + transport = tran.(*http.Transport) |
| 164 | + } |
| 165 | + |
| 166 | + config := opt.Get(TLSConfig) |
| 167 | + if config != nil { |
| 168 | + tlsConfig = config.(*tls.Config) |
| 169 | + } |
| 170 | + |
| 171 | + // set tls config to transport |
| 172 | + if config != nil { |
| 173 | + if transport == DefaultTransport { |
| 174 | + transport = newDefaultTransport() |
| 175 | + } |
| 176 | + |
| 177 | + transport.TLSClientConfig = tlsConfig |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + if cb.supportHTTPS { |
| 182 | + if transport.TLSClientConfig == nil || transport.DialTLS == nil { |
| 183 | + return nil, fmt.Errorf("in https mode, tls should be set") |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return &Client{ |
| 188 | + client: &http.Client{Transport: transport}, |
| 189 | + transport: transport, |
| 190 | + }, nil |
| 191 | +} |
0 commit comments