You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

transport.go 2.5 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // +build go1.7 go1.8
  2. /*
  3. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  4. * Copyright 2017-2018 MinIO, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package minio_ext
  19. import (
  20. "crypto/tls"
  21. "crypto/x509"
  22. "net"
  23. "net/http"
  24. "time"
  25. "golang.org/x/net/http2"
  26. )
  27. // DefaultTransport - this default transport is similar to
  28. // http.DefaultTransport but with additional param DisableCompression
  29. // is set to true to avoid decompressing content with 'gzip' encoding.
  30. var DefaultTransport = func(secure bool) (http.RoundTripper, error) {
  31. tr := &http.Transport{
  32. Proxy: http.ProxyFromEnvironment,
  33. DialContext: (&net.Dialer{
  34. Timeout: 30 * time.Second,
  35. KeepAlive: 30 * time.Second,
  36. }).DialContext,
  37. MaxIdleConns: 1024,
  38. MaxIdleConnsPerHost: 1024,
  39. IdleConnTimeout: 90 * time.Second,
  40. TLSHandshakeTimeout: 10 * time.Second,
  41. ExpectContinueTimeout: 1 * time.Second,
  42. // Set this value so that the underlying transport round-tripper
  43. // doesn't try to auto decode the body of objects with
  44. // content-encoding set to `gzip`.
  45. //
  46. // Refer:
  47. // https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
  48. DisableCompression: true,
  49. }
  50. if secure {
  51. rootCAs, _ := x509.SystemCertPool()
  52. if rootCAs == nil {
  53. // In some systems (like Windows) system cert pool is
  54. // not supported or no certificates are present on the
  55. // system - so we create a new cert pool.
  56. rootCAs = x509.NewCertPool()
  57. }
  58. // Keep TLS config.
  59. tlsConfig := &tls.Config{
  60. RootCAs: rootCAs,
  61. // Can't use SSLv3 because of POODLE and BEAST
  62. // Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
  63. // Can't use TLSv1.1 because of RC4 cipher usage
  64. MinVersion: tls.VersionTLS12,
  65. }
  66. tr.TLSClientConfig = tlsConfig
  67. // Because we create a custom TLSClientConfig, we have to opt-in to HTTP/2.
  68. // See https://github.com/golang/go/issues/14275
  69. if err := http2.ConfigureTransport(tr); err != nil {
  70. return nil, err
  71. }
  72. }
  73. return tr, nil
  74. }