From 51fe7e1c8da9d12dd9fdea3a631f5bd2228b8c7a Mon Sep 17 00:00:00 2001 From: Nayan <33187059+GShadow5@users.noreply.github.com> Date: Thu, 8 May 2025 21:31:43 -0400 Subject: [PATCH] add function comments --- .vscode/settings.json | 3 +- client_handler.c | 13 ----- client_handler.h | 13 +++++ logging.h | 38 +++++++++++++-- response_builder.h | 107 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 17 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 52dfd2f..1905e88 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "types.h": "c", "random": "c", "request_handler.h": "c", - "response_builder.h": "c" + "response_builder.h": "c", + "time.h": "c" } } \ No newline at end of file diff --git a/client_handler.c b/client_handler.c index 8752d39..9f078ce 100644 --- a/client_handler.c +++ b/client_handler.c @@ -4,19 +4,6 @@ #define BUFFER_SIZE 1024 -/** - * Handles a single client connection. - * - * @param args A pointer to the file descriptor of the client - * - * @return NULL - * - * @details - * This function is designed to be run in a separate thread. It will receive - * requests from the client until the client disconnects, at which point it will - * return. Reading the requests and building responses is handled by - * handle_request. - */ void* client_handler(void* args) { log_message(LOG_DEBUG, "Client handler started"); // Cast args diff --git a/client_handler.h b/client_handler.h index 8a2750d..5d0c7c4 100644 --- a/client_handler.h +++ b/client_handler.h @@ -11,6 +11,19 @@ #include #include +/** + * Handles a single client connection. Meant to be run in a separate thread. + * + * @param args A pointer to the file descriptor of the client + * + * @return NULL + * + * @details + * This function is designed to be run in a separate thread. It will receive + * requests from the client until the client disconnects, at which point it will + * return. Reading the requests and building responses is handled by + * handle_request. + */ void* client_handler(void* args); #endif \ No newline at end of file diff --git a/logging.h b/logging.h index b3c874c..3b858ff 100644 --- a/logging.h +++ b/logging.h @@ -12,10 +12,42 @@ #define LOG_WARN 2 #define LOG_ERROR 3 +/** + * Sets the global log level for the logger. + * + * @param level The log level to set. Possible values are LOG_DEBUG, LOG_INFO, + * LOG_WARN, and LOG_ERROR. Messages below this level will not be logged. + */ void set_log_level(int level); -void log_message(int level, const char* fmt, ...); - +/** + * Opens the log file for appending log messages. + * + * @details + * This function attempts to open the log file specified by LOG_FILE_NAME + * in append mode. If the file cannot be opened, an error message is printed + * to stderr. + */ void open_log_file(); -void close_log_file(); \ No newline at end of file +/** + * Close the log file if it is open. This should be called when the program is + * about to exit to ensure that the log file is properly closed. + * + * @details + * If the log file is not open, this function does nothing. + */ +void close_log_file(); + +/** + * Logs a message with the given level and format string to the console and to + * the log file. If the level is less than the current log level, the message is + * not logged. The message is formatted according to fmt and varargs, and is + * prefixed with a timestamp and a string corresponding to the log level. + * + * @param level The log level to log the message at. Possible values are + * LOG_DEBUG, LOG_INFO, LOG_WARN, and LOG_ERROR. + * @param fmt The format string for the message to log + * @param ... The variadic arguments to format the string with + */ +void log_message(int level, const char* fmt, ...); diff --git a/response_builder.h b/response_builder.h index 02d42f8..0347524 100644 --- a/response_builder.h +++ b/response_builder.h @@ -25,25 +25,132 @@ typedef enum { NOT_IMPLEMENTED, } status_code; +/** + * Handles a GET request by serving up a file from the public directory. + * + * @param req The request to handle + * @param res The response to write to + * + * @details + * This function will extract the path from the request URL and attempt to + * serve up the file at that path from the public directory. The MIME type of + * the file is determined by the extension of the file. The function will + * return a 404 status code if the file does not exist. + */ void response_handle_get(http_request* req, http_response* res); +/** + * Handles a POST request. + * + * @param req The request to handle + * @param res The response to write to + * + * @details + * This function is currently a stub and will always return a 501 status code. + * It will not handle POST requests. + */ void response_handle_post(http_request* req, http_response* res); +/** + * Handles a DELETE request. + * + * @param req The request to handle + * @param res The response to write to + * + * @details + * This function is currently a stub and will always return a 501 status code. + * It will not handle DELETE requests. + */ void response_handle_delete(http_request* req, http_response* res); +/** + * Handles a PUT request. + * + * @param req The request to handle + * @param res The response to write to + * + * @details + * This function is currently a stub and will always return a 501 status code. + * It will not handle PUT requests. + */ void response_handle_put(http_request* req, http_response* res); +/** + * Builds an HTTP response for a static file. + * + * @param file_path The path to the file to serve + * @param content_type The content type of the file + * @param status_code The status code of the response + * @param res The response to populate + * + * @details + * This function opens the given file and reads it into a buffer. It will then + * set the status line, content length, content type, and body of the given + * response to the appropriate values. If there is an error reading the file, + * it will set the status code to 500 and the body to an appropriate error + * message. + */ void response_build_static_file(char* file_path, content_type content_type, status_code status_code, http_response* res); +/** + * Serves a 404 Not Found error page. + * + * @param res The http_response struct to populate with the 404 response. + * + * @details + * This function sets the response to serve a static 404 error HTML file + * from the public directory. It uses the response_build_static_file function + * to construct the response with the appropriate content type and status code. + */ void serve_404(http_response* res); +/** + * Serves a 500 Internal Server Error error page. + * + * @param res The http_response struct to populate with the 500 response. + * + * @details + * This function sets the response to serve a static 500 error HTML file + * from the public directory. It uses the response_build_static_file function + * to construct the response with the appropriate content type and status code. + */ void serve_500(http_response* res); +/** + * Serves a 403 Forbidden error page. + * + * @param res The http_response struct to populate with the 403 response. + * + * @details + * This function sets the response to serve a static 403 error HTML file + * from the public directory. It uses the response_build_static_file function + * to construct the response with the appropriate content type and status code. + */ void serve_403(http_response* res); +/** + * Serves a 501 Not Implemented error page. + * + * @param res The http_response struct to populate with the 501 response. + * + * @details + * This function sets the response to serve a static 501 error HTML file + * from the public directory. It uses the response_build_static_file function + * to construct the response with the appropriate content type and status code. + */ void serve_501(http_response* res); +/** + * Serves a 400 Bad Request error page. + * + * @param res The http_response struct to populate with the 400 response. + * + * @details + * This function sets the response to serve a static 400 error HTML file + * from the public directory. It uses the response_build_static_file function + * to construct the response with the appropriate content type and status code. + */ void serve_400(http_response* res); #endif \ No newline at end of file