From 87778052f41561eaae6e41a1b22f65310ed5f2cd Mon Sep 17 00:00:00 2001 From: Yhz Date: Thu, 23 Apr 2020 23:16:04 +0800 Subject: [PATCH] move http parser to http utils Signed-off-by: Yhz --- .../web_impl/handler/WebRequestHandler.h | 10 ---- core/src/server/web_impl/utils/Util.cpp | 54 +++++++++++++++++++ core/src/server/web_impl/utils/Util.h | 47 ++-------------- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/core/src/server/web_impl/handler/WebRequestHandler.h b/core/src/server/web_impl/handler/WebRequestHandler.h index 7330427009..7ab050cc13 100644 --- a/core/src/server/web_impl/handler/WebRequestHandler.h +++ b/core/src/server/web_impl/handler/WebRequestHandler.h @@ -77,16 +77,6 @@ class WebRequestHandler { return context_ptr; } - private: - Status - ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, bool nullable = true); - - Status - ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, bool nullable = true); - - Status - ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, bool nullable = true); - private: void AddStatusToJson(nlohmann::json& json, int64_t code, const std::string& msg); diff --git a/core/src/server/web_impl/utils/Util.cpp b/core/src/server/web_impl/utils/Util.cpp index 0108e8ab99..048fac3535 100644 --- a/core/src/server/web_impl/utils/Util.cpp +++ b/core/src/server/web_impl/utils/Util.cpp @@ -11,6 +11,8 @@ #include "server/web_impl/utils/Util.h" +#include "utils/ValidationUtil.h" + namespace milvus { namespace server { namespace web { @@ -54,6 +56,58 @@ CopyBinRowRecords(const OList::ObjectWrapper>::ObjectWrapper& reco return Status::OK(); } +Status +ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, + bool nullable) { + auto query = query_params.get(key.c_str()); + if (nullptr != query.get() && query->getSize() > 0) { + std::string value_str = query->std_str(); + if (!ValidationUtil::ValidateStringIsNumber(value_str).ok()) { + return Status(ILLEGAL_QUERY_PARAM, + "Query param \'offset\' is illegal, only non-negative integer supported"); + } + + value = std::stol(value_str); + } else if (!nullable) { + return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); + } + + return Status::OK(); +} + +Status +ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, + bool nullable) { + auto query = query_params.get(key.c_str()); + if (nullptr != query.get() && query->getSize() > 0) { + value = query->std_str(); + } else if (!nullable) { + return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); + } + + return Status::OK(); +} + +Status +ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, + bool nullable) { + auto query = query_params.get(key.c_str()); + if (nullptr != query.get() && query->getSize() > 0) { + std::string value_str = query->std_str(); + if (!ValidationUtil::ValidateStringIsBool(value_str).ok()) { + return Status(ILLEGAL_QUERY_PARAM, "Query param \'all_required\' must be a bool"); + } + value = value_str == "True" || value_str == "true"; + return Status::OK(); + } + + if (!nullable) { + return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); + } + + return Status::OK(); +} + } // namespace web } // namespace server } // namespace milvus diff --git a/core/src/server/web_impl/utils/Util.h b/core/src/server/web_impl/utils/Util.h index de4d6e9df5..6f2fac2d11 100644 --- a/core/src/server/web_impl/utils/Util.h +++ b/core/src/server/web_impl/utils/Util.h @@ -29,56 +29,15 @@ Status CopyBinRowRecords(const OList::ObjectWrapper>::ObjectWrapper& records, std::vector& vectors); Status -WebRequestHandler::ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, - bool nullable) { - auto query = query_params.get(key.c_str()); - if (nullptr != query.get() && query->getSize() > 0) { - std::string value_str = query->std_str(); - if (!ValidationUtil::ValidateStringIsNumber(value_str).ok()) { - return Status(ILLEGAL_QUERY_PARAM, - "Query param \'offset\' is illegal, only non-negative integer supported"); - } - - value = std::stol(value_str); - } else if (!nullable) { - return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); - } - - return Status::OK(); -} +ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, bool nullable = true); Status -WebRequestHandler::ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, - bool nullable) { - auto query = query_params.get(key.c_str()); - if (nullptr != query.get() && query->getSize() > 0) { - value = query->std_str(); - } else if (!nullable) { - return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); - } - - return Status::OK(); -} +ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, bool nullable = true); Status -WebRequestHandler::ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, - bool nullable) { - auto query = query_params.get(key.c_str()); - if (nullptr != query.get() && query->getSize() > 0) { - std::string value_str = query->std_str(); - if (!ValidationUtil::ValidateStringIsBool(value_str).ok()) { - return Status(ILLEGAL_QUERY_PARAM, "Query param \'all_required\' must be a bool"); - } - value = value_str == "True" || value_str == "true"; - return Status::OK(); - } +ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, bool nullable = true); - if (!nullable) { - return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required"); - } - return Status::OK(); -} } // namespace web } // namespace server