File: fetch.go

package info (click to toggle)
golang-github-chromedp-cdproto 0.0~git20230109.6b041c6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,584 kB
  • sloc: makefile: 2
file content (438 lines) | stat: -rw-r--r-- 18,475 bytes parent folder | download | duplicates (3)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// 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"

	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/io"
	"github.com/chromedp/cdproto/network"
)

// DisableParams disables the fetch domain.
type DisableParams struct{}

// Disable disables the fetch domain.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-disable
func Disable() *DisableParams {
	return &DisableParams{}
}

// Do executes Fetch.disable against the provided context.
func (p *DisableParams) Do(ctx context.Context) (err error) {
	return cdp.Execute(ctx, 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.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-enable
//
// 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(ctx context.Context) (err error) {
	return cdp.Execute(ctx, 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.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-failRequest
//
// 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(ctx context.Context) (err error) {
	return cdp.Execute(ctx, 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,omitempty"`       // Response headers.
	BinaryResponseHeaders string         `json:"binaryResponseHeaders,omitempty"` // Alternative way of specifying response headers as a \0-separated series of name: value pairs. Prefer the above method unless you need to represent some non-UTF8 values that can't be transmitted over the protocol as text.
	Body                  string         `json:"body,omitempty"`                  // A response body. If absent, original response body will be used if the request is intercepted at the response stage and empty body will be used if the request is intercepted at the request stage.
	ResponsePhrase        string         `json:"responsePhrase,omitempty"`        // A textual representation of responseCode. If absent, a standard phrase matching responseCode is used.
}

// FulfillRequest provides response to the request.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-fulfillRequest
//
// parameters:
//
//	requestID - An id the client received in requestPaused event.
//	responseCode - An HTTP response code.
func FulfillRequest(requestID RequestID, responseCode int64) *FulfillRequestParams {
	return &FulfillRequestParams{
		RequestID:    requestID,
		ResponseCode: responseCode,
	}
}

// WithResponseHeaders response headers.
func (p FulfillRequestParams) WithResponseHeaders(responseHeaders []*HeaderEntry) *FulfillRequestParams {
	p.ResponseHeaders = responseHeaders
	return &p
}

// WithBinaryResponseHeaders alternative way of specifying response headers
// as a \0-separated series of name: value pairs. Prefer the above method unless
// you need to represent some non-UTF8 values that can't be transmitted over the
// protocol as text.
func (p FulfillRequestParams) WithBinaryResponseHeaders(binaryResponseHeaders string) *FulfillRequestParams {
	p.BinaryResponseHeaders = binaryResponseHeaders
	return &p
}

// WithBody a response body. If absent, original response body will be used
// if the request is intercepted at the response stage and empty body will be
// used if the request is intercepted at the request stage.
func (p FulfillRequestParams) WithBody(body string) *FulfillRequestParams {
	p.Body = body
	return &p
}

// WithResponsePhrase a textual representation of responseCode. If absent, a
// standard phrase matching 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(ctx context.Context) (err error) {
	return cdp.Execute(ctx, 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 headers. Note that the overrides do not extend to subsequent redirect hops, if a redirect happens. Another override may be applied to a different request produced by a redirect.
	InterceptResponse bool           `json:"interceptResponse,omitempty"` // If set, overrides response interception behavior for this request.
}

// ContinueRequest continues the request, optionally modifying some of its
// parameters.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueRequest
//
// 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 headers. Note that the overrides
// do not extend to subsequent redirect hops, if a redirect happens. Another
// override may be applied to a different request produced by a redirect.
func (p ContinueRequestParams) WithHeaders(headers []*HeaderEntry) *ContinueRequestParams {
	p.Headers = headers
	return &p
}

// WithInterceptResponse if set, overrides response interception behavior for
// this request.
func (p ContinueRequestParams) WithInterceptResponse(interceptResponse bool) *ContinueRequestParams {
	p.InterceptResponse = interceptResponse
	return &p
}

// Do executes Fetch.continueRequest against the provided context.
func (p *ContinueRequestParams) Do(ctx context.Context) (err error) {
	return cdp.Execute(ctx, 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.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueWithAuth
//
// 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(ctx context.Context) (err error) {
	return cdp.Execute(ctx, CommandContinueWithAuth, p, nil)
}

// ContinueResponseParams continues loading of the paused response,
// optionally modifying the response headers. If either responseCode or headers
// are modified, all of them must be present.
type ContinueResponseParams struct {
	RequestID             RequestID      `json:"requestId"`                       // An id the client received in requestPaused event.
	ResponseCode          int64          `json:"responseCode,omitempty"`          // An HTTP response code. If absent, original response code will be used.
	ResponsePhrase        string         `json:"responsePhrase,omitempty"`        // A textual representation of responseCode. If absent, a standard phrase matching responseCode is used.
	ResponseHeaders       []*HeaderEntry `json:"responseHeaders,omitempty"`       // Response headers. If absent, original response headers will be used.
	BinaryResponseHeaders string         `json:"binaryResponseHeaders,omitempty"` // Alternative way of specifying response headers as a \0-separated series of name: value pairs. Prefer the above method unless you need to represent some non-UTF8 values that can't be transmitted over the protocol as text.
}

// ContinueResponse continues loading of the paused response, optionally
// modifying the response headers. If either responseCode or headers are
// modified, all of them must be present.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueResponse
//
// parameters:
//
//	requestID - An id the client received in requestPaused event.
func ContinueResponse(requestID RequestID) *ContinueResponseParams {
	return &ContinueResponseParams{
		RequestID: requestID,
	}
}

// WithResponseCode an HTTP response code. If absent, original response code
// will be used.
func (p ContinueResponseParams) WithResponseCode(responseCode int64) *ContinueResponseParams {
	p.ResponseCode = responseCode
	return &p
}

// WithResponsePhrase a textual representation of responseCode. If absent, a
// standard phrase matching responseCode is used.
func (p ContinueResponseParams) WithResponsePhrase(responsePhrase string) *ContinueResponseParams {
	p.ResponsePhrase = responsePhrase
	return &p
}

// WithResponseHeaders response headers. If absent, original response headers
// will be used.
func (p ContinueResponseParams) WithResponseHeaders(responseHeaders []*HeaderEntry) *ContinueResponseParams {
	p.ResponseHeaders = responseHeaders
	return &p
}

// WithBinaryResponseHeaders alternative way of specifying response headers
// as a \0-separated series of name: value pairs. Prefer the above method unless
// you need to represent some non-UTF8 values that can't be transmitted over the
// protocol as text.
func (p ContinueResponseParams) WithBinaryResponseHeaders(binaryResponseHeaders string) *ContinueResponseParams {
	p.BinaryResponseHeaders = binaryResponseHeaders
	return &p
}

// Do executes Fetch.continueResponse against the provided context.
func (p *ContinueResponseParams) Do(ctx context.Context) (err error) {
	return cdp.Execute(ctx, CommandContinueResponse, 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.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-getResponseBody
//
// 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(ctx context.Context) (body []byte, err error) {
	// execute
	var res GetResponseBodyReturns
	err = cdp.Execute(ctx, 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.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-takeResponseBodyAsStream
//
// 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(ctx context.Context) (stream io.StreamHandle, err error) {
	// execute
	var res TakeResponseBodyAsStreamReturns
	err = cdp.Execute(ctx, 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"
	CommandContinueResponse         = "Fetch.continueResponse"
	CommandGetResponseBody          = "Fetch.getResponseBody"
	CommandTakeResponseBodyAsStream = "Fetch.takeResponseBodyAsStream"
)