From d79ef4f3565683a47517cddba4c2b6169e17d022 Mon Sep 17 00:00:00 2001 From: Shakyan Kushwaha Date: Tue, 27 Aug 2024 12:40:26 +0530 Subject: [PATCH] Code refactoring --- .../presto_cpp/main/PrestoServer.cpp | 20 ++++++++++++------- .../presto_cpp/main/http/HttpConstants.h | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/presto-native-execution/presto_cpp/main/PrestoServer.cpp b/presto-native-execution/presto_cpp/main/PrestoServer.cpp index 68cf4e36ef201..2b92c720b5554 100644 --- a/presto-native-execution/presto_cpp/main/PrestoServer.cpp +++ b/presto-native-execution/presto_cpp/main/PrestoServer.cpp @@ -29,6 +29,7 @@ #include "presto_cpp/main/http/filters/HttpEndpointLatencyFilter.h" #include "presto_cpp/main/http/filters/InternalAuthenticationFilter.h" #include "presto_cpp/main/http/filters/StatsFilter.h" +#include "presto_cpp/main/http/HttpConstants.h" #include "presto_cpp/main/operators/BroadcastExchangeSource.h" #include "presto_cpp/main/operators/BroadcastWrite.h" #include "presto_cpp/main/operators/LocalPersistentShuffle.h" @@ -149,6 +150,15 @@ std::string nodeState2String(NodeState nodeState) { return fmt::format(":{}>", static_cast(nodeState)); } +std::string getRequestBody(const std::vector>& body) { + std::string requestBody; + for (const auto& buf : body) { + // Convert IOBuf to fbstring and then to std::string + requestBody += buf->moveToFbString().toStdString(); + } + return requestBody; +} + PrestoServer::PrestoServer(const std::string& configDirectoryPath) : configDirectoryPath_(configDirectoryPath), signalHandler_(std::make_unique(this)), @@ -371,11 +381,7 @@ void PrestoServer::run() { proxygen::HTTPMessage* /*message*/, const std::vector>& body , proxygen::ResponseHandler* downstream) { - std::string requestBody; - for (const auto& buf : body) { - // Convert IOBuf to fbstring and then to std::string - requestBody += buf->moveToFbString().toStdString(); - } + std::string requestBody=getRequestBody(body); try { folly::dynamic jsonBody = folly::parseJson(requestBody); if (jsonBody == "SHUTTING_DOWN") { @@ -395,12 +401,12 @@ void PrestoServer::run() { // Throw a 400 Bad Request error if the body has some other value http::sendErrorResponse( downstream, - "Bad Request: Body must contain 'SHUTTING_DOWN'.",400); + "Bad Request: Body must contain 'SHUTTING_DOWN'.",http::kHttpBadRequest); } } catch (const folly::json::parse_error& e) { // Handle JSON parse errors and send a 400 Bad Request response - http::sendErrorResponse(downstream, e.what(), 400); // 400 Bad Request + http::sendErrorResponse(downstream, e.what(), http::kHttpBadRequest); // 400 Bad Request } }); httpServer_->registerGet( diff --git a/presto-native-execution/presto_cpp/main/http/HttpConstants.h b/presto-native-execution/presto_cpp/main/http/HttpConstants.h index 55c1d41266c9c..2949725f27cfc 100644 --- a/presto-native-execution/presto_cpp/main/http/HttpConstants.h +++ b/presto-native-execution/presto_cpp/main/http/HttpConstants.h @@ -17,6 +17,7 @@ namespace facebook::presto::http { const uint16_t kHttpOk = 200; const uint16_t kHttpAccepted = 202; const uint16_t kHttpNoContent = 204; +const uint16_t kHttpBadRequest = 400; const uint16_t kHttpUnauthorized = 401; const uint16_t kHttpNotFound = 404; const uint16_t kHttpInternalServerError = 500;