|
3 | 3 | /* ::: :::::::: */
|
4 | 4 | /* HttpResponse.cpp :+: :+: :+: */
|
5 | 5 | /* +:+ +:+ +:+ */
|
6 |
| -/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */ |
| 6 | +/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */ |
7 | 7 | /* +#+#+#+#+#+ +#+ */
|
8 | 8 | /* 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 */ |
10 | 10 | /* */
|
11 | 11 | /* ************************************************************************** */
|
12 | 12 |
|
@@ -91,28 +91,36 @@ std::string HttpResponse::toString() const
|
91 | 91 | /// @brief Creates an Static HttpResponse object by reading the contents of a file.
|
92 | 92 | /// @param filePath The path to the file to be read.
|
93 | 93 | /// @return HttpResponse The created HttpResponse object.
|
94 |
| -HttpResponse HttpResponse::fromFile(const std::string filePath) |
| 94 | +void HttpResponse::fromFile(const std::string filePath) |
95 | 95 | {
|
96 |
| - HttpResponse resp; |
97 | 96 | std::ifstream file(filePath.data());
|
98 | 97 |
|
99 | 98 | if (!file.is_open())
|
100 |
| - return (notFound_404()); |
| 99 | + { |
| 100 | + *this = notFound_404(); |
| 101 | + return; |
| 102 | + } |
101 | 103 | file.seekg(0, std::ios::end);
|
102 | 104 | std::streamsize fileSize = file.tellg();
|
103 | 105 | file.seekg(0, std::ios::beg);
|
104 | 106 | if (fileSize <= 0)
|
105 |
| - return internalServerError_500(); |
| 107 | + { |
| 108 | + *this = internalServerError_500(); |
| 109 | + return; |
| 110 | + } |
106 | 111 | std::string fileContents(fileSize, '\0');
|
107 | 112 | if (!file.read(&fileContents[0], fileSize))
|
108 |
| - return (file.close(), internalServerError_500()); |
| 113 | + { |
| 114 | + file.close(); |
| 115 | + *this = internalServerError_500(); |
| 116 | + return; |
| 117 | + } |
109 | 118 | file.close();
|
110 | 119 | std::stringstream ss;
|
111 | 120 | 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"); |
116 | 124 | }
|
117 | 125 |
|
118 | 126 | ////////////////////////////////////////////////////////////////////////////////
|
@@ -200,7 +208,7 @@ void HttpResponse::setStatusCode(int code)
|
200 | 208 | /// @return HttpResponse
|
201 | 209 | HttpResponse HttpResponse::_errorResponse(int code)
|
202 | 210 | {
|
203 |
| - HttpResponse resp(fromFile(getErrorPagePath(code))); |
| 211 | + HttpResponse resp(getErrorPagePath(code)); |
204 | 212 | resp.setStatusCode(code);
|
205 | 213 | return (resp);
|
206 | 214 | }
|
|
0 commit comments