Skip to content

Commit

Permalink
Implement processHeadRequest for HTTP HEAD support
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mmichie committed Nov 1, 2023
1 parent fbdf174 commit ec47508
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/http.cc
Expand Up @@ -256,9 +256,47 @@ void Http::processPostRequest(map<string, string> headermap) {
sock->writeLine("yeah right d00d\n");
}

/**
* Processes an HTTP HEAD request.
* @param headermap A map containing parsed HTTP headers.
*/
void Http::processHeadRequest(map<string, string> 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<string, string> headermap, string request_line,
Expand Down

0 comments on commit ec47508

Please sign in to comment.