Skip to content

Commit

Permalink
Fixed multi header handling by adapting model to actually be a slice
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobm-splunk authored and daveshanley committed Mar 11, 2024
1 parent e59e9ce commit 0c00400
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions daemon/build_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ func ReconstructURL(r *http.Request, protocol, host, basepath string, port strin
return url
}

func ExtractHeaders(resp *http.Response) map[string]any {
headers := make(map[string]any)
func ExtractHeaders(resp *http.Response) map[string][]string {
headers := make(map[string][]string)
for k, v := range resp.Header {
headers[k] = v[0]
headers[k] = v
}
return headers
}
Expand Down
10 changes: 6 additions & 4 deletions daemon/handle_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (ws *WiretapService) handleMockRequest(
time.Sleep(5 * time.Millisecond)

// wiretap needs to work from anywhere, so allow everything.
headers := make(map[string]any)
headers := make(map[string][]string)
setCORSHeaders(headers)
headers["Content-Type"] = "application/json"
headers["Content-Type"] = []string{"application/json"}

buff := bytes.NewBuffer(mock)

Expand All @@ -52,8 +52,10 @@ func (ws *WiretapService) handleMockRequest(
resp.Header = header
// write headers
for k, v := range headers {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(v))
header.Add(k, fmt.Sprint(v))
for j := range v {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(j))
header.Add(k, fmt.Sprint(v))
}
}

// if there was an error building the mock, return a 404
Expand Down
12 changes: 7 additions & 5 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {

// write headers
for k, v := range headers {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(v))
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)

Expand All @@ -236,8 +238,8 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
_, _ = request.HttpResponseWriter.Write(body)
}

func setCORSHeaders(headers map[string]any) {
headers["Access-Control-Allow-Headers"] = "*"
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "OPTIONS,POST,GET,DELETE,PATCH,PUT"
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"}
}

0 comments on commit 0c00400

Please # to comment.