Skip to content

Commit

Permalink
feat: add SetOnResponse cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
tomMoulard committed Jan 18, 2023
1 parent f0789b6 commit 85e708c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ To choose a Rule you have to fill the `Type` field with one of the following:
- 'RewriteValueRule': to rewrite header values
- 'Set' : to Set a header

Each Rule can be named with the `Name` field
Each Rule can be named with the `Name` field.

Each Rule can also be configured to change headers on the request or the
response by using the `SetOnResponse` configuration.
If `SetOnResponse` is set to `true`, the header will be changed on the response.
Otherwise, it will be changed on the request.
Its default value is `false`.

### Rename

Expand Down Expand Up @@ -177,6 +183,7 @@ Foo: X-Test
# Modified header:
Foo: Y-Test
```

### Careful

The rules will be evaluated in the order of definition
Expand Down
8 changes: 7 additions & 1 deletion pkg/handler/deleter/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ func Validate(rule types.Rule) error {
return nil
}

func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
func Handle(rw http.ResponseWriter, req *http.Request, rule types.Rule) {
if rule.SetOnResponse {
rw.Header().Del(rule.Name)

return
}

req.Header.Del(rule.Header)
}
1 change: 0 additions & 1 deletion pkg/handler/deleter/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func TestValidation(t *testing.T) {
{
name: "valid rule",
rule: types.Rule{
Name: "Delete Rule",
Header: "not-empty",
Type: types.Delete,
},
Expand Down
8 changes: 7 additions & 1 deletion pkg/handler/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Validate(rule types.Rule) error {
return nil
}

func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
func Handle(rw http.ResponseWriter, req *http.Request, rule types.Rule) {
val, ok := req.Header[rule.Header]
if !ok {
return
Expand All @@ -26,6 +26,12 @@ func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
newHeaderVal += rule.Sep + getValue(value, rule.HeaderPrefix, req)
}

if rule.SetOnResponse {
rw.Header().Set(rule.Name, newHeaderVal)

return
}

req.Header.Set(rule.Header, newHeaderVal)
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/handler/rename/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@ func Validate(rule types.Rule) error {
return nil
}

func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
func Handle(rw http.ResponseWriter, req *http.Request, rule types.Rule) {
for headerName, headerValues := range req.Header {
if matched := rule.Regexp.Match([]byte(headerName)); !matched {
continue
}

req.Header.Del(headerName)
if rule.SetOnResponse {
rw.Header().Del(headerName)
} else {
req.Header.Del(headerName)
}

for _, val := range headerValues {
req.Header.Set(rule.Value, val)
if rule.SetOnResponse {
rw.Header().Set(rule.Value, val)
} else {
req.Header.Set(rule.Value, val)
}
}
}
}
27 changes: 22 additions & 5 deletions pkg/handler/rewrite/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,34 @@ func Validate(rule types.Rule) error {
return nil
}

func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
for headerName, headerValues := range req.Header {
func Handle(rw http.ResponseWriter, req *http.Request, rule types.Rule) {
headers := req.Header
if rule.SetOnResponse {
headers = rw.Header()
}

for headerName, headerValues := range headers {
if matched := rule.Regexp.Match([]byte(headerName)); !matched {
continue
}

req.Header.Del(headerName)
if rule.SetOnResponse {
rw.Header().Del(headerName)
} else {
req.Header.Del(headerName)
}

for _, headerValue := range headerValues {
replacedHeaderValue := rule.ValueReplace
ruleValueRegexp := regexp.MustCompile(rule.Value)
captures := ruleValueRegexp.FindStringSubmatch(headerValue)

if len(captures) == 0 || captures[0] == "" {
req.Header.Add(headerName, headerValue)
if rule.SetOnResponse {
rw.Header().Set(rule.Header, replacedHeaderValue)
} else {
req.Header.Set(headerName, headerValue)
}

continue
}
Expand All @@ -49,7 +62,11 @@ func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
replacedHeaderValue = strings.ReplaceAll(replacedHeaderValue, placeholder, capture)
}

req.Header.Add(headerName, replacedHeaderValue)
if rule.SetOnResponse {
rw.Header().Set(rule.Header, replacedHeaderValue)
} else {
req.Header.Set(headerName, replacedHeaderValue)
}
}
}
}
6 changes: 6 additions & 0 deletions pkg/handler/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ func Validate(rule types.Rule) error {
}

func Handle(_ http.ResponseWriter, req *http.Request, rule types.Rule) {
if rule.SetOnResponse {
req.Header.Set(rule.Header, rule.Value)

return
}

req.Header.Set(rule.Header, rule.Value)
}
2 changes: 2 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Rule struct {
Value string `yaml:"Value"`
ValueReplace string `yaml:"ValueReplace"` // value used as replacement in rewrite
Values []string `yaml:"Values"` // values to join
// if SetOnResponse is true, the header will be changed on the response. It will be on the request otherwise (default).
SetOnResponse bool `yaml:"SetOnRequest"`
}

var ErrMissingRequiredFields = errors.New("missing required fields")
Expand Down

0 comments on commit 85e708c

Please sign in to comment.