-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_redirect.c
70 lines (53 loc) · 1.03 KB
/
example_redirect.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "mows.h"
static mows *m;
static void
http_index(mows *m, mows_request *r, int s)
{
(void)m;
(void)r;
mows_redirect302("redirect.html", s);
}
void
ctrl_c_handler(int s)
{
(void)s;
mows_stop(m);
mows_free(m);
exit(EXIT_SUCCESS);
}
int
main()
{
int rc;
struct sigaction sa;
/* setup Ctrl+C handler */
memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_handler = ctrl_c_handler;
sigaction(SIGINT, &sa, NULL);
m = mows_alloc(NULL);
if (!m) {
fprintf(stderr, "Can't allocate memory\n");
return EXIT_FAILURE;
}
mows_set_root(m, "./media/");
if (!mows_add_page(m, "/", &http_index)) {
fprintf(stderr, "Can't add page\n");
return EXIT_FAILURE;
}
rc = mows_start(m, "127.0.0.1", 8081, 1);
if (rc != 0) {
fprintf(stderr, "Can't start server, error: %s\n",
strerror(rc));
return EXIT_FAILURE;
}
printf("Press ENTER or Ctrl+C to stop\n");
getchar();
mows_stop(m);
mows_free(m);
return EXIT_SUCCESS;
}