本文转自:https://blog.csdn.net/Rin_Cherish/article/details/78045993
最近两天,需要给客户提供一个程序状态的http查询接口,一开始想用nginx访问fcgi,但是觉得复杂了,就一个状态查询功能而已。于是就使用了 libevent提供的http库。
使用完后总是要分享一下的,开始源码分享吧
/*************************************************************************
* > File Name: http.c
* > Author: liuqianghua
* > Mail: liuqianghua@xunlei.com
* > Created Time: Wed 20 Sep 2017 07:17:48 AM CST
************************************************************************/
#include <signal.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/buffer.h>
#include <event2/keyvalq_struct.h>
static void hello_request_cb(struct evhttp_request *req, void *arg);
static void version_cb (struct evhttp_request *req, void *arg);
int main(int argc, char **argv)
{
struct event_base *base;
struct evhttp *http;
struct evhttp_bound_socket *handle;
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return (1);
/* firstly, you should build a event_base looper */
base = event_base_new();
if (!base) {
fprintf(stderr, "Couldn't create an event_base: exiting\n");
return 1;
}
/* sencondly, Create a new evhttp object to handle requests. */
http = evhttp_new(base);
if (!http) {
fprintf(stderr, "couldn't create evhttp. Exiting.\n");
return 1;
}
/* 想想我们在nginx里面配的各种uri,就知道这是什么了,是的就是那个东西*/
evhttp_set_cb(http, "/hello", hello_request_cb, NULL);
/* We want to accept arbitrary requests, so we need to set a "generic"
* * cb. We can also add callbacks for specific paths. */
/* 这是一个兜底的回调,即上面的各种uri都未匹配的情况下,进入这个通用的匹配,
* 就相当于nginx的404页面或者nginx version的页面的作用*/
evhttp_set_gencb(http, version_cb, argv[1]);
/* Now we tell the evhttp what port to listen on */
handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", 8090);
if (!handle) {
fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
(int)8090);
return 1;
}
event_base_dispatch(base);
return 0;
}
static void hello_request_cb(struct evhttp_request *req, void *arg)
{
const char *cmdtype;
struct evkeyvalq *headers;
struct evkeyval *header;
struct evbuffer *buf;
switch (evhttp_request_get_command(req)) {
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
case EVHTTP_REQ_POST: cmdtype = "POST"; break;
case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
default: cmdtype = "unknown"; break;
}
printf("Received a %s request for %s\nHeaders:\n",
cmdtype, evhttp_request_get_uri(req));
headers = evhttp_request_get_input_headers(req);
for (header = headers->tqh_first; header;
header = header->next.tqe_next) {
printf(" %s: %s\n", header->key, header->value);
}
buf = evhttp_request_get_input_buffer(req);
/* 读取报文实体的数据 */
puts("Input data: <<<");
while (evbuffer_get_length(buf)) {
int n;
char cbuf[128];
n = evbuffer_remove(buf, cbuf, sizeof(buf)-1);
if (n > 0)
(void) fwrite(cbuf, 1, n, stdout);
}
/* 向报文实体输出数据 */
puts(">>>");
evhttp_send_reply(req, 200, "OK", NULL);
}
static void version_cb (struct evhttp_request *req, void *arg)
{
fprintf (stderr, "what is my version \n");
evhttp_send_reply(req, 200, "OK", NULL);
}
编译 gcc -g http.c -levent
运行 ./a.out
在浏览器中分别输入
http://192.168.47.131:8090/hello2
http://192.168.47.131:8090/hello
得到如下输出:
what is my version
Received a GET request for /hello
Headers:
Host: 192.168.47.131:8090
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Input data: <<<
>>>