2018-09-03 22:54:02 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
#include "../common/socket_helper.h"
|
2018-09-08 21:26:34 +00:00
|
|
|
#include "socket_server.h"
|
2018-09-03 22:54:02 +00:00
|
|
|
#include <errno.h>
|
2018-09-10 03:34:29 +00:00
|
|
|
#include <stdbool.h>
|
2018-09-03 22:54:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-09-09 23:51:51 +00:00
|
|
|
#include <string.h>
|
2018-09-10 03:34:29 +00:00
|
|
|
#include <sys/signal.h>
|
2018-09-08 21:26:34 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2018-09-03 22:54:02 +00:00
|
|
|
|
2018-09-10 03:34:29 +00:00
|
|
|
bool running = true;
|
|
|
|
|
|
|
|
void handle_signal(int signal) {
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
2018-09-03 22:54:02 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
if (argc != 2) {
|
|
|
|
printf("%s", USAGE_STRING);
|
|
|
|
}
|
|
|
|
char *port = argv[1];
|
|
|
|
long port_num = strtol(port, NULL, 10);
|
|
|
|
if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) {
|
|
|
|
printf("%s", PORT_ERROR);
|
2018-09-10 03:34:29 +00:00
|
|
|
return 1;
|
2018-09-03 22:54:02 +00:00
|
|
|
}
|
2018-09-10 03:34:29 +00:00
|
|
|
struct sigaction sa = {.sa_handler = &handle_signal, .sa_flags = 0};
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
|
2018-09-08 21:26:34 +00:00
|
|
|
struct server_info info = setup(port_num);
|
2018-09-09 23:51:51 +00:00
|
|
|
if (info.status == STATUS_ERROR) {
|
|
|
|
printf("%s\n", strerror(errno));
|
2018-09-10 03:34:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while (running) {
|
|
|
|
enum socket_result serve_result = serve_one_request(info.sock_fd);
|
|
|
|
if (serve_result != RESULT_OK && errno != EINTR) {
|
|
|
|
printf("Got error code %d when serving a request.\n", serve_result);
|
|
|
|
}
|
2018-09-09 23:51:51 +00:00
|
|
|
}
|
2018-09-08 21:26:34 +00:00
|
|
|
close(info.sock_fd);
|
2018-09-03 22:54:02 +00:00
|
|
|
}
|