mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
3
dial.go
3
dial.go
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gospider007/gtls"
|
||||
"github.com/gospider007/http1"
|
||||
"github.com/gospider007/ja3"
|
||||
"github.com/gospider007/tools"
|
||||
utls "github.com/refraction-networking/utls"
|
||||
@@ -547,7 +548,7 @@ func (obj *Dialer) clientVerifyHttps(ctx context.Context, conn net.Conn, proxyAd
|
||||
}
|
||||
connectReq.Header = hdr
|
||||
connectReq.Host = remoteAddress.Host
|
||||
if err = connectReq.Write(conn); err != nil {
|
||||
if err = http1.HttpWrite(connectReq, bufio.NewWriter(conn), connectReq.Header.Clone(), nil); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := http.ReadResponse(bufio.NewReader(conn), connectReq)
|
||||
|
||||
14
go.mod
14
go.mod
@@ -7,15 +7,15 @@ require (
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/gospider007/bar v0.0.0-20250217074946-47896d8de2ba
|
||||
github.com/gospider007/bs4 v0.0.0-20250413121342-fed910fb00c9
|
||||
github.com/gospider007/gson v0.0.0-20250630120534-cce6e3c6756d
|
||||
github.com/gospider007/gtls v0.0.0-20250630120509-4e99c91661ee
|
||||
github.com/gospider007/http1 v0.0.0-20250717080213-6dc69bcb2a33
|
||||
github.com/gospider007/http2 v0.0.0-20250711035043-daabc8e205b3
|
||||
github.com/gospider007/http3 v0.0.0-20250711035127-be7539ebc56a
|
||||
github.com/gospider007/gson v0.0.0-20250718004537-ff15820964bd
|
||||
github.com/gospider007/gtls v0.0.0-20250718003831-90cdeb97a23f
|
||||
github.com/gospider007/http1 v0.0.0-20250718004641-26f982c140cf
|
||||
github.com/gospider007/http2 v0.0.0-20250718004700-7af5b064e352
|
||||
github.com/gospider007/http3 v0.0.0-20250718004757-02ceb5fa2d6e
|
||||
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-20250711035142-241d6041da43
|
||||
github.com/gospider007/websocket v0.0.0-20250630120328-1ec26253d082
|
||||
github.com/gospider007/tools v0.0.0-20250718003809-7c62cd0ee920
|
||||
github.com/gospider007/websocket v0.0.0-20250718010025-4c017acfd478
|
||||
github.com/klauspost/compress v1.18.0
|
||||
github.com/minio/minlz v1.0.1
|
||||
github.com/quic-go/quic-go v0.53.0
|
||||
|
||||
28
go.sum
28
go.sum
@@ -112,26 +112,26 @@ github.com/gospider007/blog v0.0.0-20250302134054-8afc12c2a9a7 h1:QP46FdP6nJET+b
|
||||
github.com/gospider007/blog v0.0.0-20250302134054-8afc12c2a9a7/go.mod h1:yDzKXJzaYENfMvleqwdo362NE+BXDhwtKXRUQUWHH+I=
|
||||
github.com/gospider007/bs4 v0.0.0-20250413121342-fed910fb00c9 h1:V/Fuzox1GNZzlZCY548Na3mdfjLmWoSqexa53d3dKyE=
|
||||
github.com/gospider007/bs4 v0.0.0-20250413121342-fed910fb00c9/go.mod h1:KBnaNWpVupRQbPZ6jz2jhX/hnRK1w30RKJvl3Micjws=
|
||||
github.com/gospider007/gson v0.0.0-20250630120534-cce6e3c6756d h1:/FaI2cPp/DgiEJ+39N0vGIKySI97fgP/ft18QovS7Io=
|
||||
github.com/gospider007/gson v0.0.0-20250630120534-cce6e3c6756d/go.mod h1:Vb5Ehy5UGQYVUW2XvzqLW1rc7/WPljQspyF3YZ/u164=
|
||||
github.com/gospider007/gtls v0.0.0-20250630120509-4e99c91661ee h1:rcQAnk5Szv/Vi9dVieU1uwKUSVPNB+lqRPggI6oZgFM=
|
||||
github.com/gospider007/gtls v0.0.0-20250630120509-4e99c91661ee/go.mod h1:ee+Fo1W5Abt9mdLOOiTrb76r0qbO6ubGmRDklCMAPIQ=
|
||||
github.com/gospider007/http1 v0.0.0-20250717080213-6dc69bcb2a33 h1:VNzJJZ/Yw72/NffOeYk61+OoMaRsrvC68aRGoT4u0QQ=
|
||||
github.com/gospider007/http1 v0.0.0-20250717080213-6dc69bcb2a33/go.mod h1:HFJILuzw8YEWk3SGHwHhymbUeF4LpuGAuzOFUHaSV58=
|
||||
github.com/gospider007/http2 v0.0.0-20250711035043-daabc8e205b3 h1:HqEs4ZAU1U7hGKwEQwE/0YYwldByzaD/YfXpSrhgdVA=
|
||||
github.com/gospider007/http2 v0.0.0-20250711035043-daabc8e205b3/go.mod h1:rji8qgw9KJ3ddOqT7xvgd0GKV7Z4+l1uSbFrgDX3blg=
|
||||
github.com/gospider007/http3 v0.0.0-20250711035127-be7539ebc56a h1:qeKaaZlrWddcvuWWv0pbT77L5oxlurYGoR96wiyS9lc=
|
||||
github.com/gospider007/http3 v0.0.0-20250711035127-be7539ebc56a/go.mod h1:4Og1NRXDGkmcoMwKfeG9vihSNkU/uNnQ3QxsxWRuWJ8=
|
||||
github.com/gospider007/gson v0.0.0-20250718004537-ff15820964bd h1:aby4HnAGVJt5pIGKkct3V4lac/5Uq4h0bjIluG4rmak=
|
||||
github.com/gospider007/gson v0.0.0-20250718004537-ff15820964bd/go.mod h1:GxCATDh+u/TLHTAI9p1kXfaGUkVNjHuY+Mhxdo4l5k8=
|
||||
github.com/gospider007/gtls v0.0.0-20250718003831-90cdeb97a23f h1:W/ug9EHRcduL40RNtsKR9Ob0VnMQF/pps6iLTmorhck=
|
||||
github.com/gospider007/gtls v0.0.0-20250718003831-90cdeb97a23f/go.mod h1:iTnRK0DU3YH7MlZZ9VdT5gQAFPKzHRtFi3EXpnosOAM=
|
||||
github.com/gospider007/http1 v0.0.0-20250718004641-26f982c140cf h1:PUyHtMJ/mvyb0nBsgwMmEnZH0fqJabPMlC9fJ1Ks3R4=
|
||||
github.com/gospider007/http1 v0.0.0-20250718004641-26f982c140cf/go.mod h1:GqV2VRFBHYl6Ovir4gnGnuUeELdw8Klg2BRVdHapeVg=
|
||||
github.com/gospider007/http2 v0.0.0-20250718004700-7af5b064e352 h1:E+Gy49dUc4WWfHA+ZA6BvueQ3MeimLJ4dptT+dk2rvg=
|
||||
github.com/gospider007/http2 v0.0.0-20250718004700-7af5b064e352/go.mod h1:TNUBmnegjmQMAdjdZqpqGvSJ1mpikVGYIAtBVvpfqhE=
|
||||
github.com/gospider007/http3 v0.0.0-20250718004757-02ceb5fa2d6e h1:kNwZM1/FnNLk2MASfk0VPdYk/nOR/wu+YrWlTixhYSM=
|
||||
github.com/gospider007/http3 v0.0.0-20250718004757-02ceb5fa2d6e/go.mod h1:Tf9Ncwbiro76j6KdO8PmLyo+YwOfilBlHB5YD2NvgaE=
|
||||
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-20250711035142-241d6041da43 h1:dZnCK7rDn8MBU2vqN7ELfG9rGAzdxuPlPVBZuEnzw2o=
|
||||
github.com/gospider007/tools v0.0.0-20250711035142-241d6041da43/go.mod h1:i0V5k/zJW/ghTLLWY40pwTVuwa/Gxm2Q1IP1vQYHBX8=
|
||||
github.com/gospider007/websocket v0.0.0-20250630120328-1ec26253d082 h1:0UAc9x1HqsNn+XDTZEcGS/d93dRO0AHryh9KMCtq9LQ=
|
||||
github.com/gospider007/websocket v0.0.0-20250630120328-1ec26253d082/go.mod h1:UiboIV1XnPhEMEd4yXPJAGO/ykHh/fG5nsJRDqpuaEw=
|
||||
github.com/gospider007/tools v0.0.0-20250718003809-7c62cd0ee920 h1:WZof+Xg/syAFRGHTPa9Cct6OLrtx8wHcAgGOC6ggFT4=
|
||||
github.com/gospider007/tools v0.0.0-20250718003809-7c62cd0ee920/go.mod h1:i0V5k/zJW/ghTLLWY40pwTVuwa/Gxm2Q1IP1vQYHBX8=
|
||||
github.com/gospider007/websocket v0.0.0-20250718010025-4c017acfd478 h1:tVIWVD9oHdmIIA506SQ7qbx4vTlckxzk/JKhuDCzkHw=
|
||||
github.com/gospider007/websocket v0.0.0-20250718010025-4c017acfd478/go.mod h1:okAxHf/CcV7d+iCl9dYRpe1rIyaOcR/DxIDj2KAzFDk=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
|
||||
@@ -145,7 +145,7 @@ func (obj *Client) retryRequest(ctx context.Context, option RequestOption, uhref
|
||||
if err != nil || loc == nil {
|
||||
return
|
||||
}
|
||||
response.Close(nil)
|
||||
response.CloseBody(nil)
|
||||
switch response.StatusCode() {
|
||||
case 307, 308:
|
||||
if response.Option().readOne {
|
||||
@@ -251,10 +251,9 @@ func (obj *Client) request(ctx *Response) (err error) {
|
||||
}
|
||||
//init ctx,cnl
|
||||
if ctx.option.Timeout > 0 { //超时
|
||||
ctx.ctx, _ = context.WithTimeout(ctx.ctx, ctx.option.Timeout)
|
||||
ctx.ctx, ctx.cnl = context.WithCancelCause(ctx.ctx)
|
||||
ctx.ctx, ctx.cnl = context.WithTimeout(ctx.ctx, ctx.option.Timeout)
|
||||
} else {
|
||||
ctx.ctx, ctx.cnl = context.WithCancelCause(ctx.Context())
|
||||
ctx.ctx, ctx.cnl = context.WithCancel(ctx.Context())
|
||||
}
|
||||
var isWebsocket bool
|
||||
//init Scheme
|
||||
|
||||
16
response.go
16
response.go
@@ -63,7 +63,7 @@ type Response struct {
|
||||
response *http.Response
|
||||
webSocket *websocket.Conn
|
||||
sse *SSE
|
||||
cnl context.CancelCauseFunc
|
||||
cnl context.CancelFunc
|
||||
option *RequestOption
|
||||
client *Client
|
||||
encoding string
|
||||
@@ -320,23 +320,23 @@ func (obj *Response) Proxys() []Address {
|
||||
|
||||
// close
|
||||
func (obj *Response) CloseConn() {
|
||||
obj.Close(errors.New("response close conn"))
|
||||
if obj.body != nil {
|
||||
obj.body.CloseWithError(errors.New("force close conn"))
|
||||
obj.body.CloseConn()
|
||||
}
|
||||
obj.cnl()
|
||||
}
|
||||
|
||||
// close
|
||||
func (obj *Response) Close(err error) {
|
||||
func (obj *Response) CloseBody(err error) {
|
||||
if err == nil {
|
||||
err = tools.ErrNoErr
|
||||
} else {
|
||||
obj.cnl()
|
||||
}
|
||||
if obj.body != nil {
|
||||
obj.body.CloseWithError(err)
|
||||
}
|
||||
if obj.cnl != nil {
|
||||
obj.cnl(err)
|
||||
}
|
||||
}
|
||||
|
||||
// read body
|
||||
@@ -424,11 +424,11 @@ func (obj *body) CloseWithError(err error) error {
|
||||
if obj.ctx.StatusCode() == 101 && obj.ctx.webSocket == nil {
|
||||
obj.ctx.webSocket = websocket.NewConn(newFakeConn(obj.ctx.body.connStream()), func() { obj.ctx.CloseConn() }, true, obj.ctx.Headers().Get("Sec-WebSocket-Extensions"))
|
||||
}
|
||||
obj.ctx.Close(nil)
|
||||
obj.ctx.CloseBody(nil)
|
||||
return nil
|
||||
} else {
|
||||
obj.err = err
|
||||
obj.ctx.Close(err)
|
||||
obj.ctx.CloseBody(err)
|
||||
obj.ctx.CloseConn()
|
||||
return err
|
||||
}
|
||||
|
||||
50
test/request/redirect_test.go
Normal file
50
test/request/redirect_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestRedirectBreakWitRequest(t *testing.T) {
|
||||
href := "https://httpbin.co/absolute-redirect/2"
|
||||
resp, err := requests.Get(nil, href, requests.RequestOption{
|
||||
ClientOption: requests.ClientOption{
|
||||
RequestCallBack: func(ctx *requests.Response) error {
|
||||
if ctx.Response() != nil {
|
||||
return requests.ErrUseLastResponse
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 302 {
|
||||
t.Fatal("resp.StatusCode()!= 302")
|
||||
}
|
||||
}
|
||||
func TestRedirectWithRequest(t *testing.T) {
|
||||
href := "https://httpbin.co/absolute-redirect/2"
|
||||
n := 0
|
||||
resp, err := requests.Get(nil, href, requests.RequestOption{
|
||||
ClientOption: requests.ClientOption{
|
||||
RequestCallBack: func(ctx *requests.Response) error {
|
||||
if ctx.Response() == nil {
|
||||
n++
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 3 {
|
||||
t.Fatal("n != 3")
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("resp.StatusCode()!= 200")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user