Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetAllHeaders method to golang HTTP filter #33821

Merged
merged 16 commits into from May 16, 2024
3 changes: 3 additions & 0 deletions contrib/golang/common/go/api/type.go
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
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
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