diff --git a/httpd/src/config/config.h b/httpd/src/config/config.h index 5caf4ac..c1ba197 100644 --- a/httpd/src/config/config.h +++ b/httpd/src/config/config.h @@ -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; diff --git a/httpd/src/daemon/daemon.c b/httpd/src/daemon/daemon.c index 0d5f52e..99c3826 100644 --- a/httpd/src/daemon/daemon.c +++ b/httpd/src/daemon/daemon.c @@ -3,8 +3,10 @@ #include #include #include +#include #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; } diff --git a/httpd/src/daemon/daemon.h b/httpd/src/daemon/daemon.h index dff31d9..5b150ec 100644 --- a/httpd/src/daemon/daemon.h +++ b/httpd/src/daemon/daemon.h @@ -11,7 +11,7 @@ int get_pid(); /* @brief */ -void daemon_init(struct config* cfg); +void daemon_init(struct config *cfg); /* @brief */ diff --git a/httpd/src/http/headers.h b/httpd/src/http/headers.h index 59592de..460cd3e 100644 --- a/httpd/src/http/headers.h +++ b/httpd/src/http/headers.h @@ -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 diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index 8977ee0..7152ffd 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -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; diff --git a/httpd/src/http/http.h b/httpd/src/http/http.h index 0a895da..7db5980 100644 --- a/httpd/src/http/http.h +++ b/httpd/src/http/http.h @@ -13,8 +13,8 @@ // === Includes -#include "../utils/string/string.h" #include "../config/config.h" +#include "../utils/string/string.h" // === Enums @@ -42,7 +42,7 @@ struct http_header struct http_request { - enum http_method method; + enum http_method method; struct string *target; struct string *queries; struct string *protocol; @@ -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 * diff --git a/httpd/src/logger/errors.h b/httpd/src/logger/errors.h index 2d9303c..bc225d8 100644 --- a/httpd/src/logger/errors.h +++ b/httpd/src/logger/errors.h @@ -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 diff --git a/httpd/src/logger/logs.h b/httpd/src/logger/logs.h index e8f337d..1e81ea3 100644 --- a/httpd/src/logger/logs.h +++ b/httpd/src/logger/logs.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 diff --git a/httpd/src/main.c b/httpd/src/main.c index 8e36192..69ace4b 100644 --- a/httpd/src/main.c +++ b/httpd/src/main.c @@ -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) diff --git a/httpd/src/server/server.h b/httpd/src/server/server.h index 407ec1a..fe95e52 100644 --- a/httpd/src/server/server.h +++ b/httpd/src/server/server.h @@ -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 diff --git a/httpd/src/utils/files/files.c b/httpd/src/utils/files/files.c index a4a30ee..fe3e215 100644 --- a/httpd/src/utils/files/files.c +++ b/httpd/src/utils/files/files.c @@ -1,7 +1,6 @@ #include "files.h" #include -// #include #include // #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; +} diff --git a/httpd/src/utils/files/files.h b/httpd/src/utils/files/files.h index 2afc287..cec66e7 100644 --- a/httpd/src/utils/files/files.h +++ b/httpd/src/utils/files/files.h @@ -25,13 +25,13 @@ // bool file_exists(const char *path); /* - * @brief + * @brief * * @param path * * @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 diff --git a/httpd/src/utils/parsing/blanks.h b/httpd/src/utils/parsing/blanks.h index 68c9778..417dc0a 100644 --- a/httpd/src/utils/parsing/blanks.h +++ b/httpd/src/utils/parsing/blanks.h @@ -8,8 +8,9 @@ // === Includes #include -#include #include +#include + #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 diff --git a/httpd/src/utils/parsing/words.h b/httpd/src/utils/parsing/words.h index 265212b..d259bff 100644 --- a/httpd/src/utils/parsing/words.h +++ b/httpd/src/utils/parsing/words.h @@ -1,12 +1,12 @@ #ifndef WORDS_H #define WORDS_H - // === Includes -#include #include +#include #include + #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 diff --git a/httpd/src/utils/string/string.h b/httpd/src/utils/string/string.h index 62bca63..3bec520 100644 --- a/httpd/src/utils/string/string.h +++ b/httpd/src/utils/string/string.h @@ -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 */ diff --git a/httpd/src/utils/time/fmt_time.h b/httpd/src/utils/time/fmt_time.h index 43f7877..81d5bd8 100644 --- a/httpd/src/utils/time/fmt_time.h +++ b/httpd/src/utils/time/fmt_time.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 diff --git a/httpd/config.txt b/httpd/tests/config.txt similarity index 90% rename from httpd/config.txt rename to httpd/tests/config.txt index 239d20d..77d4974 100644 --- a/httpd/config.txt +++ b/httpd/tests/config.txt @@ -1,6 +1,7 @@ [global] log = true pid_file = /tmp/HTTPd.pid +daemon = start [[vhosts]] server_name = my_server diff --git a/httpd/config_reader.sh b/httpd/tests/config_reader.sh similarity index 100% rename from httpd/config_reader.sh rename to httpd/tests/config_reader.sh