-
Notifications
You must be signed in to change notification settings - Fork 16
/
handle_request.go
115 lines (98 loc) · 3.75 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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package daemon
import (
"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"
"github.com/pb33f/wiretap/shared"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
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)
// 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)
break
}
}
}
// check if this is a root request
if fp == ws.config.StaticDir {
fp = filepath.Join(ws.config.StaticDir, "index.html")
}
localStat, _ := os.Stat(fp)
if localStat != nil {
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)
newReq := cloneRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
Port: config.RedirectPort,
DropHeaders: config.Headers.DropHeaders,
})
apiRequest := cloneRequest(CloneRequest{
Request: request.HttpRequest,
Protocol: config.RedirectProtocol,
Host: config.RedirectHost,
Port: config.RedirectPort,
DropHeaders: config.Headers.DropHeaders,
})
// 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)", request.HttpRequest.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)
}