如何直接获取 libmicrohttpd 库中POST上来的整个数据

          由于 libmicrohttpd 库在处理POST数据的时候是与表单的形势处理的, HTTP协议中表单的提交和解析有特定的格式。但是在我们的需求中,我们POST上来的数据可能只是一个普通的XML体,并不是按照表单格式提交上来的数据,我们需要处理整个的XML体, 这个时候, libmicrohttpd 不能获取到整个 POST上来的BODY, 追踪源码,我们可以添加一点点代码即可获取该数据。修改的源码如下 :


/**
* 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数据:


	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);

上面代码中的 body 即是我们POST提交上来的整个数据块。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langeldep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值