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