8e618192 by JessonChan Committed by astaxie

better code and fixed

1 parent 3415a5b0
...@@ -85,7 +85,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { ...@@ -85,7 +85,14 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest {
85 ProtoMajor: 1, 85 ProtoMajor: 1,
86 ProtoMinor: 1, 86 ProtoMinor: 1,
87 } 87 }
88 return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil, nil} 88 return &BeegoHttpRequest{
89 url: url,
90 req: &req,
91 paras: map[string]string{},
92 files: map[string]string{},
93 setting: defaultSetting,
94 resp: &resp,
95 }
89 } 96 }
90 97
91 // Get returns *BeegoHttpRequest with GET method. 98 // 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 {
157 } 164 }
158 165
159 // SetUserAgent sets User-Agent header field 166 // SetUserAgent sets User-Agent header field
160 func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest { 167 func (b *BeegoHttpRequest) SetUserAgent(userAgent string) *BeegoHttpRequest {
161 b.setting.UserAgent = useragent 168 b.setting.UserAgent = userAgent
162 return b 169 return b
163 } 170 }
164 171
165 // Debug sets show debug or not when executing request. 172 // Debug sets show debug or not when executing request.
166 func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { 173 func (b *BeegoHttpRequest) Debug(isDebug bool) *BeegoHttpRequest {
167 b.setting.ShowDebug = isdebug 174 b.setting.ShowDebug = isDebug
168 return b 175 return b
169 } 176 }
170 177
...@@ -409,12 +416,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { ...@@ -409,12 +416,8 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
409 b.dump = dump 416 b.dump = dump
410 } 417 }
411 418
412 resp, err := client.Do(b.req) 419 b.resp, err = client.Do(b.req)
413 if err != nil { 420 return b.resp, err
414 return nil, err
415 }
416 b.resp = resp
417 return resp, nil
418 } 421 }
419 422
420 // String returns the body string in response. 423 // 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) {
435 return b.body, nil 438 return b.body, nil
436 } 439 }
437 resp, err := b.getResponse() 440 resp, err := b.getResponse()
438 if err != nil { 441 if resp == nil || resp.Body == nil {
439 return nil, err 442 return nil, err
440 } 443 }
441 if resp.Body == nil {
442 return nil, nil
443 }
444 defer resp.Body.Close() 444 defer resp.Body.Close()
445 if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" { 445 if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
446 reader, err := gzip.NewReader(resp.Body) 446 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) {
451 } else { 451 } else {
452 b.body, err = ioutil.ReadAll(resp.Body) 452 b.body, err = ioutil.ReadAll(resp.Body)
453 } 453 }
454 if err != nil { 454 return b.body, err
455 return nil, err
456 }
457 return b.body, nil
458 } 455 }
459 456
460 // ToFile saves the body data in response to one file. 457 // ToFile saves the body data in response to one file.
461 // it calls Response inner. 458 // it calls Response inner.
462 func (b *BeegoHttpRequest) ToFile(filename string) error { 459 func (b *BeegoHttpRequest) ToFile(filename string) error {
463 f, err := os.Create(filename) 460 resp, err := b.getResponse()
464 if err != nil { 461 if resp == nil || resp.Body == nil {
465 return err 462 return err
466 } 463 }
467 defer f.Close() 464 defer resp.Body.Close()
468 465
469 resp, err := b.getResponse() 466 f, err := os.Create(filename)
470 if err != nil { 467 if err != nil {
471 return err 468 return err
472 } 469 }
473 if resp.Body == nil { 470 defer f.Close()
474 return nil 471
475 }
476 defer resp.Body.Close()
477 _, err = io.Copy(f, resp.Body) 472 _, err = io.Copy(f, resp.Body)
478 return err 473 return err
479 } 474 }
...@@ -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
510 if err != nil { 505 if err != nil {
511 return nil, err 506 return nil, err
512 } 507 }
513 conn.SetDeadline(time.Now().Add(rwTimeout)) 508 err = conn.SetDeadline(time.Now().Add(rwTimeout))
514 return conn, nil 509 return conn, err
515 } 510 }
516 } 511 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!