mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
2
body.go
2
body.go
@@ -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 {
|
||||
|
||||
6
conn.go
6
conn.go
@@ -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)
|
||||
}
|
||||
|
||||
37
tools.go
37
tools.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user