feat(rewriter): pass structured url to ease request rewriting
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
@ -74,7 +74,7 @@ func (l *Layer) ResponseTransformer(layer *store.Layer) proxy.ResponseTransforme
|
||||
}
|
||||
}
|
||||
|
||||
func New() *Layer {
|
||||
func New(funcs ...OptionFunc) *Layer {
|
||||
return &Layer{}
|
||||
}
|
||||
|
||||
|
16
internal/proxy/director/layer/rewriter/options.go
Normal file
16
internal/proxy/director/layer/rewriter/options.go
Normal file
@ -0,0 +1,16 @@
|
||||
package rewriter
|
||||
|
||||
type Options struct {
|
||||
}
|
||||
|
||||
type OptionFunc func(opts *Options)
|
||||
|
||||
func NewOptions(funcs ...OptionFunc) *Options {
|
||||
opts := &Options{}
|
||||
|
||||
for _, fn := range funcs {
|
||||
fn(opts)
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
@ -12,9 +12,27 @@ type RequestEnv struct {
|
||||
Request RequestInfo `expr:"request"`
|
||||
}
|
||||
|
||||
type URLEnv struct {
|
||||
Scheme string `expr:"scheme"`
|
||||
Opaque string `expr:"opaque"`
|
||||
User UserInfoEnv `expr:"user"`
|
||||
Host string `expr:"host"`
|
||||
Path string `expr:"path"`
|
||||
RawPath string `expr:"rawPath"`
|
||||
RawQuery string `expr:"rawQuery"`
|
||||
Fragment string `expr:"fragment"`
|
||||
RawFragment string `expr:"rawFragment"`
|
||||
}
|
||||
|
||||
type UserInfoEnv struct {
|
||||
Username string `expr:"username"`
|
||||
Password string `expr:"password"`
|
||||
}
|
||||
|
||||
type RequestInfo struct {
|
||||
Method string `expr:"method"`
|
||||
URL string `expr:"url"`
|
||||
URL URLEnv `expr:"url"`
|
||||
RawURL string `expr:"rawUrl"`
|
||||
Proto string `expr:"proto"`
|
||||
ProtoMajor int `expr:"protoMajor"`
|
||||
ProtoMinor int `expr:"protoMinor"`
|
||||
@ -33,18 +51,32 @@ func (l *Layer) applyRequestRules(r *http.Request, options *LayerOptions) error
|
||||
return nil
|
||||
}
|
||||
|
||||
engine, err := rule.NewEngine[*RequestEnv](
|
||||
ruleHTTP.WithRequestFuncs(r),
|
||||
rule.WithRules(options.Rules.Request...),
|
||||
)
|
||||
engine, err := l.getRequestRuleEngine(r, options)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
env := &RequestEnv{
|
||||
Request: RequestInfo{
|
||||
Method: r.Method,
|
||||
URL: r.URL.String(),
|
||||
Method: r.Method,
|
||||
URL: URLEnv{
|
||||
Scheme: r.URL.Scheme,
|
||||
Opaque: r.URL.Opaque,
|
||||
User: UserInfoEnv{
|
||||
Username: r.URL.User.Username(),
|
||||
Password: func() string {
|
||||
passwd, _ := r.URL.User.Password()
|
||||
return passwd
|
||||
}(),
|
||||
},
|
||||
Host: r.URL.Host,
|
||||
Path: r.URL.Path,
|
||||
RawPath: r.URL.RawPath,
|
||||
RawQuery: r.URL.RawQuery,
|
||||
Fragment: r.URL.Fragment,
|
||||
RawFragment: r.URL.RawFragment,
|
||||
},
|
||||
RawURL: r.URL.String(),
|
||||
Proto: r.Proto,
|
||||
ProtoMajor: r.ProtoMajor,
|
||||
ProtoMinor: r.ProtoMinor,
|
||||
@ -65,6 +97,18 @@ func (l *Layer) applyRequestRules(r *http.Request, options *LayerOptions) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Layer) getRequestRuleEngine(r *http.Request, options *LayerOptions) (*rule.Engine[*RequestEnv], error) {
|
||||
engine, err := rule.NewEngine[*RequestEnv](
|
||||
rule.WithRules(options.Rules.Request...),
|
||||
ruleHTTP.WithRequestFuncs(r),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return engine, nil
|
||||
}
|
||||
|
||||
type ResponseEnv struct {
|
||||
Request RequestInfo `expr:"request"`
|
||||
Response ResponseInfo `expr:"response"`
|
||||
@ -84,23 +128,37 @@ type ResponseInfo struct {
|
||||
}
|
||||
|
||||
func (l *Layer) applyResponseRules(r *http.Response, options *LayerOptions) error {
|
||||
rules := options.Rules.Request
|
||||
rules := options.Rules.Response
|
||||
if len(rules) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
engine, err := rule.NewEngine[*ResponseEnv](
|
||||
rule.WithRules(options.Rules.Response...),
|
||||
ruleHTTP.WithResponseFuncs(r),
|
||||
)
|
||||
engine, err := l.getResponseRuleEngine(r, options)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
env := &ResponseEnv{
|
||||
Request: RequestInfo{
|
||||
Method: r.Request.Method,
|
||||
URL: r.Request.URL.String(),
|
||||
Method: r.Request.Method,
|
||||
URL: URLEnv{
|
||||
Scheme: r.Request.URL.Scheme,
|
||||
Opaque: r.Request.URL.Opaque,
|
||||
User: UserInfoEnv{
|
||||
Username: r.Request.URL.User.Username(),
|
||||
Password: func() string {
|
||||
passwd, _ := r.Request.URL.User.Password()
|
||||
return passwd
|
||||
}(),
|
||||
},
|
||||
Host: r.Request.URL.Host,
|
||||
Path: r.Request.URL.Path,
|
||||
RawPath: r.Request.URL.RawPath,
|
||||
RawQuery: r.Request.URL.RawQuery,
|
||||
Fragment: r.Request.URL.Fragment,
|
||||
RawFragment: r.Request.URL.RawFragment,
|
||||
},
|
||||
RawURL: r.Request.URL.String(),
|
||||
Proto: r.Request.Proto,
|
||||
ProtoMajor: r.Request.ProtoMajor,
|
||||
ProtoMinor: r.Request.ProtoMinor,
|
||||
@ -131,3 +189,15 @@ func (l *Layer) applyResponseRules(r *http.Response, options *LayerOptions) erro
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Layer) getResponseRuleEngine(r *http.Response, options *LayerOptions) (*rule.Engine[*ResponseEnv], error) {
|
||||
engine, err := rule.NewEngine[*ResponseEnv](
|
||||
rule.WithRules(options.Rules.Response...),
|
||||
ruleHTTP.WithResponseFuncs(r),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return engine, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user