-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
57 lines (45 loc) · 1.11 KB
/
main.c
File metadata and controls
57 lines (45 loc) · 1.11 KB
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
/*
* main.c
*
* Created on: May 31, 2018
* Author: hhdang
*/
#include <signal.h>
#include <string.h>
#include "simple_http_server.h"
#include "util.h"
static void siginfoHandler(int sig, siginfo_t *si, void *ucontext)
{
simple_http_server_stop();
}
static void register_signal_handler()
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_sigaction = siginfoHandler;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGINT, &act, NULL) == -1){
fprintf(stderr, "Cannot register signal handlr for SIGINT\n");
}
}
static void urlIndexHandler(FILE *f)
{
const char*str = "<H1>HELLO!</H1>";
fprintf(f, "HTTP/1.1 200"
"\r\n"
"content-length: %zu"
"\r\n"
"content-type: text/html; charset=iso-8859-1"
"\r\n"
"\r\n"
"%s", strlen(str), str);
}
int main(int argc, char **argv)
{
register_signal_handler();
simple_http_server_init(6637);
simple_http_server_add_handler("/", urlIndexHandler);
simple_http_server_start();
simple_http_server_destroy_handlers();
return 0;
}