美文网首页
libmicrohttpd获取http body

libmicrohttpd获取http body

作者: pandazhong | 来源:发表于2018-10-13 14:18 被阅读0次

libmicrohttpd是一个c实现的轻量级,高性能的http server,可以嵌入程序中,但是这个库获取http的body比较麻烦。

#include <stdio.h>

#include <string>

#include <stdlib.h>

#include "microhttpd.h"

#define PAGE "<html><head><title>panda/title>"\

"</head><body>hello, i am panda, nice to meet you.</body></html>"

struct postStatus {

bool status;

char *buff;

};

bool getHttpBody(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version,

const char *upload_data, size_t *upload_data_size, void **ptr,std::string &body)

{

struct postStatus *post = NULL;

post = (struct postStatus*)*ptr;

if (post == NULL) {

post = (postStatus *)malloc(sizeof(struct postStatus));

post->status = false;

*ptr = post;

}

if (!post->status) {

post->status = true;

return false;

}

else {

if (*upload_data_size != 0) {

post->buff = (char *)malloc(*upload_data_size + 1);

memset(post->buff, 0, *upload_data_size + 1);

_snprintf(post->buff, *upload_data_size, "%s", upload_data);

*upload_data_size = 0;

return false;

}

else {

body = post->buff;

free(post->buff);

}

}

if (post != NULL)

{

free(post);

}

return true;

}

int onProcess(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version,

const char *upload_data, size_t *upload_data_size, void **ptr)

{

const char * page = (const char *)cls;

struct MHD_Response * response;

int ret;

std::string body;

if (getHttpBody(connection, connection,  url, method, version, upload_data, upload_data_size, ptr, body) == false)

{

return MHD_YES;

}

printf("%s\n", body.c_str());

// 第三个参数建议建议使用MHD_RESPMEM_MUST_COPY

response = MHD_create_response_from_buffer(strlen(page), (void*)page, MHD_RESPMEM_MUST_COPY);

ret = MHD_queue_response(connection, MHD_HTTP_OK, response);

MHD_destroy_response(response);

return ret;

}

int main(int argc, char* argv[])

{

struct MHD_Daemon *d;

d = MHD_start_daemon(

MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,

4000, // http端口号

NULL, NULL, &onProcess, PAGE,

MHD_OPTION_END);

if (d == NULL)

return 1;

(void)getc(stdin);

MHD_stop_daemon(d);

return 0;

}

相关文章

  • libmicrohttpd获取http body

    libmicrohttpd是一个c实现的轻量级,高性能的http server,可以嵌入程序中,但是这个库获取ht...

  • PHP 获取HTTP body 内容

    PHP 获取Http body的内容 有时候我们获取数据时需要根据Header中的格式来解析,比如上传一个json...

  • libhttpserver 入门

    libhttpserver 依赖libmicrohttpd,所以我们要先安装这个libmicrohttpd htt...

  • HTTP Body

  • HTTP: body

    Requests body 请求报文的报文体大致上可以分为如下两种: Single-resource bodies...

  • @ResponseBody和@RequestBody

    @ResponseBody: 表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数...

  • 获取元素和事件注册

    1、获取元素 获取body元素可以直接通过:document.body获取获取html元素可以直接通过:docum...

  • PHP 谷歌支付验证

    PHP 获取Http body的内容 有时候我们获取数据时需要根据Header中的格式来解析,比如上传一个json...

  • JavaScript之dom查询方法

    获取body标签在document中有一个属性body,它保存的是body的引用var body = docume...

  • Python 实战:week1 实战作业

    运行结果: 代码: 项目代码 小结 获取内容 body 和 head 不一定要从 HTML 的 body 中获取,...

网友评论

      本文标题:libmicrohttpd获取http body

      本文链接:https://www.haomeiwen.com/subject/cheoaftx.html