12 Commits

Author SHA1 Message Date
965d1e6d16 fix: FromResponse not return error. 2018-12-23 00:15:24 +08:00
a0d8830242 golang open files 是keepalives的问题. 2018-12-18 03:16:17 +08:00
823e5e0bcf add default keepalvies false 2018-12-18 02:17:04 +08:00
30876ae2bf test fd 2018-12-18 01:46:41 +08:00
75fc71ccb4 丑陋的解压 2018-12-18 01:42:38 +08:00
e165b6a0a0 test open files 2018-12-18 01:36:18 +08:00
2307e6eb10 req is nil with Client.Do 2018-12-18 01:09:29 +08:00
28d4c9a297 fix code 2018-12-18 01:06:21 +08:00
18de9c2c1f req body close 2018-12-18 01:04:32 +08:00
1f673f991f delete body copy 2018-12-18 00:57:31 +08:00
2e315b1b52 add request close 2018-12-18 00:50:09 +08:00
16859fc78e add close Reader 2018-12-18 00:21:27 +08:00
4 changed files with 23 additions and 22 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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/",

View File

@@ -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)
} }