libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] POST processing "any" mime type


From: Andreas Wehrmann
Subject: Re: [libmicrohttpd] POST processing "any" mime type
Date: Fri, 23 May 2014 06:56:20 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0


On 05/22/2014 07:49 PM, Shantanu Tushar Jha wrote:
[Please CC me in replies, I'm not subscribed to the list]

Hi,

I was searching about how to process application/json in POST data in a
server written using MHD. I came across this thread
http://lists.gnu.org/archive/html/libmicrohttpd/2011-11/msg00013.html where
Andreas says that he implemented a generic processor to handle
application/JSON.

I want to do the same, but MHD_AccessHandlerCallback::upload_data is always
NULL, and I can't use MHD_create_post_processor because
MHD_PostDataIterator works on a key/data basis. How do I register a generic
POST processor? (My existing code is at
https://github.com/shaan7/fibrous/blob/master/cpp/httpserver.cpp#L42 )

Thanks,


The following are incomplete snippets from my code.
Note: "request" is a private handle.

When I get a new connection, I do:

else if( request_method == MHD_HTTP_METHOD_POST )
{
    request->request_type = POST;
    request->postProcessor = MHD_create_post_processor( connection, 1024,
Server_impl::post_iterator,
reinterpret_cast< void* >( request ) );

    if( request->postProcessor == NULL )
    {
        if( content_type == NULL )
        {
PlogNotice( "[HTTPD] unable to create POST processor: unknown content type!" );
            return MHD_NO;
        }

        /* check for other MIME types supported by us */
        if( isJsonType( content_type ) && jsonCallback )
        {
            return MHD_YES;
        }
        else if( isXmlType( content_type ) && xmlCallback )
        {
            return MHD_YES;
        }
        else
        {
/* NOTE: is it better here to return "501 - not implemented"? */ PlogWarn( "[HTTPD] unable to handle POST request with content of type %s",
                        content_type );
            return MHD_NO;
        }
    }
}

****************************************************
When I get more data I do:

else if( request->request_type == POST )
{
    if( 0 != *upload_data_size )
    {
if( MHD_post_process( request->postProcessor, upload_data, *upload_data_size ) == MHD_NO )
        {
            if( isJsonType( content_type ) || isXmlType( content_type ) )
            {
request->postRawData.append( upload_data, *upload_data_size );
            }
            else
            {
PlogError( "[HTTPD] don't know how to handle content of type: %s",
                            content_type );
                return MHD_NO;
            }
        }
        *upload_data_size = 0;

        return MHD_YES;
    }
else /* received data completely, now inform user and build answer page */
    {
std::string rsp; /* response to send in case of user handled mime types */ bool handleJsonRequest = isJsonType( content_type ) && jsonCallback;
        bool handleXmlRequest   = isXmlType( content_type ) && xmlCallback;

        if( handleJsonRequest )
        {
            jsonCallback( requested_url, request->postRawData, rsp );
        }
        else if( handleXmlRequest )
        {
            xmlCallback( requested_url, request->postRawData, rsp );
        }
        else if( setClearSilverDataCB )
        {
            std::string fileName( getFilename( requested_url ) );
            setClearSilverDataCB( fileName, request->postFormData );
        }

        /* --- cut here --- */
    }
}

Regards,
Andreas




reply via email to

[Prev in Thread] Current Thread [Next in Thread]