From c73fe8cca595518181126832722b133df015b5d3 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 28 Jun 2024 10:46:38 +0200 Subject: [PATCH] feat(rewriter): pass structured url to ease request rewriting --- doc/fr/references/layers/rewriter.md | 16 ++- .../proxy/director/layer/rewriter/layer.go | 2 +- .../proxy/director/layer/rewriter/options.go | 16 +++ .../proxy/director/layer/rewriter/rules.go | 98 ++++++++++++++++--- 4 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 internal/proxy/director/layer/rewriter/options.go diff --git a/doc/fr/references/layers/rewriter.md b/doc/fr/references/layers/rewriter.md index 7a9b212..9f4afdc 100644 --- a/doc/fr/references/layers/rewriter.md +++ b/doc/fr/references/layers/rewriter.md @@ -66,7 +66,21 @@ La requête en cours de traitement. { method: "string", // Méthode HTTP host: "string", // Nom d'hôte (`Host`) associé à la requête - url: "string", // URL associée à la requête + url: { // URL associée à la requête sous sa forme structurée + "scheme": "string", // Schéma HTTP de l'URL + "opaque": "string", // Données opaque de l'URL + "user": { // Identifiants d'URL (Basic Auth) + "username": "", + "password": "" + }, + "host": "string", // Nom d'hôte (:) de l'URL + "path": "string", // Chemin de l'URL (format assaini) + "rawPath": "string", // Chemin de l'URL (format brut) + "rawQuery": "string", // Variables d'URL (format brut) + "fragment" : "string", // Fragment d'URL (format assaini) + "rawFragment" : "string" // Fragment d'URL (format brut) + }, + rawUrl: "string", // URL associée à la requête (format assaini) proto: "string", // Numéro de version du protocole utilisé protoMajor: "int", // Numéro de version majeure du protocole utilisé protoMinor: "int", // Numéro de version mineur du protocole utilisé diff --git a/internal/proxy/director/layer/rewriter/layer.go b/internal/proxy/director/layer/rewriter/layer.go index a20a21e..fa74431 100644 --- a/internal/proxy/director/layer/rewriter/layer.go +++ b/internal/proxy/director/layer/rewriter/layer.go @@ -74,7 +74,7 @@ func (l *Layer) ResponseTransformer(layer *store.Layer) proxy.ResponseTransforme } } -func New() *Layer { +func New(funcs ...OptionFunc) *Layer { return &Layer{} } diff --git a/internal/proxy/director/layer/rewriter/options.go b/internal/proxy/director/layer/rewriter/options.go new file mode 100644 index 0000000..2734f16 --- /dev/null +++ b/internal/proxy/director/layer/rewriter/options.go @@ -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 +} diff --git a/internal/proxy/director/layer/rewriter/rules.go b/internal/proxy/director/layer/rewriter/rules.go index 7bcc5a3..466a04a 100644 --- a/internal/proxy/director/layer/rewriter/rules.go +++ b/internal/proxy/director/layer/rewriter/rules.go @@ -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 +}