From 61ad52835bb24b539db2c4ce1788f188f5603acc Mon Sep 17 00:00:00 2001 From: gospider <2216403312@qq.com> Date: Tue, 23 Apr 2024 15:45:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requests.go | 9 ++------- response.go | 6 +----- roundTripper.go | 10 ++------- tools.go | 54 ++++++------------------------------------------- 4 files changed, 11 insertions(+), 68 deletions(-) diff --git a/requests.go b/requests.go index 197a498..6231a6b 100644 --- a/requests.go +++ b/requests.go @@ -3,6 +3,7 @@ package requests import ( "context" "errors" + "fmt" "io" "net" "strings" @@ -319,13 +320,7 @@ func (obj *Client) request(ctx context.Context, option *RequestOption) (response if option.Referer != "" { reqs.Header.Set("Referer", option.Referer) } else if reqs.URL.Scheme != "" && reqs.URL.Host != "" { - referBuild := builderPool.Get().(*strings.Builder) - referBuild.WriteString(reqs.URL.Scheme) - referBuild.WriteString("://") - referBuild.WriteString(reqs.URL.Host) - reqs.Header.Set("Referer", referBuild.String()) - referBuild.Reset() - builderPool.Put(referBuild) + reqs.Header.Set("Referer", fmt.Sprintf("%s://%s", reqs.URL.Scheme, reqs.URL.Host)) } } diff --git a/response.go b/response.go index a4da3b4..eacdb59 100644 --- a/response.go +++ b/response.go @@ -271,11 +271,7 @@ func (obj *Response) ReadBody() (err error) { return errors.New("already read body") } obj.readBody = true - bBody := bufferPool.Get().(*bytes.Buffer) - defer func() { - bBody.Reset() - bufferPool.Put(bBody) - }() + bBody := bytes.NewBuffer(nil) if obj.bar && obj.ContentLength() > 0 { _, err = io.Copy(&barBody{ bar: bar.NewClient(obj.response.ContentLength), diff --git a/roundTripper.go b/roundTripper.go index 594911c..1d01085 100644 --- a/roundTripper.go +++ b/roundTripper.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "net" "net/textproto" "net/url" @@ -35,14 +36,7 @@ func (obj *reqTask) inPool() bool { } func getKey(ctxData *reqCtxData, req *http.Request) (key string) { - b := builderPool.Get().(*strings.Builder) - b.WriteString(getAddr(ctxData.proxy)) - b.WriteString("@") - b.WriteString(getAddr(req.URL)) - key = b.String() - b.Reset() - builderPool.Put(b) - return + return fmt.Sprintf("%s@%s", getAddr(ctxData.proxy), getAddr(req.URL)) } type roundTripper struct { diff --git a/tools.go b/tools.go index 9d1ac33..f83d11f 100644 --- a/tools.go +++ b/tools.go @@ -2,7 +2,6 @@ package requests import ( "bufio" - "bytes" "context" "errors" "fmt" @@ -13,7 +12,6 @@ import ( "net/url" "strconv" "strings" - "sync" _ "unsafe" "github.com/gospider007/ja3" @@ -43,18 +41,12 @@ func getAddr(uurl *url.URL) (addr string) { } _, port, _ := net.SplitHostPort(uurl.Host) if port == "" { - bs := builderPool.Get().(*strings.Builder) - bs.WriteString(uurl.Host) - bs.WriteString(":") if uurl.Scheme == "https" { - bs.WriteString("443") + port = "443" } else { - bs.WriteString("80") + port = "80" } - addr = bs.String() - bs.Reset() - builderPool.Put(bs) - return + return fmt.Sprintf("%s:%s", uurl.Host, port) } return uurl.Host } @@ -131,18 +123,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err if r.Header.Get("Content-Length") == "" && shouldSendContentLength(r) { r.Header.Set("Content-Length", fmt.Sprint(r.ContentLength)) } - bs := builderPool.Get().(*strings.Builder) - defer func() { - bs.Reset() - builderPool.Put(bs) - }() - bs.WriteString(r.Method) - bs.WriteString(" ") - bs.WriteString(ruri) - bs.WriteString(" ") - bs.WriteString(r.Proto) - bs.WriteString("\r\n") - if _, err = w.WriteString(bs.String()); err != nil { + if _, err = w.WriteString(fmt.Sprintf("%s %s %s\r\n", r.Method, ruri, r.Proto)); err != nil { return err } for _, k := range orderHeaders { @@ -154,12 +135,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err continue } for _, v := range vs { - bs.Reset() - bs.WriteString(k) - bs.WriteString(": ") - bs.WriteString(v) - bs.WriteString("\r\n") - if _, err = w.WriteString(bs.String()); err != nil { + if _, err = w.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)); err != nil { return err } } @@ -174,13 +150,7 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err continue } for _, v := range vs { - - bs.Reset() - bs.WriteString(k) - bs.WriteString(": ") - bs.WriteString(v) - bs.WriteString("\r\n") - if _, err = w.WriteString(bs.String()); err != nil { + if _, err = w.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)); err != nil { return err } } @@ -197,18 +167,6 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err return w.Flush() } -var bufferPool sync.Pool -var builderPool sync.Pool - -func init() { - bufferPool.New = func() interface{} { - return bytes.NewBuffer(nil) - } - builderPool.New = func() interface{} { - return &strings.Builder{} - } -} - func newRequestWithContext(ctx context.Context, method string, u *url.URL, body io.Reader) (*http.Request, error) { req := (&http.Request{}).WithContext(ctx) if method == "" {