From 7a614bd0d4b7af987956395332a3d4e9b783d87a Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 28 Nov 2025 18:49:42 +0100 Subject: [PATCH] fix: ff --- httpd/src/http/http.c | 23 ++++++++++++++++------- httpd/src/utils/string/string.c | 19 +++++++++++++++++++ httpd/src/utils/string/string.h | 11 +++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index 6df9389..c53c468 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -9,6 +9,7 @@ #include #include +#include "../config/config.h" #include "../logger/logs.h" #include "../utils/files/files.h" #include "../utils/parsing/words.h" @@ -30,16 +31,16 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req) ssize_t i = 0; ssize_t skipped; - if (res == NULL) + if (res == NULL || req == NULL) return ERR_HTTP_INTERNAL_ERROR; // Method - if (strncmp(req->data, "GET", strlen("GET")) == 0) + if (string_compare_n_str(req, "GET", strlen("GET")) == 0) { res->method = GET; i += strlen("GET"); } - else if (strncmp(req->data, "HEAD", strlen("HEAD")) == 0) + else if (string_compare_n_str(req, "HEAD", strlen("HEAD")) == 0) { res->method = HEAD; i += strlen("HEAD"); @@ -176,10 +177,17 @@ static void check_req(struct http_request *req, struct http_response *resp) resp->status_code = 405; // Protocol - char *protocol = string_to_charptr(req->protocol); - if (strcmp(protocol, HTTP_VERSION) != 0) + // Oui il y a plus beau, mais on a le temps ou on l'a pas, moi je l'ai pas, + // alors si t'es pas content t'as qu'à le modifier toi même vu que tu as + // visiblement le code source. <3 + if (string_compare_strictly_n_str(req->protocol, "HTTP/", strlen("HTTP/")) + != 0) + resp->status_code = 400; + else if (string_compare_strictly_n_str(req->protocol, "HTTP/1.1", + strlen("HTTP/1.1") != 0)) resp->status_code = 505; - free(protocol); + + printf("%s %d\n", req->protocol->data, resp->status_code); } // === Functions @@ -304,7 +312,8 @@ struct http_response *generate_response(struct http_request *req) return NULL; // Protocol - char *protocol = HTTP_VERSION; + // char *protocol = HTTP_VERSION; + char *protocol = "HTTP/1.1"; res->protocol = string_create(protocol, strlen(protocol)); // Target diff --git a/httpd/src/utils/string/string.c b/httpd/src/utils/string/string.c index 62300b0..5a28b5a 100644 --- a/httpd/src/utils/string/string.c +++ b/httpd/src/utils/string/string.c @@ -154,3 +154,22 @@ char *string_to_charptr(struct string *str) return res; } + +// WARNING takes n as valid, will not stop on '\0' +int string_compare_strictly_n_str(const struct string *str1, const char *str2, + size_t n) +{ + if (str1->size < n) + return -1; + + size_t i = 0; + int res = 0; + while (i < n) + { + res += str1->data[i]; + res -= str2[i]; + i++; + } + + return res; +} diff --git a/httpd/src/utils/string/string.h b/httpd/src/utils/string/string.h index 0d01688..a69f47c 100644 --- a/httpd/src/utils/string/string.h +++ b/httpd/src/utils/string/string.h @@ -75,4 +75,15 @@ void string_destroy(struct string *str); */ char *string_to_charptr(struct string *str); +/* + ** @brief TODO + ** + ** @param str1 + ** @param str2 + ** @param n + ** + ** @return + */ +int string_compare_strictly_n_str(const struct string *str1, const char *str2, + size_t n); #endif /* ! STRING_H */