httplib support gzip
Showing
1 changed file
with
11 additions
and
1 deletions
| ... | @@ -32,6 +32,7 @@ package httplib | ... | @@ -32,6 +32,7 @@ package httplib |
| 32 | 32 | ||
| 33 | import ( | 33 | import ( |
| 34 | "bytes" | 34 | "bytes" |
| 35 | "compress/gzip" | ||
| 35 | "crypto/tls" | 36 | "crypto/tls" |
| 36 | "encoding/json" | 37 | "encoding/json" |
| 37 | "encoding/xml" | 38 | "encoding/xml" |
| ... | @@ -50,7 +51,7 @@ import ( | ... | @@ -50,7 +51,7 @@ import ( |
| 50 | "time" | 51 | "time" |
| 51 | ) | 52 | ) |
| 52 | 53 | ||
| 53 | var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} | 54 | var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false, true} |
| 54 | var defaultCookieJar http.CookieJar | 55 | var defaultCookieJar http.CookieJar |
| 55 | var settingMutex sync.Mutex | 56 | var settingMutex sync.Mutex |
| 56 | 57 | ||
| ... | @@ -122,6 +123,7 @@ type BeegoHttpSettings struct { | ... | @@ -122,6 +123,7 @@ type BeegoHttpSettings struct { |
| 122 | Proxy func(*http.Request) (*url.URL, error) | 123 | Proxy func(*http.Request) (*url.URL, error) |
| 123 | Transport http.RoundTripper | 124 | Transport http.RoundTripper |
| 124 | EnableCookie bool | 125 | EnableCookie bool |
| 126 | Gzip bool | ||
| 125 | } | 127 | } |
| 126 | 128 | ||
| 127 | // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. | 129 | // BeegoHttpRequest provides more useful methods for requesting one url than http.Request. |
| ... | @@ -434,7 +436,15 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -434,7 +436,15 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 434 | return nil, nil | 436 | return nil, nil |
| 435 | } | 437 | } |
| 436 | defer resp.Body.Close() | 438 | defer resp.Body.Close() |
| 439 | if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" { | ||
| 440 | reader, err := gzip.NewReader(resp.Body) | ||
| 441 | if err != nil { | ||
| 442 | return nil, err | ||
| 443 | } | ||
| 444 | b.body, err = ioutil.ReadAll(reader) | ||
| 445 | } else { | ||
| 437 | b.body, err = ioutil.ReadAll(resp.Body) | 446 | b.body, err = ioutil.ReadAll(resp.Body) |
| 447 | } | ||
| 438 | if err != nil { | 448 | if err != nil { |
| 439 | return nil, err | 449 | return nil, err |
| 440 | } | 450 | } | ... | ... |
-
Please register or sign in to post a comment