forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_requests.go
206 lines (172 loc) · 5.92 KB
/
batch_requests.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// RequestDefinition defines a batch request
type RequestDefinition struct {
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
RelativeURL string `json:"relative_url"`
}
// BatchRequestStructure defines a batch request order
type BatchRequestStructure struct {
Requests []RequestDefinition `json:"requests"`
SuppressParallelExecution bool `json:"suppress_parallel_execution"`
}
// BatchReplyUnit encodes a request suitable for replying to a batch request
type BatchReplyUnit struct {
RelativeURL string `json:"relative_url"`
Code int `json:"code"`
Headers http.Header `json:"headers"`
Body string `json:"body"`
}
// BatchRequestHandler handles batch requests on /tyk/batch for any API Definition that has the feature enabled
type BatchRequestHandler struct {
API *APISpec
}
// doAsyncRequest runs an async request and replies to a channel
func (b *BatchRequestHandler) doAsyncRequest(req *http.Request, relURL string, out chan BatchReplyUnit) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error("Webhook request failed: ", err)
return
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warning("Body read failure! ", err)
return
}
out <- BatchReplyUnit{
RelativeURL: relURL,
Code: resp.StatusCode,
Headers: resp.Header,
Body: string(content),
}
}
// doSyncRequest will make the same request but return a BatchReplyUnit
func (b *BatchRequestHandler) doSyncRequest(req *http.Request, relURL string) BatchReplyUnit {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error("Webhook request failed: ", err)
return BatchReplyUnit{}
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warning("Body read failure! ", err)
return BatchReplyUnit{}
}
return BatchReplyUnit{
RelativeURL: relURL,
Code: resp.StatusCode,
Headers: resp.Header,
Body: string(content),
}
}
func (b *BatchRequestHandler) DecodeBatchRequest(r *http.Request) (BatchRequestStructure, error) {
var batchRequest BatchRequestStructure
err := json.NewDecoder(r.Body).Decode(&batchRequest)
return batchRequest, err
}
func (b *BatchRequestHandler) ConstructRequests(batchRequest BatchRequestStructure, unsafe bool) ([]*http.Request, error) {
requestSet := []*http.Request{}
for i, requestDef := range batchRequest.Requests {
// We re-build the URL to ensure that the requested URL is actually for the API in question
// URLs need to be built absolute so they go through the rate limiting and request limiting machinery
var absURL string
if !unsafe {
absUrlHeader := "http://localhost:" + strconv.Itoa(globalConf.ListenPort)
absURL = strings.Join([]string{absUrlHeader, strings.Trim(b.API.Proxy.ListenPath, "/"), requestDef.RelativeURL}, "/")
} else {
absURL = requestDef.RelativeURL
}
request, err := http.NewRequest(requestDef.Method, absURL, strings.NewReader(requestDef.Body))
if err != nil {
log.Error("Failure generating batch request for request spec index: ", i)
return nil, err
}
// Add headers
for k, v := range requestDef.Headers {
request.Header.Set(k, v)
}
requestSet = append(requestSet, request)
}
return requestSet, nil
}
func (b *BatchRequestHandler) MakeRequests(batchRequest BatchRequestStructure, requestSet []*http.Request) []BatchReplyUnit {
replySet := []BatchReplyUnit{}
if len(batchRequest.Requests) != len(requestSet) {
log.Error("Something went wrong creating requests, they are of mismatched lengths!", len(batchRequest.Requests), len(requestSet))
}
if !batchRequest.SuppressParallelExecution {
replies := make(chan BatchReplyUnit)
for index, req := range requestSet {
go b.doAsyncRequest(req, batchRequest.Requests[index].RelativeURL, replies)
}
for range batchRequest.Requests {
replySet = append(replySet, <-replies)
}
} else {
for index, req := range requestSet {
reply := b.doSyncRequest(req, batchRequest.Requests[index].RelativeURL)
replySet = append(replySet, reply)
}
}
return replySet
}
// HandleBatchRequest is the actual http handler for a batch request on an API definition
func (b *BatchRequestHandler) HandleBatchRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
return
}
// Decode request
batchRequest, err := b.DecodeBatchRequest(r)
if err != nil {
log.Error("Could not decode batch request, decoding failed: ", err)
doJSONWrite(w, 400, apiError("Batch request malformed"))
return
}
// Construct the requests
requestSet, err := b.ConstructRequests(batchRequest, false)
if err != nil {
doJSONWrite(w, 400, apiError(fmt.Sprintf("Batch request creation failed , request structure malformed")))
return
}
// Run requests and collate responses
replySet := b.MakeRequests(batchRequest, requestSet)
// Respond
doJSONWrite(w, 200, replySet)
}
// HandleBatchRequest is the actual http handler for a batch request on an API definition
func (b *BatchRequestHandler) ManualBatchRequest(requestObject []byte) []byte {
// Decode request
var batchRequest BatchRequestStructure
if err := json.Unmarshal(requestObject, &batchRequest); err != nil {
log.Error("Could not decode batch request, decoding failed: ", err)
return nil
}
// Construct the unsafe requests
requestSet, err := b.ConstructRequests(batchRequest, true)
if err != nil {
log.Error("Batch request creation failed , request structure malformed: ", err)
return nil
}
// Run requests and collate responses
replySet := b.MakeRequests(batchRequest, requestSet)
// Encode responses
replyMessage, err := json.Marshal(&replySet)
if err != nil {
log.Error("Couldn't encode response to string! ", err)
return nil
}
return replyMessage
}