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 *log_file;
|
||||
bool log;
|
||||
char* protocol_version;
|
||||
char *protocol_version;
|
||||
|
||||
struct server_config *servers;
|
||||
enum daemon daemon;
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@
|
|||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../server/server.h"
|
||||
#include "../utils/files/files.h"
|
||||
|
||||
static struct config *config;
|
||||
|
||||
|
|
@ -43,7 +45,23 @@ void stop_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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ int get_pid();
|
|||
|
||||
/* @brief
|
||||
*/
|
||||
void daemon_init(struct config* cfg);
|
||||
void daemon_init(struct config *cfg);
|
||||
|
||||
/* @brief
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ ssize_t parse_headers(struct http_request *res, struct string *req,
|
|||
*
|
||||
* @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
|
||||
|
|
@ -67,7 +67,7 @@ struct http_header* get_header(struct http_header *headers, const char* field);
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
struct http_header* create_header(const char* field, const char* value);
|
||||
struct http_header *create_header(const char *field, const char *value);
|
||||
|
||||
/*
|
||||
* @brief
|
||||
|
|
|
|||
|
|
@ -326,10 +326,6 @@ struct string *format_response(struct http_response *resp)
|
|||
|
||||
string_concat_str(res, "\r\n", 2);
|
||||
|
||||
// Time
|
||||
char *time = get_time();
|
||||
string_concat_str(res, time, strlen(time));
|
||||
|
||||
// Headers
|
||||
struct http_header *cur_header = resp->headers;
|
||||
while (cur_header != NULL)
|
||||
|
|
@ -337,6 +333,11 @@ struct string *format_response(struct http_response *resp)
|
|||
string_concat_str(res, cur_header->field->data,
|
||||
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);
|
||||
|
||||
cur_header = cur_header->next;
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@
|
|||
|
||||
// === Includes
|
||||
|
||||
#include "../utils/string/string.h"
|
||||
#include "../config/config.h"
|
||||
#include "../utils/string/string.h"
|
||||
|
||||
// === Enums
|
||||
|
||||
|
|
@ -66,13 +66,13 @@ struct http_response
|
|||
*
|
||||
* @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
|
||||
*
|
||||
* @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
|
||||
*
|
||||
|
|
@ -81,7 +81,7 @@ struct http_response
|
|||
* @return A pointer to the structure containing the request infos on success,
|
||||
* 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
|
||||
*
|
||||
|
|
@ -90,7 +90,7 @@ struct http_response
|
|||
* @return A pointer to the generated response struct on success,
|
||||
* 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
|
||||
* string
|
||||
|
|
@ -100,7 +100,7 @@ struct http_response
|
|||
* @return A pointer to the string containing the response on success,
|
||||
* 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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ void print_err();
|
|||
|
||||
/* @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()
|
||||
*/
|
||||
char* get_err();
|
||||
char *get_err();
|
||||
|
||||
#endif // ! ERRORS_H
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ struct logs_config
|
|||
{
|
||||
bool enabled;
|
||||
int logfile_fd;
|
||||
struct server_config* server_cfg;
|
||||
struct server_config *server_cfg;
|
||||
};
|
||||
|
||||
/* @brief Initializes the logging module
|
||||
|
|
@ -18,7 +18,7 @@ struct logs_config
|
|||
*
|
||||
* @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.
|
||||
* 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 ...
|
||||
*/
|
||||
void print_log(char* format, ...);
|
||||
void print_log(char *format, ...);
|
||||
|
||||
/* @brief Prints request logs with the adequate format in the logfile
|
||||
*
|
||||
|
|
@ -35,7 +35,7 @@ void print_log(char* format, ...);
|
|||
* @param target
|
||||
* @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
|
||||
*
|
||||
|
|
@ -44,8 +44,7 @@ void log_request(char* request_type, char* target, char* client_ip);
|
|||
* @param target
|
||||
* @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
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ int main(int argc, char **argv)
|
|||
// Initialize modules
|
||||
log_init(config); // Ignore ret val
|
||||
http_init(config);
|
||||
daemon_init(config);
|
||||
|
||||
// Start server
|
||||
switch (config->daemon)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@
|
|||
* @param hpstname
|
||||
* @param port
|
||||
*/
|
||||
void start_server(const char* host, const char* port);
|
||||
void start_server(const char *host, const char *port);
|
||||
|
||||
#endif // ! SERVER_H
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#include "files.h"
|
||||
|
||||
#include <stdio.h>
|
||||
// #include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
// #include "../string/string.h"
|
||||
|
|
@ -59,3 +58,33 @@ ssize_t get_file_content_size(const char *path)
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@
|
|||
*
|
||||
* @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
|
||||
|
|
@ -40,8 +40,7 @@ int is_directory(const char* path);
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
char* get_file(const char* path);
|
||||
|
||||
char *get_file(const char *path);
|
||||
|
||||
/*
|
||||
* @brief
|
||||
|
|
@ -50,7 +49,7 @@ int is_directory(const char* path);
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
struct string* get_file_content(const char *path);
|
||||
struct string *get_file_content(const char *path);
|
||||
|
||||
/*
|
||||
* @brief
|
||||
|
|
@ -59,7 +58,7 @@ struct string* get_file_content(const char *path);
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
bool check_filename(struct string* path);
|
||||
bool check_filename(struct string *path);
|
||||
|
||||
/*
|
||||
* @brief
|
||||
|
|
@ -68,7 +67,7 @@ bool check_filename(struct string* path);
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
bool sanitize_filename(struct string* filename);
|
||||
// bool sanitize_filename(struct string *filename);
|
||||
|
||||
/*
|
||||
* @brief
|
||||
|
|
@ -77,6 +76,24 @@ bool sanitize_filename(struct string* filename);
|
|||
*
|
||||
* @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
|
||||
|
|
|
|||
|
|
@ -8,8 +8,9 @@
|
|||
// === Includes
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "../string/string.h"
|
||||
|
||||
// === Functions
|
||||
|
|
@ -33,6 +34,6 @@ bool is_blank(char c);
|
|||
/*
|
||||
* 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
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#ifndef WORDS_H
|
||||
#define WORDS_H
|
||||
|
||||
|
||||
// === Includes
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "../string/string.h"
|
||||
|
||||
// === Functions
|
||||
|
|
@ -19,7 +19,7 @@ bool str_contains(const char *str, char c);
|
|||
/*
|
||||
* 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
|
||||
|
|
@ -30,8 +30,8 @@ ssize_t read_word_delim(struct string *str, size_t offset, struct string **res,
|
|||
/*
|
||||
* Doc: TODO
|
||||
*/
|
||||
ssize_t read_word_restrict(struct string *str, size_t offset, struct string **res,
|
||||
const char *restr);
|
||||
ssize_t read_word_restrict(struct string *str, size_t offset,
|
||||
struct string **res, const char *restr);
|
||||
|
||||
/*
|
||||
* Doc: TODO
|
||||
|
|
|
|||
|
|
@ -63,6 +63,6 @@ void string_destroy(struct string *str);
|
|||
**
|
||||
** @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 */
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@
|
|||
*
|
||||
* @return A NULL-terminated string containing the fromatted GMT time
|
||||
*/
|
||||
char* get_time();
|
||||
char *get_time();
|
||||
|
||||
#endif // ! FMT_TIME_H
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
[global]
|
||||
log = true
|
||||
pid_file = /tmp/HTTPd.pid
|
||||
daemon = start
|
||||
|
||||
[[vhosts]]
|
||||
server_name = my_server
|
||||
Loading…
Add table
Add a link
Reference in a new issue