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
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