使用libevent实现简单http服务(一)

使用libevent实现一个简单的http服务器,源码如下:

//简单http服务
#include <event.h>
#include <evhttp.h>
#include <event2/thread.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <assert.h>
#include <string>
#include <unistd.h>
#include <functional>
#include <memory>

///
//监听类
class simple_listener
{
public:
    simple_listener(const std::string &addr, int port)
        : m_address(addr)
        , m_port(port)
        , m_socket(-1)
    {}
    
    ~simple_listener(void) { clear(); }
    
    bool start(void)
    {
        assert(m_socket == -1);
        do {
            if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
                 fprintf(stderr, "ERROR failed to set sigpipe signal\n");
                 break;
             }

             if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                 fprintf(stderr, "ERROR failed to create socket\n");
                 break;
             }

             int one = 1;
             if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int)) < 0) {
                 fprintf(stderr, "ERROR failed to set reuseaddr\n");
                 break;
             }

             struct sockaddr_in addr;
             memset(&addr, 0, sizeof(addr));
             addr.sin_family = AF_INET;
             addr.sin_addr.s_addr = inet_addr(m_address.c_str());
             addr.sin_port = htons(m_port);
             if (bind(m_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
                 fprintf(stderr, "ERORR failed to bind %s:%d\n", m_address.c_str(), m_port);
                 break;
             }

             if (listen(m_socket, 512) < 0) {
                 fprintf(stderr, "ERROR failed to listen\n");
                 break;
             }

             int flags;
             if ((flags = fcntl(m_socket, F_GETFL, 0)) < 0 || fcntl(m_socket, F_SETFL, flags | O_NONBLOCK) < 0) {
                 fprintf(stderr, "ERROR failed to set nonblock\n");
                 break;
             }

            return true;
        } while (false);
        
        clear();
        
        return false;
    }
    
    void stop(void) { clear(); }
    
    int get_socket(void) const { return m_socket; }
    
    simple_listener(const simple_listener &) = delete;
    simple_listener(simple_listener &&) = delete;
    simple_listener &operator=(const simple_listener &) = delete;
    simple_listener &operator=(simple_listener &&) = delete;
    
protected:
    void clear(void)
    {
        if (m_socket != -1) {
            close(m_socket);
            m_socket = -1;
        }
    }
    
    const std::string m_address; //监听地址
    const int m_port; //监听端口
    int m_socket; //socket 句柄
};

///
//处理类
class simple_processor
{
public:
    simple_processor(int identify) : m_identify(identify) {}
    
    void process(struct evhttp_request *ctx)
    {
        fprintf(stdout, "INFO process %d recv request!\n", m_identify);
        evhttp_add_header(evhttp_request_get_output_headers(ctx), "Content-Type", "text/html");
        evbuffer *buf = evbuffer_new();
        std::string str = "Hello World !";
        evbuffer_add(buf, str.c_str(), str.size());
        evhttp_send_reply(ctx, 200, "OK", buf);
    }
    
    simple_processor(const simple_processor &) = delete;
    simple_processor(simple_processor &&) = delete;
    simple_processor operator=(const simple_processor &) = delete;
    simple_processor operator=(simple_processor &&) = delete;
    
private:
    const int m_identify; //唯一标识
};

///
//http服务类
//L 监听类,必须支持int get_socket(void)
//P 处理类,必须支持void process(struct evhttp_request *)
template <typename L, typename P>
class simple_httpserver
{
public:
    static void http_callback(evhttp_request *ctx, void *args)
    {
        assert(args != nullptr);
        auto pthis = (simple_httpserver *)args;
        pthis->process(ctx);
    }
    
public:
    simple_httpserver(std::shared_ptr<L> &listener, std::shared_ptr<P> &processer)
        : m_listener_ptr(listener)
        , m_proccessor_ptr(processer)
        , m_event_base_ptr(nullptr)
        , m_http_ptr(nullptr)
    {}
    
    ~simple_httpserver(void) { clear();}
    
    bool start(void)
    {
        assert(m_event_base_ptr == nullptr && m_http_ptr == nullptr);
        do {
            m_event_base_ptr = event_base_new();
            if (m_event_base_ptr == nullptr) {
                fprintf(stderr, "ERROR failed to new event base \n");
                break;
            }
            
            m_http_ptr = evhttp_new(m_event_base_ptr);
            if (m_http_ptr == nullptr) {
                fprintf(stderr, "ERROR failed to new event http\n");
                break;
            }
            
            if (evhttp_accept_socket(m_http_ptr, m_listener_ptr->get_socket()) < 0) {
                fprintf(stderr, "ERROR failed to accept socket");
                break;
            }
            
            evhttp_set_gencb(m_http_ptr, &simple_httpserver::http_callback, this);
            
            return true;
        } while (false);
        
        clear();
        return false;
    }
    
