...
1 package http
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "net/http"
8 "strings"
9 )
10
11 type header struct {
12 Name string
13 Value string
14 }
15 type HTTPResponse struct {
16 httpCode int
17 ht *HTTP
18 xbody []byte
19 body_retrieved bool
20 err error
21 finalurl string
22 header map[string]string
23 allheaders []*header
24 received_cookies []*http.Cookie
25 resp *http.Response
26 }
27
28
29 func (h *HTTPResponse) IsSuccess() bool {
30 if h.Error() != nil {
31 return false
32 }
33 if h.HTTPCode() < 200 {
34 return false
35 }
36 if h.HTTPCode() >= 299 {
37 return false
38 }
39 return true
40 }
41
42 func (h *HTTPResponse) HTTPCode() int {
43 return h.httpCode
44 }
45 func (h *HTTPResponse) Cookies() []*http.Cookie {
46 return h.received_cookies
47 }
48 func (h *HTTPResponse) AllHeaders() []*header {
49 return h.allheaders
50 }
51 func (h *HTTPResponse) Body() []byte {
52 if h.body_retrieved {
53 return h.xbody
54 }
55 if h.resp == nil {
56 return nil
57 }
58 b := &bytes.Buffer{}
59 _, err := io.Copy(b, h.BodyReader())
60 if err != nil {
61 fmt.Printf("[go-easyops] http - bodyreader for \"%s\" failed silently (%s)\n", h.finalurl, err)
62 return nil
63 }
64 h.setBody(b.Bytes())
65 return h.xbody
66 }
67 func (h *HTTPResponse) setBody(b []byte) {
68 h.xbody = b
69 h.body_retrieved = true
70 }
71 func (h *HTTPResponse) Error() error {
72 return h.err
73 }
74 func (h *HTTPResponse) Header(name string) string {
75 name = strings.ToLower(name)
76 return h.header[name]
77 }
78
79
80 func (h *HTTPResponse) FinalURL() string {
81 return h.finalurl
82 }
83 func (h *HTTPResponse) BodyReader() io.Reader {
84 return h.resp.Body
85 }
86
View as plain text