6
6
/* By: aoumad <[email protected] > +#+ +:+ +#+ */
7
7
/* +#+#+#+#+#+ +#+ */
8
8
/* Created: 2023/03/26 23:05:21 by aoumad #+# #+# */
9
- /* Updated: 2023/05/08 16:17:03 by aoumad ### ########.fr */
9
+ /* Updated: 2023/05/10 23:56:28 by aoumad ### ########.fr */
10
10
/* */
11
11
/* ************************************************************************** */
12
12
@@ -111,6 +111,9 @@ std::map<std::string, std::string> request::get_headers() const
111
111
112
112
void request::parse_request (std::string request)
113
113
{
114
+ // std::cout << "___________33______" << std::endl;
115
+ // std::cout << request << std::endl;
116
+ // std::cout << "________55_________" << std::endl;
114
117
// Split the request into lines
115
118
std::vector<std::string> lines;
116
119
std::istringstream iss (request);
@@ -141,7 +144,8 @@ void request::parse_request(std::string request)
141
144
// Trim leading and trailing whitespaces from the value
142
145
value.erase (0 , value.find_first_not_of (" \t\r " ));
143
146
value.erase (value.find_last_not_of (" \t\r " ) + 1 );
144
- this ->_headers [key] = value;
147
+ if (this ->_headers .find (key) == this ->_headers .end ())
148
+ this ->_headers [key] = value;
145
149
}
146
150
147
151
// function that checks if the request is POST or PUT to see if there is no content-length to return error
@@ -176,11 +180,18 @@ void request::parse_request(std::string request)
176
180
if (content_len_str != " " )
177
181
{
178
182
size_t content_len = std::stoi (content_len_str);
179
- this ->_body = lines.back ().substr (0 , content_len);
183
+ if (content_len > request.size ())
184
+ {
185
+ std::cerr << " Invalid Content-Length" << std::endl;
186
+ exit (1 );
187
+ }
188
+ this ->_body = request.substr (request.size () - content_len);
189
+ // std::cout << "S T A R T O F REQUEST B O D Y" << std::endl;
190
+ // std::cout << this->_body << std::endl;
191
+ // std::cout << "E N D O F REQUEST B O D Y" << std::endl;
180
192
}
181
193
else
182
194
{
183
- // std::cout << "____----_-_-_-_--_-_____-_-_-_-____-_-_-_-_-------" << std::endl;
184
195
if (content_len_str == " " || this ->_method == " POST" )
185
196
{
186
197
set_body (" " );
@@ -199,4 +210,9 @@ void request::print_request()
199
210
for (std::map<std::string, std::string>::const_iterator it = this ->_headers .begin (); it != this ->_headers .end (); ++it)
200
211
std::cout << it->first << " : " << it->second << std::endl;
201
212
std::cout << " Body: " << this ->_body << std::endl;
213
+ }
214
+
215
+ std::string request::get_boundary () const
216
+ {
217
+ return (this ->_boundary );
202
218
}
0 commit comments