package component12import (3"errors"4"strings"56"github.com/leslie-qiwa/flat"7"github.com/projectdiscovery/retryablehttp-go"8)910// ErrSetValue is a error raised when a value cannot be set11var ErrSetValue = errors.New("could not set value")1213func IsErrSetValue(err error) bool {14if err == nil {15return false16}17return strings.Contains(err.Error(), "could not set value")18}1920// ErrKeyNotFound is a error raised when a key is not found21var ErrKeyNotFound = errors.New("key not found")2223// Component is a component for a request24type Component interface {25// Name returns the name of the component26Name() string27// Parse parses the component and returns the28// parsed component29Parse(req *retryablehttp.Request) (bool, error)30// Iterate iterates over all values of a component31// ex in case of query component, it will iterate over each query parameter32// depending on the rule if mode is single33// request is rebuilt for each value in this callback34// and in case of multiple, request will be rebuilt after iteration of all values35Iterate(func(key string, value interface{}) error) error36// SetValue sets a value in the component37// for a key38//39// After calling setValue for mutation, the value must be40// called again so as to reset the body to its original state.41SetValue(key string, value string) error42// Delete deletes a key from the component43// If it is applicable44Delete(key string) error45// Rebuild returns a new request with the46// component rebuilt47Rebuild() (*retryablehttp.Request, error)48// Clones current state of this component49Clone() Component50}5152const (53// RequestBodyComponent is the name of the request body component54RequestBodyComponent = "body"55// RequestQueryComponent is the name of the request query component56RequestQueryComponent = "query"57// RequestPathComponent is the name of the request url component58RequestPathComponent = "path"59// RequestHeaderComponent is the name of the request header component60RequestHeaderComponent = "header"61// RequestCookieComponent is the name of the request cookie component62RequestCookieComponent = "cookie"63)6465// Components is a list of all available components66var Components = []string{67RequestBodyComponent,68RequestQueryComponent,69RequestHeaderComponent,70RequestPathComponent,71RequestCookieComponent,72}7374// New creates a new component for a componentType75func New(componentType string) Component {76switch componentType {77case "body":78return NewBody()79case "query":80return NewQuery()81case "path":82return NewPath()83case "header":84return NewHeader()85case "cookie":86return NewCookie()87}88return nil89}9091var (92flatOpts = &flat.Options{93Safe: true,94Delimiter: "~",95}96)979899