Skip to content

Commit

Permalink
Add GetAllHeaders method to golang HTTP filter (#33821)
Browse files Browse the repository at this point in the history
Signed-off-by: Willem Veerman <6502426+willemveerman@users.noreply.github.com>
  • Loading branch information
willemveerman committed May 16, 2024
1 parent ab99cd3 commit 6e7370d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contrib/golang/common/go/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type HeaderMap interface {

// RangeWithCopy calls f sequentially for each key and value copied from the map.
RangeWithCopy(f func(key, value string) bool)

// GetAllHeaders returns all the headers.
GetAllHeaders() map[string][]string
}

type RequestHeaderMap interface {
Expand Down
24 changes: 24 additions & 0 deletions contrib/golang/filters/http/source/go/pkg/http/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ func (h *requestOrResponseHeaderMapImpl) RangeWithCopy(f func(key, value string)
}
}

func (h *requestOrResponseHeaderMapImpl) GetAllHeaders() map[string][]string {
h.mutex.Lock()
defer h.mutex.Unlock()
h.initHeaders()
copiedHeaders := make(map[string][]string)
for key, value := range h.headers {
copiedHeaders[key] = make([]string, len(value))
copy(copiedHeaders[key], value)
}
return copiedHeaders
}

// api.RequestHeaderMap
type requestHeaderMapImpl struct {
requestOrResponseHeaderMapImpl
Expand Down Expand Up @@ -318,6 +330,18 @@ func (h *requestOrResponseTrailerMapImpl) RangeWithCopy(f func(key, value string
}
}

func (h *requestOrResponseTrailerMapImpl) GetAllHeaders() map[string][]string {
h.mutex.Lock()
defer h.mutex.Unlock()
h.initTrailers()
copiedHeaders := make(map[string][]string)
for key, value := range h.headers {
copiedHeaders[key] = make([]string, len(value))
copy(copiedHeaders[key], value)
}
return copiedHeaders
}

// api.RequestTrailerMap
type requestTrailerMapImpl struct {
requestOrResponseTrailerMapImpl
Expand Down
27 changes: 27 additions & 0 deletions contrib/golang/filters/http/test/test_data/basic/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"time"
Expand All @@ -20,6 +21,7 @@ type filter struct {
method string
path string
host string
all_headers map[string][]string

// for bad api call testing
header api.RequestHeaderMap
Expand Down Expand Up @@ -181,6 +183,31 @@ func (f *filter) decodeHeaders(header api.RequestHeaderMap, endStream bool) api.
return true
})

test_header_key := "test-header-copy"

old_value := "old-value"

header.Set(test_header_key, old_value)

f.all_headers = make(map[string][]string)

header.RangeWithCopy(func(key, value string) bool {
f.all_headers[key] = append(f.all_headers[key], value)
return true
})

header_map := header.GetAllHeaders()

if !reflect.DeepEqual(f.all_headers, header_map) {
return f.fail("GetAllHeaders returned incorrect data, expected:\n%v\n got:\n%v", f.all_headers, header_map)
}

header.Set(test_header_key, "new-value")

if !reflect.DeepEqual(header_map[test_header_key], []string{old_value}) {
return f.fail("GetAllHeaders output changed - expected '%v', got '%v'", []string{old_value}, header_map[test_header_key])
}

origin, found := header.Get("x-test-header-0")
hdrs := header.Values("x-test-header-0")
if found {
Expand Down

0 comments on commit 6e7370d

Please sign in to comment.