您的位置:首页 > 其它

libevent 写了一个简单地web服务器

2016-04-21 15:35 501 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: