1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <stdint.h>
4
- #include <stdarg.h>
5
- #include <signal.h>
6
4
#include <unistd.h>
7
- #include <setjmp.h>
8
- #include <errno.h>
9
5
#include <string.h>
10
- #include <pthread.h>
11
- #include <sys/socket.h>
12
- #include <sys/resource.h>
13
- #include <sys/param.h>
14
- #include <sys/wait.h>
15
- #include <sys/queue.h>
16
- #include <linux/sched.h>
17
- #include <netdb.h>
18
6
#include <err.h>
19
7
20
8
#include <dynamic.h>
23
11
24
12
#include "setup.h"
25
13
26
- static char date [] = "Thu, 01 Jan 1970 00:00:00 GMT" ;
27
-
28
- static int timer_event (void * state , int type , void * data )
29
- {
30
- (void ) state ;
31
- (void ) data ;
32
- if (type != REACTOR_TIMER_EVENT_CALL )
33
- err (1 , "timer" );
34
-
35
- reactor_http_date (date );
36
- return REACTOR_OK ;
37
- }
38
-
39
- static int plaintext (reactor_http * http )
14
+ static reactor_status tfb (reactor_event * event )
40
15
{
41
- char content_length [11 ], * body = "Hello, World!" ;
42
- size_t size ;
43
-
44
- size = strlen (body );
45
- reactor_util_u32toa (size , content_length );
46
- reactor_http_send_response (http , (reactor_http_response []){{
47
- .version = 1 ,
48
- .status = 200 ,
49
- .reason = {"OK" , 2 },
50
- .headers = 4 ,
51
- .header [0 ] = {{"Server" , 6 }, {"libreactor" , 10 }},
52
- .header [1 ] = {{"Date" , 4 }, {date , strlen (date )}},
53
- .header [2 ] = {{"Content-Type" , 12 }, {"text/plain" , 10 }},
54
- .header [3 ] = {{"Content-Length" , 14 }, {content_length , strlen (content_length )}},
55
- .body = {body , size }
56
- }});
57
- return REACTOR_OK ;
16
+ reactor_server_session * session = (reactor_server_session * ) event -> data ;
17
+
18
+ if (reactor_vector_equal (session -> request -> target , reactor_vector_string ("/json" ))) {
19
+ char json_msg [32 ];
20
+ (void ) clo_encode ((clo []) {clo_object ({"message" , clo_string ("Hello, World!" )})}, json_msg , sizeof (json_msg ));
21
+ reactor_server_ok (session , reactor_vector_string ("application/json" ), reactor_vector_string (json_msg ));
22
+ return REACTOR_OK ;
23
+ }
24
+ else if (reactor_vector_equal (session -> request -> target , reactor_vector_string ("/plaintext" ))) {
25
+ reactor_server_ok (session , reactor_vector_string ("text/plain" ), reactor_vector_string ("Hello, World!" ));
26
+ return REACTOR_OK ;
27
+ }
28
+ else {
29
+ reactor_server_ok (session , reactor_vector_string ("text/plain" ), reactor_vector_string ("Hello from libreactor!\n" ));
30
+ return REACTOR_OK ;
31
+ }
58
32
}
59
33
60
- static int json (reactor_http * http )
61
- {
62
- char body [4096 ], content_length [11 ];
63
- size_t size ;
64
-
65
- (void ) clo_encode ((clo []) {clo_object ({"message" , clo_string ("Hello, World!" )})}, body , sizeof (body ));
66
- size = strlen (body );
67
- reactor_util_u32toa (size , content_length );
68
- reactor_http_send_response (http , (reactor_http_response []){{
69
- .version = 1 ,
70
- .status = 200 ,
71
- .reason = {"OK" , 2 },
72
- .headers = 4 ,
73
- .header [0 ] = {{"Server" , 6 }, {"libreactor" , 10 }},
74
- .header [1 ] = {{"Date" , 4 }, {date , strlen (date )}},
75
- .header [2 ] = {{"Content-Type" , 12 }, {"application/json" , 16 }},
76
- .header [3 ] = {{"Content-Length" , 14 }, {content_length , strlen (content_length )}},
77
- .body = {body , size }
78
- }});
79
- return REACTOR_OK ;
80
- }
81
-
82
- int http_event (void * state , int type , void * data )
83
- {
84
- reactor_http * http = state ;
85
- reactor_http_request * request ;
86
-
87
- if (reactor_unlikely (type != REACTOR_HTTP_EVENT_REQUEST ))
88
- {
89
- reactor_http_close (http );
90
- free (http );
91
- return REACTOR_ABORT ;
92
- }
93
- request = data ;
94
-
95
- if (reactor_http_header_value (& request -> path , "/plaintext" ))
96
- return plaintext (http );
97
- else if (reactor_http_header_value (& request -> path , "/json" ))
98
- return json (http );
99
- else
100
- {
101
- reactor_http_send_response (http , (reactor_http_response []){{
102
- .version = 1 ,
103
- .status = 404 ,
104
- .reason = {"Not Found" , 9 },
105
- .headers = 3 ,
106
- .header [0 ] = {{"Server" , 6 }, {"libreactor" , 10 }},
107
- .header [1 ] = {{"Date" , 4 }, {date , strlen (date )}},
108
- .header [2 ] = {{"Content-Length" , 14 }, {"0" , 1 }},
109
- .body = {NULL , 0 }
110
- }});
111
- return REACTOR_OK ;
112
- }
113
- }
114
-
115
- static int tcp_event (void * state , int type , void * data )
116
- {
117
- reactor_http * http ;
118
-
119
- if (type != REACTOR_TCP_EVENT_ACCEPT )
120
- err (1 , "tcp" );
121
-
122
- http = malloc (sizeof * http );
123
- if (!http )
124
- abort ();
125
- (void ) reactor_http_open (http , http_event , http , * (int * ) data , REACTOR_HTTP_FLAG_SERVER );
126
- return REACTOR_OK ;
127
- }
128
34
129
35
int main ()
130
36
{
131
- reactor_timer timer ;
132
- reactor_tcp tcp ;
37
+ reactor_server server ;
133
38
134
- setup (1 , 0 );
135
- (void ) reactor_core_construct ();
136
- (void ) reactor_timer_open (& timer , timer_event , & timer , 1 , 1000000000 );
137
- (void ) reactor_tcp_open (& tcp , tcp_event , & tcp , "0.0.0.0" , "8080" , REACTOR_TCP_FLAG_SERVER );
138
- (void ) reactor_core_run ();
139
- reactor_core_destruct ();
140
- }
39
+ setup ();
40
+ reactor_construct ();
41
+ reactor_server_construct (& server , NULL , NULL );
42
+ reactor_server_route (& server , tfb , NULL );
43
+ (void ) reactor_server_open (& server , "0.0.0.0" , "8080" );
44
+
45
+ reactor_run ();
46
+ reactor_destruct ();
47
+ }
0 commit comments