1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-17 17:50:54 +00:00

better code and fixed

This commit is contained in:
JessonChan 2015-04-26 02:04:34 +08:00
parent 4498a02c15
commit e1d20aea5d

View File

@ -85,7 +85,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,
paras: 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 +164,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
} }
@ -409,12 +416,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, 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 +438,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 +451,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 +505,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
} }
} }