Libevent::evhttp服務器


 

#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "evhttp.h"
#include "event.h"
#include "event2/http.h"
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
#include "event2/bufferevent_compat.h"
#include "event2/http_struct.h"
#include "event2/http_compat.h"
#include "event2/util.h"
#include "event2/listener.h"

 

int main()
{
    struct evhttp *http_server = NULL;
    short http_port = 8081;
    char *http_addr = "0.0.0.0";

    //初始化
    event_init();
    //啟動http服務端
    http_server = evhttp_start(http_addr, http_port);
    if (http_server == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "http server start failed.");
        return -1;
    }

    //設置請求超時時間(s)
    evhttp_set_timeout(http_server, 5);
    //設置事件處理函數,evhttp_set_cb針對每一個事件(請求)注冊一個處理函數,
    //區別於evhttp_set_gencb函數,是對所有請求設置一個統一的處理函數
    evhttp_set_cb(http_server, "/post", http_handler_testpost_msg, NULL);
    evhttp_set_cb(http_server, "/get", http_handler_testget_msg, NULL);

    //循環監聽
    event_dispatch();
    //實際上不會釋放,代碼不會運行到這一步
    evhttp_free(http_server);

    return 0;
}

 

 

//解析post請求數據
void get_post_message(char *buf, struct evhttp_request *req)
{
    size_t post_size = 0;

    post_size = evbuffer_get_length(req->input_buffer);//獲取數據長度
    printf("====line:%d,post len:%d\n", __LINE__, post_size);
    if (post_size <= 0)
    {
        printf("====line:%d,post msg is empty!\n", __LINE__);
        return;
    }
    else
    {
        size_t copy_len = post_size > BUF_MAX ? BUF_MAX : post_size;
        printf("====line:%d,post len:%d, copy_len:%d\n", __LINE__, post_size, copy_len);
        memcpy(buf, evbuffer_pullup(req->input_buffer, -1), copy_len);
        buf[post_size] = '\0';
        printf("====line:%d,post msg:%s\n", __LINE__, buf);
    }
}


//處理post請求
void http_handler_testpost_msg(struct evhttp_request *req, void *arg)
{
    if (req == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "input param req is null.\n");
        return;
    }

    char buf[BUF_MAX] = { 0 };
    get_post_message(buf, req);//獲取請求數據,一般是json格式的數據
    if (buf == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "get_post_message return null.\n");
        return;
    }
    else
    {
        //可以使用json庫解析需要的數據
        printf("====line:%d,request data:%s", __LINE__, buf);
    }

    //回響應
    struct evbuffer *retbuff = NULL;
    retbuff = evbuffer_new();
    if (retbuff == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "retbuff is null.\n");
        return;
    }
    evbuffer_add_printf(retbuff, "Receive post request,Thamks for the request!\n");
    evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
    evbuffer_free(retbuff);
}

 

//解析http頭,主要用於get請求時解析uri和請求參數
char *find_http_header(struct evhttp_request *req, struct evkeyvalq *params, const char *query_char)
{
    
    if (req == NULL || params == NULL || query_char == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "input params is null.\n");
        return NULL;
    }

    struct evhttp_uri *decoded = NULL;
    char *query = NULL;
    char *query_result = NULL;
    const char *path;
    const char *uri = evhttp_request_get_uri(req);//獲取請求uri
    
    if (uri == NULL)
    {
        printf("====line:%d,evhttp_request_get_uri return null\n", __LINE__);
        return NULL;
    }
    else
    {
        printf("====line:%d,Got a GET request for <%s>\n", __LINE__, uri);
    }

    /*
    //解碼uri
    decoded = evhttp_uri_parse(uri);
    if (!decoded)
    {
        printf("====line:%d,It's not a good URI. Sending BADREQUEST\n", __LINE__);
        evhttp_send_error(req, HTTP_BADREQUEST, 0);
        return;
    }
    //獲取uri中的path部分
    path = evhttp_uri_get_path(decoded);
    if (path == NULL)
    {
        path = "/";
    }
    else
    {
        printf("====line:%d,path is:%s\n", __LINE__, path);
    }

    //獲取uri中的參數部分
    query = (char*)evhttp_uri_get_query(decoded);
    if (query == NULL)
    {
        printf("====line:%d,evhttp_uri_get_query return null\n", __LINE__);
        return NULL;
    }*/

    //查詢指定參數的值
    evhttp_parse_query_str(query, params);
    query_result = (char*)evhttp_find_header(params, query_char);
    return query_result;
}


//處理get請求
void http_handler_testget_msg(struct evhttp_request *req, void *arg)
{

    if (req == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "input param req is null.\n");
        return;
    }

    char *sign = NULL;
    char *data = NULL;
    struct evkeyvalq sign_params = { 0 };
    sign = find_http_header(req, &sign_params, "sign");//獲取get請求uri中的sign參數
    if (sign == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "request uri no param sign.\n");
    }
    else
    {
        printf("====line:%d,get request param: sign=[%s]\n", __LINE__, sign);
    }
    
    data = find_http_header(req, &sign_params, "data");//獲取get請求uri中的data參數
    if (data == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "request uri no param data.\n");
    }
    else
    {
        printf("====line:%d,get request param: data=[%s]\n", __LINE__, data);
    }
    printf("\n");

    //回響應
    struct evbuffer *retbuff = NULL;
    retbuff = evbuffer_new();
    if (retbuff == NULL)
    {
        printf("====line:%d,%s\n", __LINE__, "retbuff is null.");
        return;
    }
    evbuffer_add_printf(retbuff, "Receive get request,Thamks for the request!");
    evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
    evbuffer_free(retbuff);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM