-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevhtp_op.c
71 lines (56 loc) · 1.85 KB
/
evhtp_op.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
/*******************************************
*
* Copyright (c) 2017-2018 SYZ
*
* https://github.com/criticalstack/libevhtp
*
gcc evhtp_op.c \
-I/home/syz/workshop/opensource/libevhtp/include \
-I/home/syz/workshop/opensource/libevhtp/build/include \
-L/home/syz/workshop/opensource/libevhtp/build -levhtp \
-L/usr/local/libevent/lib \
-levent -pthread -lcrypto -levent_openssl \
-levent_core -levent_extra -levent_core -lssl -o release/evhtp_op_c
*
*******************************************/
#include <errno.h>
#include <evhtp/evhtp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void testcb(evhtp_request_t* req, void* a) {
const char* str = a;
evbuffer_add_printf(req->buffer_out, "%s %lu", str, strlen(str));
evhtp_send_reply(req, EVHTP_RES_OK);
}
void issue161cb(evhtp_request_t* req, void* a) {
struct evbuffer* b = evbuffer_new();
if (evhtp_request_get_proto(req) == EVHTP_PROTO_10) {
evhtp_request_set_keepalive(req, 0);
}
evhtp_send_reply_start(req, EVHTP_RES_OK);
evbuffer_add(b, "foo", 3);
evhtp_send_reply_body(req, b);
evbuffer_add(b, "bar\n\n", 5);
evhtp_send_reply_body(req, b);
evhtp_send_reply_end(req);
evhtp_safe_free(b, evbuffer_free);
}
int main(int argc, char** argv) {
evbase_t* evbase = event_base_new();
evhtp_t* htp = evhtp_new(evbase, NULL);
evhtp_set_cb(htp, "/simple/", testcb, "simple");
evhtp_set_cb(htp, "/1/ping", testcb, "one");
evhtp_set_cb(htp, "/1/ping.json", testcb, "two");
evhtp_set_cb(htp, "/issue161", issue161cb, NULL);
#ifndef EVHTP_DISABLE_EVTHR
evhtp_use_threads_wexit(htp, NULL, NULL, 8, NULL);
#endif
evhtp_bind_socket(htp, "0.0.0.0", 8081, 2048);
event_base_loop(evbase, 0);
evhtp_unbind_socket(htp);
evhtp_safe_free(htp, evhtp_free);
evhtp_safe_free(evbase, event_base_free);
return 0;
}