Skip to content

Commit

Permalink
httpbody wrapper blockmatcher with func passing
Browse files Browse the repository at this point in the history
feature: implement net.HttpBody to allow streaming filters on http body request and response
refactoring: blockContent,blockContentHex filters to use the new implementation

refactor: move ErrBlock form proxy to net
refactor: reuse types and enum and expose options to set httpbody matcher config
drop noleak because of transport related leaks
feature: read write body wrapper and test api with porting compress filter

Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
  • Loading branch information
szuecs committed Nov 20, 2023
1 parent 69f2b6a commit f0678f3
Show file tree
Hide file tree
Showing 12 changed files with 1,096 additions and 379 deletions.
61 changes: 43 additions & 18 deletions filters/block/block.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package block

import (
"bytes"
"encoding/hex"
"errors"

"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/net"
)

var (
Expand All @@ -16,10 +18,16 @@ type blockSpec struct {
hex bool
}

type toBlockKeys struct{ Str []byte }

func (b toBlockKeys) String() string {
return string(b.Str)
}

type block struct {
toblockList []toblockKeys
toblockList []toBlockKeys
maxEditorBuffer uint64
maxBufferHandling maxBufferHandling
maxBufferHandling net.MaxBufferHandling
}

// NewBlockFilter *deprecated* version of NewBlock
Expand Down Expand Up @@ -52,7 +60,7 @@ func (bs *blockSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
return nil, filters.ErrInvalidFilterParameters
}

sargs := make([]toblockKeys, 0, len(args))
sargs := make([]toBlockKeys, 0, len(args))
for _, w := range args {
v, ok := w.(string)
if !ok {
Expand All @@ -63,33 +71,50 @@ func (bs *blockSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if err != nil {
return nil, err
}
sargs = append(sargs, toblockKeys{str: a})
sargs = append(sargs, toBlockKeys{Str: a})
} else {
sargs = append(sargs, toblockKeys{str: []byte(v)})
sargs = append(sargs, toBlockKeys{Str: []byte(v)})
}
}

b := &block{
return &block{
toblockList: sargs,
maxBufferHandling: maxBufferBestEffort,
maxBufferHandling: net.MaxBufferBestEffort,
maxEditorBuffer: bs.MaxMatcherBufferSize,
}
}, nil
}

return *b, nil
func blockMatcher(matches []toBlockKeys) func(b []byte) (int, error) {
return func(b []byte) (int, error) {
for _, s := range matches {
s := s
println("blockMatcher:", string(b), len(string(b)), "contains?:", string(s.Str))
if bytes.Contains(b, s.Str) {
b = nil
return 0, net.ErrBlocked
}
}
return len(b), nil
}
}

func (b block) Request(ctx filters.FilterContext) {
func (b *block) Request(ctx filters.FilterContext) {
req := ctx.Request()
if req.ContentLength == 0 {
return
}

req.Body = newMatcher(
req.Body,
b.toblockList,
b.maxEditorBuffer,
b.maxBufferHandling,
)
// fix filter chaining - https://github.com/zalando/skipper/issues/2605
ctx.Request().Header.Del("Content-Length")
ctx.Request().ContentLength = -1

req.Body = net.WrapBodyWithOptions(
req.Context(),
net.BodyOptions{
MaxBufferHandling: b.maxBufferHandling,
ReadBufferSize: b.maxEditorBuffer,
},
blockMatcher(b.toblockList),
req.Body)
}

func (block) Response(filters.FilterContext) {}
func (*block) Response(filters.FilterContext) {}
Loading

0 comments on commit f0678f3

Please sign in to comment.