<span style="font-size:18px;">void DateDeal
{
}
void httpd_handler(struct evhttp_request *req, void *arg)
{
const char *uri;
uri = evhttp_request_uri(req);
char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
string result=DataDeal(uri,post_data); //url 和数据的处理函数
cout<<"result==="<<result.c_str()<<endl;
//HTTP header
evhttp_add_header(req->output_headers, "Server", MYHTTPD_SIGNATURE);
evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8");
evhttp_add_header(req->output_headers, "Connection", "close");
struct evbuffer *buf;
buf = evbuffer_new();
evbuffer_add_printf(buf, "%s\n",result.c_str());
evhttp_send_reply(req, HTTP_OK, "OK", buf);
evbuffer_free(buf);
}
int main()
{
event_init();
//http server
struct evhttp *httpd;
httpd = evhttp_start(g_JsServerIp, g_JsServerPt);
printf("ip===%s\nport=%d\n",g_JsServerIp,g_JsServerPt);
printf("success\n");
//generic callback
int httpd_option_timeout = 120;
evhttp_set_timeout(httpd, httpd_option_timeout);
evhttp_set_gencb(httpd, httpd_handler, NULL);//处理post请求的http内容
//callback
event_dispatch();
evhttp_free(httpd);
}
</span>
libevent 写了一个简单地web服务器
最新推荐文章于 2024-04-20 19:54:59 发布