2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2020-09-25 17:35:47 -04:00
|
|
|
"context"
|
2018-03-15 16:03:41 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-10-31 02:39:09 -04:00
|
|
|
"fmt"
|
2019-08-13 07:13:11 -04:00
|
|
|
"net"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2019-07-25 23:55:09 -04:00
|
|
|
"strings"
|
2018-03-15 16:03:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// URL - improved JSON friendly url.URL.
|
|
|
|
type URL url.URL
|
|
|
|
|
|
|
|
// IsEmpty - checks URL is empty or not.
|
|
|
|
func (u URL) IsEmpty() bool {
|
|
|
|
return u.String() == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// String - returns string representation of URL.
|
|
|
|
func (u URL) String() string {
|
|
|
|
// if port number 80 and 443, remove for http and https scheme respectively
|
|
|
|
if u.Host != "" {
|
2018-04-19 20:24:43 -04:00
|
|
|
host, err := ParseHost(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
switch {
|
|
|
|
case u.Scheme == "http" && host.Port == 80:
|
|
|
|
fallthrough
|
|
|
|
case u.Scheme == "https" && host.Port == 443:
|
|
|
|
u.Host = host.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uu := url.URL(u)
|
|
|
|
return uu.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON - converts to JSON string data.
|
|
|
|
func (u URL) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(u.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - parses given data into URL.
|
|
|
|
func (u *URL) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var s string
|
|
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow empty string
|
|
|
|
if s == "" {
|
|
|
|
*u = URL{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ru *URL
|
|
|
|
if ru, err = ParseURL(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*u = *ru
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:39:09 -04:00
|
|
|
// ParseHTTPURL - parses a string into HTTP URL, string is
|
|
|
|
// expected to be of form http:// or https://
|
|
|
|
func ParseHTTPURL(s string) (u *URL, err error) {
|
|
|
|
u, err = ParseURL(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch u.Scheme {
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected scheme found %s", u.Scheme)
|
|
|
|
case "http", "https":
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// ParseURL - parses string into URL.
|
|
|
|
func ParseURL(s string) (u *URL, err error) {
|
|
|
|
var uu *url.URL
|
|
|
|
if uu, err = url.Parse(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:42:10 -05:00
|
|
|
if uu.Hostname() == "" {
|
2018-03-15 16:03:41 -04:00
|
|
|
if uu.Scheme != "" {
|
|
|
|
return nil, errors.New("scheme appears with empty host")
|
|
|
|
}
|
2019-11-26 14:42:10 -05:00
|
|
|
} else {
|
|
|
|
portStr := uu.Port()
|
|
|
|
if portStr == "" {
|
|
|
|
switch uu.Scheme {
|
|
|
|
case "http":
|
|
|
|
portStr = "80"
|
|
|
|
case "https":
|
|
|
|
portStr = "443"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err = ParseHost(net.JoinHostPort(uu.Hostname(), portStr)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean path in the URL.
|
|
|
|
// Note: path.Clean() is used on purpose because in MS Windows filepath.Clean() converts
|
|
|
|
// `/` into `\` ie `/foo` becomes `\foo`
|
|
|
|
if uu.Path != "" {
|
|
|
|
uu.Path = path.Clean(uu.Path)
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:55:09 -04:00
|
|
|
// path.Clean removes the trailing '/' and converts '//' to '/'.
|
|
|
|
if strings.HasSuffix(s, "/") && !strings.HasSuffix(uu.Path, "/") {
|
|
|
|
uu.Path += "/"
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
v := URL(*uu)
|
|
|
|
u = &v
|
|
|
|
return u, nil
|
|
|
|
}
|
2019-09-12 19:44:51 -04:00
|
|
|
|
|
|
|
// IsNetworkOrHostDown - if there was a network error or if the host is down.
|
2020-11-21 01:55:35 -05:00
|
|
|
// expectTimeouts indicates that *context* timeouts are expected and does not
|
2020-10-29 12:52:11 -04:00
|
|
|
// indicate a downed host. Other timeouts still returns down.
|
|
|
|
func IsNetworkOrHostDown(err error, expectTimeouts bool) bool {
|
2019-09-12 19:44:51 -04:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-19 16:53:49 -05:00
|
|
|
|
2020-09-25 17:35:47 -04:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-19 16:53:49 -05:00
|
|
|
|
2020-10-29 12:52:11 -04:00
|
|
|
if expectTimeouts && errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-19 16:53:49 -05:00
|
|
|
|
2019-09-12 19:44:51 -04:00
|
|
|
// We need to figure if the error either a timeout
|
|
|
|
// or a non-temporary error.
|
2020-11-21 01:55:35 -05:00
|
|
|
urlErr := &url.Error{}
|
|
|
|
if errors.As(err, &urlErr) {
|
|
|
|
switch urlErr.Err.(type) {
|
|
|
|
case *net.DNSError, *net.OpError, net.UnknownNetworkError:
|
|
|
|
return true
|
2019-09-12 19:44:51 -04:00
|
|
|
}
|
2020-11-21 01:55:35 -05:00
|
|
|
}
|
2020-10-12 17:19:46 -04:00
|
|
|
|
2020-11-21 01:55:35 -05:00
|
|
|
var e net.Error
|
|
|
|
if errors.As(err, &e) {
|
2019-09-12 19:44:51 -04:00
|
|
|
if e.Timeout() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 17:19:46 -04:00
|
|
|
|
2019-09-12 19:44:51 -04:00
|
|
|
// Fallback to other mechanisms.
|
2020-11-21 01:55:35 -05:00
|
|
|
switch {
|
|
|
|
case strings.Contains(err.Error(), "Connection closed by foreign host"):
|
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), "TLS handshake timeout"):
|
2019-09-12 19:44:51 -04:00
|
|
|
// If error is - tlsHandshakeTimeoutError.
|
2020-11-21 01:55:35 -05:00
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), "i/o timeout"):
|
2019-09-12 19:44:51 -04:00
|
|
|
// If error is - tcp timeoutError.
|
2020-11-21 01:55:35 -05:00
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), "connection timed out"):
|
2019-09-12 19:44:51 -04:00
|
|
|
// If err is a net.Dial timeout.
|
2020-11-21 01:55:35 -05:00
|
|
|
return true
|
2021-04-08 19:40:38 -04:00
|
|
|
case strings.Contains(err.Error(), "connection reset by peer"):
|
|
|
|
// IF err is a peer reset on a socket.
|
|
|
|
return true
|
|
|
|
case strings.Contains(err.Error(), "broken pipe"):
|
|
|
|
// IF err is a broken pipe on a socket.
|
|
|
|
return true
|
2020-11-21 01:55:35 -05:00
|
|
|
case strings.Contains(strings.ToLower(err.Error()), "503 service unavailable"):
|
2019-09-12 19:44:51 -04:00
|
|
|
// Denial errors
|
2020-11-21 01:55:35 -05:00
|
|
|
return true
|
2019-09-12 19:44:51 -04:00
|
|
|
}
|
2020-11-21 01:55:35 -05:00
|
|
|
return false
|
2019-09-12 19:44:51 -04:00
|
|
|
}
|