2018-10-04 20:44:06 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-10-04 20:44:06 -04:00
|
|
|
*
|
|
|
|
* 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 rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultRESTTimeout - default RPC timeout is one minute.
|
|
|
|
const DefaultRESTTimeout = 1 * time.Minute
|
|
|
|
|
2019-05-29 13:21:47 -04:00
|
|
|
// NetworkError - error type in case of errors related to http/transport
|
|
|
|
// for ex. connection refused, connection reset, dns resolution failure etc.
|
|
|
|
// All errors returned by storage-rest-server (ex errFileNotFound, errDiskNotFound) are not considered to be network errors.
|
|
|
|
type NetworkError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkError) Error() string {
|
|
|
|
return n.Err.Error()
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
// Client - http based RPC client.
|
|
|
|
type Client struct {
|
2018-11-20 14:07:19 -05:00
|
|
|
httpClient *http.Client
|
|
|
|
httpIdleConnsCloser func()
|
|
|
|
url *url.URL
|
|
|
|
newAuthToken func() string
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:08:58 -04:00
|
|
|
// URL query separator constants
|
|
|
|
const (
|
|
|
|
resourceSep = "/"
|
|
|
|
querySep = "?"
|
|
|
|
)
|
|
|
|
|
2019-06-08 18:54:41 -04:00
|
|
|
// CallWithContext - make a REST call with context.
|
|
|
|
func (c *Client) CallWithContext(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (reply io.ReadCloser, err error) {
|
2019-08-06 15:08:58 -04:00
|
|
|
req, err := http.NewRequest(http.MethodPost, c.url.String()+resourceSep+method+querySep+values.Encode(), body)
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
2019-05-29 13:21:47 -04:00
|
|
|
return nil, &NetworkError{err}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
2019-06-08 18:54:41 -04:00
|
|
|
req = req.WithContext(ctx)
|
2018-10-04 20:44:06 -04:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.newAuthToken())
|
|
|
|
req.Header.Set("X-Minio-Time", time.Now().UTC().Format(time.RFC3339))
|
2019-01-17 07:58:18 -05:00
|
|
|
if length > 0 {
|
|
|
|
req.ContentLength = length
|
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2019-05-29 13:21:47 -04:00
|
|
|
return nil, &NetworkError{err}
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-02-06 15:07:03 -05:00
|
|
|
defer xhttp.DrainBody(resp.Body)
|
2018-10-04 20:44:06 -04:00
|
|
|
// Limit the ReadAll(), just in case, because of a bug, the server responds with large data.
|
2019-02-06 15:07:03 -05:00
|
|
|
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, 4096))
|
2018-10-04 20:44:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-02 15:25:34 -04:00
|
|
|
if len(b) > 0 {
|
|
|
|
return nil, errors.New(string(b))
|
|
|
|
}
|
|
|
|
return nil, errors.New(resp.Status)
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:54:41 -04:00
|
|
|
// Call - make a REST call.
|
|
|
|
func (c *Client) Call(method string, values url.Values, body io.Reader, length int64) (reply io.ReadCloser, err error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
return c.CallWithContext(ctx, method, values, body, length)
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:07:19 -05:00
|
|
|
// Close closes all idle connections of the underlying http client
|
|
|
|
func (c *Client) Close() {
|
|
|
|
if c.httpIdleConnsCloser != nil {
|
|
|
|
c.httpIdleConnsCloser()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:44:06 -04:00
|
|
|
func newCustomDialContext(timeout time.Duration) func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: timeout,
|
|
|
|
KeepAlive: timeout,
|
|
|
|
DualStack: true,
|
|
|
|
}
|
|
|
|
|
2019-04-27 18:14:16 -04:00
|
|
|
return dialer.DialContext(ctx, network, addr)
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:07:19 -05:00
|
|
|
// NewClient - returns new REST client.
|
2019-02-14 20:53:46 -05:00
|
|
|
func NewClient(url *url.URL, tlsConfig *tls.Config, timeout time.Duration, newAuthToken func() string) (*Client, error) {
|
2018-11-20 14:07:19 -05:00
|
|
|
// Transport is exactly same as Go default in https://golang.org/pkg/net/http/#RoundTripper
|
|
|
|
// except custom DialContext and TLSClientConfig.
|
|
|
|
tr := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: newCustomDialContext(timeout),
|
2019-05-17 03:22:25 -04:00
|
|
|
MaxIdleConnsPerHost: 256,
|
|
|
|
IdleConnTimeout: 60 * time.Second,
|
2019-02-14 20:53:46 -05:00
|
|
|
TLSHandshakeTimeout: 30 * time.Second,
|
|
|
|
ExpectContinueTimeout: 10 * time.Second,
|
2018-11-20 14:07:19 -05:00
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
DisableCompression: true,
|
|
|
|
}
|
2018-10-04 20:44:06 -04:00
|
|
|
return &Client{
|
2018-11-20 14:07:19 -05:00
|
|
|
httpClient: &http.Client{Transport: tr},
|
|
|
|
httpIdleConnsCloser: tr.CloseIdleConnections,
|
|
|
|
url: url,
|
|
|
|
newAuthToken: newAuthToken,
|
2019-02-14 20:53:46 -05:00
|
|
|
}, nil
|
2018-10-04 20:44:06 -04:00
|
|
|
}
|