Skip to content

Commit 9e4b5ab

Browse files
committed
hotfix: Make HttpResponse::fromFile() method into void return
1 parent 4b71224 commit 9e4b5ab

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

include/HttpResponse.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* HttpResponse.hpp :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/14 17:27:02 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 22:26:03 by sanghupa ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,7 +35,8 @@ class HttpResponse
3535
std::string getBody();
3636
std::string toString() const;
3737

38-
static HttpResponse fromFile(const std::string filePath);
38+
void fromFile(const std::string filePath);
39+
// static HttpResponse fromFile(const std::string filePath);
3940
static HttpResponse badRequest_400();
4041
static HttpResponse forbidden_403();
4142
static HttpResponse notFound_404();

src/server/HttpResponse.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* HttpResponse.cpp :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/14 17:35:57 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 22:34:17 by sanghupa ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

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

9998
if (!file.is_open())
100-
return (notFound_404());
99+
{
100+
*this = notFound_404();
101+
return;
102+
}
101103
file.seekg(0, std::ios::end);
102104
std::streamsize fileSize = file.tellg();
103105
file.seekg(0, std::ios::beg);
104106
if (fileSize <= 0)
105-
return internalServerError_500();
107+
{
108+
*this = internalServerError_500();
109+
return;
110+
}
106111
std::string fileContents(fileSize, '\0');
107112
if (!file.read(&fileContents[0], fileSize))
108-
return (file.close(), internalServerError_500());
113+
{
114+
file.close();
115+
*this = internalServerError_500();
116+
return;
117+
}
109118
file.close();
110119
std::stringstream ss;
111120
ss << fileSize;
112-
resp.setBody(fileContents);
113-
resp.setHeader("Content-Length", ss.str());
114-
resp.setHeader("Connection", "close");
115-
return (resp);
121+
this->setBody(fileContents);
122+
this->setHeader("Content-Length", ss.str());
123+
this->setHeader("Connection", "close");
116124
}
117125

118126
////////////////////////////////////////////////////////////////////////////////
@@ -200,7 +208,7 @@ void HttpResponse::setStatusCode(int code)
200208
/// @return HttpResponse
201209
HttpResponse HttpResponse::_errorResponse(int code)
202210
{
203-
HttpResponse resp(fromFile(getErrorPagePath(code)));
211+
HttpResponse resp(getErrorPagePath(code));
204212
resp.setStatusCode(code);
205213
return (resp);
206214
}

src/server/StaticFileHandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/14 21:51:41 by sanghupa ### ########.fr */
9+
/* Updated: 2024/07/14 22:23:57 by sanghupa ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -34,6 +34,7 @@ HttpResponse StaticFileHandler::handleRequest(const HttpRequest request)
3434
return (_handleFileNotFound());
3535
std::string mimeType = _getMimeType(filePath);
3636
HttpResponse resp(filePath);
37+
// HttpResponse resp = HttpResponse::fromFile(filePath);
3738
resp.setHeader(_getMimeType(filePath), mimeType);
3839

3940
return (resp);

0 commit comments

Comments
 (0)