-
Notifications
You must be signed in to change notification settings - Fork 0
/
taalspel.c
131 lines (110 loc) · 3.55 KB
/
taalspel.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*Build inside of https://github.com/ellzey/libevhtp
by adding these lines
add_executable(taalspel taalspel.c)
target_link_libraries(taalspel libevhtp ${LIBEVHTP_EXTERNAL_LIBS} ${SYS_LIBS})
to CMakeLists.txt and doing
cd build
cmake ..
make
make test
./taalspel
open test-form.html in firefox and watch command line for 3rd and 4th test
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <evhtp.h>
//FIXME#include <mongo.h>
void
receive_plurals(evhtp_request_t * req, void * a) {
if (req->method != htp_method_POST) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
int max_len = 1024;
int len = evbuffer_get_length(req->buffer_in);
if (len == 0 || len >= max_len) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
char buf[max_len];
evbuffer_remove(req->buffer_in, buf, len);
buf[len] = '\0';
if (buf[0] != 'd' || buf[1] != '=' || strstr(buf + 2, "=") != NULL || strstr(buf, "&") != NULL) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
printf("d: %s\n", buf + 2);
evhtp_send_reply(req, EVHTP_RES_OK);
}
void
provide_score(evhtp_request_t * req, void * a) {
if (req->method != htp_method_POST) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
int max_len = 1024;
int len = evbuffer_get_length(req->buffer_in);
if (len == 0 || len >= max_len) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
char buf[max_len];
evbuffer_remove(req->buffer_in, buf, len);
buf[len] = '\0';
char* amp_pos = strchr(buf, '&');
if (amp_pos == NULL || amp_pos != strrchr(buf, '&')) {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
buf[amp_pos - buf] = '\0';
if (buf[0] == 'u') {
if (buf[1] != '=' || strstr(buf + 2, "=") != NULL || strstr(buf, "&") != NULL || amp_pos[2] != '=' || strstr(amp_pos + 3, "=") != NULL || amp_pos[1] != 'd') {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
printf("u: %s\n", buf + 2);
printf("d: %s\n", amp_pos + 3);
} else {
if (buf[0] == 'd') {
if (buf[1] != '=' || strstr(buf + 2, "=") != NULL || strstr(buf, "&") != NULL || amp_pos[2] != '=' || strstr(amp_pos + 3, "=") != NULL || amp_pos[1] != 'u') {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
printf("u: %s\n", amp_pos + 3);
printf("d: %s\n", buf + 2);
} else {
evhtp_send_reply(req, EVHTP_RES_BADREQ);
return;
}
}
evhtp_send_reply(req, EVHTP_RES_OK);
}
int
main(int argc, char ** argv) {
evbase_t * evbase = event_base_new();
evhtp_t * htp = evhtp_new(evbase, NULL);
evhtp_callback_t * cb_plurals = NULL;
evhtp_callback_t * cb_score = NULL;
int num_threads = 6;
cb_plurals = evhtp_set_cb(htp, "/blahblah/plurals", receive_plurals, "plurals");
cb_score = evhtp_set_cb(htp, "/blahblah/score", provide_score, "score");
#ifndef EVHTP_DISABLE_EVTHR
evhtp_use_threads(htp, NULL, num_threads, NULL);
#else
if (num_threads != 1) {
printf("Error: multithreading is not supported\n");
exit(1);
}
#endif
evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024);
event_base_loop(evbase, 0);
evhtp_unbind_socket(htp);
evhtp_callback_free(cb_score);
evhtp_callback_free(cb_plurals);
evhtp_free(htp);
event_base_free(evbase);
return 0;
}