由於 libmicrohttpd 庫在處理POST數據的時候是與表單的形勢處理的, HTTP協議中表單的提交和解析有特定的格式。但是在我們的需求中,我們POST上來的數據可能只是一個普通的XML體,並不是按照表單格式提交上來的數據,我們需要處理整個的XML體, 這個時候, libmicrohttpd 不能獲取到整個 POST上來的BODY, 追蹤源碼,我們可以添加一點點代碼即可獲取該數據。修改的源碼如下 :
[cpp]
/**
* Get a particular header value. If multiple
* values match the kind, return any one of them.
*
* @param connection connection to get values from
* @param kind what kind of value are we looking for
* @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;//只需要添加改行代碼即可
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 particular header value. If multiple
* values match the kind, return any one of them.
*
* @param connection connection to get values from
* @param kind what kind of value are we looking for
* @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;//只需要添加改行代碼即可
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;
}
在上層應用層 ,我們可以用如下的代碼來獲取BODY數據:
[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);