#include "custom_resource_handler.h"
#include "include/cef_parser.h"
#include <regex>
CustomResourceHandler::CustomResourceHandler() {
}
bool CustomResourceHandler::Open(CefRefPtr<CefRequest> request,
bool& handle_request,
CefRefPtr<CefCallback> callback) {
CefString url = request->GetURL();
cef_uri_unescape_rule_t unescape_rule = UU_SPACES;
CefString& decoded_value = CefURIDecode(url, true, unescape_rule);
CefURLParts urlParts;
if (CefParseURL(url, urlParts)) {
CefString filePath(&urlParts.path);
std::wstring wfilepath = CefURIDecode(CefString(&urlParts.query), true, unescape_rule);
cef_string_userfree_utf8_t output = cef_string_userfree_utf8_alloc();
cef_string_utf16_to_utf8(filePath.c_str(), filePath.length(), output);
std::string filepath(output->str, output->length);
cef_string_userfree_utf8_free(output);
//std::regex pattern("//resources");
//std::string fulpath = std::regex_replace(filepath, pattern, "./resources");
//std::wstring str = std::regex_replace(output, pattern, L"");
//CefRefPtr<CefStreamReader> fileStream = CefStreamReader::CreateForFile(CefString(filePath));
//fileStream->Read()
std::string::size_type pos = filepath.rfind('.');
extension_ = filepath.substr(pos == std::string::npos ? filepath.length() : pos + 1);
char filename_c[256];
snprintf(filename_c, sizeof(filename_c), "%s", ("./resources"+filepath).c_str());
FILE* pf = fopen(filename_c, "r");
if (pf) {
readpos_ = 0;
datalen_ = fread(databuf_, sizeof(char), sizeof(databuf_), pf);
fclose(pf);
callback->Continue();
return true;
}
}
return false;
}
void CustomResourceHandler::GetResponseHeaders(CefRefPtr<CefResponse> response,
int64_t& response_length,
CefString& redirectUrl) {
CefString mimeType = CefGetMimeType(extension_);
response->SetMimeType(mimeType);
response->SetStatus(200);
//response->SetHeaderByName("Access-Control-Allow-Origin", "*", true);
//response->SetHeaderByName("Access-Control-Allow-Methods", "POST,PUT,GET,OPTIONS,DELETE", true);
response_length = datalen_;
}
void CustomResourceHandler::Cancel() {
// Cancel the response...
}
bool CustomResourceHandler::Read(void* data_out,
int bytes_to_read,
int& bytes_read,
CefRefPtr<CefResourceReadCallback> callback) {
int remain = datalen_ - readpos_;
int readlen = bytes_to_read > remain ? remain : bytes_to_read;
memcpy(data_out, databuf_ + readpos_, readlen);
readpos_ += readlen;
bytes_read = readlen;
return remain > 0 ? true : false;
}
serve content in CEF without http server
https://bbonczek.github.io/jekyll/update/2018/04/24/serve-content-in-cef-without-http-server.html
在cef中使用自定义协议(scheme)
https://www.cnblogs.com/sinceret/p/10417941.html
Registering custom schema handlers.
https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=18259
Chromium Embedded Framework (CEF) Automated Builds
https://cef-builds.spotifycdn.com/index.html