Example cefclient. How to get the response body.

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Example cefclient. How to get the response body.

Postby maxktutby » Thu Apr 09, 2020 4:48 am

I am taking the first steps to use CEF in Windows.
My task:
1. Track a specific url when the operator is working in the browser window
2. Analyze the content of the response body sent by the server before it is uploaded to the window.

I solve the first step in the
Code: Select all
ClientHandler::OnBeforeBrowse(...)
and even determine the size of the body by analyzing the value of the receivedcontent_length variable in the
Code: Select all
ClientHandler::OnResourceLoadComplete(...)
.

However, I can't figure out how to get the actual content of the response body sent by the server.

Thank you very much in advance & best regards!
maxktutby
Newbie
 
Posts: 5
Joined: Sun Dec 23, 2018 4:32 pm

Re: Example cefclient. How to get the response body.

Postby charles » Thu Apr 09, 2020 7:02 am

You need to implement a custom CefResponseFilter (there are some examples in the cef sample apps) and store the contents of the received response into some sort of buffer. Once OnResourceLoadComplete is called, it's safe to access the stored buffer data.

The easiest filter example to start from is likely the one that just copies over the data from the in into the out buffer.
charles
Techie
 
Posts: 15
Joined: Wed Apr 01, 2015 6:59 am

Re: Example cefclient. How to get the response body.

Postby maxktutby » Thu Apr 09, 2020 10:40 am

Thanks.
The hint worked. To complete the topic, I present the working code of the corrected ClientHandler::GetResourceResponseFilter(...)
Code: Select all
CefRefPtr<CefResponseFilter> ClientHandler::GetResourceResponseFilter(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame,
    CefRefPtr<CefRequest> request,
    CefRefPtr<CefResponse> response) {
  CEF_REQUIRE_IO_THREAD();

  class TestingTheFilter : public CefResponseFilter {
  public:
     TestingTheFilter() {}
     bool InitFilter() OVERRIDE { return true; }
     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) OVERRIDE {
        const size_t max_write = std::min(data_in_size, data_out_size);
        memcpy(data_out, data_in, max_write);
        data_out_size = max_write;
        data_out_written = data_in_size;
        LOG(INFO) << "The data here:" << std::string((char *)data_out);
        data_in_read = data_in_size;
        return RESPONSE_FILTER_DONE;
     }
  private:
     IMPLEMENT_REFCOUNTING(TestingTheFilter);
  };

  const std::string& url = request->GetURL();
  const std::string url_testing = "Here is the address of the required site";

  if (url == url_testing)
   return new TestingTheFilter();
  else
   return test_runner::GetResourceResponseFilter(browser, frame, request,
                                                response);
}
maxktutby
Newbie
 
Posts: 5
Joined: Sun Dec 23, 2018 4:32 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 37 guests