Skip to content

Commit

Permalink
fix(buildResponse): split buildResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dantol29 committed May 17, 2024
1 parent 93c9955 commit 549e280
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
113 changes: 59 additions & 54 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,8 @@ void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HT
std::string serverName;
std::cout << GREEN << "Number of server blocks: " << _config.getServerBlocks().size() << RESET << std::endl;
std::cout << "Request host: " << request.getSingleHeader("host").second << std::endl;
std::cout << "Request target: " << request.getRequestTarget() << std::endl;

std::string requestTarget = request.getRequestTarget();
// if there is "?" in the request target, we need to remove it
if (std::find(requestTarget.begin(), requestTarget.end(), '?') != requestTarget.end())
requestTarget = (requestTarget.substr(0, requestTarget.find("?")));

size_t http = requestTarget.find("http://");
if (http != std::string::npos)
{
std::string remove = "http://";
requestTarget.erase(http, remove.length());
}
request.setRequestTarget(requestTarget);
std::cout << "Request target: " << request.getRequestTarget() << std::endl;
formRequestTarget(request);

for (size_t i = 0; i < _config.getServerBlocks().size(); i++)
{
Expand All @@ -244,51 +231,14 @@ void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HT
}
if (serverName == request.getSingleHeader("host").second)
{
// _config.setServerBlockIndex(i);
serverBlock = _config.getServerBlocks()[i];
directive = serverBlock.getDirectives();
std::cout << "Request target in block: " << request.getRequestTarget() << std::endl;

for (size_t i = 0; i < serverBlock.getLocations().size(); i++)
{
std::cout << "Location: " << serverBlock.getLocations()[i]._path << " == " << request.getRequestTarget()
<< std::endl;
if (request.getRequestTarget() == serverBlock.getLocations()[i]._path)
{
std::cout << "Location found" << std::endl;
directive = serverBlock.getLocations()[i];
break;
}
}
findLocationBlock(request, _config.getServerBlocks()[i], directive);
break;
}
else if (i == _config.getServerBlocks().size() - 1)
{
static StaticContentHandler staticContentInstance;
// if error already occurred, we don't want to overwrite it
if (response.getStatusCode() != 0)
{
Debug::log("Error response" + toString(response.getStatusCode()), Debug::NORMAL);
response.setErrorResponse(response.getStatusCode());
conn.setHasDataToSend(true);
return;
}
// if no server name is found, use the default server block
staticContentInstance.handleNotFound(response);
response.setStatusCode(404, "No server block is matching the request host");
conn.setHasDataToSend(true);
Debug::log("Exiting buildResponse", Debug::NORMAL);
return;
}
std::cout << "Index: " << i << std::endl;
return (handleServerBlockError(conn, response));
}

std::string root = serverBlock.getRoot();

std::cout << "Root: " << root << std::endl;
if (root[root.size() - 1] != '/')
root = root + "/";
std::cout << RED << "Root: " << root << RESET << std::endl;
std::cout << RED << "Root: " << directive._root << RESET << std::endl;

Router router(directive);

Expand Down Expand Up @@ -779,3 +729,58 @@ void Server::printServerSockets() const
std::cout << *it << std::endl;
}
}

void Server::formRequestTarget(HTTPRequest &request)
{
std::string requestTarget = request.getRequestTarget();

// if there is "?" in the request target, we need to remove it
if (std::find(requestTarget.begin(), requestTarget.end(), '?') != requestTarget.end())
requestTarget = (requestTarget.substr(0, requestTarget.find("?")));

// if there is "http://" in the request target, we need to remove it
size_t http = requestTarget.find("http://");
if (http != std::string::npos)
{
std::string remove = "http://";
requestTarget.erase(http, remove.length());
}
request.setRequestTarget(requestTarget);
std::cout << "Request target: " << request.getRequestTarget() << std::endl;
}

void Server::findLocationBlock(HTTPRequest &request, ServerBlock& serverBlock, Directives &directive)
{
directive = serverBlock.getDirectives();

for (size_t i = 0; i < serverBlock.getLocations().size(); i++)
{
std::cout << "Location: " << serverBlock.getLocations()[i]._path << " == " << request.getRequestTarget()
<< std::endl;
if (request.getRequestTarget() == serverBlock.getLocations()[i]._path)
{
std::cout << "Location found" << std::endl;
directive = serverBlock.getLocations()[i];
break;
}
}
}

void Server::handleServerBlockError(Connection& conn, HTTPResponse &response)
{
// if error already occurred, we don't want to overwrite it
if (response.getStatusCode() != 0)
{
Debug::log("Error response" + toString(response.getStatusCode()), Debug::NORMAL);
response.setErrorResponse(response.getStatusCode());
conn.setHasDataToSend(true);
return;
}

static StaticContentHandler staticContentInstance;

staticContentInstance.handleNotFound(response);
response.setStatusCode(404, "No server block is matching the request host");
conn.setHasDataToSend(true);
return;
}
5 changes: 5 additions & 0 deletions src/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class Server
void writeToClient(Connection &conn, size_t &i, HTTPResponse &response);
void closeClientConnection(Connection &conn, size_t &i);

/* for buildResponse */
void formRequestTarget(HTTPRequest &request);
void findLocationBlock(HTTPRequest &request, ServerBlock& serverBlock, Directives &directive);
void handleServerBlockError(Connection& conn, HTTPResponse &response);

/* Not avaiable constructors */
// Copy constructor
Server(const Server &other);
Expand Down
11 changes: 7 additions & 4 deletions src/ServerBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,23 @@ void ServerBlock::setIndex(std::vector<std::string> str, bool isLocation)

void ServerBlock::setRoot(std::string &str, bool isLocation)
{
// add a slash at the end if there is none
if (str.size() > 1 && str[str.size() - 1] != '/')
str = str + "/";
// remove slash at the beginning
if (str.size() < 1 && str[0] == '/')
str = str.substr(1);

if (!isLocation)
{
if (_directives._root.size() > 0)
throw("root already set");
if (str[0] == '/')
str = str.substr(1);
_directives._root = str;
}
else
{
if (_locations.back()._root.size() > 0)
throw("root already set");
if (str[0] == '/')
str = str.substr(1);
_locations.back()._root = str;
}
}
Expand Down

0 comments on commit 549e280

Please sign in to comment.