github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/requester/http_client.go (about)

     1  package requester
     2  
     3  import (
     4  	"crypto/tls"
     5  	"net/http"
     6  	"net/http/cookiejar"
     7  	"time"
     8  )
     9  
    10  // HTTPClient http client
    11  type HTTPClient struct {
    12  	http.Client
    13  	transport *http.Transport
    14  	https     bool
    15  	UserAgent string
    16  }
    17  
    18  // NewHTTPClient 返回 HTTPClient 的指针,
    19  // 预设了一些配置
    20  func NewHTTPClient() *HTTPClient {
    21  	h := &HTTPClient{
    22  		Client: http.Client{
    23  			Timeout: 30 * time.Second,
    24  		},
    25  		UserAgent: UserAgent,
    26  	}
    27  	h.Client.Jar, _ = cookiejar.New(nil)
    28  	return h
    29  }
    30  
    31  func (h *HTTPClient) lazyInit() {
    32  	if h.transport == nil {
    33  		h.transport = &http.Transport{
    34  			Proxy:       proxyFunc,
    35  			DialContext: dialContext,
    36  			Dial:        dial,
    37  			// DialTLS:     h.dialTLSFunc(),
    38  			TLSClientConfig: &tls.Config{
    39  				InsecureSkipVerify: !h.https,
    40  			},
    41  			TLSHandshakeTimeout:   10 * time.Second,
    42  			DisableKeepAlives:     false,
    43  			DisableCompression:    false, // gzip
    44  			MaxIdleConns:          100,
    45  			IdleConnTimeout:       90 * time.Second,
    46  			ResponseHeaderTimeout: 10 * time.Second,
    47  			ExpectContinueTimeout: 10 * time.Second,
    48  		}
    49  		h.Client.Transport = h.transport
    50  	}
    51  }
    52  
    53  // SetUserAgent 设置 UserAgent 浏览器标识
    54  func (h *HTTPClient) SetUserAgent(ua string) {
    55  	h.UserAgent = ua
    56  }
    57  
    58  // SetProxy 设置代理
    59  func (h *HTTPClient) SetProxy(proxyAddr string) {
    60  	h.lazyInit()
    61  	u, err := checkProxyAddr(proxyAddr)
    62  	if err != nil {
    63  		h.transport.Proxy = http.ProxyFromEnvironment
    64  		return
    65  	}
    66  
    67  	h.transport.Proxy = http.ProxyURL(u)
    68  }
    69  
    70  // SetCookiejar 设置 cookie
    71  func (h *HTTPClient) SetCookiejar(jar http.CookieJar) {
    72  	h.Client.Jar = jar
    73  }
    74  
    75  // ResetCookiejar 清空 cookie
    76  func (h *HTTPClient) ResetCookiejar() {
    77  	h.Jar, _ = cookiejar.New(nil)
    78  }
    79  
    80  // SetHTTPSecure 是否启用 https 安全检查, 默认不检查
    81  func (h *HTTPClient) SetHTTPSecure(b bool) {
    82  	h.https = b
    83  	h.lazyInit()
    84  	if b {
    85  		h.transport.TLSClientConfig = nil
    86  	} else {
    87  		h.transport.TLSClientConfig = &tls.Config{
    88  			InsecureSkipVerify: !b,
    89  		}
    90  	}
    91  }
    92  
    93  // SetKeepAlive 设置 Keep-Alive
    94  func (h *HTTPClient) SetKeepAlive(b bool) {
    95  	h.lazyInit()
    96  	h.transport.DisableKeepAlives = !b
    97  }
    98  
    99  // SetGzip 是否启用Gzip
   100  func (h *HTTPClient) SetGzip(b bool) {
   101  	h.lazyInit()
   102  	h.transport.DisableCompression = !b
   103  }
   104  
   105  // SetResponseHeaderTimeout 设置目标服务器响应超时时间
   106  func (h *HTTPClient) SetResponseHeaderTimeout(t time.Duration) {
   107  	h.lazyInit()
   108  	h.transport.ResponseHeaderTimeout = t
   109  }
   110  
   111  // SetTLSHandshakeTimeout 设置tls握手超时时间
   112  func (h *HTTPClient) SetTLSHandshakeTimeout(t time.Duration) {
   113  	h.lazyInit()
   114  	h.transport.TLSHandshakeTimeout = t
   115  }
   116  
   117  // SetTimeout 设置 http 请求超时时间, 默认30s
   118  func (h *HTTPClient) SetTimeout(t time.Duration) {
   119  	h.Client.Timeout = t
   120  }