Exemplo n.º 1
0
void RunRequestTest(CefRefPtr<CefBrowser> browser)
{
	// Create a new request
  CefRefPtr<CefRequest> request(CefRequest::CreateRequest());

  // Set the request URL
  request->SetURL("http://tests/request");

  // Add post data to the request.  The correct method and content-
  // type headers will be set by CEF.
  CefRefPtr<CefPostDataElement> postDataElement(
      CefPostDataElement::CreatePostDataElement());
  std::string data = "arg1=val1&arg2=val2";
  postDataElement->SetToBytes(data.length(), data.c_str());
  CefRefPtr<CefPostData> postData(CefPostData::CreatePostData());
  postData->AddElement(postDataElement);
  request->SetPostData(postData);

  // Add a custom header
  CefRequest::HeaderMap headerMap;
  headerMap.insert(
      std::make_pair("X-My-Header", "My Header Value"));
  request->SetHeaderMap(headerMap);

  // Load the request
  browser->GetMainFrame()->LoadRequest(request);
}
Exemplo n.º 2
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();
}
Exemplo n.º 3
0
bool CWebView::LoadURL ( const SString& strURL, bool bFilterEnabled, const SString& strPostData, bool bURLEncoded )
{
    if ( !m_pWebView )
        return false;

    CefURLParts urlParts;
    if ( !CefParseURL ( strURL, urlParts ) )
        return false; // Invalid URL

    // Are we allowed to browse this website?
    if ( bFilterEnabled && g_pCore->GetWebCore ()->GetURLState ( UTF16ToMbUTF8 ( urlParts.host.str ), true ) != eURLState::WEBPAGE_ALLOWED )
        return false;

    // Load it!
    auto pFrame = m_pWebView->GetMainFrame ();
    if ( strPostData.empty () )
    {
        pFrame->LoadURL ( strURL );
    }
    else
    {
        // Load URL first, see https://bitbucket.org/chromiumembedded/cef/issue/579
        pFrame->LoadURL ( "about:blank" );

        // Perform HTTP POST
        auto request = CefRequest::Create ();
        auto postData = CefPostData::Create ();
        auto postDataElement = CefPostDataElement::Create ();
        postDataElement->SetToBytes ( strPostData.size (), strPostData.c_str () );
        postData->AddElement ( postDataElement );

        if ( bURLEncoded )
        {
            CefRequest::HeaderMap headerMap;
            headerMap.insert ( std::make_pair ( "Content-Type", "application/x-www-form-urlencoded" ) );
            headerMap.insert ( std::make_pair ( "Content-Length", std::to_string ( strPostData.size () ) ) );
            //headerMap.insert ( std::make_pair ( "Connection", "close" ) );
            request->SetHeaderMap ( headerMap );
        }

        request->SetURL ( strURL );
        request->SetMethod ( "POST" );
        request->SetPostData ( postData );
        pFrame->LoadRequest ( request );
    }
   
    return true;
}
Exemplo n.º 4
0
bool CaffeineClientHandler::OnBeforeResourceLoad(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame,
    CefRefPtr<CefRequest> request)
{
//    REQUIRE_IO_THREAD();

    CefRequest::HeaderMap x;

    request->GetHeaderMap(x);
    x.erase(TEXT("User-Agent"));
    x.insert(CefRequest::HeaderMap::value_type(TEXT("User-Agent"), user_agent));
    request->SetHeaderMap(x);

    return !ValidateRequestLoad(browser, frame, request);
}
Exemplo n.º 5
0
bool CaffeineClientApp::PostCrashLogs(CefString CrashLog, CefString AppVersion, CefString UserDescription)
{
    CefRefPtr<CefRequest> request = CefRequest::Create();
    wstring url(DIAGNOSTICS_URL);

    request->SetURL(url);
    request->SetMethod("POST");
    request->SetFlags(UR_FLAG_ALLOW_CACHED_CREDENTIALS|UR_FLAG_REPORT_UPLOAD_PROGRESS);
    
    // Add post data to the request.  The correct method and content-
    // type headers will be set by CEF.
    CefRefPtr<CefPostDataElement> Data = CefPostDataElement::Create();
//    Data->SetToFile(CrashLog);
    //  TODO:  Consider creating a memory mapped file
    HANDLE hZipFile = CreateFile(CrashLog.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD BigBufferSize = GetFileSize(hZipFile, NULL);
    if (BigBufferSize != INVALID_FILE_SIZE)
    {
        LPBYTE BigBuffer = new BYTE[BigBufferSize];

        DWORD dwBytesRead = 0;
        ReadFile(hZipFile, BigBuffer, BigBufferSize, &dwBytesRead, NULL);
        CloseHandle(hZipFile);
        Data->SetToBytes(dwBytesRead, BigBuffer);
    
        CefRefPtr<CefPostData> POSTBody = CefPostData::Create();
        POSTBody->AddElement(Data);
        request->SetPostData(POSTBody);

        CefRequest::HeaderMap headerMap;
        request->GetHeaderMap(headerMap);
        headerMap.insert(make_pair("Content-Type", "application/zip"));
        request->SetHeaderMap(headerMap);
    
//        CefV8Context::GetCurrentContext()->GetFrame()->LoadRequest(request);

        // Create the client instance.
        CefRefPtr<CaffeineRequestClient> client = new CaffeineRequestClient();
    
        // Start the request. MyRequestClient callbacks will be executed asynchronously.
        CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, client.get());
        
        delete [] BigBuffer;
    }

    return true;
}
Exemplo n.º 6
0
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;
}
Exemplo n.º 7
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);

}
Exemplo n.º 8
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;
}