feat: Even more functional
This commit is contained in:
parent
5dbcc3e3d0
commit
dd87ff0fbe
9 changed files with 62 additions and 32 deletions
|
|
@ -170,17 +170,22 @@ void http_init(struct config *cfg)
|
||||||
// TODO handle logs
|
// TODO handle logs
|
||||||
void handle_request(int client_fd)
|
void handle_request(int client_fd)
|
||||||
{
|
{
|
||||||
ssize_t nread = 0;
|
|
||||||
char buffer[BUFFER_SIZE]; // Declared in server.h
|
char buffer[BUFFER_SIZE]; // Declared in server.h
|
||||||
struct string *str = string_create(NULL, 0);
|
struct string *str = string_create(NULL, 0);
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Store request
|
// 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);
|
||||||
}
|
}
|
||||||
|
string_concat_str(str, buffer, nread);
|
||||||
|
|
||||||
// Parse request
|
// Parse request
|
||||||
struct http_request *req = parse_request(str);
|
struct http_request *req = parse_request(str);
|
||||||
|
|
@ -204,8 +209,16 @@ void handle_request(int client_fd)
|
||||||
|
|
||||||
// Send response
|
// Send response
|
||||||
ssize_t nsent;
|
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
|
// Send file
|
||||||
struct http_header *cl_header = get_header(resp->headers, "Content-Length");
|
struct http_header *cl_header = get_header(resp->headers, "Content-Length");
|
||||||
if (cl_header != NULL)
|
if (cl_header != NULL)
|
||||||
|
|
@ -277,7 +290,8 @@ struct http_response *generate_response(struct http_request *req)
|
||||||
// Headers
|
// Headers
|
||||||
char *time = get_time();
|
char *time = get_time();
|
||||||
append_header(&res->headers, create_header("Date", 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)
|
if (res->status_code == 200)
|
||||||
{
|
{
|
||||||
char buf[21] = { 0 }; // (21 ~= log10(2^64)) + 1 (null byte)
|
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
|
// Status message
|
||||||
string_concat_str(res, buf, strlen(buf));
|
string_concat_str(res, buf, strlen(buf));
|
||||||
|
|
||||||
string_concat_str(res, "/r/n", 2);
|
string_concat_str(res, "\r\n", 2);
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
struct http_header *cur_header = resp->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,
|
string_concat_str(res, cur_header->field->data,
|
||||||
cur_header->field->size);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
@ -13,10 +15,10 @@ static struct logs_config config;
|
||||||
|
|
||||||
// === Functions
|
// === Functions
|
||||||
|
|
||||||
void errlog_init(bool enabled, FILE *logfile_stream)
|
void errlog_init(bool enabled, int logfile_fd)
|
||||||
{
|
{
|
||||||
config.enabled = enabled;
|
config.enabled = enabled;
|
||||||
config.logfile_stream = logfile_stream;
|
config.logfile_fd = logfile_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_err()
|
void print_err()
|
||||||
|
|
@ -30,17 +32,17 @@ void print_log_err(char *format, ...)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Log prefix (time and server name)
|
// 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);
|
config.server_cfg->server_name);
|
||||||
|
|
||||||
// Print actual log
|
// Print actual log
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
vfprintf(config.logfile_stream, format, args);
|
vdprintf(config.logfile_fd, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
// New line
|
// New line
|
||||||
fprintf(config.logfile_stream, "\n");
|
dprintf(config.logfile_fd, "\n");
|
||||||
|
|
||||||
// Print to stderr
|
// Print to stderr
|
||||||
fprintf(stderr, "Error: %s", get_err());
|
fprintf(stderr, "Error: %s", get_err());
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
/* @brief Initialize the error logging submodule
|
/* @brief Initialize the error logging submodule
|
||||||
* @warning Do not use 'as is', use log_init() instead
|
* @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
|
/* @brief Retrieves the last error with errno and prints the corresponding
|
||||||
* error message in the logs and stderr
|
* error message in the logs and stderr
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "logs.h"
|
#include "logs.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "../utils/time/fmt_time.h"
|
#include "../utils/time/fmt_time.h"
|
||||||
|
#include "errors.h"
|
||||||
|
|
||||||
// === Static variables
|
// === Static variables
|
||||||
|
|
||||||
|
|
@ -15,11 +16,17 @@ static struct logs_config config;
|
||||||
|
|
||||||
int log_init(struct config *global_config)
|
int log_init(struct config *global_config)
|
||||||
{
|
{
|
||||||
|
int return_value = 0;
|
||||||
config.enabled = global_config->log;
|
config.enabled = global_config->log;
|
||||||
config.logfile_stream = fopen(global_config->log_file, "w");
|
config.server_cfg = global_config->servers;
|
||||||
if (config.logfile_stream == NULL)
|
config.logfile_fd = open(global_config->log_file, O_WRONLY);
|
||||||
return 1;
|
if (config.logfile_fd <= 0)
|
||||||
return 0;
|
{
|
||||||
|
config.enabled = false;
|
||||||
|
return_value = 1;
|
||||||
|
}
|
||||||
|
errlog_init(config.enabled, config.logfile_fd);
|
||||||
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_log(char *format, ...)
|
void print_log(char *format, ...)
|
||||||
|
|
@ -28,17 +35,17 @@ void print_log(char *format, ...)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Log prefix (time and server name)
|
// 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);
|
config.server_cfg->server_name);
|
||||||
|
|
||||||
// Print actual log
|
// Print actual log
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
vfprintf(config.logfile_stream, format, args);
|
vdprintf(config.logfile_fd, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
// New line
|
// New line
|
||||||
fprintf(config.logfile_stream, "\n");
|
dprintf(config.logfile_fd, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_request(char *request_type, char *target, char *client_ip)
|
void log_request(char *request_type, char *target, char *client_ip)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
struct logs_config
|
struct logs_config
|
||||||
{
|
{
|
||||||
bool enabled;
|
bool enabled;
|
||||||
FILE* logfile_stream;
|
int logfile_fd;
|
||||||
struct server_config* server_cfg;
|
struct server_config* server_cfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ int main(int argc, char **argv)
|
||||||
return ERR_ARG;
|
return ERR_ARG;
|
||||||
|
|
||||||
// Initialize modules
|
// Initialize modules
|
||||||
log_init(config);
|
log_init(config); // Ignore ret val
|
||||||
http_init(config);
|
http_init(config);
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,10 @@
|
||||||
|
// === Definitions
|
||||||
|
|
||||||
|
#define _POSIX_C_SOURCE 200112L
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
// === Includes
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
|
@ -10,10 +17,6 @@
|
||||||
#include "../logger/errors.h"
|
#include "../logger/errors.h"
|
||||||
// #include "../logger/logs.h"
|
// #include "../logger/logs.h"
|
||||||
|
|
||||||
// === Definitions
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 1024
|
|
||||||
|
|
||||||
// === Static functions
|
// === Static functions
|
||||||
|
|
||||||
// Creates and bind the server communication socket
|
// Creates and bind the server communication socket
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef SERVER_H
|
#ifndef SERVER_H
|
||||||
#define SERVER_H
|
#define SERVER_H
|
||||||
|
|
||||||
#define _POSIX_C_SOURCE 200112L
|
// #define _POSIX_C_SOURCE 200112L
|
||||||
|
|
||||||
/* @brief
|
/* @brief
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -104,10 +104,12 @@ char *string_to_charptr(struct string *str)
|
||||||
if (str == NULL || str->data == NULL)
|
if (str == NULL || str->data == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
char *res = realloc(str->data, str->size + 1);
|
char *res = calloc(str->size + 1, sizeof(char));
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
memcpy(res, str->data, str->size);
|
||||||
|
|
||||||
res[str->size] = '\0';
|
res[str->size] = '\0';
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue