libevent简单的http实现

1 #include <sys/types.h>

2 #include <sys/time.h>

3 #include <sys/queue.h>

4 #include <stdlib.h>

5 #include <err.h>

6 #include <event.h>

7 #include <evhttp.h>

8

9 void generic_handler(struct evhttp_request *req, void *arg)

10 {

11 struct evbuffer *buf;

12 buf = evbuffer_new();

13

14 if (buf == NULL)

15 err(1, "failed to create response buffer");

16

17 evbuffer_add_printf(buf, "Requested: %s", evhttp_request_uri(req));

18 evhttp_send_reply(req, HTTP_OK, "OK", buf);

19

20 evbuffer_free(buf);

21 }

22

23 int main(int argc, char **argv)

24 {

25 struct evhttp *httpd;

26 event_init();

27 httpd = evhttp_start("0.0.0.0", 8080);

28

29 /* Set a callback for requests to "/specific". */

30 /* evhttp_set_cb(httpd, "/specific", another_handler, NULL); */

31

32 /* Set a callback for all other requests. */

33 evhttp_set_gencb(httpd, generic_handler, NULL);

34

35 event_dispatch();

36

37 /* Not reached in this code as it is now. */

38 evhttp_free(httpd);

39 return 0;

40 }


gcc s_http.c -levent
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值