fix: fixed a major bug in Makefile that made clean delete .h files, reworked src tree with a new time utility, made logger a full module and changed http strings to internal strings to handle null chacacters, + misc changes

This commit is contained in:
Gu://em_ 2025-11-21 19:39:15 +01:00
parent 3beeb31c9b
commit 5e609624a0
13 changed files with 144 additions and 44 deletions

View file

@ -6,12 +6,18 @@ LDLIBS =
CFLAGS_DBG = -g
ASAN_DBG_FLAGS = -fsanitize=address
UTILS_SRCS = src/utils/string/string.c
CONFIG_SRCS = src/config/config.c
SRCS = $(UTILS_SRCS) $(CONFIG_SRCS) src/main.c
UTILS_SRCS = src/utils/string/string.c \
src/utils/time/fmt_time.c
MODULES_SRCS = src/config/config.c \
src/server/server.c \
src/http/http.c \
src/logger/logs.c
SRCS = $(UTILS_SRCS) \
$(MODULES_SRCS) \
src/main.c
UTILS_OBJS = ${UTILS_SRCS:.c=.o}
CONFIG_OBJS = ${CONFIG_SRCS:.c=.o}
MODULES_OBJS = ${MODULES_SRCS:.c=.o}
OBJS = ${SRCS:.c=.o}
TARGET=httpd

View file

@ -97,7 +97,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
// Server name
case 's':
cfg->servers->server_name = string_create(optarg, strlen(optarg));
cfg->servers->server_name = optarg;
break;
// Port
@ -236,7 +236,6 @@ struct config *parse_configuration(int argc, char *argv[])
void config_destroy(struct config *config)
{
string_destroy(config->servers->server_name);
free(config->servers);
free(config);
}

View file

@ -1,6 +1,8 @@
#ifndef HTTP_H
#define HTTP_H
#include "../utils/string/string.h"
// === Enums
enum http_method
@ -9,37 +11,37 @@ enum http_method
POST,
PUT,
DELETE,
// patch,
// head,
// options,
// connect,
// trace
// PATCH,
// HEAD,
// OPTIONS,
// CONNECT,
// TRACE
};
// === Structures
struct http_header
{
char *field;
char *value;
struct string *field;
struct string *value;
struct http_header *next;
};
struct http_request
{
enum http_method method;
char *path;
char *protocol;
char *protocol_version;
struct string *path;
struct string *protocol;
struct string *protocol_version;
struct http_header *headers; // Headers linked list
};
struct http_response
{
char *protocol;
char *protocol_version;
struct string *protocol;
struct string *protocol_version;
int status_code;
char *status_msg;
struct string *status_msg;
struct http_header *headers; // Headers linked list
};

View file

@ -1 +0,0 @@
utils/logs

48
httpd/src/logger/errors.c Normal file
View file

@ -0,0 +1,48 @@
#include "errors.h"
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include "logs.h"
// === Static variable
static struct logs_config config;
// === Functions
void errlog_init(bool enabled, FILE *logfile_stream)
{
config.enabled = enabled;
config.logfile_stream = logfile_stream;
}
void print_err()
{
print_log_err("%s", get_err());
}
void print_log_err(char *format, ...)
{
// Log prefix (time and server name)
fprintf(config.logfile_stream, "%s [%s] ", get_time(),
config.server_cfg->server_name);
// Print actual log
va_list args;
va_start(args, format);
vfprintf(config.logfile_stream, format, args);
va_end(args);
// New line
fprintf(config.logfile_stream, "\n");
// Print to stderr
fprintf(stderr, "Error: %s", get_err());
}
char *get_err()
{
return strerror(errno);
}

