From f2bbadcf7005c03a8fd4b93b5756ca3357332421 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 2 Aug 2023 14:55:39 +0200 Subject: [PATCH] Move Boundary retrival into Utilities --- src/main.cpp | 18 +++--------------- src/utilities.cpp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c553f88..a7f70b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1019,7 +1019,6 @@ int main(int argc, char *argv[]) { ID_SELECT_FREELANCER_NAME }); - string freelancerName, freelancerID; pqxx::result freelancerNameResult = Database::executePreparedStatement_SELECT_FREELANCER_NAME(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); freelancerName = freelancerNameResult.at(0).at(0).c_str(); @@ -1027,25 +1026,14 @@ int main(int argc, char *argv[]) { freelancerID = freelancerIDResult.at(0).at(0).c_str(); string formDataBoundary; - - //get boundary - for (const auto &header: postRequest.headers) { - if (header.first == "Content-Type") { - string boundaryHeaderName = "boundary="; - std::size_t contentTypeStart = header.second.find(boundaryHeaderName); - if (contentTypeStart == string::npos) { - return crow::response(400, "Boundary could not be found"); - break; - } - contentTypeStart += boundaryHeaderName.size() + 1; - formDataBoundary = header.second.substr(contentTypeStart, header.second.size()); - } - } + if (!Utilities::getBoundaryFromPostRequest(formDataBoundary, postRequest)) + return crow::response(400, "Boundary could not be found"); //check if boundary is in body size_t positionBoundaryStart = postRequest.body.find(formDataBoundary); if (positionBoundaryStart == string::npos) return crow::response(400, "Content Boundary could not be found in the body of the request"); + size_t positionBoundaryEnd = postRequest.body.find(formDataBoundary + "--"); if (positionBoundaryEnd == string::npos) return crow::response(400, "Content Boundary could not be found in the body of the request"); diff --git a/src/utilities.cpp b/src/utilities.cpp index fccfa1a..ef982d9 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -826,5 +826,25 @@ namespace Utilities { return false; return true; } + + /* + * Parses headers of a request to find the boundary. + * boundary is written onto formDataBoundary + * returns false if no boundary was found + */ + bool getBoundaryFromPostRequest(std::string& formDataBoundary, const crow::request& postRequest) { + for (const auto &header: postRequest.headers) { + if (header.first == "Content-Type") { + std::string boundaryHeaderName = "boundary="; + std::size_t contentTypeStart = header.second.find(boundaryHeaderName); + if (contentTypeStart == std::string::npos) + return false; + contentTypeStart += boundaryHeaderName.size() + 1; + formDataBoundary = header.second.substr(contentTypeStart, header.second.size()); + break; + } + } + return true; + } } #endif \ No newline at end of file