add function comments

This commit is contained in:
Nayan
2025-05-08 21:31:43 -04:00
parent b52ce7dd0c
commit 51fe7e1c8d
5 changed files with 157 additions and 17 deletions

View File

@@ -4,6 +4,7 @@
"types.h": "c", "types.h": "c",
"random": "c", "random": "c",
"request_handler.h": "c", "request_handler.h": "c",
"response_builder.h": "c" "response_builder.h": "c",
"time.h": "c"
} }
} }

View File

@@ -4,19 +4,6 @@
#define BUFFER_SIZE 1024 #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) { void* client_handler(void* args) {
log_message(LOG_DEBUG, "Client handler started"); log_message(LOG_DEBUG, "Client handler started");
// Cast args // Cast args

View File

@@ -11,6 +11,19 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
/**
* 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); void* client_handler(void* args);
#endif #endif

View File

@@ -12,10 +12,42 @@
#define LOG_WARN 2 #define LOG_WARN 2
#define LOG_ERROR 3 #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 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 open_log_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(); 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, ...);

View File

@@ -25,25 +25,132 @@ typedef enum {
NOT_IMPLEMENTED, NOT_IMPLEMENTED,
} status_code; } 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); 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); 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); 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); 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, void response_build_static_file(char* file_path, content_type content_type,
status_code status_code, http_response* res); 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); 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); 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); 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); 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); void serve_400(http_response* res);
#endif #endif