CEF3 cannot recognize custom response header

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.

CEF3 cannot recognize custom response header

Postby frontier » Tue Jun 30, 2015 2:04 am

Hello

I try to let CEF application load local html file using custom resource handler.

When load local document with CefResourceHandler and custom headerMap, CEF does not change it's default charset to custom charset.
But when load remote document, CEF does change it's charset to http header's charset.

Does anybody knows why this happened?
Below is my implementations.
(My CEF version is 2171 build, but same problem occurred at master branch)

Code: Select all
CefRefPtr<CefResourceHandler> MyHandler::GetResourceHandler(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request) {
  CEF_REQUIRE_UI_THREAD();
  if (frame->IsMain() && request->GetURL().ToString() == request_url_) {
    return new MyResourceHandler(request_content_type_, request_mime_,  _html, _htmlsize);
  }
}

myhandler.cpp

Code: Select all
class MyResourceHandler : public CefResourceHandler {
  public:
  MyResourceHandler(std::string& content_type, std::string& mime_type, char* response_data, int response_size)
                : content_type_(content_type),
    mime_type_(mime_type),
    response_data_(response_data),
    response_size_(response_size),
    offset_(0)
  {
  }

  bool ProcessRequest(CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback) OVERRIDE {
    CefRequest::HeaderMap headerMap;
    request->GetHeaderMap(headerMap);
    headerMap.erase("Accept-Charset");
    headerMap.insert(std::make_pair("Accept-Charset", "UTF-8"));
    request->SetHeaderMap(headerMap);
    callback->Continue();
    return true;
  }

  void GetResponseHeaders(CefRefPtr<CefResponse> response, int64& response_length, CefString& redirectUrl) OVERRIDE {
    response->SetStatus(200);
    response->SetStatusText("OK");
    response->SetMimeType(mime_type_);

    CefResponse::HeaderMap headerMap;
    response->GetHeaderMap(headerMap);
    headerMap.erase("Content-Type");
    headerMap.insert(std::make_pair("Content-Type", content_type_)); // this headermap setting is ignored by browser. why???
    response->SetHeaderMap(headerMap);

    response_length = response_size_;
  }

  bool ReadResponse(void* response_data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefCallback> callback) OVERRIDE {
    bool has_data = false;
    size_t size = static_cast<size_t>(response_size_);
    if (offset_ < size) {
        int t_size = std::min(bytes_to_read, static_cast<int>(size - offset_));
        memcpy(response_data_out, response_data_ + offset_, t_size);
        offset_ += t_size;
        bytes_read = t_size;
        has_data = true;
    }
    return has_data;
  }

  void Cancel() OVERRIDE {
  }

  private:
    std::string content_type_;
    std::string mime_type_;
    char* response_data_;
    int response_size_;
    size_t offset_;

  IMPLEMENT_REFCOUNTING(MyResourceHandler);
};

myresourcehandler.cpp

Plz excuse my poor english.
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CEF3 cannot recognize custom response header

Postby Czarek » Wed Jul 01, 2015 1:24 pm

I've also had an issue with charset when using CefResourceHandler, looks like a bug in CEF, see this topic: viewtopic.php?p=18401#p18401
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: CEF3 cannot recognize custom response header

Postby frontier » Wed Jul 01, 2015 8:17 pm

Thank you, Czarek.
I think forced insert meta tag to html could be solution.
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CEF3 cannot recognize custom response header

Postby frontier » Wed Jul 01, 2015 9:28 pm

I solved it!

Insert <meta charset="..."> tag inside <head>..</head> works.
For this implementation, insertion point is very important.
When insert meta tag just after <head>, everything works find and CEF changes it's charset very well.
On the other hand, when insert meta tag just before </head>, html body's charset is changed but charset of resources(like js, css included in head tag) stay default.
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CEF3 cannot recognize custom response header

Postby Czarek » Sun May 29, 2016 12:33 am

Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 43 guests