libevent简单的http实现

阅读更多
1 #include

  2 #include

  3 #include

  4 #include

  5 #include

  6 #include

  7 #include

  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

你可能感兴趣的:(gcc)