Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 965d1e6d16 | |||
| a0d8830242 | |||
| 823e5e0bcf | |||
| 30876ae2bf | |||
| 75fc71ccb4 | |||
| e165b6a0a0 | |||
| 2307e6eb10 | |||
| 28d4c9a297 | |||
| 18de9c2c1f | |||
| 1f673f991f | |||
| 2e315b1b52 | |||
| 16859fc78e |
22
response.go
22
response.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"compress/zlib"
|
"compress/zlib"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@@ -21,34 +20,29 @@ func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
|||||||
// 复制response 返回内容 并且测试是否有解压的需求
|
// 复制response 返回内容 并且测试是否有解压的需求
|
||||||
srcbuf, err := ioutil.ReadAll(resp.Body)
|
srcbuf, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
cbuf := bytes.NewBuffer([]byte{})
|
content := ""
|
||||||
_, err = io.Copy(cbuf, bytes.NewReader(srcbuf))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.Body = ioutil.NopCloser(cbuf)
|
|
||||||
|
|
||||||
content := string(srcbuf)
|
|
||||||
srcReader := bytes.NewReader(srcbuf)
|
srcReader := bytes.NewReader(srcbuf)
|
||||||
|
|
||||||
if r, err := gzip.NewReader(srcReader); err == nil {
|
if r, err := gzip.NewReader(srcReader); err == nil {
|
||||||
|
defer r.Close()
|
||||||
buf, err := ioutil.ReadAll(r)
|
buf, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
content = string(buf)
|
content = string(buf)
|
||||||
} else if r, err := zlib.NewReader(srcReader); err == nil {
|
} else if r, err := zlib.NewReader(srcReader); err == nil {
|
||||||
|
defer r.Close()
|
||||||
buf, err := ioutil.ReadAll(r)
|
buf, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
content = string(buf)
|
content = string(buf)
|
||||||
|
} else {
|
||||||
|
content = string(srcbuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Response{DContent: content, GResponse: resp}, nil
|
return &Response{DContent: content, GResponse: resp}, nil
|
||||||
|
|||||||
20
session.go
20
session.go
@@ -154,6 +154,9 @@ const (
|
|||||||
// CDialTimeout 一个Connect过程的Timeout
|
// CDialTimeout 一个Connect过程的Timeout
|
||||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||||
|
|
||||||
|
// CKeepAlives 默认不KeepAlives, 容易被一直KeepAlives 没关闭链接
|
||||||
|
CKeepAlives
|
||||||
|
|
||||||
// CProxy 代理链接
|
// CProxy 代理链接
|
||||||
CProxy // http, https, socks5
|
CProxy // http, https, socks5
|
||||||
|
|
||||||
@@ -173,7 +176,9 @@ const (
|
|||||||
// NewSession 创建Session
|
// NewSession 创建Session
|
||||||
func NewSession() *Session {
|
func NewSession() *Session {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
transport := &http.Transport{DisableCompression: true}
|
transport := &http.Transport{DisableCompression: true, DisableKeepAlives: true}
|
||||||
|
|
||||||
|
EnsureTransporterFinalized(transport)
|
||||||
|
|
||||||
client.Transport = transport
|
client.Transport = transport
|
||||||
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
@@ -206,6 +211,8 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
}
|
}
|
||||||
case CDialTimeout:
|
case CDialTimeout:
|
||||||
// 没时间实现这些小细节
|
// 没时间实现这些小细节
|
||||||
|
case CKeepAlives:
|
||||||
|
ses.transport.DisableKeepAlives = !values.(bool)
|
||||||
case CCookiejar:
|
case CCookiejar:
|
||||||
v := values.(bool)
|
v := values.(bool)
|
||||||
if v {
|
if v {
|
||||||
@@ -356,16 +363,15 @@ func (ses *Session) Delete(url string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseIdleConnections closes the idle connections that a session client may make use of
|
// // CloseIdleConnections closes the idle connections that a session client may make use of
|
||||||
// 从levigross/grequests 借鉴
|
// // 从levigross/grequests 借鉴
|
||||||
func (ses *Session) CloseIdleConnections() {
|
// func (ses *Session) CloseIdleConnections() {
|
||||||
ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
// ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
||||||
}
|
// }
|
||||||
|
|
||||||
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
|
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
|
||||||
// the runtime will close the idle connections (so that they won't leak)
|
// the runtime will close the idle connections (so that they won't leak)
|
||||||
// this function was adopted from Hashicorp's go-cleanhttp package
|
// this function was adopted from Hashicorp's go-cleanhttp package
|
||||||
// 暂时不用, 标记到以后是否起作用
|
|
||||||
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
||||||
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
||||||
(*transportInt).CloseIdleConnections()
|
(*transportInt).CloseIdleConnections()
|
||||||
|
|||||||
@@ -319,7 +319,7 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
func TestSession_SetConfigInsecure(t *testing.T) {
|
func TestSession_SetConfigInsecure(t *testing.T) {
|
||||||
|
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
ses.SetConfig(CInsecure, false)
|
ses.SetConfig(CInsecure, true)
|
||||||
|
|
||||||
for _, badSSL := range []string{
|
for _, badSSL := range []string{
|
||||||
"https://self-signed.badssl.com/",
|
"https://self-signed.badssl.com/",
|
||||||
|
|||||||
@@ -311,5 +311,6 @@ func (wf *Workflow) Execute() (*Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return FromHTTPResponse(resp)
|
return FromHTTPResponse(resp)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user