From 68beb0fcbbe18d2dd9f2cb2a7919189a8504cd23 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 11 Jun 2024 02:21:57 +0800 Subject: [PATCH] optimize code --- helper/httptool/http.go | 45 +++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/helper/httptool/http.go b/helper/httptool/http.go index c928a24..6415070 100644 --- a/helper/httptool/http.go +++ b/helper/httptool/http.go @@ -165,49 +165,47 @@ func SetBody(req *http.Request, types int, form map[string]any) (err error) { } type BodyBuffer struct { - pointer int - Data *[]byte -} - -func (b *BodyBuffer) Reset() { - *b.Data = (*b.Data)[:0] - b.pointer = 0 + Offset int + Data *[]byte } func (b *BodyBuffer) Write(p []byte) (int, error) { - if b.pointer == 0 { + if b.Offset == 0 { copy(*b.Data, p) if len(p) <= len(*b.Data) { - b.pointer += len(p) + b.Offset += len(p) return len(p), nil } - b.pointer += len(*b.Data) + b.Offset += len(*b.Data) return len(*b.Data), nil } - if len(p)+b.pointer <= len(*b.Data) { - copy((*b.Data)[b.pointer:b.pointer+len(p)], p) - b.pointer += len(p) + if len(p)+b.Offset <= len(*b.Data) { + copy((*b.Data)[b.Offset:b.Offset+len(p)], p) + b.Offset += len(p) return len(p), nil } - l := len(*b.Data) - b.pointer + l := len(*b.Data) - b.Offset if l <= 0 { return 0, nil } - copy((*b.Data)[b.pointer:], p[:l]) + copy((*b.Data)[b.Offset:], p[:l]) return l, nil } func (b *BodyBuffer) Read(p []byte) (int, error) { - if len(p) <= len(*b.Data) { - copy(p, *b.Data) - *b.Data = (*b.Data)[len(p):] + data := (*b.Data)[b.Offset:] + if len(p) <= len(data) { + copy(p, data[0:len(p)]) + b.Offset += len(p) return len(p), nil } - if len(*b.Data) <= 0 { + if len(data) <= 0 { + b.Offset = 0 return 0, io.EOF } - copy(p, *b.Data) - return len(*b.Data), io.EOF + copy(p, data) + b.Offset = 0 + return len(data), io.EOF } func NewBodyBuffer(bytes *[]byte) BodyBuffer { @@ -225,7 +223,10 @@ func PostClient(u string, types int, form map[string]any, a ...any) (cli *http.C URL: parse, Header: http.Header{}, } - SetBody(req, types, form) + err = SetBody(req, types, form) + if err != nil { + return + } SetArgs(cli, req, a...) return }