-
Notifications
You must be signed in to change notification settings - Fork 18
/
handle_request.go
192 lines (162 loc) · 5.64 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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package daemon
import (
_ "embed"
"fmt"
"github.com/pb33f/libopenapi-validator/parameters"
"github.com/pb33f/libopenapi-validator/requests"
"github.com/pb33f/libopenapi-validator/responses"
"github.com/pb33f/ranch/model"
"github.com/pb33f/ranch/plank/utils"
configModel "github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/shared"
"io"
"net/http"
"os"
"path/filepath"
"text/template"
"time"
)
//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)
// serve it.
http.ServeFile(request.HttpResponseWriter, request.HttpRequest, tmpFile.Name())
return
}
if !localStat.IsDir() {
http.ServeFile(request.HttpResponseWriter, request.HttpRequest, fp)
return
}
}
}
var returnedResponse *http.Response
var returnedError error
// create validators.
requestValidator := requests.NewRequestBodyValidator(ws.docModel)
paramValidator := parameters.NewParameterValidator(ws.docModel)
responseValidator := responses.NewResponseBodyValidator(ws.docModel)
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 := cloneRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
Port: config.RedirectPort,
DropHeaders: dropHeaders,
InjectHeaders: injectHeaders,
Auth: auth,
Variables: config.CompiledVariables,
})
apiRequest := cloneRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
Port: config.RedirectPort,
DropHeaders: dropHeaders,
InjectHeaders: injectHeaders,
Auth: auth,
Variables: config.CompiledVariables,
})
// validate the request
go ws.validateRequest(request, newReq, requestValidator, paramValidator, responseValidator)
// call the API being requested.
returnedResponse, returnedError = ws.callAPI(apiRequest)
if returnedResponse == nil && returnedError != nil {
utils.Log.Infof("[wiretap] request %s: Failed (%d)", apiRequest.URL.String(), 500)
go ws.broadcastResponseError(request, cloneResponse(returnedResponse), returnedError)
request.HttpResponseWriter.WriteHeader(500)
wtError := shared.GenerateError("Unable to call API", 500, returnedError.Error(), "")
_, _ = request.HttpResponseWriter.Write(shared.MarshalError(wtError))
return
} else {
// validate response
go ws.validateResponse(request, responseValidator, cloneResponse(returnedResponse))
}
// send response back to client.
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.
headers["Access-Control-Allow-Headers"] = "*"
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "OPTIONS,POST,GET,DELETE,PATCH,PUT"
// write headers
for k, v := range headers {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(v))
}
utils.Log.Infof("[wiretap] request %s: completed (%d)", request.HttpRequest.URL.String(), returnedResponse.StatusCode)
// write the status code and body
request.HttpResponseWriter.WriteHeader(returnedResponse.StatusCode)
_, _ = request.HttpResponseWriter.Write(body)
}