Skip to content

Commit

Permalink
Merge pull request #7 from san-ghun/dev/config
Browse files Browse the repository at this point in the history
hotfix: Make HttpResponse::fromFile() method into void return
  • Loading branch information
miooo0o authored Jul 14, 2024
2 parents 4b71224 + 9e4b5ab commit 7afd22c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
7 changes: 4 additions & 3 deletions include/HttpResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* HttpResponse.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/14 17:27:02 by minakim ### ########.fr */
/* Updated: 2024/07/14 22:26:03 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,7 +35,8 @@ class HttpResponse
std::string getBody();
std::string toString() const;

static HttpResponse fromFile(const std::string filePath);
void fromFile(const std::string filePath);
// static HttpResponse fromFile(const std::string filePath);
static HttpResponse badRequest_400();
static HttpResponse forbidden_403();
static HttpResponse notFound_404();
Expand Down
32 changes: 20 additions & 12 deletions src/server/HttpResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* HttpResponse.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/14 17:35:57 by minakim ### ########.fr */
/* Updated: 2024/07/14 22:34:17 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -91,28 +91,36 @@ std::string HttpResponse::toString() const
/// @brief Creates an Static HttpResponse object by reading the contents of a file.
/// @param filePath The path to the file to be read.
/// @return HttpResponse The created HttpResponse object.
HttpResponse HttpResponse::fromFile(const std::string filePath)
void HttpResponse::fromFile(const std::string filePath)
{
HttpResponse resp;
std::ifstream file(filePath.data());

if (!file.is_open())
return (notFound_404());
{
*this = notFound_404();
return;
}
file.seekg(0, std::ios::end);
std::streamsize fileSize = file.tellg();
file.seekg(0, std::ios::beg);
if (fileSize <= 0)
return internalServerError_500();
{
*this = internalServerError_500();
return;
}
std::string fileContents(fileSize, '\0');
if (!file.read(&fileContents[0], fileSize))
return (file.close(), internalServerError_500());
{
file.close();
*this = internalServerError_500();
return;
}
file.close();
std::stringstream ss;
ss << fileSize;
resp.setBody(fileContents);
resp.setHeader("Content-Length", ss.str());
resp.setHeader("Connection", "close");
return (resp);
this->setBody(fileContents);
this->setHeader("Content-Length", ss.str());
this->setHeader("Connection", "close");
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -200,7 +208,7 @@ void HttpResponse::setStatusCode(int code)
/// @return HttpResponse
HttpResponse HttpResponse::_errorResponse(int code)
{
HttpResponse resp(fromFile(getErrorPagePath(code)));
HttpResponse resp(getErrorPagePath(code));
resp.setStatusCode(code);
return (resp);
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/StaticFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: sanghupa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
/* Updated: 2024/07/14 21:51:41 by sanghupa ### ########.fr */
/* Updated: 2024/07/14 22:23:57 by sanghupa ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,6 +34,7 @@ HttpResponse StaticFileHandler::handleRequest(const HttpRequest request)
return (_handleFileNotFound());
std::string mimeType = _getMimeType(filePath);
HttpResponse resp(filePath);
// HttpResponse resp = HttpResponse::fromFile(filePath);
resp.setHeader(_getMimeType(filePath), mimeType);

return (resp);
Expand Down

0 comments on commit 7afd22c

Please sign in to comment.