feat: Even more functional

This commit is contained in:
Gu://em_ 2025-11-26 23:32:01 +01:00
parent 5dbcc3e3d0
commit dd87ff0fbe
9 changed files with 62 additions and 32 deletions

View file

@ -170,17 +170,22 @@ void http_init(struct config *cfg)
// TODO handle logs
void handle_request(int client_fd)
{
ssize_t nread = 0;
char buffer[BUFFER_SIZE]; // Declared in server.h
struct string *str = string_create(NULL, 0);
if (str == NULL)
return;
// Store request
while ((nread = recv(client_fd, buffer, BUFFER_SIZE, 0)) > 0)
ssize_t nread = 0;
// This line here is so hard...
// When you realise that you have to remake all your project to be able to
// parse requests of BUFFER_SIZE size...
// Rendez les bijoux de la couronne (svp)
while ((nread = recv(client_fd, buffer, BUFFER_SIZE, 0)) == BUFFER_SIZE)
{
string_concat_str(str, buffer, nread);
}
string_concat_str(str, buffer, nread);
// Parse request
struct http_request *req = parse_request(str);
@ -204,8 +209,16 @@ void handle_request(int client_fd)
// Send response
ssize_t nsent;
while ((nsent = send(client_fd, res, res->size, 0)) > 0)
;
size_t total_sent = 0;
while (total_sent < res->size)
{
nsent =
send(client_fd, res->data + total_sent, res->size - total_sent, 0);
if (nsent <= 0)
break;
total_sent += nsent;
}
// Send file
struct http_header *cl_header = get_header(resp->headers, "Content-Length");
if (cl_header != NULL)
@ -277,7 +290,8 @@ struct http_response *generate_response(struct http_request *req)
// Headers
char *time = get_time();
append_header(&res->headers, create_header("Date", time));
free(time); // Yes, the one that completely disapeared this year
// free(time); // Yes, the one that completely disapeared this year
// Oopa
if (res->status_code == 200)
{
char buf[21] = { 0 }; // (21 ~= log10(2^64)) + 1 (null byte)
@ -308,7 +322,7 @@ struct string *format_response(struct http_response *resp)
// Status message
string_concat_str(res, buf, strlen(buf));
string_concat_str(res, "/r/n", 2);
string_concat_str(res, "\r\n", 2);
// Headers
struct http_header *cur_header = resp->headers;
@ -317,10 +331,12 @@ struct string *format_response(struct http_response *resp)
string_concat_str(res, cur_header->field->data,
cur_header->field->size);
string_concat_str(res, "/r/n", 2);
string_concat_str(res, "\r\n", 2);
cur_header = cur_header->next;
}
string_concat_str(res, "/r/n", 2);
string_concat_str(res, "\r\n", 2);
return res;
}

View file

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "errors.h"
#include <errno.h>
@ -13,10 +15,10 @@ static struct logs_config config;
// === Functions
void errlog_init(bool enabled, FILE *logfile_stream)
void errlog_init(bool enabled, int logfile_fd)
{
config.enabled = enabled;
config.logfile_stream = logfile_stream;
config.logfile_fd = logfile_fd;
}
void print_err()
@ -30,17 +32,17 @@ void print_log_err(char *format, ...)
return;
// Log prefix (time and server name)
fprintf(config.logfile_stream, "%s [%s] ", get_time(),
dprintf(config.logfile_fd, "%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);
vdprintf(config.logfile_fd, format, args);
va_end(args);
// New line
fprintf(config.logfile_stream, "\n");
dprintf(config.logfile_fd, "\n");
// Print to stderr
fprintf(stderr, "Error: %s", get_err());

View file

@ -7,7 +7,7 @@
/* @brief Initialize the error logging submodule
* @warning Do not use 'as is', use log_init() instead
*/
void errlog_init(bool enabled, FILE *logfile_stream);
void errlog_init(bool enabled, int logfile_fd);
/* @brief Retrieves the last error with errno and prints the corresponding
* error message in the logs and stderr

View file

@ -1,11 +1,12 @@
#define _POSIX_C_SOURCE 200809L
#include "logs.h"
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "../utils/time/fmt_time.h"
#include "errors.h"
// === Static variables
@ -15,11 +16,17 @@ static struct logs_config config;
int log_init(struct config *global_config)
{
int return_value = 0;
config.enabled = global_config->log;
config.logfile_stream = fopen(global_config->log_file, "w");
if (config.logfile_stream == NULL)
return 1;
return 0;
config.server_cfg = global_config->servers;
config.logfile_fd = open(global_config->log_file, O_WRONLY);
if (config.logfile_fd <= 0)
{
config.enabled = false;
return_value = 1;
}
errlog_init(config.enabled, config.logfile_fd);
return return_value;
}
void print_log(char *format, ...)
@ -28,17 +35,17 @@ void print_log(char *format, ...)
return;
// Log prefix (time and server name)
fprintf(config.logfile_stream, "%s [%s] ", get_time(),
dprintf(config.logfile_fd, "%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);
vdprintf(config.logfile_fd, format, args);
va_end(args);
// New line
fprintf(config.logfile_stream, "\n");
dprintf(config.logfile_fd, "\n");
}
void log_request(char *request_type, char *target, char *client_ip)

View file

@ -8,7 +8,7 @@
struct logs_config
{
bool enabled;
FILE* logfile_stream;
int logfile_fd;
struct server_config* server_cfg;
};

View file

@ -16,7 +16,7 @@ int main(int argc, char **argv)
return ERR_ARG;
// Initialize modules
log_init(config);
log_init(config); // Ignore ret val
http_init(config);
// Start server

View file

@ -1,3 +1,10 @@
// === Definitions
#define _POSIX_C_SOURCE 200112L
#define BUFFER_SIZE 1024
// === Includes
#include "server.h"
#include <netdb.h>
@ -10,10 +17,6 @@
#include "../logger/errors.h"
// #include "../logger/logs.h"
// === Definitions
#define BUFFER_SIZE 1024
// === Static functions
// Creates and bind the server communication socket

View file

@ -1,7 +1,7 @@
#ifndef SERVER_H
#define SERVER_H
#define _POSIX_C_SOURCE 200112L
// #define _POSIX_C_SOURCE 200112L
/* @brief
*

View file

@ -104,10 +104,12 @@ char *string_to_charptr(struct string *str)
if (str == NULL || str->data == NULL)
return NULL;
char *res = realloc(str->data, str->size + 1);
char *res = calloc(str->size + 1, sizeof(char));
if (res == NULL)
return NULL;
memcpy(res, str->data, str->size);
res[str->size] = '\0';
return res;