mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
37
conn.go
37
conn.go
@@ -70,7 +70,7 @@ func (obj *connecotr) httpReq(task *reqTask, done chan struct{}) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (obj *connecotr) taskMain(task *reqTask) (err error) {
|
||||
func (obj *connPool) taskMain(conn *connecotr, task *reqTask) (err error) {
|
||||
defer func() {
|
||||
if err != nil && task.reqCtx.option.ErrCallBack != nil {
|
||||
task.reqCtx.err = err
|
||||
@@ -80,12 +80,6 @@ func (obj *connecotr) taskMain(task *reqTask) (err error) {
|
||||
err = err2
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, errLastTaskRuning) {
|
||||
task.isNotice = true
|
||||
}
|
||||
obj.CloseWithError(tools.WrapError(err, "taskMain close with error"))
|
||||
}
|
||||
if err == nil {
|
||||
task.cnl(errNoErr)
|
||||
} else {
|
||||
@@ -93,30 +87,40 @@ func (obj *connecotr) taskMain(task *reqTask) (err error) {
|
||||
}
|
||||
if err == nil && task.reqCtx.response != nil && task.reqCtx.response.Body != nil {
|
||||
select {
|
||||
case <-obj.forceCtx.Done():
|
||||
err = context.Cause(obj.forceCtx)
|
||||
case <-conn.forceCtx.Done():
|
||||
err = context.Cause(conn.forceCtx)
|
||||
case <-task.ctx.Done():
|
||||
err = context.Cause(task.ctx)
|
||||
case <-task.bodyCtx.Done():
|
||||
if context.Cause(task.bodyCtx) != context.Canceled {
|
||||
if context.Cause(task.bodyCtx) != errNoErr {
|
||||
err = context.Cause(task.bodyCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, errLastTaskRuning) {
|
||||
task.isNotice = true
|
||||
}
|
||||
conn.CloseWithError(tools.WrapError(err, "taskMain close with error"))
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-obj.forceCtx.Done(): //force conn close
|
||||
err = context.Cause(obj.forceCtx)
|
||||
case <-conn.forceCtx.Done(): //force conn close
|
||||
err = context.Cause(conn.forceCtx)
|
||||
task.enableRetry = true
|
||||
task.isNotice = true
|
||||
return
|
||||
default:
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go obj.httpReq(task, done)
|
||||
go conn.httpReq(task, done)
|
||||
select {
|
||||
case <-obj.forceCtx.Done(): //force conn close
|
||||
err = tools.WrapError(context.Cause(obj.forceCtx), "taskMain delete ctx error: ")
|
||||
case <-conn.forceCtx.Done(): //force conn close
|
||||
err = tools.WrapError(context.Cause(conn.forceCtx), "taskMain delete ctx error: ")
|
||||
case <-time.After(task.reqCtx.option.ResponseHeaderTimeout):
|
||||
err = errors.New("ResponseHeaderTimeout error: ")
|
||||
case <-task.ctx.Done():
|
||||
err = context.Cause(task.ctx)
|
||||
case <-done:
|
||||
if task.reqCtx.response == nil {
|
||||
err = context.Cause(task.ctx)
|
||||
@@ -190,7 +194,8 @@ func (obj *connPool) rwMain(done chan struct{}, conn *connecotr) {
|
||||
if task == nil {
|
||||
return
|
||||
}
|
||||
if conn.taskMain(task) != nil {
|
||||
err := obj.taskMain(conn, task)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
6
go.mod
6
go.mod
@@ -10,9 +10,9 @@ require (
|
||||
github.com/gospider007/gtls v0.0.0-20250610060422-446e017b9858
|
||||
github.com/gospider007/http2 v0.0.0-20250616085600-a49f075f841e
|
||||
github.com/gospider007/http3 v0.0.0-20250625134649-90c214b46d1c
|
||||
github.com/gospider007/ja3 v0.0.0-20250626122029-44a230c1d67e
|
||||
github.com/gospider007/ja3 v0.0.0-20250627013834-1d2966014638
|
||||
github.com/gospider007/re v0.0.0-20250217075352-bcb79f285d6c
|
||||
github.com/gospider007/tools v0.0.0-20250623092633-4c8b251d2f4a
|
||||
github.com/gospider007/tools v0.0.0-20250627013901-3925ccd90381
|
||||
github.com/gospider007/websocket v0.0.0-20250429035144-b1cf6819063a
|
||||
github.com/klauspost/compress v1.18.0
|
||||
github.com/quic-go/quic-go v0.53.0
|
||||
@@ -43,7 +43,7 @@ require (
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/google/gopacket v1.1.19 // indirect
|
||||
github.com/google/pprof v0.0.0-20250607225305-033d6d78b36a // indirect
|
||||
github.com/google/pprof v0.0.0-20250629210550-e611ec304b22 // indirect
|
||||
github.com/gospider007/blog v0.0.0-20250302134054-8afc12c2a9a7 // indirect
|
||||
github.com/gospider007/kinds v0.0.0-20250217075226-10f199f7215d // indirect
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||
|
||||
12
go.sum
12
go.sum
@@ -99,8 +99,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
|
||||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
|
||||
github.com/google/pprof v0.0.0-20250607225305-033d6d78b36a h1://KbezygeMJZCSHH+HgUZiTeSoiuFspbMg1ge+eFj18=
|
||||
github.com/google/pprof v0.0.0-20250607225305-033d6d78b36a/go.mod h1:5hDyRhoBCxViHszMt12TnOpEI4VVi+U8Gm9iphldiMA=
|
||||
github.com/google/pprof v0.0.0-20250629210550-e611ec304b22 h1:RanZAubGQRlhKdX83NviyIduq4DsO2zFmSgPuTlnkMc=
|
||||
github.com/google/pprof v0.0.0-20250629210550-e611ec304b22/go.mod h1:5hDyRhoBCxViHszMt12TnOpEI4VVi+U8Gm9iphldiMA=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
|
||||
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
|
||||
@@ -118,14 +118,14 @@ github.com/gospider007/http2 v0.0.0-20250616085600-a49f075f841e h1:fO2FFvKS/BAX7
|
||||
github.com/gospider007/http2 v0.0.0-20250616085600-a49f075f841e/go.mod h1:Uz/rGu9oVkQmroHDeDSS1yhWqaLGHj5UbJdvAXu645E=
|
||||
github.com/gospider007/http3 v0.0.0-20250625134649-90c214b46d1c h1:1Dl1rNnqfK058bN0uZARvSEoVxE2f7EDVjtR6JusUkg=
|
||||
github.com/gospider007/http3 v0.0.0-20250625134649-90c214b46d1c/go.mod h1:w+rzup5IPPQROTZ096eRn7zAfTRgU3PmIqShPYJoaBg=
|
||||
github.com/gospider007/ja3 v0.0.0-20250626122029-44a230c1d67e h1:1VOK/9rPq7e+7YgG2EpoGhF0wtLNnjHSYvzRrzC4yrM=
|
||||
github.com/gospider007/ja3 v0.0.0-20250626122029-44a230c1d67e/go.mod h1:9Ps3Vpibr16aR/XaMACEr0pKoFAgyLQ2QgXdttik8OA=
|
||||
github.com/gospider007/ja3 v0.0.0-20250627013834-1d2966014638 h1:qQmKi3FsCK0WJcDOq4wpOzwVZ2UWto1Da05SGRVBKPQ=
|
||||
github.com/gospider007/ja3 v0.0.0-20250627013834-1d2966014638/go.mod h1:m62r83RcU8Dx7yyXNDnOfa8AfjPKAkOSzPZzgQRfP1k=
|
||||
github.com/gospider007/kinds v0.0.0-20250217075226-10f199f7215d h1:+Fih572EdNmYCK1L82MyWOj9tAwIgZ2JqepmasAclg8=
|
||||
github.com/gospider007/kinds v0.0.0-20250217075226-10f199f7215d/go.mod h1:3u6J+nbdWhCNJGiQjq/hKSukn2k6ebjd98rd2+fzKNo=
|
||||
github.com/gospider007/re v0.0.0-20250217075352-bcb79f285d6c h1:8/Cf+c2680tkWJ+ueZ9RLLK5R5R8nhE8pNBUPHjkvkM=
|
||||
github.com/gospider007/re v0.0.0-20250217075352-bcb79f285d6c/go.mod h1:dd8aDIUG1vDPP5r+vHBtiUK0Zn6uk3SsWt1ZvmCCHLs=
|
||||
github.com/gospider007/tools v0.0.0-20250623092633-4c8b251d2f4a h1:PibP4NH4MkSHE6Ix9FY6+lJXXN7Ezym0DWiEhf7Nkzk=
|
||||
github.com/gospider007/tools v0.0.0-20250623092633-4c8b251d2f4a/go.mod h1:x7fyDzsG7lPJO6Q3D6DIzdUA+J9RAQoyOdmcP1XQF1k=
|
||||
github.com/gospider007/tools v0.0.0-20250627013901-3925ccd90381 h1:07ui7z5YavmvAxVEkkhwpMng3vJ/6wIZRVuimM1M7V8=
|
||||
github.com/gospider007/tools v0.0.0-20250627013901-3925ccd90381/go.mod h1:xfS5GwtnDblCI/vbvA5epqjwWb+olFWygUVDSYV01f4=
|
||||
github.com/gospider007/websocket v0.0.0-20250429035144-b1cf6819063a h1:DJMk+oALIJg3ArN5/DIPrQagWQL1Qi/c3DlXT9l1dGo=
|
||||
github.com/gospider007/websocket v0.0.0-20250429035144-b1cf6819063a/go.mod h1:0yVYF7b5kRayyUzAVgg6h1x+eZujKoSaA4n2krG5F7g=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
|
||||
10
http.go
10
http.go
@@ -8,7 +8,6 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gospider007/tools"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
@@ -21,6 +20,7 @@ type reqReadWriteCtx struct {
|
||||
readCtx context.Context
|
||||
readCnl context.CancelCauseFunc
|
||||
}
|
||||
|
||||
type clientConn struct {
|
||||
err error
|
||||
readWriteCtx *reqReadWriteCtx
|
||||
@@ -68,12 +68,16 @@ func (obj *clientConn) send(req *http.Request, orderHeaders []interface {
|
||||
return
|
||||
}
|
||||
rawBody := res.Body
|
||||
isStream := res.StatusCode == 101 || strings.Contains(res.Header.Get("Content-Type"), "text/event-stream")
|
||||
isStream := res.StatusCode == 101
|
||||
pr, pw := io.Pipe()
|
||||
go func() {
|
||||
var readErr error
|
||||
defer func() {
|
||||
obj.readWriteCtx.readCnl(readErr)
|
||||
if readErr == nil {
|
||||
obj.readWriteCtx.readCnl(errNoErr)
|
||||
} else {
|
||||
obj.readWriteCtx.readCnl(readErr)
|
||||
}
|
||||
}()
|
||||
if rawBody != nil {
|
||||
_, readErr = tools.Copy(pw, rawBody)
|
||||
|
||||
@@ -321,7 +321,7 @@ func (obj *Response) ReadBody() (err error) {
|
||||
} else {
|
||||
obj.Close()
|
||||
if obj.response.StatusCode == 101 && obj.webSocket == nil {
|
||||
obj.webSocket = websocket.NewConn(newFakeConn(obj.body.connStream()), true, obj.Headers().Get("Sec-WebSocket-Extensions"))
|
||||
obj.webSocket = websocket.NewConn(newFakeConn(obj.body.connStream()), func() { obj.CloseConn() }, true, obj.Headers().Get("Sec-WebSocket-Extensions"))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user