-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.c
73 lines (62 loc) · 1.37 KB
/
http_test.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
#include "http_server.h"
#include <stdio.h>
#include <string.h>
struct http_server
{
struct http_request_interest hri;
};
static struct http_server server;
static int count = 0;
static void
request_arrived(void *cookie)
{
struct http_request *hr;
if (!http_dequeue_request(&server.hri, &hr))
{
fprintf(stderr, "Unable to dequeue HTTP request\n");
return;
}
printf("request has arrived\n%s %s\n", hr->method, hr->uri);
http_request_set_status_code(hr, 200);
http_request_set_content_length(hr, 16);
http_request_set_content_type(hr, "text/plain");
http_request_set_server(hr, "My tiny little web server");
http_request_start_reply(hr);
http_request_write(hr, "0123456789ABCDEF", 16);
http_request_end_reply(hr);
if (count++ == 4)
iv_quit();
}
static int
start_server(void)
{
struct http_request_interest *hri = &server.hri;
int rc;
memset(&server, 0, sizeof(server));
hri->ip = "0.0.0.0";
hri->port = "8080";
hri->method = "GET";
hri->host = NULL;
hri->uri = NULL;
hri->cookie = &server;
hri->handler = request_arrived;
rc = http_register_interest(hri);
if (rc < 0)
return 1;
return 0;
}
static void
stop_server(void)
{
http_unregister_interest(&server.hri);
}
int main(int argc, char *argv[])
{
iv_init();
if (start_server() < 0)
return 1;
iv_main();
stop_server();
iv_deinit();
return 0;
}