diff --git a/conf/simple.conf b/conf/simple.conf index 37b502d..1bd3455 100644 --- a/conf/simple.conf +++ b/conf/simple.conf @@ -62,8 +62,8 @@ server:{ root:./www/web4; # root folder of site directory, full or relative path, mandatory parameter autoindex: on; # cgi-bin folder index:index.html; # default page when requesting a directory, index.html by default - CGI_folder:./www/web4/cgi/; # cgi-bin folder - CGI_extension:cgi; # cgi-bin folder +# CGI_folder:./www/web4/cgi/; # cgi-bin folder +# CGI_extension:cgi; # cgi-bin folder }; server:{ diff --git a/mandatory/inc/FileContent.hpp b/mandatory/inc/FileContent.hpp index 6d9f324..e352a15 100644 --- a/mandatory/inc/FileContent.hpp +++ b/mandatory/inc/FileContent.hpp @@ -10,6 +10,7 @@ #include #include #include "colors.h" +#include "utils.hpp" #include "CGI.hpp" // #include "Server.hpp" #include "ListDir.hpp" @@ -31,13 +32,15 @@ class FileContent : public StatusCode bool isAutoIndex; std::string homeFolder; struct stat fileStat; + long long startRange; + long long currentSendingPosition; + long long lastSendingPosition; CGI *cgiModule; ListDir *listDir; //Server *server; size_t completeContentSize; std::string splitFileFromArgs(const std::string &); bool FileOrFolerExtists(const std::string &); - size_t startRange; size_t endRange; // bool isCgi; public: @@ -48,9 +51,9 @@ class FileContent : public StatusCode int openFile(); bool setFileName(const std::string &); void setIsCGI(bool isCgi); - void setStartRange(size_t); - void setEndRange(size_t); - size_t getStartRange(); + void setStartRange(long long); + void setEndRange(long long); + long long getStartRange(); std::string getFileName(); std::string getContent(); void setIsFileOpen(bool); @@ -71,4 +74,7 @@ class FileContent : public StatusCode void setHomeFolder(const std::string &); std::string getHomeFolder(); void setCGIModule(CGI *); + long long getFileSize(); + long long getCurrentSendingPosition(); + long long getLastSendingPosition(); }; diff --git a/mandatory/inc/utils.hpp b/mandatory/inc/utils.hpp index e75a855..c380178 100644 --- a/mandatory/inc/utils.hpp +++ b/mandatory/inc/utils.hpp @@ -18,7 +18,7 @@ #include #include #include -#define MAX_SENT_BYTES 8024 +#define MAX_SENT_BYTES 16048 //8024 template @@ -54,7 +54,17 @@ void printLog(std::string type ,std::string message); std::string decodeURL(const std::string& url); template -T min(T a, T b); +T minimum(T a, T b) +{ + if(a < b) + return a; + return b; +}; template -T max(T a, T b); +T maximum(T a, T b) +{ + if (a > b) + return a; + return b; +}; \ No newline at end of file diff --git a/mandatory/src/FileContent.cpp b/mandatory/src/FileContent.cpp index 53b2ad4..f101dc8 100644 --- a/mandatory/src/FileContent.cpp +++ b/mandatory/src/FileContent.cpp @@ -11,6 +11,8 @@ FileContent::FileContent() : StatusCode() isFileOpen = false; isFistFragment = true; isAutoIndex = false; + currentSendingPosition = 0; + lastSendingPosition = 0; // isAutoIndex = srv->getAutoIndex(); // indexInHomeFolder = srv->getRoot() + srv->getIndex(); // this->cgiModule = srv->cgiModuleClone(); @@ -35,7 +37,6 @@ FileContent::~FileContent() int FileContent::openFile() { file.open(fileName.c_str(), std::ios::out | std::ios::binary); //| std::ios::app | std::ios::ate - if (file.is_open()) { return 1; @@ -45,8 +46,8 @@ int FileContent::openFile() std::string FileContent::getContent() { - std::cout << "StartRange: " << startRange << std::endl; - std::cout << "EndRange: " << endRange << std::endl; + // std::cout << "StartRange: " << startRange << std::endl; + // std::cout << "EndRange: " << endRange << std::endl; std::string content; if (this->getIsFileOpen()) { @@ -62,6 +63,7 @@ std::string FileContent::getContent() { content = this->getFileContentForStatusCode(e); } + this->setIsSendComplete(true); return content; } else if (isAutoIndex && this->isInputDirectory()) @@ -76,32 +78,35 @@ std::string FileContent::getContent() { content = ""; char buffer[MAX_SENT_BYTES]; - if (startRange) - file.seekg(startRange, std::ios::beg); - //std::cout << "enviando paquete:" << file.tellg() << std::endl; - if (file.read(buffer, MAX_SENT_BYTES)) + if (this->startRange != 0 && this->getFirstFragment()) { - if (file.eof()) - { - file.close(); - this->setIsSendComplete(true); - } + //file.seekg(0, std::ios::beg); + file.seekg(startRange, std::ios::beg); + lastSendingPosition = currentSendingPosition; + file.read(buffer, MAX_SENT_BYTES); content.append(buffer, file.gcount()); - return content; + //this->setIsSendComplete(true); } else { - std::cout << "Cierro fichero " <setIsSendComplete(true); + } } } else { content = this->getCodeContent(NOT_FOUND_CODE); + this->setIsSendComplete(true); } - this->setIsSendComplete(true); return (content); } @@ -273,16 +278,34 @@ void FileContent::setIsCGI(bool isCgi) } -void FileContent::setStartRange(size_t range) +void FileContent::setStartRange(long long range) { + lastSendingPosition = range; this->startRange = range; } -size_t FileContent::getStartRange() +long long FileContent::getStartRange() { return startRange; } -void FileContent::setEndRange(size_t range) +void FileContent::setEndRange(long long range) { this->endRange = range; -} \ No newline at end of file +} + +long long FileContent::getFileSize() +{ + return static_cast(fileStat.st_size); +} + +long long FileContent::getCurrentSendingPosition() +{ + if (currentSendingPosition < 0) + return this->getFileSize()-1; + return currentSendingPosition; +} + +long long FileContent::getLastSendingPosition() +{ + return lastSendingPosition; +} diff --git a/mandatory/src/Header.cpp b/mandatory/src/Header.cpp index 1c0817a..6736334 100644 --- a/mandatory/src/Header.cpp +++ b/mandatory/src/Header.cpp @@ -92,7 +92,7 @@ std::string Header::generateHeader() const if (lastModified != "") header += "Last-modified: " + lastModified + "\r\n"; if(contentLength != 0) - header += "Content-length: " + toString(contentLength) + "\r\n"; + header += "Content-Length: " + toString(contentLength) + "\r\n"; header += "Content-Type: " + contentType + "\r\n"; for (std::map::const_iterator it = attributes.begin(); it != attributes.end(); ++it) header += it->first + ": " + it->second + "\r\n"; @@ -175,7 +175,7 @@ void Header::printReceivedHeader() std::cout << "Server: " << server << std::endl; std::cout << "Date: " << date << std::endl; std::cout << "Last-modified: " << lastModified << std::endl; - std::cout << "Content-length: " << contentLength << std::endl; + std::cout << "Content-Length: " << contentLength << std::endl; std::cout << "Content-Type: " << contentType << std::endl; for (std::map::const_iterator it = attributes.begin(); it != attributes.end(); ++it) std::cout << it->first << ": " << it->second << std::endl; diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index f52f438..ff31b2f 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -125,19 +125,43 @@ ListeningSocket *ListeningSocket::clone(int fd) std::string ListeningSocket::getAnswerToSend() { + // long long sendingValue; std::string answer; std::string filePath = this->getFileName(); std::string file_content = this->getContent(); + 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"); + // 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 (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; + } return (answer); }