|
@@ -5,6 +5,7 @@ import (
|
|
|
"compress/gzip"
|
|
|
"compress/zlib"
|
|
|
"context"
|
|
|
+ "crypto/tls"
|
|
|
"fmt"
|
|
|
"golang.org/x/net/proxy"
|
|
|
"io"
|
|
@@ -88,6 +89,7 @@ type RequestPromise struct {
|
|
|
proxy func(*http.Request) (*url.URL, error)
|
|
|
dialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
client *http.Client
|
|
|
+ isSkipTls bool
|
|
|
}
|
|
|
|
|
|
// 返回一个http请求配置对象,默认带上压缩头
|
|
@@ -127,6 +129,12 @@ func NewPoolingHttpClient() *http.Client {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 设置https忽略本地证书校验
|
|
|
+func (r *RequestPromise) SetSkipTls() *RequestPromise {
|
|
|
+ r.isSkipTls = true
|
|
|
+ return r
|
|
|
+}
|
|
|
+
|
|
|
// 设置http header
|
|
|
func (r *RequestPromise) SetHeader(key string, value string) *RequestPromise {
|
|
|
if len(strings.TrimSpace(key)) == 0 {
|
|
@@ -235,14 +243,26 @@ func (r *RequestPromise) Call(httpMethod string, targetUri string, data io.Reade
|
|
|
|
|
|
func (r *RequestPromise) initClient() {
|
|
|
// http client handle
|
|
|
+ if r.timeout <= 0 {
|
|
|
+ r.timeout = DefaultHttpTimeout // default timeout
|
|
|
+ }
|
|
|
if nil == r.client { // create new http client instance
|
|
|
- r.client = &http.Client{Timeout: DefaultHttpTimeout} // default timeout
|
|
|
+ r.client = &http.Client{Timeout: r.timeout}
|
|
|
}
|
|
|
- if r.timeout != 0 {
|
|
|
+ if r.timeout > 0 {
|
|
|
r.client.Timeout = r.timeout
|
|
|
}
|
|
|
- if r.client.Timeout < 0 {
|
|
|
- r.client.Timeout = 0
|
|
|
+ if r.isSkipTls {
|
|
|
+ if nil == r.client.Transport {
|
|
|
+ r.client.Transport = &http.Transport{}
|
|
|
+ }
|
|
|
+ transport := (r.client.Transport).(*http.Transport)
|
|
|
+ transport.TLSClientConfig = &tls.Config{
|
|
|
+ InsecureSkipVerify: true,
|
|
|
+ //VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
+ // return nil
|
|
|
+ //},
|
|
|
+ }
|
|
|
}
|
|
|
if nil != r.proxy || nil != r.dialContext {
|
|
|
if nil == r.client.Transport {
|