- 分离 Request 的配置态与执行态,修复二次 Do、raw 模式网络配置失效和 body 来源互斥问题 - 新增 starnet trace 抽象,补齐 DNS/连接/TLS/重试事件,并优化动态 transport 缓存与代理解析路径 - 收紧非法代理为 fail-fast,多目标目标回退仅限幂等请求,修复 Host/TLS/SNI 等语义边界 - 补充防御性拷贝、专项回归测试、本地代理/TLS 用例与 README 行为说明
133 lines
4.0 KiB
Go
133 lines
4.0 KiB
Go
package starnet
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// WithTimeout 设置请求总超时时间
|
|
// timeout > 0: 为本次请求注入 context 超时
|
|
// timeout = 0: 不额外设置请求总超时
|
|
// timeout < 0: 禁用 starnet 默认总超时
|
|
func WithTimeout(timeout time.Duration) RequestOpt {
|
|
return requestOptFromMutation(mutateTimeout(timeout))
|
|
}
|
|
|
|
// WithDialTimeout 设置连接超时时间
|
|
func WithDialTimeout(timeout time.Duration) RequestOpt {
|
|
return requestOptFromMutation(mutateDialTimeout(timeout))
|
|
}
|
|
|
|
// WithProxy 设置代理
|
|
func WithProxy(proxy string) RequestOpt {
|
|
return requestOptFromMutation(mutateProxy(proxy))
|
|
}
|
|
|
|
// WithDialFunc 设置自定义 Dial 函数
|
|
func WithDialFunc(fn func(ctx context.Context, network, addr string) (net.Conn, error)) RequestOpt {
|
|
return requestOptFromMutation(mutateDialFunc(fn))
|
|
}
|
|
|
|
// WithTLSConfig 设置 TLS 配置
|
|
func WithTLSConfig(tlsConfig *tls.Config) RequestOpt {
|
|
return requestOptFromMutation(mutateTLSConfig(tlsConfig))
|
|
}
|
|
|
|
// WithTLSServerName 设置显式 TLS ServerName/SNI。
|
|
func WithTLSServerName(serverName string) RequestOpt {
|
|
return requestOptFromMutation(mutateTLSServerName(serverName))
|
|
}
|
|
|
|
// WithTraceHooks 设置请求 trace 回调。
|
|
func WithTraceHooks(hooks *TraceHooks) RequestOpt {
|
|
return requestOptFromMutation(mutateTraceHooks(hooks))
|
|
}
|
|
|
|
// WithSkipTLSVerify 设置是否跳过 TLS 验证
|
|
func WithSkipTLSVerify(skip bool) RequestOpt {
|
|
return requestOptFromMutation(mutateSkipTLSVerify(skip))
|
|
}
|
|
|
|
// WithCustomIP 设置自定义 IP
|
|
func WithCustomIP(ips []string) RequestOpt {
|
|
return requestOptFromMutation(mutateCustomIP(ips))
|
|
}
|
|
|
|
// WithAddCustomIP 添加自定义 IP
|
|
func WithAddCustomIP(ip string) RequestOpt {
|
|
return requestOptFromMutation(mutateAddCustomIP(ip))
|
|
}
|
|
|
|
// WithCustomDNS 设置自定义 DNS 服务器
|
|
func WithCustomDNS(dnsServers []string) RequestOpt {
|
|
return requestOptFromMutation(mutateCustomDNS(dnsServers))
|
|
}
|
|
|
|
// WithAddCustomDNS 添加自定义 DNS 服务器
|
|
func WithAddCustomDNS(dns string) RequestOpt {
|
|
return requestOptFromMutation(mutateAddCustomDNS(dns))
|
|
}
|
|
|
|
// WithLookupFunc 设置自定义 DNS 解析函数
|
|
func WithLookupFunc(fn func(ctx context.Context, host string) ([]net.IPAddr, error)) RequestOpt {
|
|
return requestOptFromMutation(mutateLookupFunc(fn))
|
|
}
|
|
|
|
// WithBasicAuth 设置 Basic 认证
|
|
func WithBasicAuth(username, password string) RequestOpt {
|
|
return requestOptFromMutation(mutateBasicAuth(username, password))
|
|
}
|
|
|
|
// WithQuery 添加查询参数
|
|
func WithQuery(key, value string) RequestOpt {
|
|
return requestOptFromMutation(mutateAddQuery(key, value))
|
|
}
|
|
|
|
// WithQueries 批量添加查询参数
|
|
func WithQueries(queries map[string]string) RequestOpt {
|
|
return requestOptFromMutation(mutateAddQueries(queries))
|
|
}
|
|
|
|
// WithContentLength 设置 Content-Length
|
|
func WithContentLength(length int64) RequestOpt {
|
|
return requestOptFromMutation(mutateContentLength(length))
|
|
}
|
|
|
|
// WithAutoCalcContentLength 设置是否自动计算 Content-Length
|
|
func WithAutoCalcContentLength(auto bool) RequestOpt {
|
|
return requestOptFromMutation(mutateAutoCalcContentLength(auto))
|
|
}
|
|
|
|
// WithUploadProgress 设置文件上传进度回调
|
|
func WithUploadProgress(fn UploadProgressFunc) RequestOpt {
|
|
return requestOptFromMutation(mutateUploadProgress(fn))
|
|
}
|
|
|
|
// WithTransport 设置自定义 Transport
|
|
func WithTransport(transport *http.Transport) RequestOpt {
|
|
return requestOptFromMutation(mutateTransport(transport))
|
|
}
|
|
|
|
// WithAutoFetch 设置是否自动获取响应体
|
|
func WithAutoFetch(auto bool) RequestOpt {
|
|
return requestOptFromMutation(mutateAutoFetch(auto))
|
|
}
|
|
|
|
// WithMaxRespBodyBytes 设置响应体最大读取字节数(<=0 表示不限制)
|
|
func WithMaxRespBodyBytes(maxBytes int64) RequestOpt {
|
|
return requestOptFromMutation(mutateMaxRespBodyBytes(maxBytes))
|
|
}
|
|
|
|
// WithRawRequest 设置原始请求
|
|
func WithRawRequest(httpReq *http.Request) RequestOpt {
|
|
return requestOptFromMutation(mutateRawRequest(httpReq))
|
|
}
|
|
|
|
// WithContext 设置 context
|
|
func WithContext(ctx context.Context) RequestOpt {
|
|
return requestOptFromMutation(mutateContext(ctx))
|
|
}
|