1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
* chttp
This is not really a library, but a little project for my education. But if you really want to use it, you can.
** Installation
#+begin_src shell
make static
# or
make shared
#+end_src
Copy the directory in the =include/= folder to your project.
** Usage
*** Initializing the server
#+begin_src c
arena arena = {0};
http_server serv = {0};
if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
return 1;
}
#+end_src
*** Adding handlers
#+begin_src c
http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
void hello_world(http_request req) {
req.resp->code = OK;
req.resp->body = CS("Hello world!\n");
http_send(req);
}
#+end_src
*** Adding middleware
#+begin_src c
http_register_global_middleware(&serv, logging_func);
// or
http_register_handler_middleware(&arena, hello_world_handler, logging_func);
void logging_func(http_middleware *self, http_request req) {
// you have to run http_run_next to run the next middleware
http_run_next(self, req);
// or you can just send the response
// http_send(req);
http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
}
#+end_src
*** Listening for requests
And to wrap things up, we need to run
#+begin_src c
listen_and_serve(&serv);
#+end_src
*** Full example
#+begin_src c
#include <chttp/http.h>
#include <chttp/const_strings.h>
void logging_func(http_middleware *self, http_request req) {
http_run_next(self, req);
http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
}
void hello_world(http_request req) {
req.resp->code = OK;
req.resp->body = CS("Hello world!\n");
http_send(req);
}
int main() {
arena arena = {0};
http_server serv = {0};
if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
return 1;
}
http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
http_register_handler_middleware(&arena, hello_world_handler, logging_func);
http_register_global_middleware(&serv, logging_func);
listen_and_serve(&serv);
return 0;
}
#+end_src
|