示例#1
0
void DumpRequestContents(CefRefPtr<CefRequest> request, std::string& str)
{
  std::stringstream ss;

  ss << "URL: " << std::string(request->GetURL());
  ss << "\nMethod: " << std::string(request->GetMethod());

  CefRequest::HeaderMap headerMap;
  request->GetHeaderMap(headerMap);
  if(headerMap.size() > 0) {
    ss << "\nHeaders:";
    CefRequest::HeaderMap::const_iterator it = headerMap.begin();
    for(; it != headerMap.end(); ++it) {
      ss << "\n\t" << std::string((*it).first) << ": " <<
          std::string((*it).second);
    }
  }

  CefRefPtr<CefPostData> postData = request->GetPostData();
  if(postData.get()) {
    CefPostData::ElementVector elements;
    postData->GetElements(elements);
    if(elements.size() > 0) {
      ss << "\nPost Data:";
      CefRefPtr<CefPostDataElement> element;
      CefPostData::ElementVector::const_iterator it = elements.begin();
      for(; it != elements.end(); ++it) {
        element = (*it);
        if(element->GetType() == PDE_TYPE_BYTES) {
          // the element is composed of bytes
          ss << "\n\tBytes: ";
          if(element->GetBytesCount() == 0)
            ss << "(empty)";
          else {
            // retrieve the data.
            size_t size = element->GetBytesCount();
            char* bytes = new char[size];
            element->GetBytes(size, bytes);
            ss << std::string(bytes, size);
            delete [] bytes;
          }
        } else if(element->GetType() == PDE_TYPE_FILE) {
          ss << "\n\tFile: " << std::string(element->GetFile());
        }
      }
    }
  }

  str = ss.str();
}
bool FWebBrowserHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, CefRefPtr<CefRequest> Request)
{
	const FString LanguageHeaderText(TEXT("Accept-Language"));
	const FString LocaleCode = FWebBrowserSingleton::GetCurrentLocaleCode();
	CefRequest::HeaderMap HeaderMap;
	Request->GetHeaderMap(HeaderMap);
	auto LanguageHeader = HeaderMap.find(*LanguageHeaderText);
	if (LanguageHeader != HeaderMap.end())
	{
		(*LanguageHeader).second = *LocaleCode;
	}
	else
	{
		HeaderMap.insert(std::pair<CefString, CefString>(*LanguageHeaderText, *LocaleCode));
	}
	Request->SetHeaderMap(HeaderMap);
	return false;
}
示例#3
0
// Implementation of the schema handler for appjs:// requests.
void AppjsSchemeHandler::Execute(CefThreadId threadId) {

  REQUIRE_UI_THREAD();

  HandleScope scope;

  Local<Object>  global = Context::GetCurrent()->Global();
  Local<Object> emitter = global->Get(String::NewSymbol("process"))->ToObject();

  const int argc = 3;

  Handle<Value>    self = WrapObject(this);
  Local<Function>    cb = FunctionTemplate::New(NodeCallback,self)->GetFunction();
  Local<Object>     req = Object::New();
  Local<String>    post = String::New("");
  Local<Object> headers = Object::New();
  Local<String>   files = String::New("");

  CefRequest::HeaderMap headerMap;
  request_->GetHeaderMap(headerMap);

  if (headerMap.size() > 0) {
    CefRequest::HeaderMap::const_iterator it = headerMap.begin();
    for ( ; it != headerMap.end(); ++it) {
      headers->Set(String::New((uint16_t*)(*it).first.c_str()),String::New((uint16_t*)(*it).second.c_str()));
    }
  }

  CefRefPtr<CefPostData> postData = request_->GetPostData();

  if(postData.get()){

    CefPostData::ElementVector elements;

    postData->GetElements(elements);

    if (elements.size() > 0) {
      CefRefPtr<CefPostDataElement> element;
      CefPostData::ElementVector::const_iterator it = elements.begin();

      for ( ; it != elements.end(); ++it) {
        element = (*it);
        if (element->GetType() == PDE_TYPE_BYTES && element->GetBytesCount()) {

          // retrieve the data.
          size_t size = element->GetBytesCount();
          char* bytes = new char[size];
          element->GetBytes(size, bytes);
          post = String::New(bytes,size);
          delete [] bytes;

        } else if (element->GetType() == PDE_TYPE_FILE) {
          //TODO Needs testing
          files = String::New((uint16_t*)element->GetFile().c_str());
        }
      }

    }
  }

  Handle<Value> method = String::New((uint16_t*)request_->GetMethod().c_str());
  Handle<Value> url = String::New((uint16_t*)request_->GetURL().c_str());

  req->Set(String::NewSymbol("method"),method);
  req->Set(String::NewSymbol("url"),url);
  req->Set(String::NewSymbol("post"),post);
  req->Set(String::NewSymbol("headers"),headers);
  req->Set(String::NewSymbol("files"),files);

  Handle<Value> argv[argc] = {String::New("appjs-request"),req,cb};
  node::MakeCallback(emitter,"emit",argc,argv);

}
示例#4
0
////////////////////////////////////////////////////////////////////
//                                                                //
// Implementation: CefRequestHandler::OnBeforeResourceLoad        //
// http://magpcss.org/ceforum/apidocs3/projects/(default)/CefRequestHandler.html#OnBeforeResourceLoad(CefRefPtr%3CCefBrowser%3E,CefRefPtr%3CCefFrame%3E,CefRefPtr%3CCefRequest%3E) //
//                                                                //
////////////////////////////////////////////////////////////////////
CefRequestHandler::ReturnValue CWebView::OnBeforeResourceLoad ( CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefRequestCallback> callback )
{
    // Mostly the same as CWebView::OnBeforeBrowse
    CefURLParts urlParts;
    if ( !CefParseURL ( request->GetURL (), urlParts ) )
        return RV_CANCEL; // Cancel if invalid URL (this line will normally not be executed)

    // Add some information to the HTTP header
    {
        CefRequest::HeaderMap headerMap;
        request->GetHeaderMap ( headerMap );
        auto iter = headerMap.find ( "User-Agent" );

        if ( iter != headerMap.end () )
        {
            // Add MTA:SA "watermark"
            iter->second = iter->second.ToString () + "; " MTA_CEF_USERAGENT;

            // Add 'Android' to get the mobile version
            SString strPropertyValue;
            if ( GetProperty ( "mobile", strPropertyValue ) && strPropertyValue == "1" )
                iter->second = iter->second.ToString () + "; Mobile Android";

            request->SetHeaderMap ( headerMap );
        }
    }

    WString scheme = urlParts.scheme.str;
    if ( scheme == L"http" || scheme == L"https" )
    {
        SString domain = UTF16ToMbUTF8 ( urlParts.host.str );
        if ( domain != "mta" )
        {
            if ( IsLocal () )
                return RV_CANCEL; // Block remote requests in local mode generally

            eURLState urlState = g_pCore->GetWebCore ()->GetURLState ( domain, true );
            if ( urlState != eURLState::WEBPAGE_ALLOWED )
            {
                // Trigger onClientBrowserResourceBlocked event
                auto func = std::bind ( &CWebBrowserEventsInterface::Events_OnResourceBlocked, m_pEventsInterface,
                    SString ( request->GetURL () ), domain, urlState == eURLState::WEBPAGE_NOT_LISTED ? 0 : 1 );
                g_pCore->GetWebCore ()->AddEventToEventQueue ( func, this, "OnResourceBlocked" );

                return RV_CANCEL; // Block if explicitly forbidden
            }

            // Allow
            return RV_CONTINUE;
        }
        else
            return RV_CONTINUE;
    }
    else if ( scheme == L"mtalocal" )
    {
        // Allow :)
        return RV_CONTINUE;
    }

     // Trigger onClientBrowserResourceBlocked event
    auto func = std::bind ( &CWebBrowserEventsInterface::Events_OnResourceBlocked, m_pEventsInterface, SString ( request->GetURL () ), "", 2 ); // reason 1 := blocked protocol scheme
    g_pCore->GetWebCore ()->AddEventToEventQueue ( func, this, "OnResourceBlocked" );

    // Block everything else
    return RV_CANCEL;
}