forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpread.go
103 lines (91 loc) · 3.1 KB
/
httpread.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package httpread
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/bpicode/fritzctl/logger"
)
var (
httpStatusBuzzwords = map[string]int{"500 Internal Server Error": 500}
)
// HTTPStatusCodeError represents an 4xx client or a 5xx server error.
type HTTPStatusCodeError struct {
error
}
func statusCodeError(code int, phrase string) *HTTPStatusCodeError {
return &HTTPStatusCodeError{error: fmt.Errorf("HTTP status code error (%d): remote replied with '%s'", code, phrase)}
}
// ReadFullyString reads a http response into a string.
// The response is checked for its status code and the http.Response.Body is closed.
func ReadFullyString(f func() (*http.Response, error)) (string, error) {
response, err := f()
if err != nil {
return "", err
}
defer response.Body.Close()
bytesRead, err := ioutil.ReadAll(response.Body)
body := string(bytesRead)
logger.Debug("DATA:", body)
statusCode, statusPhrase := guessStatusCode(response.StatusCode, response.Status, body)
if statusCode >= 400 {
return body, statusCodeError(statusCode, statusPhrase)
}
return body, err
}
func guessStatusCode(claimedCode int, claimedPhrase, body string) (int, string) {
if claimedCode >= 400 {
return claimedCode, claimedPhrase // This is already bad enough.
}
// There are web servers that send the wrong status code, but provide some hint in the text/html.
for k, v := range httpStatusBuzzwords {
if strings.Contains(strings.ToLower(body), strings.ToLower(k)) {
return v, k
}
}
return claimedCode, claimedPhrase
}
// DecodeError represents an error related to unmarshalling.
type DecodeError struct {
error
}
func decodeError(err error) *DecodeError {
return &DecodeError{error: fmt.Errorf("unable to parse remote response: %s", err.Error())}
}
// ReadFullyXML reads a http response into a data container using an XML decoder.
// The response is checked for its status code and the http.Response.Body is closed.
func ReadFullyXML(f func() (*http.Response, error), v interface{}) error {
return readDecode(f, func(r io.Reader, v interface{}) error {
read, _ := ioutil.ReadAll(r)
logger.Debug("DATA:", string(read))
return xml.NewDecoder(bytes.NewReader(read)).Decode(v)
}, v)
}
// ReadFullyJSON reads a http response into a data container using a json decoder.
// The response is checked for its status code and the http.Response.Body is closed.
func ReadFullyJSON(f func() (*http.Response, error), v interface{}) error {
return readDecode(f, func(r io.Reader, v interface{}) error {
read, _ := ioutil.ReadAll(r)
logger.Debug("DATA:", string(read))
return json.NewDecoder(bytes.NewReader(read)).Decode(v)
}, v)
}
func readDecode(f func() (*http.Response, error), decode func(r io.Reader, v interface{}) error, v interface{}) error {
response, err := f()
if err != nil {
return fmt.Errorf("error obtaining HTTP response from remote: %s", err.Error())
}
defer response.Body.Close()
err = decode(response.Body, v)
if response.StatusCode >= 400 {
return statusCodeError(response.StatusCode, response.Status)
}
if err != nil {
return decodeError(err)
}
return nil
}