add http client control

This commit is contained in:
兔子 2025-07-14 18:23:14 +08:00
parent c1eaf43058
commit a8eed30db5
Signed by: b612
GPG Key ID: 99DD2222B612B612

81
curl.go
View File

@ -855,6 +855,14 @@ func (r *Request) AddFileWithNameAndTypeNoError(formName, filepath, filename, fi
return r
}
func (r *Request) HttpClient() (*http.Client, error) {
err := applyOptions(r)
if err != nil {
return nil, err
}
return r.rawClient, nil
}
type RequestFile struct {
FormName string
FileName string
@ -1263,6 +1271,7 @@ type Response struct {
*http.Response
req Request
data *Body
rawClient *http.Client
}
type Body struct {
@ -1325,6 +1334,24 @@ func (r *Response) Body() *Body {
return r.data
}
func (r *Response) Close() error {
if r != nil && r.data != nil && r.data.raw != nil {
return r.Response.Body.Close()
}
return nil
}
func (r *Response) CloseAll() error {
if r.rawClient != nil {
r.rawClient.CloseIdleConnections()
}
return r.Close()
}
func (r *Response) HttpClient() *http.Client {
return r.rawClient
}
func Curl(r *Request) (*Response, error) {
r.errInfo = nil
err := applyOptions(r)
@ -1336,6 +1363,7 @@ func Curl(r *Request) (*Response, error) {
Response: resp,
req: *r,
data: new(Body),
rawClient: r.rawClient,
}
if err != nil {
res.Response = &http.Response{}
@ -1376,6 +1404,17 @@ func NewRequestWithContext(ctx context.Context, uri string, method string, opts
return newRequest(ctx, uri, method, opts...)
}
func NewHttpClient(opts ...RequestOpt) (*http.Client, error) {
req, err := newRequest(context.Background(), "", "", opts...)
if err != nil {
return nil, err
}
defer func() {
req = nil
}()
return req.HttpClient()
}
func newRequest(ctx context.Context, uri string, method string, opts ...RequestOpt) (*Request, error) {
var req *http.Request
var err error
@ -1393,7 +1432,7 @@ func newRequest(ctx context.Context, uri string, method string, opts ...RequestO
method: method,
RequestOpts: RequestOpts{
rawRequest: req,
rawClient: new(http.Client),
rawClient: nil,
timeout: DefaultTimeout,
dialTimeout: DefaultDialTimeout,
autoFetchRespBody: DefaultFetchRespBody,
@ -1416,10 +1455,13 @@ func newRequest(ctx context.Context, uri string, method string, opts ...RequestO
}
}
}
if r.transport == nil {
if r.transport == nil && !r.doRawTransport {
r.transport = &http.Transport{}
}
if r.doRawTransport {
if r.rawClient == nil && !r.doRawClient {
r.rawClient = new(http.Client)
}
if !r.doRawTransport {
if r.skipTLSVerify {
if r.transport.TLSClientConfig == nil {
r.transport.TLSClientConfig = &tls.Config{}
@ -1670,3 +1712,36 @@ func copyWithContext(ctx context.Context, recall func(string, int64, int64), fil
}
}
}
func NewReqWithClient(client *http.Client, uri string, opts ...RequestOpt) *Request {
return NewSimpleRequestWithClient(client, uri, "GET", opts...)
}
func NewReqWithContextWithClient(ctx context.Context, client *http.Client, uri string, opts ...RequestOpt) *Request {
return NewSimpleRequestWithContextWithClient(ctx, client, uri, "GET", opts...)
}
func NewSimpleRequestWithClient(client *http.Client, uri string, method string, opts ...RequestOpt) *Request {
r, _ := NewRequestWithContextWithClient(context.Background(), client, uri, method, opts...)
return r
}
func NewRequestWithClient(client *http.Client, uri string, method string, opts ...RequestOpt) (*Request, error) {
return NewRequestWithContextWithClient(context.Background(), client, uri, method, opts...)
}
func NewSimpleRequestWithContextWithClient(ctx context.Context, client *http.Client, uri string, method string, opts ...RequestOpt) *Request {
r, _ := NewRequestWithContextWithClient(ctx, client, uri, method, opts...)
return r
}
func NewRequestWithContextWithClient(ctx context.Context, client *http.Client, uri string, method string, opts ...RequestOpt) (*Request, error) {
req, err := newRequest(context.Background(), uri, method, opts...)
if err != nil {
return nil, err
}
req.rawClient = client
req.SetDoRawClient(true)
req.SetDoRawTransport(true)
return nil, err
}