28
httpd/src/logger/errors.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef ERRORS_H
#define ERRORS_H
#include <stdbool.h>
#include <stdio.h>
/* @brief Initialize the error logging submodule
* @warning Do not use 'as is', use log_init() instead
*/
void errlog_init(bool enabled, FILE *logfile_stream);
/* @brief Retrieves the last error with errno and prints the corresponding
* error message in the logs and stderr
*/
void print_err();
/* @brief Prints error logs, just like print_log(), and also to stderr
*
*/
void print_log_err(char* format, ...);
/* @brief Prints error logs, just like print_log()
*
*
*/
char* get_err();
#endif // ! ERRORS_H

View file

@ -3,23 +3,14 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
// === Static variable
#include "../utils/fmt_time/fmt_time.h"
// === Static variables
static struct logs_config config;
// === Static functions
// Returns a string containing the formatted GMT time
static char *get_time(void)
{
time_t local_ts = time(NULL);
struct tm *gmt_time = gmtime(&local_ts);
return asctime(gmt_time);
}
// === Functions
int log_init(struct config *global_config)

View file

@ -3,7 +3,7 @@
#include <stdio.h>
#include "../../config/config.h"
#include "../config/config.h"
struct logs_config
{
@ -18,7 +18,6 @@ struct logs_config
*
* @return 0 on success, an error code otherwise
*/
int log_init(struct config* config);
/* @brief Prints logs (or not) conformly to the config given by the user.
@ -28,7 +27,6 @@ int log_init(struct config* config);
* @param format
* @param ...
*/
void print_log(char* format, ...);
/* @brief Prints request logs with the adequate format in the logfile

View file

@ -2,6 +2,8 @@
#include <stdio.h>
#include "config/config.h"
#include "logger/logs.h"
#include "server/server.h"
#define ERR_ARG 2
@ -11,5 +13,8 @@ int main(int argc, char **argv)
if (config == NULL)
return ERR_ARG;
log_init(config);
start_server("localhost", config->servers->port);
return 0;
}

View file

@ -1,22 +1,22 @@
#include "server.h"
#define _POSIX_C_SOURCE 200112L
#include <netdb.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/socket.h>
#include <unistd.h>
#include "../logger/errors.h"
#include "../logger/logs.h"
// === Definitions
#define BUFFER_SIZE 1024
// === Static functions
// TODO
// See getaddrinfo(3) at RETURN VALUE
static void print_getaddr_error(int err_code)
{}
// Creates and bind the server communication socket
static int get_socket(const char *hostname, const char *port)
{
@ -31,7 +31,7 @@ static int get_socket(const char *hostname, const char *port)
int err = getaddrinfo(hostname, port, &hints, &client_addr);
if (err != 0)
{
print_getaddr_error(err);
print_err();
return -1;
}
@ -45,7 +45,10 @@ static int get_socket(const char *hostname, const char *port)
sock_fd = socket(cur_addr->ai_family, cur_addr->ai_socktype,
cur_addr->ai_protocol);
if (sock_fd == -1)
{
cur_addr = cur_addr->ai_next;
continue;
}
// ... Has something to do with not changing port
int sock_opt = -1;
@ -88,9 +91,9 @@ static void send_back(int client_fd)
// === Functions
void start_server(const char *hostname, const char *port)
void start_server(const char *host, const char *port)
{
int server_socket = get_socket(hostname, port);
int server_socket = get_socket(host, port);
if (server_socket == -1)
// TODO log that
return;

View file

@ -6,6 +6,6 @@
* @param hpstname
* @param port
*/
void start_server(const char* hostname, const char* port);
void start_server(const char* host, const char* port);
#endif // ! SERVER_H

View file

@ -0,0 +1,10 @@
#include <time.h>
#include "fmt_time.h"
static char *get_time(void)
{
time_t local_ts = time(NULL);
struct tm *gmt_time = gmtime(&local_ts);
return asctime(gmt_time);
}

View file

@ -0,0 +1,11 @@
#ifndef FMT_TIME_H
#define FMT_TIME_H
/* @brief Calculates the GMT time based on machine's local time
* and returns it as a string
*
* @return A NULL-terminated string containing the fromatted GMT time
*/
char* get_time();
#endif // ! FMT_TIME_H