-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
228 lines (174 loc) · 4.46 KB
/
request.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
package pubnub
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/pubnub/go/pnerr"
)
type StatusResponse struct {
Error error
Category StatusCategory
Operation OperationType
StatusCode int
TlsEnabled bool
Uuid string
AuthKey string
Origin string
OriginalResponse string
AffectedChannels []string
AffectedChannelGroups []string
}
type ResponseInfo struct {
Operation OperationType
StatusCode int
TlsEnabled bool
Origin string
Uuid string
AuthKey string
OriginalResponse *http.Response
}
func executeRequest(opts endpointOpts) ([]byte, StatusResponse, error) {
err := opts.validate()
if err != nil {
log.Println(err)
return nil,
createStatus(PNUnknownCategory, "", ResponseInfo{}, err),
err
}
url, err := buildUrl(opts)
if err != nil {
return nil,
createStatus(PNUnknownCategory, "", ResponseInfo{}, err),
err
}
log.Println("pubnub: >>>", url)
log.Println(opts.httpMethod())
var req *http.Request
if opts.httpMethod() == "POST" {
b, err := opts.buildBody()
if err != nil {
return nil,
createStatus(PNUnknownCategory, "", ResponseInfo{}, err),
err
}
body := bytes.NewReader(b)
req, err = newRequest("POST", url, body)
} else if opts.httpMethod() == "DELETE" {
req, err = newRequest("DELETE", url, nil)
} else {
req, err = newRequest("GET", url, nil)
}
if err != nil {
return nil,
createStatus(PNUnknownCategory, "", ResponseInfo{}, err),
err
}
ctx := opts.context()
if ctx != nil {
// with !go1.7 you can't assign context directly to a request,
// the request.cancel is mapped to the ctx.Done() channel instead
// go1.7 can assign context to an executed request
req = setRequestContext(req, ctx)
}
client := opts.client()
res, err := client.Do(req)
// Host lookup failed
if err != nil {
log.Println(err.Error())
e := pnerr.NewConnectionError("Failed to execute request", err)
log.Println(e.Error())
return nil,
createStatus(PNUnknownCategory, "", ResponseInfo{}, e),
e
}
val, status, err := parseResponse(res)
// Already wrapped error
if err != nil {
return nil, status, err
}
responseInfo := ResponseInfo{
StatusCode: res.StatusCode,
OriginalResponse: res,
Operation: opts.operationType(),
Origin: url.Host,
}
if url.Scheme == "https" {
responseInfo.TlsEnabled = true
}
if uuid, ok := url.Query()["uuid"]; ok {
responseInfo.Uuid = uuid[0]
}
if auth, ok := url.Query()["auth"]; ok {
responseInfo.AuthKey = auth[0]
}
status = createStatus(PNUnknownCategory, string(val), responseInfo, nil)
return val, status, nil
}
func newRequest(method string, u *url.URL, body io.Reader) (*http.Request,
error) {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
req := &http.Request{
Method: method,
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Body: rc,
Host: u.Host,
}
return req, nil
}
func parseResponse(resp *http.Response) ([]byte, StatusResponse, error) {
status := StatusResponse{}
if resp.StatusCode != 200 {
// Errors like 400, 403, 500
log.Println(resp.Body)
e := pnerr.NewServerError(resp.StatusCode, resp.Body)
log.Println(e.Error())
if resp.StatusCode == 408 {
status = createStatus(PNTimeoutCategory, "", ResponseInfo{}, e)
return nil, status, e
}
if resp.StatusCode == 400 {
status = createStatus(PNBadRequestCategory, "", ResponseInfo{}, e)
return nil, status, e
}
status = createStatus(PNUnknownCategory, "", ResponseInfo{}, e)
return nil, status, e
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
e := pnerr.NewResponseParsingError("Error reading response body", resp.Body, err)
log.Println(e)
return nil, status, e
}
log.Println("pubnub: <<<", resp.Status, string(body))
return body, status, nil
}
func createStatus(category StatusCategory, response string,
responseInfo ResponseInfo, err error) StatusResponse {
resp := StatusResponse{}
if response != "" {
resp.OriginalResponse = response
}
if err != nil {
resp.Error = err
}
resp.StatusCode = responseInfo.StatusCode
resp.TlsEnabled = responseInfo.TlsEnabled
resp.Origin = responseInfo.Origin
resp.Uuid = responseInfo.Uuid
resp.AuthKey = responseInfo.AuthKey
resp.Operation = responseInfo.Operation
resp.Category = category
resp.AffectedChannels = []string{}
resp.AffectedChannelGroups = []string{}
return resp
}