Custom scheme: how to use request PostDataElement ?

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: how to use request PostDataElement ?

Postby Mirco » Fri Aug 05, 2011 1:24 pm

Dear Reader,

I'm busy programming a scheme handler in CEF rev. 259 (coming from rev. 231 the programming interface is really cleaned up, my compliments on that !).

In my scheme handler I'm implementing the ProcessRequest function:
Code: Select all
ProcessRequest(CefRefPtr<CefRequest> request, CefString& redirectUrl, CefRefPtr<CefResponse> response, int* response_length)


Now I'm stuck on a POST request. I can get the request Content-Type out of the headers, but what I don't understand is how to get the request http-body bytes. I found the request->GetPostData function to retrieve the PostData.

I'm having the following questions:
1) How do I convert CefPostData to request http-body-bytes ?
2) CefPostData is build on a list of elements, which kan be retrieved by CefPostData::GetElements. Each element can be bytes, a filename or empty. Must I concatenate these elements myself into a http-body or is there some helper function ?

Any help or pointers are welcome !

Kind regards,
Mirco Babin (The Netherlands)
Mirco
Techie
 
Posts: 29
Joined: Mon Jun 06, 2011 1:24 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby magreenblatt » Mon Aug 08, 2011 11:02 am

Hi Mirco,

If your content type is "application/x-www-form-urlencoded" then you should have a single CefPostDataElement of type PDE_TYPE_BYTES that contains the POST data in URL encoded format. The http://code.google.com/p/chromiumembedd ... g_util.cpp source file provides an example of how to retrieve the CefPostDataElement contents. See http://www.w3.org/TR/html4/interact/for ... -17.13.4.1 for more information on the URL encoded format. CEF does not currently expose a parser for URL encoded data but many already exist on the internet (take a look at http://code.google.com/p/libjingle/sour ... lencode.cc for example).

Regards,
Marshall
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby Mirco » Tue Aug 09, 2011 12:37 pm

Hi Marshall,

Thanks for the answer. I think I understand the PostData now. PostData::GetElements is already in the format specified by Content-Type.

So to get the http-request-body-bytes you have to iterate through CefPostData::GetElements(), copying bytes or copying the contents of a file. The resulting bytestream is in the Content-Type specified by the request header.

The GET already worked.
The POST with x-www-url-encoded works now.
The POST with multipart/form-data works also (without file upload)
I'm now busy testing the POST with multipart/form-data and fileupload.

I do have one suggestion for the function
virtual size_t CefPostDataElement::GetBytes(size_t size, void* bytes) =0;
Because I'm reading in the http-request-body-bytes in chunks of 4096 bytes, I manually have to keep track at which offset I am. GetBytes can only return bytes from the beginning. It would be convient if there was a:
virtual size_t CefPostDataElement::GetBytes(size_t offset, size_t size, void* bytes) =0;
offset being a byte-offset in the original buffer


Now I'm coding something like
Code: Select all
    unsigned char *mybuffer;
    unsigned int offset = 0;
    unsigned int send;
    unsigned int len = element->GetBytesCount();

    malloc(mybuffer, len);
    element->GetBytes(len, mybuffer);

    while (len>0)
    {
        if (len>4096)
            send = 4096;
        else
            send = len;

        MySendBytes(&mybuffer[offset], send);

        offset += send;
        len -= send;
    }

    free(mybuffer);


With this new suggested function this could be reduced to (no malloc):
Code: Select all
    unsigned char mybuffer[4096]
    unsigned int offset = 0;
    unsigned int send;
    unsigned int len = element->GetBytesCount();

    while (len>0)
    {
        if (len>4096)
            send = 4096;
        else
            send = len;

        element->GetBytes(offset, len, mybuffer);
        MySendBytes(mybuffer, send);

        offset += send;
        len -= send;
    }



Kind Regards,
Mirco
Mirco
Techie
 
Posts: 29
Joined: Mon Jun 06, 2011 1:24 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby magreenblatt » Tue Aug 09, 2011 1:03 pm

Mirco wrote:I do have one suggestion for the function
Code: Select all
virtual size_t CefPostDataElement::GetBytes(size_t size, void* bytes) =0;
Because I'm reading in the http-request-body-bytes in chunks of 4096 bytes, I manually have to keep track at which offset I am. GetBytes can only return bytes from the beginning. It would be convient if there was a:
Code: Select all
virtual size_t CefPostDataElement::GetBytes(size_t offset, size_t size, void* bytes) =0;

offset being a byte-offset in the original buffer

I agree that this could be useful. Please create a new issue for this in the CEF issue tracker.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby Mirco » Tue Aug 09, 2011 2:01 pm

Hi Marshall,

I created the issue http://code.google.com/p/chromiumembedded/issues/detail?id=308. Well, this actually is the first time ever I entered an issue for an open source project. So it could be the format is not as desired.

The POST multipart/form-data with file upload also works correctly.
Thanks again for pointing out the PostData::GetElements() is already in the x-www-urlencoded or multipart/form-data encoding.

Kind regards,
Mirco
Mirco
Techie
 
Posts: 29
Joined: Mon Jun 06, 2011 1:24 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby magreenblatt » Tue Aug 09, 2011 2:35 pm

Mirco wrote:Hi Marshall,

I created the issue http://code.google.com/p/chromiumembedded/issues/detail?id=308. Well, this actually is the first time ever I entered an issue for an open source project. So it could be the format is not as desired.

The POST multipart/form-data with file upload also works correctly.
Thanks again for pointing out the PostData::GetElements() is already in the x-www-urlencoded or multipart/form-data encoding.

Kind regards,
Mirco

The issue looks good, thanks. I'm glad you were able to get everything working.

Regards,
Marshall
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Custom scheme: how to use request PostDataElement ?

Postby silver » Tue Apr 21, 2015 7:51 am

magreenblatt wrote:If your content type is "application/x-www-form-urlencoded" then you should have a single CefPostDataElement of type PDE_TYPE_BYTES that contains the POST data in URL encoded format.


This should be mentioned in the docs.
User avatar
silver
Techie
 
Posts: 48
Joined: Tue Feb 24, 2015 5:39 am


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 50 guests