Commit 8e618192 authored by JessonChan's avatar JessonChan Committed by astaxie

better code and fixed

parent 3415a5b0
...@@ -85,7 +85,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { ...@@ -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 { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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 {
f, err := os.Create(filename) resp, err := b.getResponse()
if err != nil { if resp == nil || resp.Body == nil {
return err return err
} }
defer f.Close() defer resp.Body.Close()
resp, err := b.getResponse() f, err := os.Create(filename)
if err != nil { if err != nil {
return err return err
} }
if resp.Body == nil { defer f.Close()
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 ...@@ -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
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment