1
0
mirror of https://github.com/fumiama/terasu.git synced 2026-06-07 19:40:27 +08:00

fix: error on different frag lens

This commit is contained in:
源文雨
2025-10-23 23:33:06 +08:00
parent 3f56d5341b
commit bda0c8de97
9 changed files with 206 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
// Package http is the same as the standard http lib
// Package http is a wrapper around the standard http library with enhanced DNS resolution and TLS handling capabilities.
package http
import (
@@ -16,18 +16,23 @@ import (
)
var (
ErrNoTLSConnection = errors.New("no tls connection")
// ErrNoTLSConnection is returned when a TLS connection cannot be established.
ErrNoTLSConnection = errors.New("no tls connection")
// ErrEmptyHostAddress is returned when the host address is empty.
ErrEmptyHostAddress = errors.New("empty host addr")
)
// defaultDialer is the default dialer used for connecting to hosts.
var defaultDialer = net.Dialer{
Timeout: 10 * time.Second,
}
// SetDefaultClientTimeout sets the default timeout for the client's dialer.
func SetDefaultClientTimeout(t time.Duration) {
defaultDialer.Timeout = t
}
// DefaultClient is the default HTTP client with custom transport settings, including DNS resolution and TLS handling.
var DefaultClient = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
@@ -40,12 +45,13 @@ var DefaultClient = http.Client{
if err != nil {
return nil, err
}
if len(addr) == 0 {
if len(addrs) == 0 {
return nil, ErrEmptyHostAddress
}
var conn net.Conn
var tlsConn *tls.Conn
for _, a := range addrs {
// Apply timeout if set, otherwise use deadline
if defaultDialer.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout)
@@ -63,7 +69,7 @@ var DefaultClient = http.Client{
ServerName: host,
MinVersion: tls.VersionTLS12,
})
// re-init ctx due to deadline settings in tcp dial
// Re-initialize context due to potential deadline changes from TCP dial
if defaultDialer.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout)
@@ -104,18 +110,22 @@ var DefaultClient = http.Client{
},
}
// Get performs an HTTP GET request using the default client.
func Get(url string) (resp *http.Response, err error) {
return DefaultClient.Get(url)
}
// Head performs an HTTP HEAD request using the default client.
func Head(url string) (resp *http.Response, err error) {
return DefaultClient.Head(url)
}
// Post performs an HTTP POST request using the default client.
func Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
return DefaultClient.Post(url, contentType, body)
}
// PostForm performs an HTTP POST request with form data using the default client.
func PostForm(url string, data url.Values) (resp *http.Response, err error) {
return DefaultClient.PostForm(url, data)
}