1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 08:40:57 +00:00

httplib support gzip

This commit is contained in:
astaxie 2015-04-08 21:45:00 +08:00
parent 23457ed2a0
commit d90ce15707

View File

@ -32,6 +32,7 @@ package httplib
import ( import (
"bytes" "bytes"
"compress/gzip"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
@ -50,7 +51,7 @@ import (
"time" "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 defaultCookieJar http.CookieJar
var settingMutex sync.Mutex var settingMutex sync.Mutex
@ -122,6 +123,7 @@ type BeegoHttpSettings struct {
Proxy func(*http.Request) (*url.URL, error) Proxy func(*http.Request) (*url.URL, error)
Transport http.RoundTripper Transport http.RoundTripper
EnableCookie bool EnableCookie bool
Gzip bool
} }
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request. // 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 return nil, nil
} }
defer resp.Body.Close() 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 { if err != nil {
return nil, err return nil, err
} }