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 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 { type RequestFile struct {
FormName string FormName string
FileName string FileName string
@ -1263,6 +1271,7 @@ type Response struct {
*http.Response *http.Response
req Request req Request
data *Body data *Body
rawClient *http.Client
} }
type Body struct { type Body struct {
@ -1325,6 +1334,24 @@ func (r *Response) Body() *Body {
return r.data 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) { func Curl(r *Request) (*Response, error) {
r.errInfo = nil r.errInfo = nil
err := applyOptions(r) err := applyOptions(r)
@ -1336,6 +1363,7 @@ func Curl(r *Request) (*Response, error) {
Response: resp, Response: resp,
req: *r, req: *r,
data: new(Body), data: new(Body),
rawClient: r.rawClient,
} }
if err != nil { if err != nil {
res.Response = &http.Response{} res.Response = &http.Response{}
@ -1376,6 +1404,17 @@ func NewRequestWithContext(ctx context.Context, uri string, method string, opts
return newRequest(ctx, uri, method, 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) { func newRequest(ctx context.Context, uri string, method string, opts ...RequestOpt) (*Request, error) {
var req *http.Request var req *http.Request
var err error var err error
@ -1393,7 +1432,7 @@ func newRequest(ctx context.Context, uri string, method string, opts ...RequestO
method: method, method: method,
RequestOpts: RequestOpts{ RequestOpts: RequestOpts{
rawRequest: req, rawRequest: req,
rawClient: new(http.Client), rawClient: nil,
timeout: DefaultTimeout, timeout: DefaultTimeout,
dialTimeout: DefaultDialTimeout, dialTimeout: DefaultDialTimeout,
autoFetchRespBody: DefaultFetchRespBody, 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{} 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.skipTLSVerify {
if r.transport.TLSClientConfig == nil { if r.transport.TLSClientConfig == nil {
r.transport.TLSClientConfig = &tls.Config{} 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
}