-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sandor Szücs <[email protected]>
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package builtin | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/zalando/skipper/filters" | ||
xencoding "golang.org/x/text/encoding" | ||
"golang.org/x/text/encoding/charmap" | ||
) | ||
|
||
type encodeTyp int | ||
|
||
const ( | ||
requestEncoder encodeTyp = iota + 1 | ||
responseEncoder | ||
) | ||
|
||
type encodeHeaderSpec struct { | ||
typ encodeTyp | ||
} | ||
|
||
type encodeHeader struct { | ||
typ encodeTyp | ||
header string | ||
encoder *xencoding.Encoder | ||
} | ||
|
||
func NewEncodeRequestHeader() *encodeHeaderSpec { | ||
return &encodeHeaderSpec{ | ||
typ: requestEncoder, | ||
} | ||
} | ||
func NewEncodeResponseHeader() *encodeHeaderSpec { | ||
return &encodeHeaderSpec{ | ||
typ: responseEncoder, | ||
} | ||
} | ||
|
||
func (spec *encodeHeaderSpec) Name() string { | ||
switch spec.typ { | ||
case requestEncoder: | ||
return filters.EncodeRequestHeaderName | ||
case responseEncoder: | ||
return filters.EncodeResponseHeaderName | ||
} | ||
return "unknown" | ||
} | ||
|
||
func (spec *encodeHeaderSpec) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
if len(args) != 2 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
header, ok := args[0].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
to, ok := args[1].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
var ( | ||
encoder *xencoding.Encoder | ||
) | ||
|
||
switch to { | ||
case "ISO8859_1": | ||
encoder = charmap.ISO8859_1.NewEncoder() | ||
case "Windows1252": | ||
encoder = charmap.Windows1252.NewEncoder() | ||
} | ||
|
||
return &encodeHeader{ | ||
typ: spec.typ, | ||
header: header, | ||
encoder: encoder, | ||
}, nil | ||
} | ||
|
||
func (f *encodeHeader) Request(ctx filters.FilterContext) { | ||
if f.typ != requestEncoder { | ||
return | ||
} | ||
|
||
s := ctx.Request().Header.Get(f.header) | ||
if s == "" { | ||
return | ||
} | ||
|
||
sNew, err := f.encoder.String(s) | ||
if err != nil { | ||
log.Errorf("Failed to encode %q: %v", s, err) | ||
} | ||
ctx.Request().Header.Set(f.header, sNew) | ||
} | ||
|
||
func (f *encodeHeader) Response(ctx filters.FilterContext) { | ||
if f.typ != responseEncoder { | ||
return | ||
} | ||
s := ctx.Response().Header.Get(f.header) | ||
if s == "" { | ||
return | ||
} | ||
|
||
sNew, err := f.encoder.String(s) | ||
if err != nil { | ||
log.Errorf("Failed to encode %q: %v", s, err) | ||
} | ||
ctx.Response().Header.Set(f.header, sNew) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters