This commit is contained in:
gospider
2024-09-12 15:22:26 +08:00
parent 72d0c76a0e
commit 86485a4596
3 changed files with 32 additions and 13 deletions

View File

@@ -140,7 +140,7 @@ func (obj *OrderMap) parseForm(ctx context.Context) (io.Reader, string, bool, er
if err != nil {
return nil, writer.FormDataContentType(), false, err
}
return body, writer.FormDataContentType(), false, err
return bytes.NewReader(body.Bytes()), writer.FormDataContentType(), false, err
}
func (obj *OrderMap) isformPip() bool {
if len(obj.keys) == 0 || len(obj.data) == 0 {

View File

@@ -150,8 +150,10 @@ func (obj *connecotr) taskMain(task *reqTask, waitBody bool) (retry bool) {
} else if task.err != nil {
if task.req.Body == nil {
retry = true
} else if body, ok := task.req.Body.(*requestBody); ok && !body.ok {
retry = true
} else if body, ok := task.req.Body.(*requestBody); ok {
if body.Seek(0, io.SeekStart); !body.readed {
retry = true
}
}
obj.CloseWithError(task.err)
}

View File

@@ -238,17 +238,38 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
}
type requestBody struct {
r io.ReadCloser
ok bool
r io.Reader
readed bool
closed bool
}
func (obj *requestBody) Read(p []byte) (n int, err error) {
obj.ok = true
obj.readed = true
return obj.r.Read(p)
}
func (obj *requestBody) Close() (err error) {
obj.ok = true
return obj.r.Close()
obj.closed = true
obj.readed = true
if closer, ok := obj.r.(io.Closer); ok {
return closer.Close()
}
return nil
}
func (obj *requestBody) Seek(offset int64, whence int) (int64, error) {
if obj.closed {
return 0, fmt.Errorf("unsupported operation: closed")
}
if !obj.readed {
return 0, nil
}
if seeker, ok := obj.r.(io.Seeker); ok {
i, err := seeker.Seek(offset, whence)
if i == 0 && err == nil {
obj.readed = false
}
return i, err
}
return 0, fmt.Errorf("unsupported operation: seeker")
}
func NewRequestWithContext(ctx context.Context, method string, u *url.URL, body io.Reader) (*http.Request, error) {
@@ -268,11 +289,7 @@ func NewRequestWithContext(ctx context.Context, method string, u *url.URL, body
if v, ok := body.(interface{ Len() int }); ok {
req.ContentLength = int64(v.Len())
}
rc, ok := body.(io.ReadCloser)
if !ok {
rc = io.NopCloser(body)
}
req.Body = &requestBody{r: rc}
req.Body = &requestBody{r: body}
}
return req, nil
}