diff --git a/httpd/Makefile b/httpd/Makefile index 204e10d..386b778 100644 --- a/httpd/Makefile +++ b/httpd/Makefile @@ -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 diff --git a/httpd/src/config/config.c b/httpd/src/config/config.c index c7f8153..dfbe9a7 100644 --- a/httpd/src/config/config.c +++ b/httpd/src/config/config.c @@ -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); } diff --git a/httpd/src/http/http.h b/httpd/src/http/http.h index 8c6714f..95a9cc6 100644 --- a/httpd/src/http/http.h +++ b/httpd/src/http/http.h @@ -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 }; diff --git a/httpd/src/logger b/httpd/src/logger deleted file mode 120000 index c098a94..0000000 --- a/httpd/src/logger +++ /dev/null @@ -1 +0,0 @@ -utils/logs \ No newline at end of file diff --git a/httpd/src/logger/errors.c b/httpd/src/logger/errors.c new file mode 100644 index 0000000..1dfa67e --- /dev/null +++ b/httpd/src/logger/errors.c @@ -0,0 +1,48 @@ +#include "errors.h" + +#include +#include +#include + +#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); +} diff --git a/httpd/src/logger/errors.h b/httpd/src/logger/errors.h new file mode 100644 index 0000000..718a652 --- /dev/null +++ b/httpd/src/logger/errors.h @@ -0,0 +1,28 @@ +#ifndef ERRORS_H +#define ERRORS_H + +#include +#include + +/* @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 diff --git a/httpd/src/utils/logs/logs.c b/httpd/src/logger/logs.c similarity index 82% rename from httpd/src/utils/logs/logs.c rename to httpd/src/logger/logs.c index 8114be6..acbe9ec 100644 --- a/httpd/src/utils/logs/logs.c +++ b/httpd/src/logger/logs.c @@ -3,23 +3,14 @@ #include #include #include -#include #include -// === 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) diff --git a/httpd/src/utils/logs/logs.h b/httpd/src/logger/logs.h similarity index 96% rename from httpd/src/utils/logs/logs.h rename to httpd/src/logger/logs.h index e4ee209..f1b309a 100644 --- a/httpd/src/utils/logs/logs.h +++ b/httpd/src/logger/logs.h @@ -3,7 +3,7 @@ #include -#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 diff --git a/httpd/src/main.c b/httpd/src/main.c index 6419b3b..d36eda7 100644 --- a/httpd/src/main.c +++ b/httpd/src/main.c @@ -2,6 +2,8 @@ #include #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; } diff --git a/httpd/src/server/server.c b/httpd/src/server/server.c index b55188a..f2f7b1f 100644 --- a/httpd/src/server/server.c +++ b/httpd/src/server/server.c @@ -1,22 +1,22 @@ #include "server.h" +#define _POSIX_C_SOURCE 200112L + #include #include #include #include #include +#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; diff --git a/httpd/src/server/server.h b/httpd/src/server/server.h index 80ad392..dac01d8 100644 --- a/httpd/src/server/server.h +++ b/httpd/src/server/server.h @@ -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 diff --git a/httpd/src/utils/time/fmt_time.c b/httpd/src/utils/time/fmt_time.c new file mode 100644 index 0000000..8181509 --- /dev/null +++ b/httpd/src/utils/time/fmt_time.c @@ -0,0 +1,10 @@ +#include + +#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); +} diff --git a/httpd/src/utils/time/fmt_time.h b/httpd/src/utils/time/fmt_time.h new file mode 100644 index 0000000..43f7877 --- /dev/null +++ b/httpd/src/utils/time/fmt_time.h @@ -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