-
Notifications
You must be signed in to change notification settings - Fork 17
/
handle_request.go
245 lines (202 loc) · 7.31 KB
/
handle_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package daemon
import (
_ "embed"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"text/template"
"time"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/ranch/model"
configModel "github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/shared"
)
//go:embed templates/socket-include.html
var staticTemplate string
type staticTemplateModel struct {
OriginalContent string
WebSocketPort string
}
func (ws *WiretapService) handleHttpRequest(request *model.Request) {
// determine if this is a request for a file or not.
if ws.config.StaticDir != "" {
fp := filepath.Join(ws.config.StaticDir, request.HttpRequest.URL.Path)
isRoot := false
// check if this is a static path catch-all
if len(ws.config.StaticPathsCompiled) > 0 {
for key := range ws.config.StaticPathsCompiled {
if ws.config.StaticPathsCompiled[key].Match(request.HttpRequest.URL.Path) {
fp = filepath.Join(ws.config.StaticDir, ws.config.StaticIndex)
isRoot = true
break
}
}
}
// check if this is a root request
if fp == ws.config.StaticDir {
isRoot = true
fp = filepath.Join(ws.config.StaticDir, "index.html")
}
localStat, _ := os.Stat(fp)
if localStat != nil {
if isRoot {
// if this root, we need to modify the index to inject some JS.
tmpFile, _ := os.CreateTemp("", "index.html")
defer os.Remove(tmpFile.Name())
tmpl, _ := template.New("index").Parse(staticTemplate)
indexBytes, _ := os.ReadFile(fp)
// prep a model
m := staticTemplateModel{
OriginalContent: string(indexBytes),
WebSocketPort: ws.config.WebSocketPort,
}
// execute the new template
_ = tmpl.Execute(tmpFile, m)
ws.config.Logger.Info("[wiretap] static file request", "url", request.HttpRequest.URL.String(), "code", 200)
// serve it.
http.ServeFile(request.HttpResponseWriter, request.HttpRequest, tmpFile.Name())
return
}
if !localStat.IsDir() {
ws.config.Logger.Info("[wiretap] static file request", "url", request.HttpRequest.URL.String(), "code", 200)
http.ServeFile(request.HttpResponseWriter, request.HttpRequest, fp)
return
}
}
}
var returnedResponse *http.Response
var returnedError error
configStore, _ := ws.controlsStore.Get(shared.ConfigKey)
config := configStore.(*shared.WiretapConfiguration)
if config.Headers == nil || len(config.Headers.DropHeaders) == 0 {
config.Headers = &shared.WiretapHeaderConfig{
DropHeaders: []string{},
}
}
var dropHeaders []string
var injectHeaders map[string]string
// add global headers with injection.
if config.Headers != nil {
dropHeaders = config.Headers.DropHeaders
injectHeaders = config.Headers.InjectHeaders
}
// now add path specific headers.
matchedPaths := configModel.FindPaths(request.HttpRequest.URL.Path, config)
auth := ""
if len(matchedPaths) > 0 {
for _, path := range matchedPaths {
auth = path.Auth
if path.Headers != nil {
dropHeaders = append(dropHeaders, path.Headers.DropHeaders...)
newInjectHeaders := path.Headers.InjectHeaders
for key := range injectHeaders {
newInjectHeaders[key] = injectHeaders[key]
}
injectHeaders = newInjectHeaders
}
break
}
}
newReq := CloneExistingRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
Port: config.RedirectPort,
DropHeaders: dropHeaders,
InjectHeaders: injectHeaders,
Auth: auth,
Variables: config.CompiledVariables,
})
apiRequest := CloneExistingRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
BasePath: config.RedirectBasePath,
Port: config.RedirectPort,
DropHeaders: dropHeaders,
InjectHeaders: injectHeaders,
Auth: auth,
Variables: config.CompiledVariables,
})
var requestErrors []*errors.ValidationError
var responseErrors []*errors.ValidationError
ws.config.Logger.Info("[wiretap] handling API request", "url", request.HttpRequest.URL.String())
// check if we're going to fail hard on validation errors. (default is to skip this)
if ws.config.HardErrors && !ws.config.MockMode {
// validate the request synchronously
requestErrors = ws.ValidateRequest(request, newReq)
} else {
// validate the request asynchronously
if !ws.config.MockMode {
go ws.ValidateRequest(request, newReq)
}
}
// short-circuit if we're using mock mode, there is no API call to make.
if ws.config.MockMode {
ws.handleMockRequest(request, config, newReq)
return
}
// call the API being requested.
returnedResponse, returnedError = ws.callAPI(apiRequest)
if returnedResponse == nil && returnedError != nil {
config.Logger.Info("[wiretap] request failed", "url", apiRequest.URL.String(), "code", 500,
"error", returnedError.Error())
go ws.broadcastResponseError(request, CloneExistingResponse(returnedResponse), returnedError)
request.HttpResponseWriter.WriteHeader(500)
wtError := shared.GenerateError("Unable to call API", 500, returnedError.Error(), "", returnedResponse)
_, _ = request.HttpResponseWriter.Write(shared.MarshalError(wtError))
return
} else {
// check if we're going to fail hard on validation errors. (default is to skip this)
if ws.config.HardErrors {
// validate response
responseErrors = ws.ValidateResponse(request, CloneExistingResponse(returnedResponse))
} else {
// validate response async
go ws.ValidateResponse(request, CloneExistingResponse(returnedResponse))
}
}
// check if this path has a delay set.
delay := configModel.FindPathDelay(request.HttpRequest.URL.Path, config)
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Millisecond) // simulate a slow response, configured for path.
} else {
if config.GlobalAPIDelay > 0 {
time.Sleep(time.Duration(config.GlobalAPIDelay) * time.Millisecond) // simulate a slow response.
}
}
body, _ := io.ReadAll(returnedResponse.Body)
headers := ExtractHeaders(returnedResponse)
// wiretap needs to work from anywhere, so allow everything.
setCORSHeaders(headers)
// write headers
for k, v := range headers {
for _, j := range v {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(j))
}
}
config.Logger.Info("[wiretap] request completed", "url", request.HttpRequest.URL.String(), "code", returnedResponse.StatusCode)
// if there are validation errors, set an error code
requestCode := config.HardErrorCode
returnCode := config.HardErrorReturnCode
switch {
case config.HardErrors && len(requestErrors) > 0 && len(responseErrors) <= 0:
request.HttpResponseWriter.WriteHeader(requestCode)
case config.HardErrors && len(requestErrors) <= 0 && len(responseErrors) > 0:
request.HttpResponseWriter.WriteHeader(returnCode)
case config.HardErrors && len(requestErrors) > 0 && len(responseErrors) > 0:
request.HttpResponseWriter.WriteHeader(returnCode)
default:
request.HttpResponseWriter.WriteHeader(returnedResponse.StatusCode)
}
_, _ = request.HttpResponseWriter.Write(body)
}
func setCORSHeaders(headers map[string][]string) {
headers["Access-Control-Allow-Headers"] = []string{"*"}
headers["Access-Control-Allow-Origin"] = []string{"*"}
headers["Access-Control-Allow-Methods"] = []string{"OPTIONS,POST,GET,DELETE,PATCH,PUT"}
}