From b8ca3f6d138934edb1dafe686588c5d365df0b55 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 26 Nov 2018 04:17:34 +0100 Subject: [PATCH] Made a function for parameter parsing --- src/http.cpp | 29 +++++++++++++++++++++++------ src/libravatarserv.hpp | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index cf459b6..ecd8506 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -41,17 +41,34 @@ const Request http::parse_request(const string &request) if (pos_digest != std::string::npos) { digest = digest.substr(0, pos_digest); - std::size_t pos_size = request.find("s=", pos_digest); - if (pos_size != std::string::npos) + + string answer; + answer = get_parameter(request, "s"); + if (!answer.empty()) { - size = static_cast(std::stoul(request.substr(pos_size + 2))); + size = static_cast(std::stoul(answer)); } - pos_size = request.find("size=", pos_digest); - if (pos_size != std::string::npos) + else { - size = static_cast(std::stoul(request.substr(pos_size + 5))); + answer = get_parameter(request, "size"); + if (!answer.empty()) + { + size = static_cast(std::stoul(answer)); + } } } return { digest, size }; } + +const string http::get_parameter(const string &request, const string ¶meter) +{ + std::size_t pos = request.find(parameter + "="); + if (pos != std::string::npos) + { + pos += parameter.length(); + return request.substr(pos, request.find('&', pos)); + } + + return ""; +} diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index 7bc7f1c..d860bdf 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -52,6 +52,7 @@ namespace http // http.cpp }; const Request parse_request(const string &request); + const string get_parameter(const string &request, const string ¶meter); } namespace hash // hash.cpp