Loop the app.
This commit is contained in:
@@ -19,16 +19,24 @@ struct {
|
||||
{ "PATCH", PATCH }
|
||||
};
|
||||
|
||||
static volatile sig_atomic_t stop = 0;
|
||||
|
||||
void handle_sigint(const int _)
|
||||
{
|
||||
stop = 1;
|
||||
printf("Caught SIGINT!\n");
|
||||
}
|
||||
|
||||
void start_http_server(http_server_t* http_server, const char *addr, const short port)
|
||||
{
|
||||
const int server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd == -1)
|
||||
{
|
||||
perror("Failed to create new socket.\n");
|
||||
perror("Failed to create new socket");
|
||||
return;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
const int opt = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
||||
perror("setsockopt(SO_REUSEADDR) failed");
|
||||
}
|
||||
@@ -41,21 +49,31 @@ void start_http_server(http_server_t* http_server, const char *addr, const short
|
||||
const int bind_c = bind(server_fd, (struct sockaddr*)&address, sizeof(address));
|
||||
if (bind_c == -1)
|
||||
{
|
||||
perror("Failed to bind address.\n");
|
||||
perror("Failed to bind address");
|
||||
return;
|
||||
}
|
||||
|
||||
const int listen_c = listen(server_fd, 3);
|
||||
if (listen_c == -1)
|
||||
{
|
||||
perror("Failed to begin listening.\n");
|
||||
perror("Failed to begin listening");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = handle_sigint;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
sigaction(SIGINT, &sa, nullptr);
|
||||
|
||||
printf("Server is listening on %s:%d...\n", addr, port);
|
||||
|
||||
size_t addr_len = sizeof(address);
|
||||
process_conn(http_server, server_fd, (struct sockaddr*)&address, (socklen_t*)&addr_len);
|
||||
|
||||
while (!stop)
|
||||
{
|
||||
process_conn(http_server, server_fd, (struct sockaddr*)&address, (socklen_t*)&addr_len);
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
}
|
||||
@@ -65,7 +83,7 @@ void process_conn(const http_server_t* http_server, const int server_fd, struct
|
||||
const int socket_d = accept(server_fd, address, addr_len);
|
||||
if (socket_d == -1)
|
||||
{
|
||||
perror("Failed to accept connection.\n");
|
||||
perror("Failed to accept connection");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -74,7 +92,7 @@ void process_conn(const http_server_t* http_server, const int server_fd, struct
|
||||
const ssize_t bytes_read = read(socket_d, buffer, 1024);
|
||||
if (bytes_read == -1)
|
||||
{
|
||||
perror("Failed white reading socket.");
|
||||
perror("Failed while reading socket");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -109,6 +127,8 @@ void process_conn(const http_server_t* http_server, const int server_fd, struct
|
||||
http_request.http_content = nullptr;
|
||||
http_request.http_content_len = 0;
|
||||
|
||||
// TODO: Needs functionality to continue reading the request if didn't receive the whole thing,
|
||||
// and figuring when there are two requests read at once. Boring packet stuff basically.
|
||||
for (int i = 0; i < bytes_read; i++)
|
||||
{
|
||||
bool drop = false;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#define SIMPLEHTTPSERVER_H
|
||||
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Reference in New Issue
Block a user