CEF3: Add response filtering support

Issue #515 resolved
Marshall Greenblatt created an issue

Original issue 515 created by magreenblatt on 2012-02-13T19:14:14.000Z:

Add response filtering support. See http://magpcss.org/ceforum/viewtopic.php?f=10&t=645 for CEF3 information.

Comments (10)

  1. Former user Account Deleted

    Comment 1. originally posted by rrrado555 on 2013-04-01T19:26:04.000Z:

    It would be helpful to support changing of the MIME type sent to the browser. Maybe response->SetMimeType().
    Use case: response contains special data identified by special MIME type, for example "application/special1". I need to convert special data to HTML and display it transparently in the browser. It requires possibility to change response MIME type to "text/html", data conversion could be done using CefContentFilter.

  2. Dmitry Azaraev

    Comment 2. originally posted by fddima on 2013-04-01T19:55:45.000Z:

    @ commentcomment 1.: if i'm rightly remember, response filtering it is something about response rewriting thing (data). but i'm agree, that it is very cool to have handle to rewrite even headers.

    I.e. it will be cool to allow rewrite whole headers on response, and provide filter for data stream.

    This is just for clarification. Thanks!

  3. Marshall Greenblatt reporter

    Comment 3. originally posted by magreenblatt on 2013-06-27T20:31:36.000Z:

    Response headers can be viewed/changed via CefNetworkDelegate::OnHeadersReceived(). I'm not sure if there's a great way to filter response content -- it may be necessary to subclass URLRequestJob and return via CefRequestInterceptor::MaybeInterceptResponse().

  4. Former user Account Deleted

    Comment 4. originally posted by bandinfinite on 2013-09-05T09:47:48.000Z:

    Is this feature being investigated now? I badly need this in my project. If no one is working on this yet, I'd like to try provide my patch since I'm not busy recently. Though I haven't read CEF3's code in depth, I think this may be not quite complicate a task.

  5. Marshall Greenblatt reporter

    Comment 5. originally posted by magreenblatt on 2013-09-05T15:31:59.000Z:

    @ commentcomment 4.: Patches welcome.

  6. Former user Account Deleted

    Comment 6. originally posted by czarek.tomczak on 2013-09-12T08:16:54.000Z:

    There is a way around to accomplish this functionality using the existing api (GetResourceHandler and URLRequest/URLRequestClient), Marshall has explained
    it on the CEF Forum:

    There's no great way to filter response contents with CEF3 currently.   
    You can use CefResourceHandler via CefRequestHandler::GetResourceHandler   
    and execute the request/return the response contents yourself using   
    CefURLRequest.
    

    http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10794

  7. Former user Account Deleted

    Comment 7. originally posted by ytpete on 2014-03-24T21:37:41.000Z:

    Does the fix in issue #1098 make this any easier? At a glance, they seem related...

  8. rrrado2

    Comment 6. originally posted by czarek.tomczak on 2013-09-12T08:16:54.000Z: There is a way around to accomplish this functionality using the existing api (GetResourceHandler and URLRequest/URLRequestClient), Marshall has explained it on the CEF Forum: There's no great way to filter response contents with CEF3 currently.
    You can use CefResourceHandler via CefRequestHandler::GetResourceHandler
    and execute the request/return the response contents yourself using
    CefURLRequest. http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10794

    I've checked again and finally got it, it seems this is the way, thank you.

  9. Marshall Greenblatt reporter

    Master revision f207a55 and 2526 branch revision 456ea5a add a new CefRequestHandler::GetResourceResponseFilter method and CefResponseFilter class for response filtering.

  10. Andrey Masterov

    Error in the class CefResponseFilter (when a reply is cut beginning)

    // Filter that writes out all of the contents unchanged.
    class PassThruResponseFilter : public CefResponseFilter {
     public:
      PassThruResponseFilter(const std::string link, const std::string name): link_(link), name_(name) {}
    
      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 {
        DCHECK((data_in_size == 0U && !data_in) || (data_in_size > 0U && data_in));
        DCHECK_EQ(data_in_read, 0U);
        DCHECK(data_out);
        DCHECK_GT(data_out_size, 0U);
        DCHECK_EQ(data_out_written, 0U);
    
        // All data will be read.
        data_in_read = data_in_size;
    
        // Write out the contents unchanged.
        data_out_written = std::min(data_in_read, data_out_size);
    
        if (data_out_written > 0) {
          memcpy(data_out, data_in, data_out_written);
    
          std::string s; s.resize(data_out_written);
          memcpy(&s[0], data_out, data_out_written);
    
          // Not copy full data in some files !!!
          std::ofstream file(name_); file << data_out_written << s;
        }
    
        return RESPONSE_FILTER_DONE;
      }
    
      private:
        std::string link_; std::string name_;
        IMPLEMENT_REFCOUNTING(PassThruResponseFilter);
    };
    

    in some files

  11. Log in to comment