From efd285a6c429c2b792f8c1d3fb80f16ec8e49267 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 10 Dec 2013 22:01:50 +0800 Subject: [PATCH] update httplib support https --- httplib/README.md | 10 +++++++++- httplib/httplib.go | 20 ++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/httplib/README.md b/httplib/README.md index e5d40612..e7945714 100644 --- a/httplib/README.md +++ b/httplib/README.md @@ -43,4 +43,12 @@ set post timeout: ## debug if you want to debug the request info, set the debug on - httplib.Get("").Debug(true) \ No newline at end of file + httplib.Get("").Debug(true) + +## support HTTPS client +if request url is https. You can set the client support tsl: + + httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) + +more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config + \ No newline at end of file diff --git a/httplib/httplib.go b/httplib/httplib.go index c28bb859..fbe571e6 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -2,6 +2,7 @@ package httplib import ( "bytes" + "crypto/tls" "encoding/json" "encoding/xml" "io" @@ -22,7 +23,7 @@ func Get(url string) *BeegoHttpRequest { req.Method = "GET" req.Header = http.Header{} req.Header.Set("User-Agent", defaultUserAgent) - return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} + return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} } func Post(url string) *BeegoHttpRequest { @@ -30,7 +31,7 @@ func Post(url string) *BeegoHttpRequest { req.Method = "POST" req.Header = http.Header{} req.Header.Set("User-Agent", defaultUserAgent) - return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} + return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} } func Put(url string) *BeegoHttpRequest { @@ -38,7 +39,7 @@ func Put(url string) *BeegoHttpRequest { req.Method = "PUT" req.Header = http.Header{} req.Header.Set("User-Agent", defaultUserAgent) - return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} + return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} } func Delete(url string) *BeegoHttpRequest { @@ -46,7 +47,7 @@ func Delete(url string) *BeegoHttpRequest { req.Method = "DELETE" req.Header = http.Header{} req.Header.Set("User-Agent", defaultUserAgent) - return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} + return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} } func Head(url string) *BeegoHttpRequest { @@ -54,7 +55,7 @@ func Head(url string) *BeegoHttpRequest { req.Method = "HEAD" req.Header = http.Header{} req.Header.Set("User-Agent", defaultUserAgent) - return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second} + return &BeegoHttpRequest{url, &req, map[string]string{}, false, 60 * time.Second, 60 * time.Second, nil} } type BeegoHttpRequest struct { @@ -64,6 +65,7 @@ type BeegoHttpRequest struct { showdebug bool connectTimeout time.Duration readWriteTimeout time.Duration + tlsClientConfig *tls.Config } func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { @@ -77,6 +79,11 @@ func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Dura return b } +func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest { + b.tlsClientConfig = config + return b +} + func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { b.req.Header.Set(key, value) return b @@ -146,7 +153,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { client := &http.Client{ Transport: &http.Transport{ - Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), + TLSClientConfig: b.tlsClientConfig, + Dial: TimeoutDialer(b.connectTimeout, b.readWriteTimeout), }, } resp, err := client.Do(b.req)