    bool run(void)
    {
        assert(m_event_base_ptr != nullptr);
            
        int ret_code = event_base_dispatch(m_event_base_ptr);
        printf("INFO event dispatch exit , ret code : %d\n", ret_code);
        return ret_code == 0;
    }
    
    bool stop(void)
    {
        if (m_event_base_ptr != nullptr)
            event_base_loopbreak(m_event_base_ptr);
        
        return true;
    }
    
    simple_httpserver(const simple_httpserver &) = delete;
    simple_httpserver(simple_httpserver &&) = delete;
    simple_httpserver &operator=(const simple_httpserver &) = delete;
    simple_httpserver &operator=(simple_httpserver &&) = delete;
    
protected:
    void clear(void)
    {
        if (m_http_ptr != nullptr) {
            evhttp_free(m_http_ptr);
            m_http_ptr = nullptr;
        }
        
        if (m_event_base_ptr != nullptr) {
            event_base_free(m_event_base_ptr);
            m_event_base_ptr = nullptr;
        }
    }
    
    void process(evhttp_request *ctx)
    {
        m_proccessor_ptr->process(ctx);
    }
    
    std::shared_ptr<L> m_listener_ptr; //socket 句柄
    std::shared_ptr<P> m_proccessor_ptr; //实例标识
    event_base *m_event_base_ptr; //event_base指针
    evhttp *m_http_ptr; //http指针
};

std::shared_ptr<simple_listener> g_listener_ptr; //监听对象
std::shared_ptr<simple_processor> g_processor_ptr; //请求处理对象
std::shared_ptr<simple_httpserver<simple_listener, simple_processor>> g_httpserver_ptr; //http服务对象

void sigterm(int sig)
{
    fprintf(stdout, "INFO recv term signal !\n");
    if (g_httpserver_ptr.get() != nullptr) {
        fprintf(stdout, "INFO httpserver before stop !\n");
        g_httpserver_ptr->stop();
        fprintf(stdout, "INFO httpserver after stop !\n");
    }
}

int main(int argc, char **argv)
{
    signal(SIGTERM, sigterm);
    g_listener_ptr = std::make_shared<simple_listener>("0.0.0.0", 8080);
    if (!g_listener_ptr->start()) {
        fprintf(stderr, "ERROR failed to open listener\n");
        return -1;
    }
    
    g_processor_ptr = std::make_shared<simple_processor>(0);
    
    g_httpserver_ptr = std::make_shared<simple_httpserver<simple_listener, simple_processor>>(g_listener_ptr, g_processor_ptr);
    if (!g_httpserver_ptr->start()) {
        fprintf(stderr, "ERROR failed to start http server\n");
        return -1;
    }
    
    g_httpserver_ptr->run();
    
	return 0;
}

以下是基于libevent实现简单HTTP服务器代码,仅供参考: ```c #include <event2/event.h> #include <event2/http.h> #include <event2/buffer.h> #include <event2/keyvalq_struct.h> static void http_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; const char *uri = evhttp_request_get_uri(req); buf = evbuffer_new(); if (!buf) { perror("failed to create response buffer"); return; } evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/plain"); if (evhttp_request_get_command(req) == EVHTTP_REQ_GET) { evbuffer_add_printf(buf, "Received a GET request for %s\n", uri); } else if (evhttp_request_get_command(req) == EVHTTP_REQ_POST) { evbuffer_add_printf(buf, "Received a POST request for %s\n", uri); } else { evbuffer_add_printf(buf, "Unsupported HTTP method\n"); } evhttp_send_reply(req, HTTP_OK, "OK", buf); evbuffer_free(buf); } int main(int argc, char **argv) { struct event_base *base; struct evhttp *http; struct evhttp_bound_socket *handle; base = event_base_new(); if (!base) { perror("failed to create event base"); return 1; } http = evhttp_new(base); if (!http) { perror("failed to create http server"); return 1; } evhttp_set_cb(http, "/", http_handler, NULL); handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", 8080); if (!handle) { perror("failed to bind socket"); return 1; } event_base_dispatch(base); evhttp_free(http); event_base_free(base); return 0; } ``` 以上代码实现了一个简单HTTP服务器,监听本机的8080端口,当有HTTP请求到来时,根据请求方法(GET或POST)返回相应的响应。其中,libevent提供了evhttp库来实现HTTP协议的处理,evhttp_request_get_uri()获取请求的URI,evhttp_request_get_output_headers()获取请求的响应头,evhttp_add_header()设置响应头,evhttp_request_get_command()获取请求的方法类型(GET或POST),evbuffer_add_printf()将格式化的字符串添加到响应体中,evhttp_send_reply()发送响应,evbuffer_free()释放响应体的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值