fetch.go 19 KB

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