diff options
| author | Shulhan <ms@kilabit.info> | 2024-02-16 00:58:41 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-02-16 01:38:51 +0700 |
| commit | 05837bccf9aadea42ad9a67947b1c0c7a5e7ac56 (patch) | |
| tree | 5b8a46e977e49ac28e4e59eb5939f1d3ba968f7e | |
| parent | a79914376206191870fbd19992de32dd60daad0b (diff) | |
| download | gorankusu-05837bccf9aadea42ad9a67947b1c0c7a5e7ac56.tar.xz | |
all: add default HTTPParamsConverter for [HTTPTarget.ParamsConverter]
The DefaultParamsConverter define default function to convert
[HTTPTarget.Params] to its equivalent parameters in HTTP, either as query
in URL or as bytes in body.
This changes introduce breaking changes in HTTPTarget where field
ConvertParams renamed to ParamsConverter.
| -rw-r--r-- | gorankusu.go | 19 | ||||
| -rw-r--r-- | http_params_converter.go | 27 | ||||
| -rw-r--r-- | http_target.go | 23 |
3 files changed, 44 insertions, 25 deletions
diff --git a/gorankusu.go b/gorankusu.go index c807336..2d49e1d 100644 --- a/gorankusu.go +++ b/gorankusu.go @@ -340,25 +340,14 @@ func (gorankusu *Gorankusu) runHTTPTarget(rr *RunRequest) (res *RunResponse, err httpc := libhttp.NewClient(httpcOpts) - var params interface{} + var params any if !rr.HTTPTarget.WithRawBody { rr.HTTPTarget.paramsToPath() - if rr.HTTPTarget.ConvertParams == nil { - switch rr.HTTPTarget.RequestType { - case libhttp.RequestTypeJSON: - params = rr.HTTPTarget.Params.ToJSONObject() - case libhttp.RequestTypeMultipartForm: - params = rr.HTTPTarget.Params.ToMultipartFormData() - default: - params = rr.HTTPTarget.Params.ToURLValues() - } - } else { - params, err = rr.HTTPTarget.ConvertParams(&rr.HTTPTarget) - if err != nil { - return nil, fmt.Errorf(`%s: %w`, logp, err) - } + params, err = rr.HTTPTarget.ParamsConverter(&rr.HTTPTarget) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) } } diff --git a/http_params_converter.go b/http_params_converter.go new file mode 100644 index 0000000..af7745b --- /dev/null +++ b/http_params_converter.go @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + +package gorankusu + +import libhttp "github.com/shuLhan/share/lib/http" + +// HTTPParamsConverter is a handler that will be called inside the Run handler +// to convert the Params values to type that will be send as request. +type HTTPParamsConverter func(target *HTTPTarget) (any, error) + +// DefaultParamsConverter define default function to convert +// [HTTPTarget.Params] to its equivalent parameters in HTTP, either as query +// in URL or as stream of bytes in body. +func DefaultParamsConverter() HTTPParamsConverter { + return func(target *HTTPTarget) (params any, err error) { + switch target.RequestType { + case libhttp.RequestTypeJSON: + params = target.Params.ToJSONObject() + case libhttp.RequestTypeMultipartForm: + params = target.Params.ToMultipartFormData() + default: + params = target.Params.ToURLValues() + } + return params, nil + } +} diff --git a/http_target.go b/http_target.go index b13bf73..6eee3aa 100644 --- a/http_target.go +++ b/http_target.go @@ -17,10 +17,6 @@ import ( libpath "github.com/shuLhan/share/lib/path" ) -// HTTPConvertParams is a handler that will be called inside the Run handler -// to convert the Params values to type that will be send as request. -type HTTPConvertParams func(target *HTTPTarget) (interface{}, error) - // HTTPRunHandler define the function type that will be called when client // send request to run the HTTP target. type HTTPRunHandler func(rr *RunRequest) (runres *RunResponse, err error) @@ -31,8 +27,12 @@ type HTTPPreAttackHandler func(rr *RunRequest) // HTTPTarget define the HTTP endpoint that can be attached to Gorankusu. type HTTPTarget struct { - Params KeyFormInput - ConvertParams HTTPConvertParams `json:"-"` + Params KeyFormInput + + // ParamsConverter define the custom function to convert the Params + // into HTTP request. + // This field is optional default to [DefaultParamsConverter]. + ParamsConverter HTTPParamsConverter `json:"-"` Headers KeyFormInput @@ -90,7 +90,7 @@ type HTTPTarget struct { // This method is provided to prevent the sync.Mutex being copied. func (ht *HTTPTarget) clone(src *HTTPTarget) { ht.Params = src.Params - ht.ConvertParams = src.ConvertParams + ht.ParamsConverter = src.ParamsConverter ht.Headers = src.Headers ht.Run = src.Run ht.PreAttack = src.PreAttack @@ -130,6 +130,9 @@ func (ht *HTTPTarget) init() (err error) { ht.Params[key] = formInput } } + if ht.ParamsConverter == nil { + ht.ParamsConverter = DefaultParamsConverter() + } if len(ht.Path) == 0 { ht.Path = "/" } @@ -230,7 +233,7 @@ func (ht *HTTPTarget) paramsToPath() { // refCopy copy original fields, methods, and handlers that cannot be // send or replaced from orig to ht. func (ht *HTTPTarget) refCopy(orig *HTTPTarget) { - ht.ConvertParams = orig.ConvertParams + ht.ParamsConverter = orig.ParamsConverter ht.RequestDumper = orig.RequestDumper ht.ResponseDumper = orig.ResponseDumper ht.WithRawBody = orig.WithRawBody @@ -262,10 +265,10 @@ func (ht *HTTPTarget) String() string { var sb strings.Builder fmt.Fprintf(&sb, `ID:%s Name:%s Hint:%s Path:%s `+ - `Params:%v ConvertParams:%v Headers:%v `+ + `Params:%v ParamsConverter:%v Headers:%v `+ `AllowAttack:%t IsCustomizable:%t`, ht.ID, ht.Name, ht.Hint, ht.Path, - ht.Params, ht.ConvertParams, ht.Headers, + ht.Params, ht.ParamsConverter, ht.Headers, ht.AllowAttack, ht.IsCustomizable) return sb.String() |
