From 17add591ea0261703de8c78b5b8b9f30f373a4a4 Mon Sep 17 00:00:00 2001 From: alecmalloc Date: Fri, 18 Apr 2025 09:50:17 +0200 Subject: [PATCH] just replaced a bunch of endl with nl --- conf/default.conf | 2 +- src/Cgi/Cgi.cpp | 9 +---- src/Config/LocationConf.cpp | 20 +++++----- src/Config/ServerConf.cpp | 4 +- src/Http/HttpRequest.cpp | 7 +--- src/Http/main.cpp | 40 ------------------- src/Response/Response.cpp | 2 +- src/Response/delete_Response.cpp | 1 - src/Response/get_Response.cpp | 3 -- src/Response/post_Response.cpp | 25 ------------ src/Server/ServerInit.cpp | 2 +- src/Server/ServerRun.cpp | 10 ++--- ...knpmmopombnjdcgaaiekajbnjb-Default.desktop | 9 ----- 13 files changed, 21 insertions(+), 113 deletions(-) delete mode 100644 uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop diff --git a/conf/default.conf b/conf/default.conf index 74339b1..582e438 100644 --- a/conf/default.conf +++ b/conf/default.conf @@ -1,5 +1,5 @@ server { - listen 0.0.0.0:8001; + listen 0.0.0.0:8000; server_name localhost; root ./; autoindex on; diff --git a/src/Cgi/Cgi.cpp b/src/Cgi/Cgi.cpp index 2d12be8..4a761a9 100644 --- a/src/Cgi/Cgi.cpp +++ b/src/Cgi/Cgi.cpp @@ -53,7 +53,6 @@ static int getVectors(ServerConf server, std::vector& ext, for (std::vector::const_iterator it = locations.begin(); it != locations.end(); ++it) { - //std::cerr << "Location Path: " << it->getPath() << ", Root Dir: " << it->getRootDir() << std::endl; std::string Compare_dir = it->getPath(); @@ -127,10 +126,8 @@ static int checkFile( HttpRequest& req, std::string& interpreter ){ pathIt++; } if( interpreter.empty() ){ - //std::cerr << "DEBUG: No interpreter found for file extension " << end << std::endl; return( 500); } - //std::cerr << "DEBUG: Interpreter for " << uri << " is " << interpreter << std::endl; return( 200 ); } @@ -328,17 +325,13 @@ static int parentProcess( HttpRequest& req, int* inputPipe, int* outputPipe, pid while (bytesWritten < totalBytes) { ssize_t result = write(inputPipe[1], data + bytesWritten, totalBytes - bytesWritten); - //std::cerr << "DEBUG: Write result: " << result << " bytes" << std::endl; if (result <= 0) { - //std::cerr << "DEBUG: Write error: " << strerror(errno) << std::endl; // Handle error break; } bytesWritten += result; } - - //std::cerr << "DEBUG: Total bytes written: " << bytesWritten << "/" << totalBytes << std::endl; // Add a small delay to ensure data is transferred before closing pipe usleep(5000); // 1ms delay @@ -378,7 +371,7 @@ static int parentProcess( HttpRequest& req, int* inputPipe, int* outputPipe, pid //if return == TIMEOUT else if( pollRet == 0 || ( startTime - time( NULL ) ) > TIMEOUT_TIME ){ - std::cout << BLUE << "INFO: Cgi: Timeout" << END << std::endl; + std::cout << BLUE << "INFO: Cgi: Timeout" << END << '\n'; close( outputPipe[0] ); kill( pid, SIGKILL ); waitpid( pid, &status, 0 ); diff --git a/src/Config/LocationConf.cpp b/src/Config/LocationConf.cpp index 399bf24..cd289c8 100644 --- a/src/Config/LocationConf.cpp +++ b/src/Config/LocationConf.cpp @@ -46,7 +46,7 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { // Print path if ( !loc.getPath().empty() ){ - os << "Path: " << loc.getPath() << std::endl; + os << "Path: " << loc.getPath() << '\n'; } // Print allowed methods @@ -57,27 +57,27 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { it != tmp.end(); ++it) { os << *it << " "; } - os << std::endl; + os << '\n'; } // Print allowed redirects if ( !loc.getAllowedRedirects().empty() ){ - os << "Allowed Redirects:" << std::endl; + os << "Allowed Redirects:" << '\n'; std::map< std::string, std::string > tmp2 = loc.getAllowedRedirects(); for (std::map::const_iterator it = tmp2.begin(); it != tmp2.end(); ++it) { - os << " Code " << it->first << ": " << it->second << std::endl; + os << " Code " << it->first << ": " << it->second << '\n'; } } // Print root directory if ( !loc.getRootDir().empty() ){ - os << "Root Directory: " << loc.getRootDir() << std::endl; + os << "Root Directory: " << loc.getRootDir() << '\n'; } // Print auto index setting if ( loc.getAutoIndex() == true ){ - os << "Auto Index: " << "On" << std::endl; + os << "Auto Index: " << "On" << '\n'; } // Print index files @@ -88,7 +88,7 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { it != tmp.end(); ++it) { os << *it << " "; } - os << std::endl; + os << '\n'; } // Print CGI paths @@ -99,7 +99,7 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { it != tmp.end(); ++it) { os << *it << " "; } - os << std::endl; + os << '\n'; } // Print CGI extensions @@ -110,12 +110,12 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { it != tmp.end(); ++it) { os << *it << " "; } - os << std::endl; + os << '\n'; } // Print upload directory if ( !loc.getUploadDir().empty() ){ - os << "Upload Directory: " << loc.getUploadDir() << std::endl; + os << "Upload Directory: " << loc.getUploadDir() << '\n'; } return os; diff --git a/src/Config/ServerConf.cpp b/src/Config/ServerConf.cpp index bb5adc4..b16992d 100644 --- a/src/Config/ServerConf.cpp +++ b/src/Config/ServerConf.cpp @@ -40,7 +40,7 @@ ServerConf& ServerConf::operator =( const ServerConf& og ) { std::ostream& operator <<( std::ostream& os, const ServerConf& server) { - os << "ServerConf Details:\n" << std::endl; + os << "ServerConf Details:" << '\n'; std::vector< std::string > tmp; // Print IP and Port @@ -96,7 +96,7 @@ std::ostream& operator <<( std::ostream& os, const ServerConf& server) { it != tmp.end(); ++it) { os << *it << " "; } - os << std::endl; + os << '\n'; } // Print LocationConfs diff --git a/src/Http/HttpRequest.cpp b/src/Http/HttpRequest.cpp index 486ca84..1823cba 100644 --- a/src/Http/HttpRequest.cpp +++ b/src/Http/HttpRequest.cpp @@ -308,9 +308,7 @@ void HttpRequest::parseBody(const std::string& rawRequest) { if (bodyStart + contentLength <= rawRequest.length()) { std::string bodyContent = rawRequest.substr(bodyStart, contentLength); setBody(bodyContent); - - // Debug output - // std::cout << "Read POST body, length: " << bodyContent.length() << std::endl; + } else { std::cerr << "Warning: Incomplete body received. Expected " << contentLength << " bytes, got " @@ -381,7 +379,6 @@ void HttpRequest::validateRequestPath(void) { // Protect against empty/null values if (_server.getRootDir().empty()) { - // std::cout << "Warning: Server root is empty" << std::endl; _response_code = 500; return; } @@ -410,7 +407,7 @@ void HttpRequest::validateRequestPath(void) { locationPath += it->getIndex()[0]; _pathInfo = PathInfo(locationPath); } else { - std::cout << "Warning: Location has no index files" << std::endl; + std::cout << "Warning: Location has no index files" << '\n'; _response_code = 404; return; } diff --git a/src/Http/main.cpp b/src/Http/main.cpp index 81a3a53..da91222 100644 --- a/src/Http/main.cpp +++ b/src/Http/main.cpp @@ -17,18 +17,6 @@ int main(int argc, char *argv[]) { return( 1 ); } - // // ALEC TESTING - // FILE* file = fopen("assets/get.example", "r"); - // if (!file) { - // std::cerr << "Failed to open the file." << std::endl; - // return 1; - // } - // int fd = fileno(file); - // HttpRequest request(fd, config); - // request.parse(); - // std::cout << request << std::endl; - // fclose(file); - // Parse Config File try { config.parse( confFile ); @@ -49,33 +37,5 @@ int main(int argc, char *argv[]) { return( 1 ); } - - - - - - - - - - // *** TESTING PARSER - //std::vector< ServerConf > tmp = config.getServerConfs(); - //for( std::vector< ServerConf >::const_iterator it = tmp.begin(); it != tmp.end(); it++ ){ - // std::cout << *it << std::endl; - //} - - // *** TESTING FOR HTTP_REQUEST - // #include - // #include - // int fd = open("get.example", O_RDONLY); - // if (fd < 0) - // return -1; - // try { - // HttpRequest http(fd); - // } - // catch (std::runtime_error &e) { - // std::cerr << "Error: " << e.what() << std::endl; - // } - return 0; } diff --git a/src/Response/Response.cpp b/src/Response/Response.cpp index 509c66f..7d6cfec 100644 --- a/src/Response/Response.cpp +++ b/src/Response/Response.cpp @@ -184,7 +184,7 @@ void Response::processResponse(HttpRequest &ReqObj){ // CGI REQUEST CHECK HANDLER // note this is the only part of the check that returns if(isCgiRequest(ReqObj.getUri())){ //should be ok but some errro with cgi handler cant find files - std::cout << "CGI REQUEST" << std::endl; + std::cout << "CGI REQUEST" << '\n'; int result = handleCgi(ReqObj); //std::cout << "CGI result" << result << '\n'; if(result == 0) { diff --git a/src/Response/delete_Response.cpp b/src/Response/delete_Response.cpp index f325c1a..8d8cfe3 100644 --- a/src/Response/delete_Response.cpp +++ b/src/Response/delete_Response.cpp @@ -26,7 +26,6 @@ void Response::HandleDeleteRequest(HttpRequest& ReqObj, PathInfo& pathInfo){ // Attempt to delete the file if (std::remove(fullPath.c_str()) == 0) { - // std::cout << "File deleted successfully: " << fullPath << std::endl; setBody("

File Deleted Successfully

"); } else { std::cerr << "Failed to delete file: " << fullPath << std::endl; diff --git a/src/Response/get_Response.cpp b/src/Response/get_Response.cpp index 130af81..d3cb996 100644 --- a/src/Response/get_Response.cpp +++ b/src/Response/get_Response.cpp @@ -5,11 +5,9 @@ std::string Response::constructFullPath(const std::string uri){ if (_locationConf && uri.find(_locationConf->getPath()) == 0) { // URI already starts with location path add uri fullPath += uri; - //std::cout << "Path constructed from URI only: " << fullPath << std::endl; } else if (_locationConf) { // Location doesn't match URI prefix, need to combine them fullPath += _locationConf->getPath() + uri; - //std::cout << "Path constructed from location + URI: " << fullPath << std::endl; } else { // No location matched fullPath += uri; @@ -125,7 +123,6 @@ void Response::HandleGetRequest(HttpRequest& ReqObj, PathInfo& pathInfo) { } } - //std::cerr << "Path is neither a file nor a directory" << std::endl; ReqObj.setResponseCode(404); throw 404; // Not Found } diff --git a/src/Response/post_Response.cpp b/src/Response/post_Response.cpp index 9ebca23..127cd13 100644 --- a/src/Response/post_Response.cpp +++ b/src/Response/post_Response.cpp @@ -14,7 +14,6 @@ int Response::checkContentLength(HttpRequest& ReqObj){ std::map >::const_iterator it = headers.find("Content-Length"); if (it == headers.end() || it->second.empty()) { - //std::cerr << "Missing Content-Length header" << std::endl; throw 411; return (411); // Length Required } @@ -25,7 +24,6 @@ int Response::checkContentLength(HttpRequest& ReqObj){ iss >> contentLength; if (iss.fail()) { - //std::cerr << "Failed to parse Content-Length header" << std::endl; throw 400; return 400; // Bad Request } @@ -57,7 +55,6 @@ void Response::HandlePostRequest(HttpRequest& ReqObj,PathInfo& pathinfo){ // We dont always need content length in a post request. esp if chunked we use transfer-encoding instead std::string postData = ReqObj.getBody(); - //std::cout << "Received POST data: " << postData << std::endl; std::string contentType = ReqObj.getHeaders()["Content-Type"][0]; // std::cout << "Content-Type" << contentType << "\n"; @@ -70,7 +67,6 @@ void Response::HandlePostRequest(HttpRequest& ReqObj,PathInfo& pathinfo){ // Extract boundary size_t boundaryPos = contentType.find("boundary="); if (boundaryPos == std::string::npos) { - // std::cerr << "No boundary found in Content-Type" << std::endl; throw 400; // Bad Request } std::string boundary = contentType.substr(boundaryPos + 9); // "boundary=" is 9 chars @@ -86,7 +82,6 @@ void Response::HandlePostRequest(HttpRequest& ReqObj,PathInfo& pathinfo){ std::string filePath = uploadPathhandler(_locationConf); std::ofstream outFile(filePath.c_str(), std::ios::out | std::ios::binary); if (!outFile.is_open()) { - // std::cerr << "Failed to open file: " << filePath << std::endl; throw 500; // Internal Server Error } @@ -95,7 +90,6 @@ void Response::HandlePostRequest(HttpRequest& ReqObj,PathInfo& pathinfo){ outFile.close(); if (outFile.fail()) { - // std::cerr << "Failed to write to file: " << filePath << std::endl; throw 500; // Internal Server Error } if (!outFile.fail()) { @@ -122,7 +116,6 @@ std::string Response::uploadPathhandler(LocationConf* locationConf) { std::string subPath = dirPath.substr(0, pos); if (!subPath.empty() && access(subPath.c_str(), F_OK) == -1) { if (mkdir(subPath.c_str(), 0777) == -1) { - // std::cerr << "Failed to create directory: " << subPath << std::endl; throw 500; // Internal Server Error } } @@ -130,7 +123,6 @@ std::string Response::uploadPathhandler(LocationConf* locationConf) { // Verify directory exists and has write permissions if (access(uploadDir.c_str(), W_OK) == -1) { - // std::cerr << "No write permission for directory: " << uploadDir << std::endl; throw 403; // Forbidden } @@ -144,7 +136,6 @@ std::string Response::uploadPathhandler(LocationConf* locationConf) { position++; // Move to next character } - // std::cout << "Saving to path: " << filePath << std::endl; return filePath; } @@ -158,9 +149,6 @@ std::string Response::uploadPathhandler(LocationConf* locationConf) { void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, const std::string& boundary) { (void)pathinfo; const std::string& postData = ReqObj.getBody(); - //std::cout << "Body received, length: " << postData.length() << std::endl; - // Debug: Print the first 50 bytes to verify content - // std::cout << "Body starts with: " << postData.substr(0, 50) << std::endl; // Chrome may or may not include leading "--" in the actual data std::string boundaryMarker = "--" + boundary; @@ -171,7 +159,6 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co if (partStart == std::string::npos) { partStart = postData.find(altBoundaryMarker); if (partStart == std::string::npos) { - //std::cerr << "Invalid multipart format - boundary not found" << std::endl; throw 400; } } @@ -179,7 +166,6 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co // Skip to the end of the boundary line partStart = postData.find("\r\n", partStart); if (partStart == std::string::npos) { - //std::cerr << "Invalid multipart format - no CRLF after boundary" << std::endl; throw 400; } partStart += 2; // Skip \r\n @@ -187,31 +173,26 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co // Find the Content-Disposition header size_t dispPos = postData.find("Content-Disposition:", partStart); if (dispPos == std::string::npos) { - // std::cerr << "No Content-Disposition header found" << std::endl; throw 400; } // Extract filename size_t filenamePos = postData.find("filename=\"", dispPos); if (filenamePos == std::string::npos) { - // std::cerr << "No filename found in Content-Disposition" << std::endl; throw 400; } filenamePos += 10; // Skip 'filename="' size_t filenameEnd = postData.find("\"", filenamePos); if (filenameEnd == std::string::npos) { - // std::cerr << "Invalid filename format" << std::endl; throw 400; } std::string filename = postData.substr(filenamePos, filenameEnd - filenamePos); - // std::cout << "Extracted filename: " << filename << std::endl; // Find the blank line that marks the start of file content size_t contentStart = postData.find("\r\n\r\n", filenameEnd); if (contentStart == std::string::npos) { - // std::cerr << "Invalid multipart format - no content start" << std::endl; throw 400; } contentStart += 4; // Skip \r\n\r\n @@ -219,14 +200,11 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co // Find the end boundary size_t contentEnd = postData.find("\r\n--" + boundary, contentStart); if (contentEnd == std::string::npos) { - // std::cerr << "Invalid multipart format - no content end" << std::endl; throw 400; } // Extract file content std::string fileContent = postData.substr(contentStart, contentEnd - contentStart); - // std::cout << "File content length: " << fileContent.length() << " bytes" << std::endl; - // std::cout << "File content: '" << fileContent << "'" << std::endl; // Create the upload directory (use your existing function) std::string uploadDir = uploadPathhandler(_locationConf); @@ -237,12 +215,10 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co // Use the original filename (sanitize it if needed) std::string filePath = dirPath + filename; - // std::cout << "Saving file to: " << filePath << std::endl; // Write only the file content to the file std::ofstream outFile(filePath.c_str(), std::ios::out | std::ios::binary); if (!outFile.is_open()) { - // std::cerr << "Failed to open file: " << filePath << std::endl; throw 500; // Internal Server Error } @@ -250,7 +226,6 @@ void Response::handleMultipartUpload(HttpRequest& ReqObj, PathInfo& pathinfo, co outFile.close(); if (outFile.fail()) { - // std::cerr << "Failed to write to file: " << filePath << std::endl; throw 500; // Internal Server Error } diff --git a/src/Server/ServerInit.cpp b/src/Server/ServerInit.cpp index a85b478..651d71f 100644 --- a/src/Server/ServerInit.cpp +++ b/src/Server/ServerInit.cpp @@ -164,7 +164,7 @@ void Server::_initServer( void ){ _sockets[ newSocket ] = std::make_pair( it2->first, *it3 ); std::cout << BLUE << "INFO: Socket: listening on: " \ - << END << it2->first << ":" << *it3 << std::endl; + << END << it2->first << ":" << *it3 << '\n'; //add newSocket to Epoll Instance addSocketEpoll( _epollFd, newSocket ); diff --git a/src/Server/ServerRun.cpp b/src/Server/ServerRun.cpp index 363c2b4..8f26551 100644 --- a/src/Server/ServerRun.cpp +++ b/src/Server/ServerRun.cpp @@ -9,7 +9,7 @@ static void handleSignal( int signal ){ if( signal == SIGINT || signal == SIGQUIT ){ running = false; std::cout << BLUE << "\nINFO: Server: Shutting down ..." << END \ - << std::endl; + << '\n'; } } @@ -108,9 +108,6 @@ static Client* findClient( Server& server, struct epoll_event event ){ //create new client with new fd and add to exxisting clients clients.push_back( newClient ); - // std::cout << BLUE << "INFO: Client: connected: " << END << clientFd - // << std::endl; - //return new client return( &clients.back() ); } @@ -238,7 +235,7 @@ static void checkEvents( Server& server, Client* client, struct epoll_event& ev if( event.events & EPOLLHUP || client->getClosed() ){ closeClient( server, client ); std::cout << BLUE << "INFO: Client: disconnected: " << \ - END << client->getEventFd() << std::endl; + END << client->getEventFd() << '\n'; return; } @@ -249,7 +246,6 @@ static void checkEvents( Server& server, Client* client, struct epoll_event& ev // check for complete request instead of checking for EUP if ( client->getComplete() ){ - // std::cout << client->getContent() << std::endl; // create temp config file for request construction Config confTMP = server.getConf(); @@ -323,7 +319,7 @@ static void checkEvents( Server& server, Client* client, struct epoll_event& ev //check for hanging conection if( event.events & EPOLLHUP || client->getClosed() ){ std::cout << BLUE << "INFO: Client: disconnected: " << \ - END << client->getEventFd() << std::endl; + END << client->getEventFd() << '\n'; closeClient( server, client ); return; } diff --git a/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop b/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop deleted file mode 100644 index f3140e1..0000000 --- a/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env xdg-open -[Desktop Entry] -Version=1.0 -Terminal=false -Type=Application -Name=Docs -Exec=/opt/google/chrome/google-chrome --profile-directory=Default --app-id=mpnpojknpmmopombnjdcgaaiekajbnjb -Icon=chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default -StartupWMClass=crx_mpnpojknpmmopombnjdcgaaiekajbnjb