From ec475088bc3cc06c3c8b7e56d87e5ff361567fcd Mon Sep 17 00:00:00 2001 From: Matt Michie Date: Tue, 31 Oct 2023 21:07:50 -0700 Subject: [PATCH] Implement processHeadRequest for HTTP HEAD support - Added a comprehensive implementation for handling HTTP HEAD requests. - The function now properly processes the HEAD request by sending an appropriate HTTP header response based on the requested file. - Also updated the logging mechanism to record HEAD requests. Todo: - Further optimization needed for Log and Mime singletons. --- src/http.cc | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/http.cc b/src/http.cc index 0b77894..887abb7 100644 --- a/src/http.cc +++ b/src/http.cc @@ -256,9 +256,47 @@ void Http::processPostRequest(map headermap) { sock->writeLine("yeah right d00d\n"); } +/** + * Processes an HTTP HEAD request. + * @param headermap A map containing parsed HTTP headers. + */ void Http::processHeadRequest(map headermap) { - (void)headermap; // Suppress unused parameter warning - sock->writeLine("HAR HAR !\n"); + string filename = headermap["HEAD"]; + string file_extension; + + filename = sanitizeFilename(filename); + + // Open file + ifstream file(filename.c_str(), ios::in | ios::binary); + + // Find the extension (assumes the extension is whatever follows the last + // '.') + file_extension = filename.substr(filename.rfind("."), filename.length()); + + // Can't find the file, send 404 header + if (!file.is_open()) { + sendHeader(404, 0, "text/html", false); + return; + } + + // Determine File Size + unsigned long size; + file.seekg(0, ios::end); + size = file.tellg(); + + // TODO: Optimize Log to be a singleton + Log log; + log.openLogFile("logs/access_log"); + log.writeLogLine(inet_ntoa(sock->client.sin_addr), "HEAD " + filename, 200, + size, headermap["Referer"], headermap["User-Agent"]); + log.closeLogFile(); + + // TODO: Optimize Mime to be a singleton + Mime mime; + mime.readMimeConfig("mime.types"); + + // Send header + sendHeader(200, size, mime.getMimeFromExtension(filename), false); } void Http::processGetRequest(map headermap, string request_line,