This commit is contained in:
Gu://em_ 2025-11-28 18:49:42 +01:00
parent eee5f940e3
commit 7a614bd0d4
3 changed files with 46 additions and 7 deletions

View file

@ -9,6 +9,7 @@
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "../config/config.h"
#include "../logger/logs.h" #include "../logger/logs.h"
#include "../utils/files/files.h" #include "../utils/files/files.h"
#include "../utils/parsing/words.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 i = 0;
ssize_t skipped; ssize_t skipped;
if (res == NULL) if (res == NULL || req == NULL)
return ERR_HTTP_INTERNAL_ERROR; return ERR_HTTP_INTERNAL_ERROR;
// Method // Method
if (strncmp(req->data, "GET", strlen("GET")) == 0) if (string_compare_n_str(req, "GET", strlen("GET")) == 0)
{ {
res->method = GET; res->method = GET;
i += strlen("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; res->method = HEAD;
i += strlen("HEAD"); i += strlen("HEAD");
@ -176,10 +177,17 @@ static void check_req(struct http_request *req, struct http_response *resp)
resp->status_code = 405; resp->status_code = 405;
// Protocol // Protocol
char *protocol = string_to_charptr(req->protocol); // Oui il y a plus beau, mais on a le temps ou on l'a pas, moi je l'ai pas,
if (strcmp(protocol, HTTP_VERSION) != 0) // 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; resp->status_code = 505;
free(protocol);
printf("%s %d\n", req->protocol->data, resp->status_code);
} }
// === Functions // === Functions
@ -304,7 +312,8 @@ struct http_response *generate_response(struct http_request *req)
return NULL; return NULL;
// Protocol // Protocol
char *protocol = HTTP_VERSION; // char *protocol = HTTP_VERSION;
char *protocol = "HTTP/1.1";
res->protocol = string_create(protocol, strlen(protocol)); res->protocol = string_create(protocol, strlen(protocol));
// Target // Target

View file

@ -154,3 +154,22 @@ char *string_to_charptr(struct string *str)
return res; 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;
}

View file

@ -75,4 +75,15 @@ void string_destroy(struct string *str);
*/ */
char *string_to_charptr(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 */ #endif /* ! STRING_H */