From a22c33fd4e50fe64f2b84a1ab92acdc4cccd33ca Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Mon, 13 Feb 2023 15:42:19 +0300 Subject: [PATCH] Add support stream mode for HTTP Request --- pkg/tcp/request.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/tcp/request.go b/pkg/tcp/request.go index 9e10a5ef..2859b756 100644 --- a/pkg/tcp/request.go +++ b/pkg/tcp/request.go @@ -1,8 +1,10 @@ package tcp import ( + "context" "errors" "fmt" + "net" "net/http" "strings" "time" @@ -10,8 +12,22 @@ import ( // Do - http.Client with support Digest Authorization func Do(req *http.Request) (*http.Response, error) { - // need to create new client each time to reset timeout + var conn net.Conn + client := http.Client{Timeout: time.Second * 5000} + + // for multipart requests return conn as Body (for write support) + if ct := req.Header.Get("Content-Type"); strings.HasPrefix(ct, "multipart/mixed") { + var d net.Dialer + client.Transport = &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + var err error + conn, err = d.DialContext(ctx, network, addr) + return conn, err + }, + } + } + res, err := client.Do(req) if err != nil { return nil, err @@ -64,5 +80,9 @@ func Do(req *http.Request) (*http.Response, error) { } } + if conn != nil { + res.Body = conn + } + return res, nil }