-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
245 lines (202 loc) · 5.54 KB
/
http.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package zwibserve
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"runtime/debug"
"strings"
)
// HTTPRequestArgs is the parameters for the request
type HTTPRequestArgs struct {
// GET, POST, ETC
Method string
URI string
Headers map[string]string
Data map[string]interface{}
Body []byte
// If JSON is to be sent in the post request
JSON interface{}
// If basic authentication is used
Username string
Password string
}
// HTTPRequestResult contains the meta information about the reply.
type HTTPRequestResult struct {
Err error
StatusCode int
Header http.Header
RawReply []byte
}
// MakeHTTPRequest makes an HTTP request
func MakeHTTPRequest(args HTTPRequestArgs, reply interface{}) HTTPRequestResult {
var body io.Reader
var length int
var result HTTPRequestResult
uri := args.URI
if args.JSON != nil {
b, err := json.Marshal(args.JSON)
if err != nil {
result.Err = err
return result
}
length = len(b)
body = bytes.NewReader(b)
}
if args.Data != nil {
data := url.Values{}
if args.Data != nil {
for name, value := range args.Data {
if values, ok := value.([]string); ok {
for _, item := range values {
data.Add(name, item)
}
} else {
data.Set(name, fmt.Sprintf("%v", value))
}
}
if args.Method != "POST" {
uri += "?" + data.Encode()
} else {
b := data.Encode()
length = len(b)
body = bytes.NewReader([]byte(b))
}
}
}
log.Printf("uri %s", uri)
if args.Body != nil {
body = bytes.NewReader(args.Body)
length = len(args.Body)
}
req, _ := http.NewRequest(args.Method, uri, body)
client := &http.Client{}
// nil map is like empty on reading
for key, value := range args.Headers {
req.Header.Add(key, value)
}
if args.Username != "" {
userpass := args.Username + ":" + args.Password
sEnc := base64.StdEncoding.EncodeToString([]byte(userpass))
req.Header.Add("Authorization", "Basic "+sEnc)
}
if args.JSON != nil {
req.Header.Set("Content-Type", "application/json")
}
if args.Method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", fmt.Sprintf("%d", length))
}
//log.Printf("request is %v", req)
resp, err := client.Do(req)
if err != nil {
result.Err = err
return result
}
defer resp.Body.Close()
result.StatusCode = resp.StatusCode
result.Header = resp.Header
responseData, err := ioutil.ReadAll(resp.Body)
//log.Printf("Got response %s", string(responseData))
if err != nil {
result.Err = err
return result
}
if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
err = json.Unmarshal(responseData, reply)
if err != nil {
result.Err = err
return result
}
} else {
switch v := reply.(type) {
case *string:
*v = string(responseData)
case *[]byte:
*v = responseData
default:
log.Printf("Content-type: %s", resp.Header.Get("Content-Type"))
log.Printf("Response: %v", string(responseData))
log.Panic(fmt.Errorf("invalid reply type; need string/byte"))
}
}
result.RawReply = responseData
return result
}
// MakeAsyncHTTPRequest makes an asyncronous http request, returning it result in a channel.
func MakeAsyncHTTPRequest(args HTTPRequestArgs, reply interface{}) chan HTTPRequestResult {
ch := make(chan HTTPRequestResult)
go func() {
ch <- MakeHTTPRequest(args, reply)
}()
return ch
}
// RecoverErrors will wrap an HTTP handler. When a panic occurs, it will
// print the stack to the log. Secondly, it will return the internal server error
// with the status header equal to the error string.
func RecoverErrors(fn http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if thing := recover(); thing != nil {
code := http.StatusInternalServerError
status := "Internal server error"
switch v := thing.(type) {
case HTTPError:
code = v.StatusCode()
status = v.Error()
default:
status = fmt.Sprintf("%v", thing)
log.Printf("%v", thing)
log.Println(string(debug.Stack()))
}
w.Header().Set("Status", status)
w.WriteHeader(code)
}
}()
fn.ServeHTTP(w, r)
}
}
type HTTPError interface {
Error() string
StatusCode() int
}
type httpError struct {
status int
message string
}
func (h httpError) Error() string {
return h.message
}
func (h httpError) StatusCode() int {
return h.status
}
// HTTPPanic will cause a panic with an HTTPError. This is expected to be
// recovered at a higher level, for example using the RecoverErrors
// middleware so the error is returned to the client.
func HTTPPanic(status int, fmtStr string, args ...interface{}) HTTPError {
panic(httpError{status, fmt.Sprintf(fmtStr, args...)})
}
// CORS wraps an HTTP request handler, adding appropriate cors headers.
// If CORS is desired, you can wrap the handler with it.
func CORS(fn http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
w.Header().Set("Access-Control-Expose-Headers", "Status, Content-Type, Content-Length")
}
// Stop here if its Preflighted OPTIONS request
if r.Method == "OPTIONS" {
return
}
fn.ServeHTTP(w, r)
}
}