-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
139 lines (109 loc) · 3.86 KB
/
main.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
131
132
133
134
135
136
137
138
/*
arfhttpd: Yet another HTTP server
Copyright (C) 2023 arf20 (Ángel Ruiz Fernandez)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
httpd.c: Program entry point
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "config.h"
#include "socket.h"
#include "tls_socket.h"
#include "cache.h"
#include "log.h"
/* Text file (no '\0's) */
int
file_read(const char *path, char **buff) {
FILE *f = fopen(path, "r");
if (f == NULL) return -1;
fseek(f, 0L, SEEK_END);
size_t s = ftell(f);
fseek(f, 0L, SEEK_SET);
*buff = malloc(s + 1);
fread(*buff, 1, s, f);
(*buff)[s] = '\0';
return s;
}
int
main(int argc, char **argv) {
printf("arfhttpd GPLv3+\n");
char *config = NULL;
if (file_read("../arfhttpd.conf", &config) < 0) {
printf("Error reading config file: %s\n", strerror(errno));
exit(1);
}
config_parse(config);
/* Print config */
string_node_t *listen_current = listen_list;
while (listen_current) {
printf("listen %s\n", listen_current->str);
listen_current = listen_current->next;
}
listen_current = tls_listen_list;
while (listen_current) {
printf("listen %s tls\n", listen_current->str);
listen_current = listen_current->next;
}
if (cert_file)
printf("certificate %s\n", cert_file);
if (cert_key_file)
printf("certificate_key %s\n", cert_key_file);
location_node_t *location_current = location_list;
while (location_current) {
printf("location %s\n", location_current->location);
config_node_t *config_current = location_current->config;
while (config_current) {
printf("\t%s ", config_type_strs[config_current->type]);
if (config_current->param1) printf("%s ", config_current->param1);
if (config_current->param2) printf("%s", config_current->param2);
printf("\n");
config_current = config_current->next;
}
location_current = location_current->next;
}
if (cache_init() < 0) {
exit(1);
}
/* Start accept threads */
server_start(listen_list);
tls_server_start(tls_listen_list, cert_file, cert_key_file);
console_log(LOG_INFO, "\t", "Server started", NULL);
/* Join accept threads (wait for them to exit) */
if (!listen_socket_list) {
console_log(LOG_ERR, "\t", "Nothing to accept", NULL);
exit(1);
}
fd_thread_node_t *listen_socket_current = listen_socket_list;
while (listen_socket_current) {
if (pthread_join(listen_socket_current->thread, NULL) != 0) {
console_log(LOG_ERR, "\t", "Error joining accept thread", NULL);
exit(1);
}
listen_socket_current = listen_socket_current->next;
}
if (!tls_listen_socket_list) {
console_log(LOG_ERR, "\t", "Nothing to accept", NULL);
exit(1);
}
listen_socket_current = tls_listen_socket_list;
while (listen_socket_current) {
if (pthread_join(listen_socket_current->thread, NULL) != 0) {
console_log(LOG_ERR, "\t", "Error joining accept thread", NULL);
exit(1);
}
listen_socket_current = listen_socket_current->next;
}
return 0;
}