fetch.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Package fetch provides the Chrome DevTools Protocol
  2. // commands, types, and events for the Fetch domain.
  3. //
  4. // A domain for letting clients substitute browser's network layer with
  5. // client code.
  6. //
  7. // Generated by the cdproto-gen command.
  8. package fetch
  9. // Code generated by cdproto-gen. DO NOT EDIT.
  10. import (
  11. "context"
  12. "encoding/base64"
  13. "github.com/chromedp/cdproto/cdp"
  14. "github.com/chromedp/cdproto/io"
  15. "github.com/chromedp/cdproto/network"
  16. )
  17. // DisableParams disables the fetch domain.
  18. type DisableParams struct{}
  19. // Disable disables the fetch domain.
  20. //
  21. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-disable
  22. func Disable() *DisableParams {
  23. return &DisableParams{}
  24. }
  25. // Do executes Fetch.disable against the provided context.
  26. func (p *DisableParams) Do(ctx context.Context) (err error) {
  27. return cdp.Execute(ctx, CommandDisable, nil, nil)
  28. }
  29. // EnableParams enables issuing of requestPaused events. A request will be
  30. // paused until client calls one of failRequest, fulfillRequest or
  31. // continueRequest/continueWithAuth.
  32. type EnableParams struct {
  33. Patterns []*RequestPattern `json:"patterns,omitempty,omitzero"` // 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.
  34. HandleAuthRequests bool `json:"handleAuthRequests"` // If true, authRequired events will be issued and requests will be paused expecting a call to continueWithAuth.
  35. }
  36. // Enable enables issuing of requestPaused events. A request will be paused
  37. // until client calls one of failRequest, fulfillRequest or
  38. // continueRequest/continueWithAuth.
  39. //
  40. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-enable
  41. //
  42. // parameters:
  43. func Enable() *EnableParams {
  44. return &EnableParams{
  45. HandleAuthRequests: false,
  46. }
  47. }
  48. // WithPatterns if specified, only requests matching any of these patterns
  49. // will produce fetchRequested event and will be paused until clients response.
  50. // If not set, all requests will be affected.
  51. func (p EnableParams) WithPatterns(patterns []*RequestPattern) *EnableParams {
  52. p.Patterns = patterns
  53. return &p
  54. }
  55. // WithHandleAuthRequests if true, authRequired events will be issued and
  56. // requests will be paused expecting a call to continueWithAuth.
  57. func (p EnableParams) WithHandleAuthRequests(handleAuthRequests bool) *EnableParams {
  58. p.HandleAuthRequests = handleAuthRequests
  59. return &p
  60. }
  61. // Do executes Fetch.enable against the provided context.
  62. func (p *EnableParams) Do(ctx context.Context) (err error) {
  63. return cdp.Execute(ctx, CommandEnable, p, nil)
  64. }
  65. // FailRequestParams causes the request to fail with specified reason.
  66. type FailRequestParams struct {
  67. RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
  68. ErrorReason network.ErrorReason `json:"errorReason"` // Causes the request to fail with the given reason.
  69. }
  70. // FailRequest causes the request to fail with specified reason.
  71. //
  72. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-failRequest
  73. //
  74. // parameters:
  75. //
  76. // requestID - An id the client received in requestPaused event.
  77. // errorReason - Causes the request to fail with the given reason.
  78. func FailRequest(requestID RequestID, errorReason network.ErrorReason) *FailRequestParams {
  79. return &FailRequestParams{
  80. RequestID: requestID,
  81. ErrorReason: errorReason,
  82. }
  83. }
  84. // Do executes Fetch.failRequest against the provided context.
  85. func (p *FailRequestParams) Do(ctx context.Context) (err error) {
  86. return cdp.Execute(ctx, CommandFailRequest, p, nil)
  87. }
  88. // FulfillRequestParams provides response to the request.
  89. type FulfillRequestParams struct {
  90. RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
  91. ResponseCode int64 `json:"responseCode"` // An HTTP response code.
  92. ResponseHeaders []*HeaderEntry `json:"responseHeaders,omitempty,omitzero"` // Response headers.
  93. BinaryResponseHeaders string `json:"binaryResponseHeaders,omitempty,omitzero"` // 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.
  94. Body string `json:"body,omitempty,omitzero"` // 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.
  95. ResponsePhrase string `json:"responsePhrase,omitempty,omitzero"` // A textual representation of responseCode. If absent, a standard phrase matching responseCode is used.
  96. }
  97. // FulfillRequest provides response to the request.
  98. //
  99. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-fulfillRequest
  100. //
  101. // parameters:
  102. //
  103. // requestID - An id the client received in requestPaused event.
  104. // responseCode - An HTTP response code.
  105. func FulfillRequest(requestID RequestID, responseCode int64) *FulfillRequestParams {
  106. return &FulfillRequestParams{
  107. RequestID: requestID,
  108. ResponseCode: responseCode,
  109. }
  110. }
  111. // WithResponseHeaders response headers.
  112. func (p FulfillRequestParams) WithResponseHeaders(responseHeaders []*HeaderEntry) *FulfillRequestParams {
  113. p.ResponseHeaders = responseHeaders
  114. return &p
  115. }
  116. // WithBinaryResponseHeaders alternative way of specifying response headers
  117. // as a \0-separated series of name: value pairs. Prefer the above method unless
  118. // you need to represent some non-UTF8 values that can't be transmitted over the
  119. // protocol as text.
  120. func (p FulfillRequestParams) WithBinaryResponseHeaders(binaryResponseHeaders string) *FulfillRequestParams {
  121. p.BinaryResponseHeaders = binaryResponseHeaders
  122. return &p
  123. }
  124. // WithBody a response body. If absent, original response body will be used
  125. // if the request is intercepted at the response stage and empty body will be
  126. // used if the request is intercepted at the request stage.
  127. func (p FulfillRequestParams) WithBody(body string) *FulfillRequestParams {
  128. p.Body = body
  129. return &p
  130. }
  131. // WithResponsePhrase a textual representation of responseCode. If absent, a
  132. // standard phrase matching responseCode is used.
  133. func (p FulfillRequestParams) WithResponsePhrase(responsePhrase string) *FulfillRequestParams {
  134. p.ResponsePhrase = responsePhrase
  135. return &p
  136. }
  137. // Do executes Fetch.fulfillRequest against the provided context.
  138. func (p *FulfillRequestParams) Do(ctx context.Context) (err error) {
  139. return cdp.Execute(ctx, CommandFulfillRequest, p, nil)
  140. }
  141. // ContinueRequestParams continues the request, optionally modifying some of
  142. // its parameters.
  143. type ContinueRequestParams struct {
  144. RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
  145. URL string `json:"url,omitempty,omitzero"` // If set, the request url will be modified in a way that's not observable by page.
  146. Method string `json:"method,omitempty,omitzero"` // If set, the request method is overridden.
  147. PostData string `json:"postData,omitempty,omitzero"` // If set, overrides the post data in the request.
  148. Headers []*HeaderEntry `json:"headers,omitempty,omitzero"` // 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.
  149. InterceptResponse bool `json:"interceptResponse"` // If set, overrides response interception behavior for this request.
  150. }
  151. // ContinueRequest continues the request, optionally modifying some of its
  152. // parameters.
  153. //
  154. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueRequest
  155. //
  156. // parameters:
  157. //
  158. // requestID - An id the client received in requestPaused event.
  159. func ContinueRequest(requestID RequestID) *ContinueRequestParams {
  160. return &ContinueRequestParams{
  161. RequestID: requestID,
  162. InterceptResponse: false,
  163. }
  164. }
  165. // WithURL if set, the request url will be modified in a way that's not
  166. // observable by page.
  167. func (p ContinueRequestParams) WithURL(url string) *ContinueRequestParams {
  168. p.URL = url
  169. return &p
  170. }
  171. // WithMethod if set, the request method is overridden.
  172. func (p ContinueRequestParams) WithMethod(method string) *ContinueRequestParams {
  173. p.Method = method
  174. return &p
  175. }
  176. // WithPostData if set, overrides the post data in the request.
  177. func (p ContinueRequestParams) WithPostData(postData string) *ContinueRequestParams {
  178. p.PostData = postData
  179. return &p
  180. }
  181. // WithHeaders if set, overrides the request headers. Note that the overrides
  182. // do not extend to subsequent redirect hops, if a redirect happens. Another
  183. // override may be applied to a different request produced by a redirect.
  184. func (p ContinueRequestParams) WithHeaders(headers []*HeaderEntry) *ContinueRequestParams {
  185. p.Headers = headers
  186. return &p
  187. }
  188. // WithInterceptResponse if set, overrides response interception behavior for
  189. // this request.
  190. func (p ContinueRequestParams) WithInterceptResponse(interceptResponse bool) *ContinueRequestParams {
  191. p.InterceptResponse = interceptResponse
  192. return &p
  193. }
  194. // Do executes Fetch.continueRequest against the provided context.
  195. func (p *ContinueRequestParams) Do(ctx context.Context) (err error) {
  196. return cdp.Execute(ctx, CommandContinueRequest, p, nil)
  197. }
  198. // ContinueWithAuthParams continues a request supplying authChallengeResponse
  199. // following authRequired event.
  200. type ContinueWithAuthParams struct {
  201. RequestID RequestID `json:"requestId"` // An id the client received in authRequired event.
  202. AuthChallengeResponse *AuthChallengeResponse `json:"authChallengeResponse"` // Response to with an authChallenge.
  203. }
  204. // ContinueWithAuth continues a request supplying authChallengeResponse
  205. // following authRequired event.
  206. //
  207. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueWithAuth
  208. //
  209. // parameters:
  210. //
  211. // requestID - An id the client received in authRequired event.
  212. // authChallengeResponse - Response to with an authChallenge.
  213. func ContinueWithAuth(requestID RequestID, authChallengeResponse *AuthChallengeResponse) *ContinueWithAuthParams {
  214. return &ContinueWithAuthParams{
  215. RequestID: requestID,
  216. AuthChallengeResponse: authChallengeResponse,
  217. }
  218. }
  219. // Do executes Fetch.continueWithAuth against the provided context.
  220. func (p *ContinueWithAuthParams) Do(ctx context.Context) (err error) {
  221. return cdp.Execute(ctx, CommandContinueWithAuth, p, nil)
  222. }
  223. // ContinueResponseParams continues loading of the paused response,
  224. // optionally modifying the response headers. If either responseCode or headers
  225. // are modified, all of them must be present.
  226. type ContinueResponseParams struct {
  227. RequestID RequestID `json:"requestId"` // An id the client received in requestPaused event.
  228. ResponseCode int64 `json:"responseCode,omitempty,omitzero"` // An HTTP response code. If absent, original response code will be used.
  229. ResponsePhrase string `json:"responsePhrase,omitempty,omitzero"` // A textual representation of responseCode. If absent, a standard phrase matching responseCode is used.
  230. ResponseHeaders []*HeaderEntry `json:"responseHeaders,omitempty,omitzero"` // Response headers. If absent, original response headers will be used.
  231. BinaryResponseHeaders string `json:"binaryResponseHeaders,omitempty,omitzero"` // 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.
  232. }
  233. // ContinueResponse continues loading of the paused response, optionally
  234. // modifying the response headers. If either responseCode or headers are
  235. // modified, all of them must be present.
  236. //
  237. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueResponse
  238. //
  239. // parameters:
  240. //
  241. // requestID - An id the client received in requestPaused event.
  242. func ContinueResponse(requestID RequestID) *ContinueResponseParams {
  243. return &ContinueResponseParams{
  244. RequestID: requestID,
  245. }
  246. }
  247. // WithResponseCode an HTTP response code. If absent, original response code
  248. // will be used.
  249. func (p ContinueResponseParams) WithResponseCode(responseCode int64) *ContinueResponseParams {
  250. p.ResponseCode = responseCode
  251. return &p
  252. }
  253. // WithResponsePhrase a textual representation of responseCode. If absent, a
  254. // standard phrase matching responseCode is used.
  255. func (p ContinueResponseParams) WithResponsePhrase(responsePhrase string) *ContinueResponseParams {
  256. p.ResponsePhrase = responsePhrase
  257. return &p
  258. }
  259. // WithResponseHeaders response headers. If absent, original response headers
  260. // will be used.
  261. func (p ContinueResponseParams) WithResponseHeaders(responseHeaders []*HeaderEntry) *ContinueResponseParams {
  262. p.ResponseHeaders = responseHeaders
  263. return &p
  264. }
  265. // WithBinaryResponseHeaders alternative way of specifying response headers
  266. // as a \0-separated series of name: value pairs. Prefer the above method unless
  267. // you need to represent some non-UTF8 values that can't be transmitted over the
  268. // protocol as text.
  269. func (p ContinueResponseParams) WithBinaryResponseHeaders(binaryResponseHeaders string) *ContinueResponseParams {
  270. p.BinaryResponseHeaders = binaryResponseHeaders
  271. return &p
  272. }
  273. // Do executes Fetch.continueResponse against the provided context.
  274. func (p *ContinueResponseParams) Do(ctx context.Context) (err error) {
  275. return cdp.Execute(ctx, CommandContinueResponse, p, nil)
  276. }
  277. // GetResponseBodyParams causes the body of the response to be received from
  278. // the server and returned as a single string. May only be issued for a request
  279. // that is paused in the Response stage and is mutually exclusive with
  280. // takeResponseBodyForInterceptionAsStream. Calling other methods that affect
  281. // the request or disabling fetch domain before body is received results in an
  282. // undefined behavior. Note that the response body is not available for
  283. // redirects. Requests paused in the _redirect received_ state may be
  284. // differentiated by responseCode and presence of location response header, see
  285. // comments to requestPaused for details.
  286. type GetResponseBodyParams struct {
  287. RequestID RequestID `json:"requestId"` // Identifier for the intercepted request to get body for.
  288. }
  289. // GetResponseBody causes the body of the response to be received from the
  290. // server and returned as a single string. May only be issued for a request that
  291. // is paused in the Response stage and is mutually exclusive with
  292. // takeResponseBodyForInterceptionAsStream. Calling other methods that affect
  293. // the request or disabling fetch domain before body is received results in an
  294. // undefined behavior. Note that the response body is not available for
  295. // redirects. Requests paused in the _redirect received_ state may be
  296. // differentiated by responseCode and presence of location response header, see
  297. // comments to requestPaused for details.
  298. //
  299. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-getResponseBody
  300. //
  301. // parameters:
  302. //
  303. // requestID - Identifier for the intercepted request to get body for.
  304. func GetResponseBody(requestID RequestID) *GetResponseBodyParams {
  305. return &GetResponseBodyParams{
  306. RequestID: requestID,
  307. }
  308. }
  309. // GetResponseBodyReturns return values.
  310. type GetResponseBodyReturns struct {
  311. Body string `json:"body,omitempty,omitzero"` // Response body.
  312. Base64encoded bool `json:"base64Encoded"` // True, if content was sent as base64.
  313. }
  314. // Do executes Fetch.getResponseBody against the provided context.
  315. //
  316. // returns:
  317. //
  318. // body - Response body.
  319. func (p *GetResponseBodyParams) Do(ctx context.Context) (body []byte, err error) {
  320. // execute
  321. var res GetResponseBodyReturns
  322. err = cdp.Execute(ctx, CommandGetResponseBody, p, &res)
  323. if err != nil {
  324. return nil, err
  325. }
  326. // decode
  327. var dec []byte
  328. if res.Base64encoded {
  329. dec, err = base64.StdEncoding.DecodeString(res.Body)
  330. if err != nil {
  331. return nil, err
  332. }
  333. } else {
  334. dec = []byte(res.Body)
  335. }
  336. return dec, nil
  337. }
  338. // TakeResponseBodyAsStreamParams returns a handle to the stream representing
  339. // the response body. The request must be paused in the HeadersReceived stage.
  340. // Note that after this command the request can't be continued as is -- client
  341. // either needs to cancel it or to provide the response body. The stream only
  342. // supports sequential read, IO.read will fail if the position is specified.
  343. // This method is mutually exclusive with getResponseBody. Calling other methods
  344. // that affect the request or disabling fetch domain before body is received
  345. // results in an undefined behavior.
  346. type TakeResponseBodyAsStreamParams struct {
  347. RequestID RequestID `json:"requestId"`
  348. }
  349. // TakeResponseBodyAsStream returns a handle to the stream representing the
  350. // response body. The request must be paused in the HeadersReceived stage. Note
  351. // that after this command the request can't be continued as is -- client either
  352. // needs to cancel it or to provide the response body. The stream only supports
  353. // sequential read, IO.read will fail if the position is specified. This method
  354. // is mutually exclusive with getResponseBody. Calling other methods that affect
  355. // the request or disabling fetch domain before body is received results in an
  356. // undefined behavior.
  357. //
  358. // See: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-takeResponseBodyAsStream
  359. //
  360. // parameters:
  361. //
  362. // requestID
  363. func TakeResponseBodyAsStream(requestID RequestID) *TakeResponseBodyAsStreamParams {
  364. return &TakeResponseBodyAsStreamParams{
  365. RequestID: requestID,
  366. }
  367. }
  368. // TakeResponseBodyAsStreamReturns return values.
  369. type TakeResponseBodyAsStreamReturns struct {
  370. Stream io.StreamHandle `json:"stream,omitempty,omitzero"`
  371. }
  372. // Do executes Fetch.takeResponseBodyAsStream against the provided context.
  373. //
  374. // returns:
  375. //
  376. // stream
  377. func (p *TakeResponseBodyAsStreamParams) Do(ctx context.Context) (stream io.StreamHandle, err error) {
  378. // execute
  379. var res TakeResponseBodyAsStreamReturns
  380. err = cdp.Execute(ctx, CommandTakeResponseBodyAsStream, p, &res)
  381. if err != nil {
  382. return "", err
  383. }
  384. return res.Stream, nil
  385. }
  386. // Command names.
  387. const (
  388. CommandDisable = "Fetch.disable"
  389. CommandEnable = "Fetch.enable"
  390. CommandFailRequest = "Fetch.failRequest"
  391. CommandFulfillRequest = "Fetch.fulfillRequest"
  392. CommandContinueRequest = "Fetch.continueRequest"
  393. CommandContinueWithAuth = "Fetch.continueWithAuth"
  394. CommandContinueResponse = "Fetch.continueResponse"
  395. CommandGetResponseBody = "Fetch.getResponseBody"
  396. CommandTakeResponseBodyAsStream = "Fetch.takeResponseBodyAsStream"
  397. )