'Generic Commit'
This commit is contained in:
@@ -9,19 +9,54 @@ UpdateHandler::UpdateHandler(Server& server) : server_(server) {}
|
|||||||
void UpdateHandler::handle_update_object(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr &)>&& callback) {
|
void UpdateHandler::handle_update_object(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr &)>&& callback) {
|
||||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||||
std::map<std::string, std::string> params;
|
std::map<std::string, std::string> params;
|
||||||
|
|
||||||
// Validate authentication and rate limit (no required query params, just auth)
|
// Validate authentication and rate limit (no required query params, just auth)
|
||||||
if (!server_.validate_write_request(req, resp, {}, params)) {
|
if (!server_.validate_write_request(req, resp, {}, params)) {
|
||||||
callback(resp);
|
callback(resp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse JSON body
|
// Parse request based on content type
|
||||||
nlohmann::json body;
|
nlohmann::json body;
|
||||||
try {
|
const auto& contentType = req->getHeader("content-type");
|
||||||
body = nlohmann::json::parse(req->getBody());
|
|
||||||
} catch (const nlohmann::json::parse_error& e) {
|
if (contentType.find("application/json") != std::string::npos) {
|
||||||
|
// Handle JSON content
|
||||||
|
try {
|
||||||
|
body = nlohmann::json::parse(req->getBody());
|
||||||
|
} catch (const nlohmann::json::parse_error& e) {
|
||||||
|
resp->setStatusCode(drogon::k400BadRequest);
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON body"}};
|
||||||
|
resp->setBody(response.dump());
|
||||||
|
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||||
|
callback(resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (contentType.find("multipart/form-data") != std::string::npos ||
|
||||||
|
contentType.find("application/x-www-form-urlencoded") != std::string::npos) {
|
||||||
|
// Handle form data
|
||||||
|
body = nlohmann::json::object();
|
||||||
|
const auto& form = req->getParameters();
|
||||||
|
|
||||||
|
// Get hash from form data
|
||||||
|
auto hash_it = form.find("hash");
|
||||||
|
if (hash_it != form.end()) {
|
||||||
|
body["hash"] = hash_it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse metadata if present
|
||||||
|
auto meta_it = form.find("metadata");
|
||||||
|
if (meta_it != form.end()) {
|
||||||
|
try {
|
||||||
|
body["metadata"] = nlohmann::json::parse(meta_it->second);
|
||||||
|
} catch (const nlohmann::json::parse_error& e) {
|
||||||
|
// If parsing as JSON fails, treat it as a string
|
||||||
|
body["metadata"] = meta_it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
resp->setStatusCode(drogon::k400BadRequest);
|
resp->setStatusCode(drogon::k400BadRequest);
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON body"}};
|
nlohmann::json response = {{"result", "error"}, {"error", "Unsupported content type. Use application/json or form data."}};
|
||||||
resp->setBody(response.dump());
|
resp->setBody(response.dump());
|
||||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||||
callback(resp);
|
callback(resp);
|
||||||
|
Reference in New Issue
Block a user