diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index 5c9aa07..954d932 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -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; } diff --git a/httpd/src/logger/errors.c b/httpd/src/logger/errors.c index fb23f4d..c5f2c6e 100644 --- a/httpd/src/logger/errors.c +++ b/httpd/src/logger/errors.c @@ -1,3 +1,5 @@ +#define _POSIX_C_SOURCE 200809L + #include "errors.h" #include @@ -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()); diff --git a/httpd/src/logger/errors.h b/httpd/src/logger/errors.h index 2543fc3..2d9303c 100644 --- a/httpd/src/logger/errors.h +++ b/httpd/src/logger/errors.h @@ -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 diff --git a/httpd/src/logger/logs.c b/httpd/src/logger/logs.c index fabd878..2f98923 100644 --- a/httpd/src/logger/logs.c +++ b/httpd/src/logger/logs.c @@ -1,11 +1,12 @@ +#define _POSIX_C_SOURCE 200809L + #include "logs.h" +#include #include -#include -#include -#include #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) diff --git a/httpd/src/logger/logs.h b/httpd/src/logger/logs.h index f1b309a..e8f337d 100644 --- a/httpd/src/logger/logs.h +++ b/httpd/src/logger/logs.h @@ -8,7 +8,7 @@ struct logs_config { bool enabled; - FILE* logfile_stream; + int logfile_fd; struct server_config* server_cfg; }; diff --git a/httpd/src/main.c b/httpd/src/main.c index 2d8bdca..8e36192 100644 --- a/httpd/src/main.c +++ b/httpd/src/main.c @@ -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 diff --git a/httpd/src/server/server.c b/httpd/src/server/server.c index 9b39a6f..850754b 100644 --- a/httpd/src/server/server.c +++ b/httpd/src/server/server.c @@ -1,3 +1,10 @@ +// === Definitions + +#define _POSIX_C_SOURCE 200112L + +#define BUFFER_SIZE 1024 + +// === Includes #include "server.h" #include @@ -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 diff --git a/httpd/src/server/server.h b/httpd/src/server/server.h index b25ee40..407ec1a 100644 --- a/httpd/src/server/server.h +++ b/httpd/src/server/server.h @@ -1,7 +1,7 @@ #ifndef SERVER_H #define SERVER_H -#define _POSIX_C_SOURCE 200112L +// #define _POSIX_C_SOURCE 200112L /* @brief * diff --git a/httpd/src/utils/string/string.c b/httpd/src/utils/string/string.c index 29df1ef..78b3879 100644 --- a/httpd/src/utils/string/string.c +++ b/httpd/src/utils/string/string.c @@ -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;