forked from chromedp/chromedp
/
fetch.go
326 lines (286 loc) · 12.4 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Package fetch provides the Chrome DevTools Protocol
// commands, types, and events for the Fetch domain.
//
// A domain for letting clients substitute browser's network layer with
// client code.
//
// Generated by the cdproto-gen command.
package fetch
// Code generated by cdproto-gen. DO NOT EDIT.
import (
"context"
"encoding/base64"
"bitbucket.org/ShipwrightTibi/chromecrawlingnew/cdproto/cdp"
"bitbucket.org/ShipwrightTibi/chromecrawlingnew/cdproto/io"
"bitbucket.org/ShipwrightTibi/chromecrawlingnew/cdproto/network"
)
// DisableParams disables the fetch domain.
type DisableParams struct{}
// Disable disables the fetch domain.
func Disable() *DisableParams {
return &DisableParams{}
}
// Do executes Fetch.disable against the provided context.
func (p *DisableParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandDisable, nil, nil)
}
// EnableParams enables issuing of requestPaused events. A request will be
// paused until client calls one of failRequest, fulfillRequest or
// continueRequest/continueWithAuth.
type EnableParams struct {
Patterns []*RequestPattern `json:"patterns,omitempty"` // If specified, only requests matching any of these patterns will produce fetchRequested event and will be paused until clients response. If not set, all requests will be affected.
HandleAuthRequests bool `json:"handleAuthRequests,omitempty"` // If true, authRequired events will be issued and requests will be paused expecting a call to continueWithAuth.
}
// Enable enables issuing of requestPaused events. A request will be paused
// until client calls one of failRequest, fulfillRequest or
// continueRequest/continueWithAuth.
//
// parameters:
func Enable() *EnableParams {
return &EnableParams{}
}
// WithPatterns if specified, only requests matching any of these patterns
// will produce fetchRequested event and will be paused until clients response.
// If not set, all requests will be affected.
func (p EnableParams) WithPatterns(patterns []*RequestPattern) *EnableParams {
p.Patterns = patterns
return &p
}
// WithHandleAuthRequests if true, authRequired events will be issued and
// requests will be paused expecting a call to continueWithAuth.
func (p EnableParams) WithHandleAuthRequests(handleAuthRequests bool) *EnableParams {
p.HandleAuthRequests = handleAuthRequests
return &p
}
// Do executes Fetch.enable against the provided context.
func (p *EnableParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandEnable, p, nil)
}
// FailRequestParams causes the request to fail with specified reason.
type FailRequestParams struct {
RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
ErrorReason network.ErrorReason `json:"errorReason"` // Causes the request to fail with the given reason.
}
// FailRequest causes the request to fail with specified reason.
//
// parameters:
// requestID - An id the client received in requestPaused event.
// errorReason - Causes the request to fail with the given reason.
func FailRequest(requestID RequestID, errorReason network.ErrorReason) *FailRequestParams {
return &FailRequestParams{
RequestID: requestID,
ErrorReason: errorReason,
}
}
// Do executes Fetch.failRequest against the provided context.
func (p *FailRequestParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandFailRequest, p, nil)
}
// FulfillRequestParams provides response to the request.
type FulfillRequestParams struct {
RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
ResponseCode int64 `json:"responseCode"` // An HTTP response code.
ResponseHeaders []*HeaderEntry `json:"responseHeaders"` // Response headers.
Body string `json:"body,omitempty"` // A response body.
ResponsePhrase string `json:"responsePhrase,omitempty"` // A textual representation of responseCode. If absent, a standard phrase mathcing responseCode is used.
}
// FulfillRequest provides response to the request.
//
// parameters:
// requestID - An id the client received in requestPaused event.
// responseCode - An HTTP response code.
// responseHeaders - Response headers.
func FulfillRequest(requestID RequestID, responseCode int64, responseHeaders []*HeaderEntry) *FulfillRequestParams {
return &FulfillRequestParams{
RequestID: requestID,
ResponseCode: responseCode,
ResponseHeaders: responseHeaders,
}
}
// WithBody a response body.
func (p FulfillRequestParams) WithBody(body string) *FulfillRequestParams {
p.Body = body
return &p
}
// WithResponsePhrase a textual representation of responseCode. If absent, a
// standard phrase mathcing responseCode is used.
func (p FulfillRequestParams) WithResponsePhrase(responsePhrase string) *FulfillRequestParams {
p.ResponsePhrase = responsePhrase
return &p
}
// Do executes Fetch.fulfillRequest against the provided context.
func (p *FulfillRequestParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandFulfillRequest, p, nil)
}
// ContinueRequestParams continues the request, optionally modifying some of
// its parameters.
type ContinueRequestParams struct {
RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
URL string `json:"url,omitempty"` // If set, the request url will be modified in a way that's not observable by page.
Method string `json:"method,omitempty"` // If set, the request method is overridden.
PostData string `json:"postData,omitempty"` // If set, overrides the post data in the request.
Headers []*HeaderEntry `json:"headers,omitempty"` // If set, overrides the request headrts.
}
// ContinueRequest continues the request, optionally modifying some of its
// parameters.
//
// parameters:
// requestID - An id the client received in requestPaused event.
func ContinueRequest(requestID RequestID) *ContinueRequestParams {
return &ContinueRequestParams{
RequestID: requestID,
}
}
// WithURL if set, the request url will be modified in a way that's not
// observable by page.
func (p ContinueRequestParams) WithURL(url string) *ContinueRequestParams {
p.URL = url
return &p
}
// WithMethod if set, the request method is overridden.
func (p ContinueRequestParams) WithMethod(method string) *ContinueRequestParams {
p.Method = method
return &p
}
// WithPostData if set, overrides the post data in the request.
func (p ContinueRequestParams) WithPostData(postData string) *ContinueRequestParams {
p.PostData = postData
return &p
}
// WithHeaders if set, overrides the request headrts.
func (p ContinueRequestParams) WithHeaders(headers []*HeaderEntry) *ContinueRequestParams {
p.Headers = headers
return &p
}
// Do executes Fetch.continueRequest against the provided context.
func (p *ContinueRequestParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandContinueRequest, p, nil)
}
// ContinueWithAuthParams continues a request supplying authChallengeResponse
// following authRequired event.
type ContinueWithAuthParams struct {
RequestID RequestID `json:"requestId"` // An id the client received in authRequired event.
AuthChallengeResponse *AuthChallengeResponse `json:"authChallengeResponse"` // Response to with an authChallenge.
}
// ContinueWithAuth continues a request supplying authChallengeResponse
// following authRequired event.
//
// parameters:
// requestID - An id the client received in authRequired event.
// authChallengeResponse - Response to with an authChallenge.
func ContinueWithAuth(requestID RequestID, authChallengeResponse *AuthChallengeResponse) *ContinueWithAuthParams {
return &ContinueWithAuthParams{
RequestID: requestID,
AuthChallengeResponse: authChallengeResponse,
}
}
// Do executes Fetch.continueWithAuth against the provided context.
func (p *ContinueWithAuthParams) Do(ctxt context.Context, h cdp.Executor) (err error) {
return h.Execute(ctxt, CommandContinueWithAuth, p, nil)
}
// GetResponseBodyParams causes the body of the response to be received from
// the server and returned as a single string. May only be issued for a request
// that is paused in the Response stage and is mutually exclusive with
// takeResponseBodyForInterceptionAsStream. Calling other methods that affect
// the request or disabling fetch domain before body is received results in an
// undefined behavior.
type GetResponseBodyParams struct {
RequestID RequestID `json:"requestId"` // Identifier for the intercepted request to get body for.
}
// GetResponseBody causes the body of the response to be received from the
// server and returned as a single string. May only be issued for a request that
// is paused in the Response stage and is mutually exclusive with
// takeResponseBodyForInterceptionAsStream. Calling other methods that affect
// the request or disabling fetch domain before body is received results in an
// undefined behavior.
//
// parameters:
// requestID - Identifier for the intercepted request to get body for.
func GetResponseBody(requestID RequestID) *GetResponseBodyParams {
return &GetResponseBodyParams{
RequestID: requestID,
}
}
// GetResponseBodyReturns return values.
type GetResponseBodyReturns struct {
Body string `json:"body,omitempty"` // Response body.
Base64encoded bool `json:"base64Encoded,omitempty"` // True, if content was sent as base64.
}
// Do executes Fetch.getResponseBody against the provided context.
//
// returns:
// body - Response body.
func (p *GetResponseBodyParams) Do(ctxt context.Context, h cdp.Executor) (body []byte, err error) {
// execute
var res GetResponseBodyReturns
err = h.Execute(ctxt, CommandGetResponseBody, p, &res)
if err != nil {
return nil, err
}
// decode
var dec []byte
if res.Base64encoded {
dec, err = base64.StdEncoding.DecodeString(res.Body)
if err != nil {
return nil, err
}
} else {
dec = []byte(res.Body)
}
return dec, nil
}
// TakeResponseBodyAsStreamParams returns a handle to the stream representing
// the response body. The request must be paused in the HeadersReceived stage.
// Note that after this command the request can't be continued as is -- client
// either needs to cancel it or to provide the response body. The stream only
// supports sequential read, IO.read will fail if the position is specified.
// This method is mutually exclusive with getResponseBody. Calling other methods
// that affect the request or disabling fetch domain before body is received
// results in an undefined behavior.
type TakeResponseBodyAsStreamParams struct {
RequestID RequestID `json:"requestId"`
}
// TakeResponseBodyAsStream returns a handle to the stream representing the
// response body. The request must be paused in the HeadersReceived stage. Note
// that after this command the request can't be continued as is -- client either
// needs to cancel it or to provide the response body. The stream only supports
// sequential read, IO.read will fail if the position is specified. This method
// is mutually exclusive with getResponseBody. Calling other methods that affect
// the request or disabling fetch domain before body is received results in an
// undefined behavior.
//
// parameters:
// requestID
func TakeResponseBodyAsStream(requestID RequestID) *TakeResponseBodyAsStreamParams {
return &TakeResponseBodyAsStreamParams{
RequestID: requestID,
}
}
// TakeResponseBodyAsStreamReturns return values.
type TakeResponseBodyAsStreamReturns struct {
Stream io.StreamHandle `json:"stream,omitempty"`
}
// Do executes Fetch.takeResponseBodyAsStream against the provided context.
//
// returns:
// stream
func (p *TakeResponseBodyAsStreamParams) Do(ctxt context.Context, h cdp.Executor) (stream io.StreamHandle, err error) {
// execute
var res TakeResponseBodyAsStreamReturns
err = h.Execute(ctxt, CommandTakeResponseBodyAsStream, p, &res)
if err != nil {
return "", err
}
return res.Stream, nil
}
// Command names.
const (
CommandDisable = "Fetch.disable"
CommandEnable = "Fetch.enable"
CommandFailRequest = "Fetch.failRequest"
CommandFulfillRequest = "Fetch.fulfillRequest"
CommandContinueRequest = "Fetch.continueRequest"
CommandContinueWithAuth = "Fetch.continueWithAuth"
CommandGetResponseBody = "Fetch.getResponseBody"
CommandTakeResponseBodyAsStream = "Fetch.takeResponseBodyAsStream"
)