1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 15:50:49 +00:00

Merge pull request #1131 from JessonChan/develop

httplib more fixed
This commit is contained in:
astaxie 2015-05-07 22:59:41 +08:00
commit b29700c3c3

View File

@ -36,6 +36,7 @@ import (
"crypto/tls"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
@ -47,32 +48,20 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"
)
var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false, true}
var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true}
var defaultCookieJar http.CookieJar
var settingMutex sync.Mutex
// createDefaultCookie creates a global cookiejar to store cookies.
func createDefaultCookie() {
settingMutex.Lock()
defer settingMutex.Unlock()
defaultCookieJar, _ = cookiejar.New(nil)
}
// Overwrite default settings
func SetDefaultSetting(setting BeegoHttpSettings) {
settingMutex.Lock()
defer settingMutex.Unlock()
defaultSetting = setting
if defaultSetting.ConnectTimeout == 0 {
defaultSetting.ConnectTimeout = 60 * time.Second
}
if defaultSetting.ReadWriteTimeout == 0 {
defaultSetting.ReadWriteTimeout = 60 * time.Second
}
}
// return *BeegoHttpRequest with specific method
@ -85,7 +74,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest {
ProtoMajor: 1,
ProtoMinor: 1,
}
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil, nil}
return &BeegoHttpRequest{
url: url,
req: &req,
params: map[string]string{},
files: map[string]string{},
setting: defaultSetting,
resp: &resp,
}
}
// Get returns *BeegoHttpRequest with GET method.
@ -157,14 +153,14 @@ func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest {
}
// SetUserAgent sets User-Agent header field
func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest {
b.setting.UserAgent = useragent
func (b *BeegoHttpRequest) SetUserAgent(userAgent string) *BeegoHttpRequest {
b.setting.UserAgent = userAgent
return b
}
// Debug sets show debug or not when executing request.
func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest {
b.setting.ShowDebug = isdebug
func (b *BeegoHttpRequest) Debug(isDebug bool) *BeegoHttpRequest {
b.setting.ShowDebug = isDebug
return b
}
@ -283,12 +279,15 @@ func (b *BeegoHttpRequest) JsonBody(obj interface{}) (*BeegoHttpRequest, error)
}
func (b *BeegoHttpRequest) buildUrl(paramBody string) {
if paramBody == "" {
return
}
// build GET url with query string
if b.req.Method == "GET" && len(paramBody) > 0 {
if strings.Index(b.url, "?") != -1 {
b.url += "&" + paramBody
} else {
if b.req.Method == "GET" {
if strings.Index(b.url, "?") == -1 {
b.url = b.url + "?" + paramBody
} else {
b.url += "&" + paramBody
}
return
}
@ -341,18 +340,14 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}
var paramBody string
if len(b.params) > 0 {
var buf bytes.Buffer
for k, v := range b.params {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
paramBody += fmt.Sprintf("&%s=%v", url.QueryEscape(k), url.QueryEscape(v))
}
paramBody = buf.String()
paramBody = paramBody[0 : len(paramBody)-1]
paramBody = paramBody[1:]
}
b.buildUrl(paramBody)
url, err := url.Parse(b.url)
if err != nil {
return nil, err
@ -362,6 +357,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
trans := b.setting.Transport
if b.setting.ConnectTimeout == 0 {
b.setting.ConnectTimeout = 60 * time.Second
}
if b.setting.ReadWriteTimeout == 0 {
b.setting.ReadWriteTimeout = 60 * time.Second
}
if trans == nil {
// create default transport
trans = &http.Transport{
@ -404,17 +406,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if b.setting.ShowDebug {
dump, err := httputil.DumpRequest(b.req, true)
if err != nil {
println(err.Error())
log.Println(err.Error())
}
b.dump = dump
}
resp, err := client.Do(b.req)
if err != nil {
return nil, err
}
b.resp = resp
return resp, nil
b.resp, err = client.Do(b.req)
return b.resp, err
}
// String returns the body string in response.
@ -435,12 +433,9 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
return b.body, nil
}
resp, err := b.getResponse()
if err != nil {
if resp == nil || resp.Body == nil {
return nil, err
}
if resp.Body == nil {
return nil, nil
}
defer resp.Body.Close()
if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(resp.Body)
@ -451,29 +446,24 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
} else {
b.body, err = ioutil.ReadAll(resp.Body)
}
if err != nil {
return nil, err
}
return b.body, nil
return b.body, err
}
// ToFile saves the body data in response to one file.
// it calls Response inner.
func (b *BeegoHttpRequest) ToFile(filename string) error {
resp, err := b.getResponse()
if resp == nil || resp.Body == nil {
return err
}
defer resp.Body.Close()
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
resp, err := b.getResponse()
if err != nil {
return err
}
if resp.Body == nil {
return nil
}
defer resp.Body.Close()
_, err = io.Copy(f, resp.Body)
return err
}
@ -510,7 +500,7 @@ func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, ad
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
err = conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, err
}
}