Add support stream mode for HTTP Request
This commit is contained in:
+21
-1
@@ -1,8 +1,10 @@
|
|||||||
package tcp
|
package tcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -10,8 +12,22 @@ import (
|
|||||||
|
|
||||||
// Do - http.Client with support Digest Authorization
|
// Do - http.Client with support Digest Authorization
|
||||||
func Do(req *http.Request) (*http.Response, error) {
|
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}
|
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)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -64,5 +80,9 @@ func Do(req *http.Request) (*http.Response, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conn != nil {
|
||||||
|
res.Body = conn
|
||||||
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user