-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfetch.go
123 lines (118 loc) · 3.03 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cdp
import (
"context"
"net/http"
"github.com/gospider007/tools"
)
func (obj *WebSock) FetchRequestEnable(preCtx context.Context) (RecvData, error) {
return obj.send(preCtx, commend{
Method: "Fetch.enable",
Params: map[string]any{
"patterns": []map[string]any{
{
"requestStage": "Request",
},
},
},
})
}
func (obj *WebSock) FetchDisable(preCtx context.Context) (RecvData, error) {
return obj.send(preCtx, commend{
Method: "Fetch.disable",
})
}
func (obj *WebSock) FetchContinueRequest(preCtx context.Context, requestId string, options ...RequestOption) (RecvData, error) {
var option RequestOption
if len(options) > 0 {
option = options[0]
}
params := map[string]any{
"requestId": requestId,
}
if option.Headers != nil {
headers := []struct {
Name string `json:"name"`
Value string `json:"value"`
}{}
for hk, hvs := range option.Headers {
for _, hv := range hvs {
headers = append(headers, struct {
Name string "json:\"name\""
Value string "json:\"value\""
}{
Name: hk,
Value: hv,
})
}
}
params["headers"] = headers
}
if option.Url != "" {
params["url"] = option.Url
}
if option.Method != "" {
params["method"] = option.Method
}
if option.PostData != "" {
params["postData"] = tools.Base64Encode(option.PostData)
}
return obj.send(preCtx, commend{
Method: "Fetch.continueRequest",
Params: params,
})
}
func (obj *WebSock) FetchFailRequest(preCtx context.Context, requestId, errorReason string) (RecvData, error) {
return obj.send(preCtx, commend{
Method: "Fetch.failRequest",
Params: map[string]any{
"requestId": requestId,
"errorReason": errorReason,
},
})
}
func (obj *WebSock) FetchGetResponseBody(preCtx context.Context, requestId string) (RecvData, error) {
return obj.send(preCtx, commend{
Method: "Fetch.getResponseBody",
Params: map[string]any{
"requestId": requestId,
},
})
}
func (obj *WebSock) FetchFulfillRequest(preCtx context.Context, requestId string, fulData FulData) (RecvData, error) {
if fulData.Headers == nil {
fulData.Headers = http.Header{
"Content-Type": []string{http.DetectContentType(tools.StringToBytes(fulData.Body))},
}
}
headers := []struct {
Name string `json:"name"`
Value string `json:"value"`
}{}
for hk, hvs := range fulData.Headers {
for _, hv := range hvs {
headers = append(headers, struct {
Name string "json:\"name\""
Value string "json:\"value\""
}{
Name: hk,
Value: hv,
})
}
}
if fulData.StatusCode == 0 {
fulData.StatusCode = 200
}
if fulData.ResponsePhrase == "" {
fulData.ResponsePhrase = "200 OK"
}
return obj.send(preCtx, commend{
Method: "Fetch.fulfillRequest",
Params: map[string]any{
"requestId": requestId,
"responseCode": fulData.StatusCode,
"responseHeaders": headers,
"body": tools.Base64Encode(fulData.Body),
"responsePhrase": fulData.ResponsePhrase,
},
})
}