Skip to content

Commit

Permalink
clean some code removing comments and std::cout
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cheel nakashima committed Jun 25, 2024
1 parent 7cb302f commit 19afaf1
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 192 deletions.
2 changes: 0 additions & 2 deletions mandatory/src/FileContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ std::string FileContent::getContent()

bool FileContent::setFileName(const std::string &file_name, const std::string &fileArgs)
{
// std::string FileAndFolder = this->splitFileFromArgs(file_name);
bool fileOrFolderExists = this->FileOrFolerExtists(file_name);
if (fileOrFolderExists)
{
std::cout << "isCGI: " << cgiModule->getIsCGI() << std::endl;
if (this->isInputDirectory() && isAutoIndex)
{

Expand Down
30 changes: 3 additions & 27 deletions mandatory/src/ListeningSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,14 @@ std::string ListeningSocket::getAnswerToSend()

if (this->getFirstFragment())
{
// if (response.getContentType().find("video/") != std::string::npos && this->request.getAttribute("Sec-Fetch-Dest").find("document") != std::string::npos)
// response.setAttribute("Accept-Ranges", "bytes");
// sendingValue = minimum(this->getFileSize(), static_cast <long long> (MAX_SENT_BYTES));
// answer = "bytes 0 -" + toString(sendingValue);
// response.setAttribute("Content-Range", answer);
// response.setAttribute("Content-Length", toString(this->getFileSize()));
answer = response.generateHeader() + file_content;
this->setFirstFragment(false);
std::cout << "Answer: " << answer.substr(0, 200) << std::endl;
std::cout << "------------------------------------------------" << std::endl;
}
else
{
// | Accept - Ranges : bytes |
// | Content - Range : bytes 828604416 - 828908176 / 828908177 |
// | Content - Length : 303761 answer = "bytes=" + toString |
// response.setStatus(this->getCodeContent(PARTIAL_CONTENT_CODE));
// response.setAttribute("Accept-Ranges", "bytes");
// answer = "bytes " + toString(this->getLastSendingPosition()) + " - " + toString(this->getCurrentSendingPosition()) + " / 83510215";
// //+toString(this->getFileSize());
// response.setAttribute("Content-Range", answer);
// sendingValue = this->getCurrentSendingPosition() - this->getLastSendingPosition();
// response.setAttribute("Content-Length", toString(sendingValue));
// answer = response.generateHeader() + file_content;
answer = file_content;
// std::cout << "Answer: " << answer.substr(0, 200) << std::endl;
// std::cout << "------------------------------------------------" << std::endl;
// std::cout << "filesize: " << this->getFileSize() << std::endl;
// std::cout << "------------------------------------------------" << std::endl;
}
else
answer = file_content;

return (answer);
}

Expand Down Expand Up @@ -196,8 +174,6 @@ void ListeningSocket::loadRequest(std::vector<Server *> servers)
this->request = Parser.getRequest();
this->response = Parser.getResponse();
this->setIsAutoIndex(Parser.getIsAutoIndex());
// std::cout << "LocationParser isCGI: " << Parser.getIsCGI() << std::endl;
// this->setCgiPath(Parser.getCGIPath());
this->setIsCGI(Parser.getIsCGI());

this->setStartRange(Parser.getStartRange());
Expand Down
11 changes: 4 additions & 7 deletions mandatory/src/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::map<std::string, int> var_names_location()
varnames[VAR_LOC_CGI_EXTENSION] = 0;
return varnames;
}
// Method to get the methods of the location

std::map<std::string, void (Location::*)(const std::string&)> getLocationMethods()
{
std::map<std::string, void (Location::*)(const std::string&)> locationMethods;
Expand All @@ -43,7 +43,7 @@ std::map<std::string, void (Location::*)(const std::string&)> getLocationMethods
return locationMethods;
}

// Default constructor

Location::Location()
{
name = "";
Expand All @@ -60,10 +60,9 @@ Location::Location()
Location::Location(std::string const &content)
{
this->isCgi = false;
// std::cout << "Location constructor" << std::endl;
this->loadData(content);
}
// Copy constructor

Location::Location(const Location& other)
{
setName(other.name);
Expand All @@ -75,7 +74,7 @@ Location::Location(const Location& other)
setAlias(other.alias);
}

// Destructor

Location::~Location() {}

// Getter methods
Expand Down Expand Up @@ -217,13 +216,11 @@ void Location::checkVariables(bool serverAutoIndex)
switch (Parser::checkLocationName(this->name))
{
case 2:
// std::cout << "CGI PATH: " << this->cgiPathStr << std::endl;
if (!Parser::checkCgiString(this->cgiPathStr, this->cgiExtensionStr))
exit(1);
this->setCgiPath(this->cgiPathStr);
this->setCgiExtension(this->cgiExtensionStr);
Parser::checkCgi(this->cgiPath, this->cgiExtension);
// std::cout << "Is cgi true: " << std::endl;
this->isCgi = true;
break ;
case 1:
Expand Down
71 changes: 25 additions & 46 deletions mandatory/src/LocationParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ int LocationParser::isAllowedMethod(Location *location)
return OK;
}


// std::string getCgiPath(std::string extension)
// {

// }

int LocationParser::matchingLocation()
{
std::vector<Location *> locations = this->server->getLocations();
Expand Down Expand Up @@ -141,7 +135,6 @@ std::string escapeNonPrintableChars(const std::string& str) {
case '\t':
escapedStr += "\\t";
break;
// Add more cases for other non-printable characters if needed
default:
escapedStr += str[i];
break;
Expand All @@ -152,7 +145,6 @@ std::string escapeNonPrintableChars(const std::string& str) {

bool isBadRequest(std::string request)
{
// std::cout << "REQUEST: " << escapeNonPrintableChars(request) << std::endl;
if (request.empty())
return true;
std::istringstream iss(request);
Expand All @@ -166,11 +158,9 @@ bool isBadRequest(std::string request)
if (std::count(line.begin(), line.end(), ' ') != 2 && splitString(line, ' ').size() != 3)
return true;
firstLine = false;
// std::cout << "FIRST LINE " << line << std::endl;
}
else
{
// std::cout << "line " << line << std::endl;
if (line.empty() || line == "\r")
break ;

Expand Down Expand Up @@ -218,7 +208,6 @@ void LocationParser::checks()
if (isDirPermissions(path, F_OK | R_OK) == true)
path += this->server->getIndex();
this->request.setPath(path);
// this->fileContent->setAutoIndex(server->getAutoIndex());
break ;
case NOT_ALLOWED :
response.setStatus("405 Method Not Allowed");
Expand All @@ -229,7 +218,7 @@ void LocationParser::checks()
}

this->request.setPath(decodeURL(this->request.getPath()));
std::cout << request.getPath() << std::endl;
// std::cout << request.getPath() << std::endl;
if (isBadRequest(receiver->getRequest()))//|| isURIMalformed(this->request.getPath())
{
response.setStatus("400 Bad Request");
Expand Down Expand Up @@ -258,37 +247,36 @@ void LocationParser::checks()
{
if (getMimeType(this->request.getPath()).find("video") != std::string::npos)
{
std::string attr = request.getAttribute("Range");
int tt = isFilePermissions(this->request.getPath(), F_OK | R_OK);
(void)tt;
if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1 && request.getAttribute("Range") != "")
{
// std::string attr = request.getAttribute("Range");
// int tt = isFilePermissions(this->request.getPath(), F_OK | R_OK);
// (void)tt;
// if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1 && request.getAttribute("Range") != "")
// {

response.setStatus("206 Partial Content");
response.setContentType(this->request.getPath());
// response.setStatus("206 Partial Content");
// response.setContentType(this->request.getPath());

if (this->request.getAttribute("Range") != "")
{
this->startRange = stringToSizeT(this->request.getAttribute("Range").substr(6, this->request.getAttribute("Range").find("-")));
std::cout << "STARTRANGE " << this->startRange << std::endl;
std::string endRangeStr = this->request.getAttribute("Range").substr(this->request.getAttribute("Range").find("-") + 1, this->request.getAttribute("Range").size());
std::cout << "ENDRANGE " << endRangeStr << std::endl;
if (endRangeStr.empty())
{
this->endRange = getFileSize(this->request.getPath());
}
else
{
this->endRange = stringToSizeT(endRangeStr);
}
}
}
else if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1)
// if (this->request.getAttribute("Range") != "")
// {
// this->startRange = stringToSizeT(this->request.getAttribute("Range").substr(6, this->request.getAttribute("Range").find("-")));
// std::cout << "STARTRANGE " << this->startRange << std::endl;
// std::string endRangeStr = this->request.getAttribute("Range").substr(this->request.getAttribute("Range").find("-") + 1, this->request.getAttribute("Range").size());
// std::cout << "ENDRANGE " << endRangeStr << std::endl;
// if (endRangeStr.empty())
// {
// this->endRange = getFileSize(this->request.getPath());
// }
// else
// {
// this->endRange = stringToSizeT(endRangeStr);
// }
// }
// }
if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1)
{
response.setContentType(this->request.getPath());
response.setContentLength(getFileSize(this->request.getPath()));
response.setStatus("200 OK");
// response.setAttribute("Accept-Ranges", "bytes");
}
else
{
Expand All @@ -298,13 +286,8 @@ void LocationParser::checks()
}
else if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1)
{
// std::cout << "FILEPERMISIOn" << this->request.getPath() << std::endl;

response.setContentType(this->request.getPath());
// response.setLastModified(this->fileContent->getLastModified());
// response.setContentLength(this->fileContent->getContentSize());
response.setStatus("200 OK");
// response.setServer(server->getServerName());
}
else if (isDirPermissions(this->request.getPath(), F_OK | R_OK) == true && this->isAutoIndex == true)
{
Expand All @@ -319,7 +302,6 @@ void LocationParser::checks()
}
else if (this->request.getMethod() == "POST")
{
// std::cout << this->receiver->getisform() << std::endl;

std::string body = receiver->getBody();
if (body.size() > (size_t)this->server->getMaxClientBodySize())
Expand Down Expand Up @@ -367,7 +349,6 @@ void LocationParser::checks()
}
else if (receiver->getisform())
{
// std::cout << "form: " << body << std::endl;
this->query = body;
response.setStatus("200 Created");
}
Expand All @@ -379,12 +360,10 @@ void LocationParser::checks()
response.setStatus("200 OK");
else
response.setStatus("404 Not Found");
// response.setServer(server->getServerName());
}
else
{
response.setStatus("405 Method Not Allowed");
// response.setServer(server->getServerName());
throw METHOD_NOT_ALLOWED_CODE;
}
}
Expand Down
38 changes: 5 additions & 33 deletions mandatory/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ in_addr_t Parser::isValidHost(std::string hostname)

std::memset(&hints, 0, sizeof(hints));
std::memset(&in_addr, 0, sizeof(in_addr));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM; // Or SOCK_DGRAM for UDP
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

int status = getaddrinfo(hostname.c_str(), NULL, &hints, &res);
Expand Down Expand Up @@ -114,9 +114,8 @@ bool Parser::checkErrorPage(std::string errorPage)
}
closedir(errorDirectory);

// std::cout << "Found files:" << std::endl;
std::sort(foundFiles.begin(), foundFiles.end());
std::cout << CHR_GREEN << getLocalTime() << " [" << "NOTICE" << "]" RESET << "\t\terror_pages found\t" << RESET ;//message << RESET << std::endl;
std::cout << CHR_GREEN << getLocalTime() << " [" << "NOTICE" << "]" RESET << "\t\terror_pages found\t" << RESET ;

if (foundFiles.size() == 0)
{
Expand Down Expand Up @@ -187,11 +186,10 @@ bool Parser::checkIndex(std::string index, std::string root)
return true;
}

// Define the maximum number of bytes allowed
const long long MAX_BYTES = 1099511627776; // 1 Terabyte for example

long long convertToBytes(const std::string& sizeStr) {
// Find the position where the numeric part ends and the unit starts
long long convertToBytes(const std::string& sizeStr)
{
size_t pos = 0;
while (pos < sizeStr.size() && (std::isdigit(sizeStr[pos]) || sizeStr[pos] == '.')) {
pos++;
Expand All @@ -201,23 +199,19 @@ long long convertToBytes(const std::string& sizeStr) {
throw std::invalid_argument("Invalid size format");
}

// Extract the numeric part and the unit part
double sizeValue = std::atoll(sizeStr.substr(0, pos).c_str());
std::string unit = sizeStr.substr(pos);

// Convert the unit to lowercase for easier comparison
for (size_t i = 0; i < unit.size(); ++i) {
unit[i] = std::tolower(unit[i]);
}

// Conversion factors
const long long BYTES_IN_KB = 1024;
const long long BYTES_IN_MB = BYTES_IN_KB * 1024;
const long long BYTES_IN_GB = BYTES_IN_MB * 1024;
const long long BYTES_IN_TB = BYTES_IN_GB * 1024;
const long long BITS_IN_BYTE = 8;

// Calculate the byte value
long long bytes;
if (unit.empty() || unit == "b") {
bytes = static_cast<long long>(sizeValue);
Expand All @@ -243,7 +237,6 @@ long long convertToBytes(const std::string& sizeStr) {
throw std::invalid_argument("Unknown size unit");
}

// Check if the byte value exceeds the maximum allowed
if (bytes > MAX_BYTES) {
throw std::overflow_error("Size exceeds the maximum allowed bytes");
}
Expand All @@ -256,12 +249,6 @@ long long Parser::checkClientBodySize(std::string maxClientBodySize)
if (maxClientBodySize.empty())
return (DEFAULT_MAX_BOD_SIZE);

// if (!isNumber(maxClientBodySize))
// {
// printLog("ERROR", "max_client_body_size\t<" + maxClientBodySize + ">\tis not a number." );
// exit(1);
// }

try {
long long nbr = convertToBytes(maxClientBodySize);
return (nbr);
Expand Down Expand Up @@ -356,14 +343,7 @@ int Parser::checkRootAliasReturn(std::string root, std::string alias, std::strin
return (ALIAS);
}
else if (!return_.empty())
{
// if (!isDirPermissions(return_, F_OK ) && (return_.find("http://") == std::string::npos && return_.find("https://") == std::string::npos))
// {
// printLog("ERROR", "return\t\t<" + return_ + ">\tnot a valid directory." );
// exit(1);
// }
return (RETURN);
}
return (0);
}

Expand Down Expand Up @@ -430,14 +410,6 @@ bool Parser::checkCgi(std::vector<std::string> paths, std::vector<std::string> e
break ;
}
}
// for (size_t size = 0; size < extensions.size(); size++)
// {
// if (extensions[size].find('.') == std::string::npos)
// {
// printLog("ERROR", "cgi_extension\t\t<" + extensions[size] + ">\tis not a valid extension" );
// return false;
// }
// }
return true;
}

Expand Down
Loading

0 comments on commit 19afaf1

Please sign in to comment.