diff --git a/httpd/Makefile b/httpd/Makefile index 35f440b..70108ef 100644 --- a/httpd/Makefile +++ b/httpd/Makefile @@ -16,7 +16,8 @@ MODULES_SRCS = src/config/config.c \ src/http/http.c \ src/http/headers.c \ src/logger/logs.c \ - src/logger/errors.c + src/logger/errors.c \ + src/daemon/daemon.c SRCS = $(UTILS_SRCS) \ $(MODULES_SRCS) \ src/main.c diff --git a/httpd/src/daemon/daemon.c b/httpd/src/daemon/daemon.c index 3962703..0d5f52e 100644 --- a/httpd/src/daemon/daemon.c +++ b/httpd/src/daemon/daemon.c @@ -1,20 +1,56 @@ #include "daemon.h" #include +#include +#include -void stop_daemon(int pid) +#include "../server/server.h" + +static struct config *config; + +// === Functions + +void daemon_init(struct config *cfg) { - if (pid != -1) + config = cfg; +} + +int get_pid() +{ + FILE *stream = fopen(config->pid_file, "r"); + if (stream == NULL) + return -2; + + char buf[10]; + size_t nread = fread(buf, sizeof(char), 10, stream); + if (nread > 8) // PID is max 8 chars + return -3; + buf[nread] = '\0'; + + int res = atoi(buf); + + fclose(stream); + + return res; +} + +void stop_daemon(void) +{ + int pid = get_pid(); + if (pid > 0) kill(pid, SIGINT); } int start_daemon(void) -{} +{ + start_server("localhost", config->servers->port); + return 0; +} -int restart_daemon(int pid) +int restart_daemon(void) { // Attempt to kill process - stop_daemon(pid); + stop_daemon(); // Start again return start_daemon(); diff --git a/httpd/src/daemon/daemon.h b/httpd/src/daemon/daemon.h index b916483..dff31d9 100644 --- a/httpd/src/daemon/daemon.h +++ b/httpd/src/daemon/daemon.h @@ -5,22 +5,26 @@ /* @brief * - * @param + * @return */ -void stop_daemon(); +int get_pid(); + +/* @brief + */ +void daemon_init(struct config* cfg); + +/* @brief + */ +void stop_daemon(void); /* @brief - * - * @param */ int start_daemon(void); /* @brief - * - * @param * * @return */ -int restart_daemon(); +int restart_daemon(void); #endif // ! DAEMON_H diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index 011709e..c105986 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -48,18 +48,20 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req) return ERR_HTTP_INVALID_INPUT; // Target (path) - skipped += read_word(req, i, &res->target); + skipped = read_word(req, i, &res->target); if (skipped <= 0) return ERR_HTTP_INVALID_INPUT; + i += skipped; // Skip space if (req->data[i++] != ' ') return ERR_HTTP_INVALID_INPUT; // Protocol - skipped += read_word(req, i, &res->protocol); + skipped = read_word(req, i, &res->protocol); if (skipped <= 0) return ERR_HTTP_INVALID_INPUT; + i += skipped; // CRLF (EOL) if (req->data[i++] != '\r' && req->data[i++] != '\n') @@ -178,7 +180,7 @@ struct http_response *generate_response(struct http_request *req) return NULL; // Protocol - char *protocol = "HTTP/1.1"; + char *protocol = HTTP_VERSION; res->protocol = string_create(protocol, strlen(protocol)); // Status code diff --git a/httpd/src/http/http.h b/httpd/src/http/http.h index c518826..0d8da69 100644 --- a/httpd/src/http/http.h +++ b/httpd/src/http/http.h @@ -106,6 +106,6 @@ void destroy_request(struct http_request *req); * * @param resp */ -void destroy_response(struct response *resp); +void destroy_response(struct http_response *resp); #endif // ! HTTP_H diff --git a/httpd/src/main.c b/httpd/src/main.c index 835fad9..2d8bdca 100644 --- a/httpd/src/main.c +++ b/httpd/src/main.c @@ -1,6 +1,7 @@ #include #include "config/config.h" +#include "daemon/daemon.h" #include "http/http.h" #include "logger/logs.h" #include "server/server.h" @@ -21,22 +22,24 @@ int main(int argc, char **argv) // Start server switch (config->daemon) { - NO_OPTION: - start_server("localhost", config->servers->port); - break; + case NO_OPTION: + start_server("localhost", config->servers->port); + break; - START: - start_daemon(); - break; + case START: + start_daemon(); + break; - RESTART: - restart_daemon(); + case RESTART: + restart_daemon(); + break; - STOP: - stop_daemon(); + case STOP: + stop_daemon(); + break; - default: - return 2; + default: + return 2; } return 0; diff --git a/httpd/src/utils/files/files.c b/httpd/src/utils/files/files.c index 0e84278..a4a30ee 100644 --- a/httpd/src/utils/files/files.c +++ b/httpd/src/utils/files/files.c @@ -1,6 +1,7 @@ #include "files.h" -// #include +#include +// #include #include // #include "../string/string.h" @@ -36,3 +37,25 @@ int is_directory(const char *path) // int nread; // while ((fgets(buf, BUFFER_SIZE, stream))) // } + +// TODO not implemented +bool check_filename(struct string *path) +{ + if (path == NULL || path->size <= 0) + return false; + + return true; +} + +ssize_t get_file_content_size(const char *path) +{ + FILE *stream = fopen(path, "r"); + if (stream == NULL) + return -2; + + fseek(stream, 0, SEEK_END); + ssize_t res = ftell(stream); + fclose(stream); + + return res; +} diff --git a/httpd/src/utils/files/files.h b/httpd/src/utils/files/files.h index 7a40c2b..2afc287 100644 --- a/httpd/src/utils/files/files.h +++ b/httpd/src/utils/files/files.h @@ -11,6 +11,8 @@ #include #include +#include "../string/string.h" + // === Functions /*