fix: even more night fixes (seems like a friday)
This commit is contained in:
parent
7633ac9de5
commit
c675e3253b
18 changed files with 111 additions and 44 deletions
|
|
@ -33,7 +33,7 @@ struct config
|
||||||
char *pid_file;
|
char *pid_file;
|
||||||
char *log_file;
|
char *log_file;
|
||||||
bool log;
|
bool log;
|
||||||
char* protocol_version;
|
char *protocol_version;
|
||||||
|
|
||||||
struct server_config *servers;
|
struct server_config *servers;
|
||||||
enum daemon daemon;
|
enum daemon daemon;
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../server/server.h"
|
#include "../server/server.h"
|
||||||
|
#include "../utils/files/files.h"
|
||||||
|
|
||||||
static struct config *config;
|
static struct config *config;
|
||||||
|
|
||||||
|
|
@ -43,7 +45,23 @@ void stop_daemon(void)
|
||||||
|
|
||||||
int start_daemon(void)
|
int start_daemon(void)
|
||||||
{
|
{
|
||||||
start_server("localhost", config->servers->port);
|
pid_t pid = fork();
|
||||||
|
if (!pid) // Daemon
|
||||||
|
{
|
||||||
|
start_server("localhost", config->servers->port);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Write pid
|
||||||
|
int err = write_pid(config->pid_file, pid);
|
||||||
|
if (err != 0)
|
||||||
|
{
|
||||||
|
kill(pid, SIGINT);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ int get_pid();
|
||||||
|
|
||||||
/* @brief
|
/* @brief
|
||||||
*/
|
*/
|
||||||
void daemon_init(struct config* cfg);
|
void daemon_init(struct config *cfg);
|
||||||
|
|
||||||
/* @brief
|
/* @brief
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ ssize_t parse_headers(struct http_request *res, struct string *req,
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
struct http_header* get_header(struct http_header *headers, const char* field);
|
struct http_header *get_header(struct http_header *headers, const char *field);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -67,7 +67,7 @@ struct http_header* get_header(struct http_header *headers, const char* field);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
struct http_header* create_header(const char* field, const char* value);
|
struct http_header *create_header(const char *field, const char *value);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
|
||||||
|
|
@ -326,10 +326,6 @@ struct string *format_response(struct http_response *resp)
|
||||||
|
|
||||||
string_concat_str(res, "\r\n", 2);
|
string_concat_str(res, "\r\n", 2);
|
||||||
|
|
||||||
// Time
|
|
||||||
char *time = get_time();
|
|
||||||
string_concat_str(res, time, strlen(time));
|
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
struct http_header *cur_header = resp->headers;
|
struct http_header *cur_header = resp->headers;
|
||||||
while (cur_header != NULL)
|
while (cur_header != NULL)
|
||||||
|
|
@ -337,6 +333,11 @@ 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, ": ", 2);
|
||||||
|
|
||||||
|
string_concat_str(res, cur_header->value->data,
|
||||||
|
cur_header->value->size);
|
||||||
|
|
||||||
string_concat_str(res, "\r\n", 2);
|
string_concat_str(res, "\r\n", 2);
|
||||||
|
|
||||||
cur_header = cur_header->next;
|
cur_header = cur_header->next;
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@
|
||||||
|
|
||||||
// === Includes
|
// === Includes
|
||||||
|
|
||||||
#include "../utils/string/string.h"
|
|
||||||
#include "../config/config.h"
|
#include "../config/config.h"
|
||||||
|
#include "../utils/string/string.h"
|
||||||
|
|
||||||
// === Enums
|
// === Enums
|
||||||
|
|
||||||
|
|
@ -42,7 +42,7 @@ struct http_header
|
||||||
|
|
||||||
struct http_request
|
struct http_request
|
||||||
{
|
{
|
||||||
enum http_method method;
|
enum http_method method;
|
||||||
struct string *target;
|
struct string *target;
|
||||||
struct string *queries;
|
struct string *queries;
|
||||||
struct string *protocol;
|
struct string *protocol;
|
||||||
|
|
@ -66,13 +66,13 @@ struct http_response
|
||||||
*
|
*
|
||||||
* @param cfg
|
* @param cfg
|
||||||
*/
|
*/
|
||||||
void http_init(struct config* cfg);
|
void http_init(struct config *cfg);
|
||||||
|
|
||||||
/* @brief Reads, parses the request and responds adequately all-in-one
|
/* @brief Reads, parses the request and responds adequately all-in-one
|
||||||
*
|
*
|
||||||
* @param client_fd
|
* @param client_fd
|
||||||
*/
|
*/
|
||||||
void handle_request(int client_fd);
|
void handle_request(int client_fd);
|
||||||
|
|
||||||
/* @brief Parses the HTTP request and splits it into a request structure
|
/* @brief Parses the HTTP request and splits it into a request structure
|
||||||
*
|
*
|
||||||
|
|
@ -81,7 +81,7 @@ struct http_response
|
||||||
* @return A pointer to the structure containing the request infos on success,
|
* @return A pointer to the structure containing the request infos on success,
|
||||||
* NULL otherwise
|
* NULL otherwise
|
||||||
*/
|
*/
|
||||||
struct http_request* parse_request(struct string* req);
|
struct http_request *parse_request(struct string *req);
|
||||||
|
|
||||||
/* @brief Generates a response to the given request
|
/* @brief Generates a response to the given request
|
||||||
*
|
*
|
||||||
|
|
@ -90,7 +90,7 @@ struct http_response
|
||||||
* @return A pointer to the generated response struct on success,
|
* @return A pointer to the generated response struct on success,
|
||||||
* NULL otherwise
|
* NULL otherwise
|
||||||
*/
|
*/
|
||||||
struct http_response* generate_response(struct http_request* req);
|
struct http_response *generate_response(struct http_request *req);
|
||||||
|
|
||||||
/* @brief Formats the given response structure into a valid HTTP response
|
/* @brief Formats the given response structure into a valid HTTP response
|
||||||
* string
|
* string
|
||||||
|
|
@ -100,7 +100,7 @@ struct http_response
|
||||||
* @return A pointer to the string containing the response on success,
|
* @return A pointer to the string containing the response on success,
|
||||||
* NULL otherwise
|
* NULL otherwise
|
||||||
*/
|
*/
|
||||||
struct string* format_response(struct http_response* resp);
|
struct string *format_response(struct http_response *resp);
|
||||||
|
|
||||||
/* @brief Free all allocated memory inside req and req itself
|
/* @brief Free all allocated memory inside req and req itself
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,10 @@ void print_err();
|
||||||
|
|
||||||
/* @brief Prints error logs, just like print_log(), and also to stderr
|
/* @brief Prints error logs, just like print_log(), and also to stderr
|
||||||
*/
|
*/
|
||||||
void print_log_err(char* format, ...);
|
void print_log_err(char *format, ...);
|
||||||
|
|
||||||
/* @brief Prints error logs, just like print_log()
|
/* @brief Prints error logs, just like print_log()
|
||||||
*/
|
*/
|
||||||
char* get_err();
|
char *get_err();
|
||||||
|
|
||||||
#endif // ! ERRORS_H
|
#endif // ! ERRORS_H
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ struct logs_config
|
||||||
{
|
{
|
||||||
bool enabled;
|
bool enabled;
|
||||||
int logfile_fd;
|
int logfile_fd;
|
||||||
struct server_config* server_cfg;
|
struct server_config *server_cfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* @brief Initializes the logging module
|
/* @brief Initializes the logging module
|
||||||
|
|
@ -18,7 +18,7 @@ struct logs_config
|
||||||
*
|
*
|
||||||
* @return 0 on success, an error code otherwise
|
* @return 0 on success, an error code otherwise
|
||||||
*/
|
*/
|
||||||
int log_init(struct config* config);
|
int log_init(struct config *config);
|
||||||
|
|
||||||
/* @brief Prints logs (or not) conformly to the config given by the user.
|
/* @brief Prints logs (or not) conformly to the config given by the user.
|
||||||
* Works like printf (because it uses it under the hood) with a
|
* Works like printf (because it uses it under the hood) with a
|
||||||
|
|
@ -27,7 +27,7 @@ int log_init(struct config* config);
|
||||||
* @param format
|
* @param format
|
||||||
* @param ...
|
* @param ...
|
||||||
*/
|
*/
|
||||||
void print_log(char* format, ...);
|
void print_log(char *format, ...);
|
||||||
|
|
||||||
/* @brief Prints request logs with the adequate format in the logfile
|
/* @brief Prints request logs with the adequate format in the logfile
|
||||||
*
|
*
|
||||||
|
|
@ -35,7 +35,7 @@ void print_log(char* format, ...);
|
||||||
* @param target
|
* @param target
|
||||||
* @param client_ip
|
* @param client_ip
|
||||||
*/
|
*/
|
||||||
void log_request(char* request_type, char* target, char* client_ip);
|
void log_request(char *request_type, char *target, char *client_ip);
|
||||||
|
|
||||||
/* @brief Prints response logs with the adequate format in the logfile
|
/* @brief Prints response logs with the adequate format in the logfile
|
||||||
*
|
*
|
||||||
|
|
@ -44,8 +44,7 @@ void log_request(char* request_type, char* target, char* client_ip);
|
||||||
* @param target
|
* @param target
|
||||||
* @param client_ip
|
* @param client_ip
|
||||||
*/
|
*/
|
||||||
void log_response(int status_code, char* request_type, char* target, char* client_ip);
|
void log_response(int status_code, char *request_type, char *target,
|
||||||
|
char *client_ip);
|
||||||
|
|
||||||
|
|
||||||
#endif // ! LOGS_H
|
#endif // ! LOGS_H
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ int main(int argc, char **argv)
|
||||||
// Initialize modules
|
// Initialize modules
|
||||||
log_init(config); // Ignore ret val
|
log_init(config); // Ignore ret val
|
||||||
http_init(config);
|
http_init(config);
|
||||||
|
daemon_init(config);
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
switch (config->daemon)
|
switch (config->daemon)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,6 @@
|
||||||
* @param hpstname
|
* @param hpstname
|
||||||
* @param port
|
* @param port
|
||||||
*/
|
*/
|
||||||
void start_server(const char* host, const char* port);
|
void start_server(const char *host, const char *port);
|
||||||
|
|
||||||
#endif // ! SERVER_H
|
#endif // ! SERVER_H
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
// #include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
// #include "../string/string.h"
|
// #include "../string/string.h"
|
||||||
|
|
@ -59,3 +58,33 @@ ssize_t get_file_content_size(const char *path)
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int write_to_file(const char *path, struct string *buf)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(path, "w");
|
||||||
|
if (stream == NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
fwrite(buf->data, sizeof(char), buf->size, stream);
|
||||||
|
|
||||||
|
fclose(stream);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int write_pid(const char *filepath, int pid)
|
||||||
|
{
|
||||||
|
FILE *stream = fopen(filepath, "w");
|
||||||
|
if (stream == NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (fprintf(stream, "%d", pid) <= 0)
|
||||||
|
{
|
||||||
|
fclose(stream);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(stream);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,13 +25,13 @@
|
||||||
// bool file_exists(const char *path);
|
// bool file_exists(const char *path);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
*
|
*
|
||||||
* @param path
|
* @param path
|
||||||
*
|
*
|
||||||
* @return 1 if path is a directory, 0 if it is not and -1 if path is not valid
|
* @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);
|
int is_directory(const char *path);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -40,8 +40,7 @@ int is_directory(const char* path);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
char* get_file(const char* path);
|
char *get_file(const char *path);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -50,7 +49,7 @@ int is_directory(const char* path);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
struct string* get_file_content(const char *path);
|
struct string *get_file_content(const char *path);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -59,7 +58,7 @@ struct string* get_file_content(const char *path);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
bool check_filename(struct string* path);
|
bool check_filename(struct string *path);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -68,7 +67,7 @@ bool check_filename(struct string* path);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
bool sanitize_filename(struct string* filename);
|
// bool sanitize_filename(struct string *filename);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief
|
* @brief
|
||||||
|
|
@ -77,6 +76,24 @@ bool sanitize_filename(struct string* filename);
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ssize_t get_file_content_size(const char* path);
|
ssize_t get_file_content_size(const char *path);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @return 0 on success, the corresponding error code otherwise
|
||||||
|
*/
|
||||||
|
int write_to_file(const char *path, struct string* buf);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @return 0 on success, the corresponding error code otherwise
|
||||||
|
*/
|
||||||
|
int write_pid(const char *filepath, int pid);
|
||||||
|
|
||||||
#endif // ! FILES_H
|
#endif // ! FILES_H
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,9 @@
|
||||||
// === Includes
|
// === Includes
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "../string/string.h"
|
#include "../string/string.h"
|
||||||
|
|
||||||
// === Functions
|
// === Functions
|
||||||
|
|
@ -33,6 +34,6 @@ bool is_blank(char c);
|
||||||
/*
|
/*
|
||||||
* Doc: TODO
|
* Doc: TODO
|
||||||
*/
|
*/
|
||||||
ssize_t skip_blanks(struct string* str, size_t offset);
|
ssize_t skip_blanks(struct string *str, size_t offset);
|
||||||
|
|
||||||
#endif // ! BLANKS_H
|
#endif // ! BLANKS_H
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#ifndef WORDS_H
|
#ifndef WORDS_H
|
||||||
#define WORDS_H
|
#define WORDS_H
|
||||||
|
|
||||||
|
|
||||||
// === Includes
|
// === Includes
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "../string/string.h"
|
#include "../string/string.h"
|
||||||
|
|
||||||
// === Functions
|
// === Functions
|
||||||
|
|
@ -19,7 +19,7 @@ bool str_contains(const char *str, char c);
|
||||||
/*
|
/*
|
||||||
* Doc: TODO
|
* Doc: TODO
|
||||||
*/
|
*/
|
||||||
ssize_t read_word(struct string* str, size_t offset, struct string** res);
|
ssize_t read_word(struct string *str, size_t offset, struct string **res);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Doc: TODO
|
* Doc: TODO
|
||||||
|
|
@ -30,8 +30,8 @@ ssize_t read_word_delim(struct string *str, size_t offset, struct string **res,
|
||||||
/*
|
/*
|
||||||
* Doc: TODO
|
* Doc: TODO
|
||||||
*/
|
*/
|
||||||
ssize_t read_word_restrict(struct string *str, size_t offset, struct string **res,
|
ssize_t read_word_restrict(struct string *str, size_t offset,
|
||||||
const char *restr);
|
struct string **res, const char *restr);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Doc: TODO
|
* Doc: TODO
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,6 @@ void string_destroy(struct string *str);
|
||||||
**
|
**
|
||||||
** @return a pointer to an allocated memory zone containing the string
|
** @return a pointer to an allocated memory zone containing the string
|
||||||
*/
|
*/
|
||||||
char* string_to_charptr(struct string *str);
|
char *string_to_charptr(struct string *str);
|
||||||
|
|
||||||
#endif /* ! STRING_H */
|
#endif /* ! STRING_H */
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,6 @@
|
||||||
*
|
*
|
||||||
* @return A NULL-terminated string containing the fromatted GMT time
|
* @return A NULL-terminated string containing the fromatted GMT time
|
||||||
*/
|
*/
|
||||||
char* get_time();
|
char *get_time();
|
||||||
|
|
||||||
#endif // ! FMT_TIME_H
|
#endif // ! FMT_TIME_H
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
[global]
|
[global]
|
||||||
log = true
|
log = true
|
||||||
pid_file = /tmp/HTTPd.pid
|
pid_file = /tmp/HTTPd.pid
|
||||||
|
daemon = start
|
||||||
|
|
||||||
[[vhosts]]
|
[[vhosts]]
|
||||||
server_name = my_server
|
server_name = my_server
|
||||||
Loading…
Add table
Add a link
Reference in a new issue