Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read and modify web response using GetResourceResponseFilter callback #229

Open
cztomczak opened this issue Jun 24, 2016 · 2 comments
Open

Comments

@cztomczak
Copy link
Owner

cztomczak commented Jun 24, 2016

Currently to read web response you have to implement ResourceHandler with the use of WebRequest object and WebRequestClient interface that allows reading/modifying web requests: see the wxpython-response.py example in the cefpython31 branch.

Upstream CEF has now exposed new API, when you only need to alter request you can do it easily using GetResourceResponseFilter. The complexity of the wxpython-response.py example that implements ResourceHandler would be no more required for such case. The new callback available in upstream:

+  ///
+  // Called on the IO thread to optionally filter resource response content.
+  // |request| and |response| represent the request and response respectively
+  // and cannot be modified in this callback.
+  ///
+  /*--cef()--*/
+  virtual CefRefPtr<CefResponseFilter> GetResourceResponseFilter(
+      CefRefPtr<CefBrowser> browser,
+      CefRefPtr<CefFrame> frame,
+      CefRefPtr<CefRequest> request,
+      CefRefPtr<CefResponse> response) {
+    return NULL;
+  }

Filter response body with CefResponseFilter::Filter:

  ///
  // Called to filter a chunk of data. |data_in| is the input buffer containing
  // |data_in_size| bytes of pre-filter data (|data_in| will be NULL if
  // |data_in_size| is zero). |data_out| is the output buffer that can accept up
  // to |data_out_size| bytes of filtered output data. Set |data_in_read| to the
  // number of bytes that were read from |data_in|. Set |data_out_written| to
  // the number of bytes that were written into |data_out|. If some or all of
  // the pre-filter data was read successfully but more data is needed in order
  // to continue filtering (filtered output is pending) return
  // RESPONSE_FILTER_NEED_MORE_DATA. If some or all of the pre-filter data was
  // read successfully and all available filtered output has been written return
  // RESPONSE_FILTER_DONE. If an error occurs during filtering return
  // RESPONSE_FILTER_ERROR. This method will be called repeatedly until there is
  // no more data to filter (resource response is complete), |data_in_read|
  // matches |data_in_size| (all available pre-filter bytes have been read), and
  // the method returns RESPONSE_FILTER_DONE or RESPONSE_FILTER_ERROR. Do not
  // keep a reference to the buffers passed to this method.
  ///
  /*--cef(optional_param=data_in,default_retval=RESPONSE_FILTER_ERROR)--*/
  virtual FilterStatus Filter(void* data_in,
                              size_t data_in_size,
                              size_t& data_in_read,
                              void* data_out,
                              size_t data_out_size,
                              size_t& data_out_written) =0;

There was also added OnResourceResponse, but it doesn't allow altering response:

+  // Called on the IO thread when a resource response is received. To allow the
+  // resource to load normally return false. To redirect or retry the resource
+  // modify |request| (url, headers or post body) and return true. The
+  // |response| object cannot be modified in this callback.
+  ///
+  /*--cef()--*/
+  virtual bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
+                                  CefRefPtr<CefFrame> frame,
+                                  CefRefPtr<CefRequest> request,
+                                  CefRefPtr<CefResponse> response) {
+    return false;
+  }

Also a bit related, new OnResourceLoadComplete callback:

+  ///
+  // Called on the IO thread when a resource load has completed. |request| and
+  // |response| represent the request and response respectively and cannot be
+  // modified in this callback. |status| indicates the load completion status.
+  // |received_content_length| is the number of response bytes actually read.
+  ///
+  /*--cef()--*/
+  virtual void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser,
+                                      CefRefPtr<CefFrame> frame,
+                                      CefRefPtr<CefRequest> request,
+                                      CefRefPtr<CefResponse> response,
+                                      URLRequestStatus status,
+                                      int64 received_content_length) {}
@cztomczak cztomczak changed the title wxpython-response.py and OnResourceResponse availability in upstream wxpython-response.py and GetResourceResponseFilter availability in upstream Jun 24, 2016
@cztomczak cztomczak moved this from wxPython to General in Refactor examples Feb 21, 2017
@cztomczak cztomczak changed the title wxpython-response.py and GetResourceResponseFilter availability in upstream Read web response contents using GetResourceResponseFilter callback May 26, 2017
@zhengtong0898
Copy link

how to use the new api of the GetResourceResponseFilter or the OnResourceResponse callback ?
cefpython version is 57.1;
platform is Windows 10;
wxpython-response.py is too complexity for me and 'image/*' data render incorrect.

is there anyone give me a example?

@cztomczak
Copy link
Owner Author

@zhengtong0898 This issue is Open meaning it is not yet implemented.

There is also this gist based on wxpython-response.py that uses Python's network library:
https://gist.github.com/yattamax/0252a3c5dc54a2f81650d5c0eafabf99

@cztomczak cztomczak changed the title Read web response contents using GetResourceResponseFilter callback Read and modify web response using GetResourceResponseFilter callback Aug 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Refactor examples
  
General
Development

No branches or pull requests

2 participants