c++ 稳定web服务器开发

采用libevhtp库


 

 

wget  https://openssl.org/source/openssl-1.0.2.tar.gz

tar xzvf openssl-1.0.2f.tar.gz
cd openssl-1.0.2f
./config --prefix=/usr         \
         --openssldir=/etc/ssl \
         --libdir=lib          \
         shared          
make
make install

 

 

libevhtp-1.2.15官网下载 libevent-2.1.12-stable.tar.gz

https://libevent.org/ 

./configure
make
make verify # (optional)
sudo make install

 

wget "https://github.com/ellzey/libevhtp/archive/1.2.15.tar.gz" -O libevhtp-1.2.15.tar.gz

cd build
cmake ..
make
make install

 

 


#include "evhtp/evhtp.h"
#include
 
using namespace std;
 
void testcb(evhtp_request_t* req, void* a)
{
    cout << "HTTP client request info as followed: " << endl;
    
    // 获取HTTP客户端的请求方式
    htp_method eRequestMethod = evhtp_request_get_method(req);
 
    // 根据不同的HTTP客户端请求方式,获取请求数据
    // HTTP客户端请求方式为GET
    if (htp_method_GET == eRequestMethod)
    {
        cout << "GET method!" << endl;
        cout << "GET data(uri) is: " << req->uri->query_raw << endl;
        unsigned char* RequestData = req->uri->query_raw;
        cout << "GET data(content) is: " << RequestData << endl;
    }
    // HTTP客户端请求方式为POST
    else if (htp_method_POST == eRequestMethod)
    {
        cout << "POST method!" << endl;
        char RequestData[1024] = {'\0'};
        evbuffer_copyout(req->buffer_in, RequestData, evhtp_request_content_len(req));
        cout << "POST data(content) is: " << RequestData << endl;
    }
    
    evbuffer_add_reference(req->buffer_out, "This is a HTTP server response.\n", 32, NULL, NULL);
    evhtp_send_reply(req, EVHTP_RES_OK);
}
 
int main(int argc, char ** argv)
{
    evbase_t* evbase = event_base_new();
    evhtp_t* htp = evhtp_new(evbase, NULL);
 
    evhtp_set_cb(htp, "/evhtptest1/", testcb, NULL);
    evhtp_bind_socket(htp, "192.168.121.47", 8081, 1024);
    event_base_loop(evbase, 0);
    
    return 0;
}

 

 

运行的堆栈是这样

 

编译脚本

 g++ -o httpserver httpserver.cpp -I/home/tool/libevhtp-1.2.15/include/ -L/home/tool/libevhtp-1.2.15/build/ -levhtp -levent -lpthread -levent -lopenssl -lssl -lcrypto

 

    event_base_loop(m_evBase, EVLOOP_ONCE|EVLOOP_NONBLOCK);

 

这里可以修改成异步的接口

 

 

支持多线程

#include
#include
#include
#include
#include
#include

#include

using namespace std;

int dump_header(evhtp_kv_t * kv, void * arg)
{
    char * buf = (char *)arg;
    int len = strlen(buf);
    len += snprintf(buf + len, 1024-len, "%s:%s\n", kv->key, kv->val);
    buf[len] = {0};
    return 0;
}

void testcb(evhtp_request_t * request, void * a) {
    if (NULL == request->uri->authority)
        cout << "authority is null" << endl;

    cout << "request method is " << evhtp_request_get_method(request) << endl;
    cout << "uri is " << request->uri->path->full << endl;
    cout << "content length is " << evhtp_request_content_len(request) << endl;
    char buf[1024] = {0};
    evbuffer_copyout(request->buffer_in, buf, evhtp_request_content_len(request));
    buf[evhtp_request_content_len(request)] = {0};
    cout << "content is " << buf << endl;

    cout << "ext_system values is " << evhtp_kv_find(request->headers_in, "fields") << endl;
    char mybuf[1024];
    mybuf[0] = {0};
    evhtp_kvs_for_each(request->headers_in, dump_header, (void *)mybuf);
    cout << "header details:\n" << mybuf << endl;

    evbuffer_add_reference(request->buffer_out, "foobar", 6, NULL, NULL);
    evhtp_send_reply(request, EVHTP_RES_OK);

}

int main(int argc, const char* argv[]) {
    evbase_t *evbase = event_base_new();
    evhtp_t  *htp    = evhtp_new(evbase, NULL);

    evhtp_set_cb(htp, "/statistics/", testcb, NULL);
    evhtp_set_glob_cb(htp, "/statistics/*", testcb, NULL);
    evhtp_use_threads(htp, NULL, 4, NULL);

    evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024);

    event_base_loop(evbase, 0);
    return 0;
}

 

编译成功后把几个文件拷贝出来

libcrypto.a  libevent.a libevent_openssl.a  libevhtp.a libssl.a

gcc 的编译选项记得添加

-ldl


 

 

你可能感兴趣的:(杂七杂八,c++)