下面是我正在处理的 mongoose webserver http 事件处理程序的 C 片段:
static void HttpEventHandler(struct mg_connection *nc, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_REQUEST) {
struct http_message *hm = (struct http_message *) ev_data;
if (mg_vcmp(&hm->method, "POST") == 0) {
pthread_t thread_id;
int rc;
rc = pthread_create(&thread_id, NULL, thr_func, /* Here I want hm body to be passed after its malloced */);
if (rc) { /* could not create thread */
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
}//if POST
mg_printf(nc, "HTTP/1.1 200 OK\r\n");
nc->flags |= MG_F_SEND_AND_CLOSE;
}
}
http post 消息正文,可使用以下语法作为字符串访问:
"%.*s", (int) hm->body.len,hm->body.p
我希望代码示例到 malloc hm->body 并将其传递给上面代码片段中的线程,也很高兴解释如何转换传递的 void *. 如果它很难,那么请 malloc ev_data 或 hm。