-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathph.c
242 lines (210 loc) · 7.74 KB
/
ph.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Author: Nicu Pavel <[email protected]>
* Copyright (c) 2021 Green Electronics LLC
* The MIT License (MIT)
*
*/
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include "config.h"
#include "debug.h"
#include "dlist.h"
#include "http.h"
#include "messages.h"
#include "server.h"
int main(int argc, char *argv[]) {
int len, rc;
int listen_sd = -1, new_sd = -1;
int shutdown = 0, clear_unused_fds = 0;
int close_connection;
char buffer[READ_BUF_LEN];
struct pollfd fds[DEFAULT_SERVER_MAX_CLIENTS];
int nfds = 2, current_size = 0, i, j;
extern ph_config_t config;
config_parse_opts(argc, argv, &config);
config_print(&config);
if (messages_init(config.max_lines) < 0) {
exit(EXIT_FAILURE);
}
if ((listen_sd = server_setup_socket(&config)) < 0) {
server_print_error(listen_sd);
exit(EXIT_FAILURE);
}
int flags = fcntl(fileno(stdin), F_GETFD, 0);
if (fcntl(fileno(stdin), F_SETFL, flags | O_NONBLOCK) < 0) {
fprintf(stderr, "Error calling fcntl in %s: %s\n", __FUNCTION__,
strerror(errno));
return EXIT_FAILURE;
}
memset(fds, 0, sizeof(fds));
fds[0].fd = listen_sd;
fds[0].events = POLLIN;
fds[1].fd = fileno(stdin);
fds[1].events = POLLIN;
if (config.timeout > 0) {
config.timeout *= 1000;
}
do {
rc = poll(fds, nfds, config.timeout);
if (rc < 0) {
perror("poll() error");
break;
}
if (rc == 0) {
fprintf(stderr, " poll() timed out.\n");
break;
}
current_size = nfds;
for (i = 0; i < current_size; i++) {
if (fds[i].revents == 0) continue;
if (fds[i].revents != POLLIN) {
debug_print("fd=%d; events: %s%s%s\n", fds[i].fd,
(fds[i].revents & POLLIN) ? "POLLIN " : "",
(fds[i].revents & POLLHUP) ? "POLLHUP " : "",
(fds[i].revents & POLLERR) ? "POLLERR " : "");
continue;
}
if (fds[i].fd == listen_sd) {
new_sd = accept(listen_sd, NULL, NULL);
if (new_sd < 0) {
if (errno != EWOULDBLOCK) {
perror("accept() error");
shutdown = 1;
}
break;
}
flags = fcntl(new_sd, F_GETFD, 0);
if (fcntl(new_sd, F_SETFD, flags | O_NONBLOCK)) {
fprintf(stderr,
"Error setting O_NONBLOCK on accepted socket!\n");
break;
}
debug_print(" Incoming connection - %d\n", new_sd);
fds[nfds].fd = new_sd;
fds[nfds].events = POLLIN;
nfds++;
} else if (fds[i].fd == fileno(stdin)) {
do {
rc = read(fds[i].fd, buffer, sizeof(buffer) - 1);
if (rc < 0) {
if (errno != EWOULDBLOCK) {
perror("recv() error");
close_connection = 1;
}
break;
}
if (rc == 0) {
debug_print("%s\n", "Connection closed\n");
close_connection = 1;
break;
}
len = rc;
if (message_add_chunk(buffer, len) < 0) {
break;
}
} while (1);
// Only save a complete line if not received faster than rate
if (message_check_save(config.rate, config.output_stdin) < 0) {
break;
}
} else {
close_connection = 0;
do {
rc = recv(fds[i].fd, buffer, sizeof(buffer) - 1, 0);
if (rc < 0) {
if (errno != EWOULDBLOCK) {
perror("recv() error");
close_connection = 1;
}
break;
}
if (rc == 0) {
debug_print("%s", "Connection closed\n");
close_connection = 1;
break;
}
len = rc;
buffer[len] = '\0';
debug_print("%s\n", buffer);
char *response = NULL;
void *result = NULL;
int type = http_parse_request(buffer, &result);
if (type == PH_HTTP_ERROR) {
response = http_response_error();
} else if (type == PH_HTTP_CLEAR) {
messages_clear();
response = http_response_ok();
} else if (type == PH_HTTP_CONFIG) {
if (http_parse_request_config(result, &config) < 0) {
response = http_response_error();
} else {
response = http_response_ok();
// Call list resize even if no config max_lines change
messages_resize(config.max_lines);
}
} else if (type == PH_HTTP_LINES) {
long int lines = 0;
if (result != NULL) {
lines = *((long int *)result);
debug_print("Lines: %ld\n", lines);
}
if (lines > config.max_lines || lines == 0)
lines = config.max_lines;
char *http_body = messages_get_formated(
lines, config.body_prefix, config.body_suffix,
config.line_delimiter);
if (http_body) {
response = http_response_lines(http_body);
free(http_body);
} else {
response = http_response_error();
}
} else {
response = http_response_error();
}
if (!response) {
break;
}
rc = send(fds[i].fd, response, strlen(response), 0);
free(response);
if (rc < 0) {
perror("send() error");
close_connection = 1;
break;
}
} while (1);
if (close_connection) {
close(fds[i].fd);
fds[i].fd = -1;
clear_unused_fds = 1;
}
} /* End of connection is readable */
} /* End of loop pollable descriptors */
if (clear_unused_fds) {
clear_unused_fds = 0;
for (i = 0; i < nfds; i++) {
if (fds[i].fd == -1) {
for (j = i; j < nfds; j++) {
fds[j].fd = fds[j + 1].fd;
}
i--;
nfds--;
}
}
}
} while (!shutdown);
for (i = 0; i < nfds; i++) {
if (fds[i].fd >= 0) close(fds[i].fd);
}
messages_clear();
return 0;
}