From 2dd70f620935f4a35737afe5b19a71da28c28f09 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 15 Jul 2025 19:28:07 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(http/parsing?= =?UTF-8?q?):=20full=20url=20percent=20decoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../requestImplementation/Delete.cpp | 19 +++++++++++++++++-- .../requestImplementation/Get.cpp | 19 +++++++++++++++++-- .../requestImplementation/Post.cpp | 19 +++++++++++++++++-- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index 69865d7..07a13f9 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ -/* Updated: 2025/07/08 11:43:21 by adjoly ### ########.fr */ +/* Updated: 2025/07/15 19:16:59 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,21 @@ Delete::Delete(std::string &data, config::Server *srv) { this->parse(data); } +static std::string urlDecode(const std::string& str) { + std::ostringstream decoded; + for (size_t i = 0; i < str.length(); ++i) { + if (str[i] == '%' && i + 2 < str.length()) { + std::string hex = str.substr(i + 1, 2); + char decodedChar = static_cast(std::strtol(hex.c_str(), 0, 16)); + decoded << decodedChar; + i += 2; + } else { + decoded << str[i]; + } + } + return decoded.str(); +} + void Delete::parse(std::string const &data) { std::istringstream stream(data); std::string line; @@ -34,7 +49,7 @@ void Delete::parse(std::string const &data) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; _method = _sanitizeStr(_method); - _target = _sanitizeStr(_target); + _target = urlDecode(_sanitizeStr(_target)); _protocol = _sanitizeStr(_protocol); } diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index b95703e..69865f4 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ -/* Updated: 2025/07/08 15:47:43 by adjoly ### ########.fr */ +/* Updated: 2025/07/15 19:16:46 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,21 @@ Get::~Get(void) { delete _url; } +static std::string urlDecode(const std::string& str) { + std::ostringstream decoded; + for (size_t i = 0; i < str.length(); ++i) { + if (str[i] == '%' && i + 2 < str.length()) { + std::string hex = str.substr(i + 1, 2); + char decodedChar = static_cast(std::strtol(hex.c_str(), 0, 16)); + decoded << decodedChar; + i += 2; + } else { + decoded << str[i]; + } + } + return decoded.str(); +} + void Get::parse(std::string const &data) { std::istringstream stream(data); std::string line; @@ -47,7 +62,7 @@ void Get::parse(std::string const &data) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; _method = _sanitizeStr(_method); - _target = _sanitizeStr(_target); + _target = urlDecode(_sanitizeStr(_target)); _protocol = _sanitizeStr(_protocol); } diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index fa00385..f31032b 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ -/* Updated: 2025/07/12 18:00:40 by mmoussou ### ########.fr */ +/* Updated: 2025/07/15 19:17:08 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,21 @@ Post::Post(std::string &data, config::Server *srv) { this->parse(data); } +static std::string urlDecode(const std::string& str) { + std::ostringstream decoded; + for (size_t i = 0; i < str.length(); ++i) { + if (str[i] == '%' && i + 2 < str.length()) { + std::string hex = str.substr(i + 1, 2); + char decodedChar = static_cast(std::strtol(hex.c_str(), 0, 16)); + decoded << decodedChar; + i += 2; + } else { + decoded << str[i]; + } + } + return decoded.str(); +} + void Post::parse(std::string const &data) { size_t header_end = data.find("\r\n\r\n"); if (header_end == std::string::npos) @@ -45,7 +60,7 @@ void Post::parse(std::string const &data) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; _method = _sanitizeStr(_method); - _target = _sanitizeStr(_target); + _target = urlDecode(_sanitizeStr(_target)); _protocol = _sanitizeStr(_protocol); }