chore: move '/internal/pkg' to '/pkg'
This commit is contained in:
33
pkg/utils/http/parser.go
Normal file
33
pkg/utils/http/parser.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 从表示 HTTP 标头的字符串解析并返回一个 http.Header 对象。
|
||||
//
|
||||
// 入参:
|
||||
// - headers: 表示 HTTP 标头的字符串。
|
||||
//
|
||||
// 出参:
|
||||
// - header: http.Header 对象。
|
||||
// - err: 错误。
|
||||
func ParseHeaders(headers string) (http.Header, error) {
|
||||
str := strings.TrimSpace(headers) + "\r\n\r\n"
|
||||
if len(str) == 4 {
|
||||
return make(http.Header), nil
|
||||
}
|
||||
|
||||
br := bufio.NewReader(strings.NewReader(str))
|
||||
tp := textproto.NewReader(br)
|
||||
|
||||
mimeHeader, err := tp.ReadMIMEHeader()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return http.Header(mimeHeader), err
|
||||
}
|
||||
33
pkg/utils/http/transport.go
Normal file
33
pkg/utils/http/transport.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 创建并返回一个 [http.DefaultTransport] 对象副本。
|
||||
//
|
||||
// 出参:
|
||||
// - transport: [http.DefaultTransport] 对象副本。
|
||||
func NewDefaultTransport() *http.Transport {
|
||||
if http.DefaultTransport != nil {
|
||||
if t, ok := http.DefaultTransport.(*http.Transport); ok {
|
||||
return t.Clone()
|
||||
}
|
||||
}
|
||||
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user