From d90ce1570717f574f2714e0a56d5291fc42a91c3 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 8 Apr 2015 21:45:00 +0800 Subject: [PATCH] httplib support gzip --- httplib/httplib.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/httplib/httplib.go b/httplib/httplib.go index bdd2d65a..284aef7b 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -32,6 +32,7 @@ package httplib import ( "bytes" + "compress/gzip" "crypto/tls" "encoding/json" "encoding/xml" @@ -50,7 +51,7 @@ import ( "time" ) -var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} +var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false, true} var defaultCookieJar http.CookieJar var settingMutex sync.Mutex @@ -122,6 +123,7 @@ type BeegoHttpSettings struct { Proxy func(*http.Request) (*url.URL, error) Transport http.RoundTripper EnableCookie bool + Gzip bool } // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. @@ -434,7 +436,15 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { return nil, nil } defer resp.Body.Close() - b.body, err = ioutil.ReadAll(resp.Body) + if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" { + reader, err := gzip.NewReader(resp.Body) + if err != nil { + return nil, err + } + b.body, err = ioutil.ReadAll(reader) + } else { + b.body, err = ioutil.ReadAll(resp.Body) + } if err != nil { return nil, err }