Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Shakyan Kushwaha authored and Shakyan Kushwaha committed Oct 4, 2024
1 parent 32b7b6a commit d79ef4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
20 changes: 13 additions & 7 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -149,6 +150,15 @@ std::string nodeState2String(NodeState nodeState) {
return fmt::format("<unknown>:{}>", static_cast<int>(nodeState));
}

std::string getRequestBody(const std::vector<std::unique_ptr<folly::IOBuf>>& 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<SignalHandler>(this)),
Expand Down Expand Up @@ -371,11 +381,7 @@ void PrestoServer::run() {
proxygen::HTTPMessage* /*message*/,
const std::vector<std::unique_ptr<folly::IOBuf>>& 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") {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d79ef4f

Please sign in to comment.