Custom scheme, standard=true, no POST data

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.

Custom scheme, standard=true, no POST data

Postby HarmlessDave » Tue Aug 04, 2015 2:14 pm

CEF 3.2272.1998, Windows.

I'm trying to pass JSON from script to C++ using a custom "client" scheme handler and XMLHttpRequest in the script code. I've set disable-web-security to allow cross-site XmlHttpRequests.

The request is arriving at ClientSchemeHandler::ProcessRequest and the URL is passed but request->GetPostData(); returns NULL.

Scheme registration (most of the handler / factory code was copied from scheme_test.cc)
Code: Select all
int APIENTRY _tWinMain(
   CefRegisterSchemeHandlerFactory("client", "app", new ClientSchemeHandlerFactory());

void CEFMyApp::OnRegisterCustomSchemes(
  registrar->AddCustomScheme("client", true, false, false);


Event handler
Code: Select all
 bool ClientSchemeHandler::ProcessRequest(CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback)
{
   CEF_REQUIRE_IO_THREAD();
   bool handled = false;
   std::string url = request->GetURL();
   int count = 0;
   CefRefPtr<CefPostData> post = request->GetPostData();
   if (post != NULL)
      count = post->GetElementCount();
   LogTRACE("ClientSchemeHandler::ProcessRequest (%s) - POST addr = %x, count = %d", url.c_str(), post, count);

Log 12:00:56 - ClientSchemeHandler::ProcessRequest (client://app/json.htm?key=free&s2=bunny&bId=1234) - POST addr = 0, count = 0

Script code on a simple test page (I tried passing as both JSON and as form-encoded):
Code: Select all
 <html>
<script>
function do_post()
{
   //alert('posting');
   var zbytes = "j={x{y{z}}}"; //"{x{y{z}}}";
   var zhttp = new XMLHttpRequest();
   var zurl = "client://app/json.htm?key=free&s2=bunny&bId=1234";
   zhttp.open("POST", zurl, true);
   zhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //   ('Content-Type', 'application/json');
   zhttp.send(zbytes);
   alert('posted');
}
</script>
<body onLoad="do_post();">
<p>Hey nonny nonny!
</p>
</body>
</html>

Any idea why the POST body is not being included in the request object?
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Custom scheme, standard=true, no POST data

Postby amaitland » Tue Aug 04, 2015 5:52 pm

From memory you can only POST data to the HTTP or HTTPS schemes.

http://www.magpcss.org/ceforum/viewtopi ... 03&p=26344
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: Custom scheme, standard=true, no POST data

Postby HarmlessDave » Tue Aug 04, 2015 6:13 pm

Thanks, yes that seems to be the case based on logging OnBeforeResourceLoad:

15:58:07 - OnBeforeResourceLoad (client://app/json.htm?key=free&studentK ... y&bId=1234) - POST addr = 0, count = 0

16:03:36 - OnBeforeResourceLoad (http://www.foo.com/cldb/json.htm?key=fr ... y&bId=1234) - POST addr = 7284c58, count = 1

This wiki page section should probably be updated: https://bitbucket.org/chromiumembedded/ ... t-handling

It’s important to register custom schemes (anything other than “HTTP”, “HTTPS”, etc) with CEF so that they’ll behave as expected. For example, if you wish your scheme to behave the same as HTTP (support POST requests and enforce HTTP access control (CORS) restrictions) it should be registered as a “standard” scheme. If your custom scheme will be performing cross-origin requests to other schemes consider using the HTTP scheme instead of a custom scheme to avoid potential issues. If you wish to use custom schemes the attributes are registered via the CefApp::OnRegisterCustomSchemes() callback.

to something like:

Only the standard "HTTP" and "HTTPS" schemes support POST data. It’s important to register custom schemes (anything other than “HTTP”, “HTTPS”, etc) with CEF so that they’ll behave as expected. [u]For example, if you wish your scheme to behave the same as HTTP (enforce HTTP access control (CORS) restrictions) ....
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Custom scheme, standard=true, no POST data

Postby amaitland » Tue Aug 04, 2015 6:43 pm

I would probably put a Note Only the standard "HTTP" and "HTTPS" schemes support POST data. at the end of the first block.

Either way, sounds like a reasonable change in updating the documentation.

See if Marshall has any comment.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: Custom scheme, standard=true, no POST data

Postby magreenblatt » Tue Aug 04, 2015 7:55 pm

It should only be a problem with sync XHR to non-HTTP schemes -- see https://bitbucket.org/chromiumembedded/cef/issues/404. Cefclient includes examples that should async POST successfully.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 38 guests