Skip to content

Commit

Permalink
Merge branch 'ernesto'
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestoAvedillo committed Jun 25, 2024
2 parents 2777277 + 484ca4b commit 94cb8d3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 34 deletions.
4 changes: 2 additions & 2 deletions conf/simple.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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:{
Expand Down
14 changes: 10 additions & 4 deletions mandatory/inc/FileContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <map>
#include "colors.h"
#include "utils.hpp"
#include "CGI.hpp"
// #include "Server.hpp"
#include "ListDir.hpp"
Expand All @@ -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:
Expand All @@ -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);
Expand All @@ -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();
};
16 changes: 13 additions & 3 deletions mandatory/inc/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <sstream>
#include <ctime>
#include <algorithm>
#define MAX_SENT_BYTES 8024
#define MAX_SENT_BYTES 16048 //8024


template <typename T>
Expand Down Expand Up @@ -54,7 +54,17 @@ void printLog(std::string type ,std::string message);
std::string decodeURL(const std::string& url);

template <typename T>
T min(T a, T b);
T minimum(T a, T b)
{
if(a < b)
return a;
return b;
};

template <typename T>
T max(T a, T b);
T maximum(T a, T b)
{
if (a > b)
return a;
return b;
};
63 changes: 43 additions & 20 deletions mandatory/src/FileContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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())
{
Expand All @@ -62,6 +63,7 @@ std::string FileContent::getContent()
{
content = this->getFileContentForStatusCode(e);
}
this->setIsSendComplete(true);
return content;
}
else if (isAutoIndex && this->isInputDirectory())
Expand All @@ -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 " <<fileName<< std::endl;
file.close();
lastSendingPosition = currentSendingPosition;
file.read(buffer, MAX_SENT_BYTES);
content.append(buffer, file.gcount());
}
currentSendingPosition = file.tellg();
if (file.eof())
{
// std::cout << "Cierro fichero " << fileName << std::endl;
file.close();
this->setIsSendComplete(true);
}
}
}
else
{
content = this->getCodeContent(NOT_FOUND_CODE);
this->setIsSendComplete(true);
}
this->setIsSendComplete(true);
return (content);
}

Expand Down Expand Up @@ -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;
}
}

long long FileContent::getFileSize()
{
return static_cast<long long>(fileStat.st_size);
}

long long FileContent::getCurrentSendingPosition()
{
if (currentSendingPosition < 0)
return this->getFileSize()-1;
return currentSendingPosition;
}

long long FileContent::getLastSendingPosition()
{
return lastSendingPosition;
}
4 changes: 2 additions & 2 deletions mandatory/src/Header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
header += it->first + ": " + it->second + "\r\n";
Expand Down Expand Up @@ -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<std::string, std::string>::const_iterator it = attributes.begin(); it != attributes.end(); ++it)
std::cout << it->first << ": " << it->second << std::endl;
Expand Down
30 changes: 27 additions & 3 deletions mandatory/src/ListeningSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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;
}
return (answer);
}

Expand Down

0 comments on commit 94cb8d3

Please sign in to comment.