From a79914376206191870fbd19992de32dd60daad0b Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 16 Feb 2024 00:40:17 +0700 Subject: all: change the signature of default request/response dumper Instead of function that use the signature of HTTPRequestDumper/ HTTPResponseDumper; change it to function that _return_ HTTPRequestDumper/ HTTPResponseDumper. In this way, the documentation can show the clear relation between function and its type. --- example.go | 4 ++-- http_request_dumper.go | 18 ++++++++++-------- http_response_dumper.go | 18 ++++++++++-------- http_target.go | 12 ++++++------ 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/example.go b/example.go index 5a34632..73c97f3 100644 --- a/example.go +++ b/example.go @@ -699,10 +699,10 @@ func (ex *Example) runWebSocketGet(rr *RunRequest) (res interface{}, err error) func requestDumperWithoutDate(req *http.Request) ([]byte, error) { req.Header.Del(libhttp.HeaderDate) - return DumpHTTPRequest(req) + return DefaultRequestDumper()(req) } func responseDumperWithoutDate(resp *http.Response) ([]byte, error) { resp.Header.Del(libhttp.HeaderDate) - return DumpHTTPResponse(resp) + return DefaultResponseDumper()(resp) } diff --git a/http_request_dumper.go b/http_request_dumper.go index 9029693..6dd15bc 100644 --- a/http_request_dumper.go +++ b/http_request_dumper.go @@ -14,16 +14,18 @@ import ( // [RunResponse] DumpRequest. type HTTPRequestDumper func(req *http.Request) ([]byte, error) -// DumpHTTPRequest define default [HTTPRequestDumper] that convert +// DefaultRequestDumper define default [HTTPRequestDumper] that convert // [http.Request] with its body to stream of bytes using // [httputil.DumpRequest]. // -// The returned dump have CRLF ("\r\n") replaced with single LF ("\n"). -func DumpHTTPRequest(req *http.Request) (raw []byte, err error) { - raw, err = httputil.DumpRequestOut(req, true) - if err != nil { - return nil, fmt.Errorf(`DumpHTTPRequest: %w`, err) +// The returned bytes have CRLF ("\r\n") replaced with single LF ("\n"). +func DefaultRequestDumper() HTTPRequestDumper { + return func(req *http.Request) (raw []byte, err error) { + raw, err = httputil.DumpRequestOut(req, true) + if err != nil { + return nil, fmt.Errorf(`DefaultRequestDumper: %w`, err) + } + raw = bytes.ReplaceAll(raw, []byte{'\r', '\n'}, []byte{'\n'}) + return raw, nil } - raw = bytes.ReplaceAll(raw, []byte{'\r', '\n'}, []byte{'\n'}) - return raw, nil } diff --git a/http_response_dumper.go b/http_response_dumper.go index a3f3055..f9554fe 100644 --- a/http_response_dumper.go +++ b/http_response_dumper.go @@ -14,16 +14,18 @@ import ( // [RunResponse] DumpResponse. type HTTPResponseDumper func(resp *http.Response) ([]byte, error) -// DumpHTTPResponse define default [HTTPResponseDumper] that convert +// DefaultResponseDumper define default [HTTPResponseDumper] that convert // [http.Response] with its body to stream of bytes using // [httputil.DumpResponse]. // -// The returned dump have CRLF ("\r\n") replaced with single LF ("\n"). -func DumpHTTPResponse(resp *http.Response) (raw []byte, err error) { - raw, err = httputil.DumpResponse(resp, true) - if err != nil { - return nil, fmt.Errorf(`DumpHTTPResponse: %w`, err) +// The returned bytes have CRLF ("\r\n") replaced with single LF ("\n"). +func DefaultResponseDumper() HTTPResponseDumper { + return func(resp *http.Response) (raw []byte, err error) { + raw, err = httputil.DumpResponse(resp, true) + if err != nil { + return nil, fmt.Errorf(`DefaultResponseDumper: %w`, err) + } + raw = bytes.ReplaceAll(raw, []byte{'\r', '\n'}, []byte{'\n'}) + return raw, nil } - raw = bytes.ReplaceAll(raw, []byte{'\r', '\n'}, []byte{'\n'}) - return raw, nil } diff --git a/http_target.go b/http_target.go index 262f8ec..b13bf73 100644 --- a/http_target.go +++ b/http_target.go @@ -44,13 +44,13 @@ type HTTPTarget struct { Attack HTTPAttackHandler `json:"-"` // RequestDumper define the handler to store [http.Request] after - // Run into [RunRequest] DumpRequest. - // Default to [DumpHTTPRequest] if its nil. + // Run into [RunRequest.DumpRequest]. + // Default to [DefaultRequestDumper] if its nil. RequestDumper HTTPRequestDumper `json:"-"` // ResponseDumper define the handler to store [http.Response] after - // Run into [RunRequest] DumpResponse. - // Default to [DumpHTTPResponse] if its nil. + // Run into [RunRequest.DumpResponse]. + // Default to [DefaultResponseDumper] if its nil. ResponseDumper HTTPResponseDumper `json:"-"` // ID of target, optional. @@ -139,10 +139,10 @@ func (ht *HTTPTarget) init() (err error) { } if ht.RequestDumper == nil { - ht.RequestDumper = DumpHTTPRequest + ht.RequestDumper = DefaultRequestDumper() } if ht.ResponseDumper == nil { - ht.ResponseDumper = DumpHTTPResponse + ht.ResponseDumper = DefaultResponseDumper() } return nil } -- cgit v1.3