This commit is contained in:
Guillem George 2025-11-25 16:49:33 +01:00
parent 5236d71e7e
commit 9d94c4d4e2
5 changed files with 97 additions and 7 deletions

View file

@ -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 \

View file

@ -4,9 +4,14 @@
#include <stdlib.h>
#include <string.h>
#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)
// {}

View file

@ -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

View file

@ -1,14 +1,21 @@
#include "files.h"
#include <stdio.h>
#include <sys/stat.h>
#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)

View file

@ -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