From 9d94c4d4e21e5a70fc4656ed9148981bfacc5c8e Mon Sep 17 00:00:00 2001 From: Guillem George Date: Tue, 25 Nov 2025 16:49:33 +0100 Subject: [PATCH] patata --- httpd/Makefile | 2 +- httpd/src/http/http.c | 49 +++++++++++++++++++++++++++++++++-- httpd/src/http/http.h | 10 +++++++ httpd/src/utils/files/files.c | 11 ++++++-- httpd/src/utils/files/files.h | 32 +++++++++++++++++++++-- 5 files changed, 97 insertions(+), 7 deletions(-) diff --git a/httpd/Makefile b/httpd/Makefile index 36eeedf..35f440b 100644 --- a/httpd/Makefile +++ b/httpd/Makefile @@ -10,7 +10,7 @@ UTILS_SRCS = src/utils/string/string.c \ src/utils/time/fmt_time.c \ src/utils/parsing/words.c \ src/utils/parsing/blanks.c \ - utils/files/files.c + src/utils/files/files.c MODULES_SRCS = src/config/config.c \ src/server/server.c \ src/http/http.c \ diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index 17291d4..30229f2 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -4,9 +4,14 @@ #include #include +#include "../utils/files/files.h" #include "../utils/parsing/words.h" #include "headers.h" +// === Static variables + +static struct config *config; + // === Static functions // Parses the status line of req, stores the result in res and returns the @@ -60,8 +65,29 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req) return i; } +static void split_target(struct http_request *req) +{ + size_t i = 0; + while (i < req->path->size) + { + if (req->path->data[i] == '?') + { + req->queries = + string_create(req->path->data + i, req->path->size - i); + req->path->size = i; + return; + } + i++; + } +} + // === Functions +void http_init(struct config *cfg) +{ + config = cfg; +} + // TODO handle logs and free adequately struct http_request *parse_request(struct string *req) { @@ -75,6 +101,9 @@ struct http_request *parse_request(struct string *req) if (nread <= 0) return NULL; + // Split path and query + split_target(res); + i += nread; // Headers @@ -85,8 +114,24 @@ struct http_request *parse_request(struct string *req) return res; } -// struct http_response *generate_response(struct http_request *req) -// {} +// TODO handle logs and free adequately +struct http_response *generate_response(struct http_request *req) +{ + // Check filename + if (!check_filename(req->path)) + return NULL; + + int err = is_directory(req->path->data); + if (err == -1) + return NULL; + + if (err == 1) + { + string_concat_str(req->path, "/", 1); + string_concat_str(req->path, config->servers->default_file, + strlen(config->servers->default_file)); + } +} // struct string *format_response(struct http_response *resp) // {} diff --git a/httpd/src/http/http.h b/httpd/src/http/http.h index c1ca4a8..a35fbbe 100644 --- a/httpd/src/http/http.h +++ b/httpd/src/http/http.h @@ -14,6 +14,7 @@ // === Includes #include "../utils/string/string.h" +#include "../config/config.h" // === Enums @@ -43,6 +44,7 @@ struct http_request { enum http_method method; struct string *path; + struct string *queries; struct string *protocol; struct http_header *headers; // Headers linked list }; @@ -57,6 +59,14 @@ struct http_response // === Functions +/* @brief Initializes the HTTP module with the given configuration + * + * @warning Do no use any other function of this module before calling that one + * + * @param cfg + */ + void http_init(struct config* cfg); + /* @brief Parses the HTTP request and splits it into a request structure * * @param req diff --git a/httpd/src/utils/files/files.c b/httpd/src/utils/files/files.c index 0729041..3b1b756 100644 --- a/httpd/src/utils/files/files.c +++ b/httpd/src/utils/files/files.c @@ -1,14 +1,21 @@ #include "files.h" #include +#include #include "../string/string.h" bool file_exists(const char *path) {} -bool is_directory(const char *path) -{} +int is_directory(const char *path) +{ + struct stat path_stat; + if (stat(path, &path_stat) != 0) + return S_ISDIR(path_stat.st_mode); + else + return -1; +} // TODO handle logging struct string *get_file_content(const char *path) diff --git a/httpd/src/utils/files/files.h b/httpd/src/utils/files/files.h index b44133b..e338d91 100644 --- a/httpd/src/utils/files/files.h +++ b/httpd/src/utils/files/files.h @@ -19,7 +19,16 @@ * * @return */ -bool file_exists(const char *path); +// bool file_exists(const char *path); + +/* + * @brief + * + * @param path + * + * @return 1 if path is a directory, 0 if it is not and -1 if path is not valid + */ +int is_directory(const char* path); /* * @brief @@ -28,7 +37,8 @@ bool file_exists(const char *path); * * @return */ -bool is_directory(const char* path); + char* get_file(const char* path); + /* * @brief @@ -39,4 +49,22 @@ bool is_directory(const char* path); */ struct string* get_file_content(const char *path); +/* + * @brief + * + * @param path + * + * @return + */ +bool check_filename(struct string* path); + +/* + * @brief + * + * @param path + * + * @return + */ +bool sanitize_filename(struct string* filename); + #endif // ! FILES_H