package rewriter import ( "net/http" "forge.cadoles.com/cadoles/bouncer/internal/rule" ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http" "github.com/pkg/errors" ) type RequestEnv struct { Request RequestInfo `expr:"request"` } type RequestInfo struct { Method string `expr:"method"` URL string `expr:"url"` Proto string `expr:"proto"` ProtoMajor int `expr:"protoMajor"` ProtoMinor int `expr:"protoMinor"` Header map[string][]string `expr:"header"` ContentLength int64 `expr:"contentLength"` TransferEncoding []string `expr:"transferEncoding"` Host string `expr:"host"` Trailer map[string][]string `expr:"trailer"` RemoteAddr string `expr:"remoteAddr"` RequestURI string `expr:"requestUri"` } func (l *Layer) applyRequestRules(r *http.Request, options *LayerOptions) error { rules := options.Rules.Request if len(rules) == 0 { return nil } engine, err := rule.NewEngine[*RequestEnv]( ruleHTTP.WithRequestFuncs(r), rule.WithRules(options.Rules.Request...), ) if err != nil { return errors.WithStack(err) } env := &RequestEnv{ Request: RequestInfo{ Method: r.Method, URL: r.URL.String(), Proto: r.Proto, ProtoMajor: r.ProtoMajor, ProtoMinor: r.ProtoMinor, Header: r.Header, ContentLength: r.ContentLength, TransferEncoding: r.TransferEncoding, Host: r.Host, Trailer: r.Trailer, RemoteAddr: r.RemoteAddr, RequestURI: r.RequestURI, }, } if _, err := engine.Apply(env); err != nil { return errors.WithStack(err) } return nil } type ResponseEnv struct { Request RequestInfo `expr:"request"` Response ResponseInfo `expr:"response"` } type ResponseInfo struct { Status string `expr:"status"` StatusCode int `expr:"statusCode"` Proto string `expr:"proto"` ProtoMajor int `expr:"protoMajor"` ProtoMinor int `expr:"protoMinor"` Header map[string][]string `expr:"header"` ContentLength int64 `expr:"contentLength"` TransferEncoding []string `expr:"transferEncoding"` Uncompressed bool `expr:"uncompressed"` Trailer map[string][]string `expr:"trailer"` } func (l *Layer) applyResponseRules(r *http.Response, options *LayerOptions) error { rules := options.Rules.Request if len(rules) == 0 { return nil } engine, err := rule.NewEngine[*ResponseEnv]( rule.WithRules(options.Rules.Response...), ruleHTTP.WithResponseFuncs(r), ) if err != nil { return errors.WithStack(err) } env := &ResponseEnv{ Request: RequestInfo{ Method: r.Request.Method, URL: r.Request.URL.String(), Proto: r.Request.Proto, ProtoMajor: r.Request.ProtoMajor, ProtoMinor: r.Request.ProtoMinor, Header: r.Request.Header, ContentLength: r.Request.ContentLength, TransferEncoding: r.Request.TransferEncoding, Host: r.Request.Host, Trailer: r.Request.Trailer, RemoteAddr: r.Request.RemoteAddr, RequestURI: r.Request.RequestURI, }, Response: ResponseInfo{ Proto: r.Proto, ProtoMajor: r.ProtoMajor, ProtoMinor: r.ProtoMinor, Header: r.Header, ContentLength: r.ContentLength, TransferEncoding: r.TransferEncoding, Trailer: r.Trailer, Status: r.Status, StatusCode: r.StatusCode, }, } if _, err := engine.Apply(env); err != nil { return errors.WithStack(err) } return nil }