How to directly obtain the whole POST data in the libmicrohttpd Library

Source: Internet
Author: User

Because the libmicrohttpd library processes POST data in the form, the form submission and parsing in the HTTP protocol have a specific format. However, in our requirements, the data we POST may only be a common XML body, not the data submitted in the form format. We need to process the entire XML body, at this time, libmicrohttpd cannot get the BODY of the entire POST. Tracking the source code, we can add a little bit of code to get the data. The modified source code is as follows:

 

 

[Cpp]
/**
* Get a special header value. If multiple
* Values match the kind, return any one of them.
*
* @ Param connection to get values from
* @ Param kind what kind of value are we looking
* @ Param key the header to look for, NULL to lookup 'trailing' value without a key
* @ Return NULL if no such item was found
*/
Const char * MHD_lookup_connection_value (struct MHD_Connection * connection, enum MHD_ValueKind kind, const char * key)
{
Struct MHD_HTTP_Header * pos;
 
If (NULL = connection)
Return NULL;
 
If (kind = MHD_POSTDATA_KIND) return connection-> read_buffer; // you only need to add the modified Code.
 
For (pos = connection-> headers_received; NULL! = Pos; pos = pos-> next)
If (0! = (Pos-> kind & kind ))&&
(Key = pos-> header) | (NULL! = Pos-> header )&&
(NULL! = Key )&&
(0 = strcasecmp (key, pos-> header )))))
Return pos-> value;
Return NULL;
}

/**
* Get a special header value. If multiple
* Values match the kind, return any one of them.
*
* @ Param connection to get values from
* @ Param kind what kind of value are we looking
* @ Param key the header to look for, NULL to lookup 'trailing' value without a key
* @ Return NULL if no such item was found
*/
Const char * MHD_lookup_connection_value (struct MHD_Connection * connection, enum MHD_ValueKind kind, const char * key)
{
Struct MHD_HTTP_Header * pos;

If (NULL = connection)
Return NULL;

If (kind = MHD_POSTDATA_KIND) return connection-> read_buffer; // you only need to add the modified Code.

For (pos = connection-> headers_received; NULL! = Pos; pos = pos-> next)
If (0! = (Pos-> kind & kind ))&&
(Key = pos-> header) | (NULL! = Pos-> header )&&
(NULL! = Key )&&
(0 = strcasecmp (key, pos-> header )))))
Return pos-> value;
Return NULL;
}
At the upper application layer, we can use the following code to obtain the BODY data:

 

 

[Cpp]
Const char * length = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH );
If (length = NULL)
Return NULL;
 
Const char * body = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, NULL );
If (body = NULL)
Return NULL;
 

Size_t len = strlen (body );

Const char * length = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH );
If (length = NULL)
Return NULL;

Const char * body = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, NULL );
If (body = NULL)
Return NULL;


Size_t len = strlen (body );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.