bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod != "POST") {
- HttpUtility::SendJsonError(response, 400, "Invalid request type. Must be POST.");
- return true;
- }
+ if (request.RequestUrl->GetPath().size() != 3)
+ return false;
- if (request.RequestUrl->GetPath().size() < 3) {
- HttpUtility::SendJsonError(response, 400, "Action is missing.");
- return true;
- }
+ if (request.RequestMethod != "POST")
+ return false;
String actionName = request.RequestUrl->GetPath()[2];
ApiAction::Ptr action = ApiAction::GetByName(actionName);
if (!action) {
- HttpUtility::SendJsonError(response, 404, "Action '" + actionName + "' could not be found.");
+ HttpUtility::SendJsonError(response, 404, "Action '" + actionName + "' does not exist.");
return true;
}
{
ApiListener::Ptr listener = ApiListener::GetInstance();
- if (!listener) {
- Log(LogCritical, "ApiListener", "No instance available.");
+ if (!listener)
return;
- }
if (object->IsActive()) {
/* Sync object config */
/* check permissions */
ApiListener::Ptr listener = ApiListener::GetInstance();
- if (!listener) {
- Log(LogCritical, "ApiListener", "No instance available.");
+ if (!listener)
return Empty;
- }
if (!listener->GetAcceptConfig()) {
Log(LogWarning, "ApiListener")
/* check permissions */
ApiListener::Ptr listener = ApiListener::GetInstance();
- if (!listener) {
- Log(LogCritical, "ApiListener", "No instance available.");
+ if (!listener)
return Empty;
- }
if (!listener->GetAcceptConfig()) {
Log(LogWarning, "ApiListener")
bool ConfigFilesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod == "GET")
- HandleGet(user, request, response);
- else
- HttpUtility::SendJsonError(response, 400, "Invalid request type. Must be GET.");
+ if (request.RequestMethod != "GET")
+ return false;
- return true;
-}
-
-void ConfigFilesHandler::HandleGet(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
-{
Dictionary::Ptr params = HttpUtility::FetchRequestParameters(request);
const std::vector<String>& urlPath = request.RequestUrl->GetPath();
String packageName = HttpUtility::GetLastParameter(params, "package");
String stageName = HttpUtility::GetLastParameter(params, "stage");
- if (!ConfigPackageUtility::ValidateName(packageName))
- return HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ if (!ConfigPackageUtility::ValidateName(packageName)) {
+ HttpUtility::SendJsonError(response, 400, "Invalid package name.");
+ return true;
+ }
- if (!ConfigPackageUtility::ValidateName(stageName))
- return HttpUtility::SendJsonError(response, 404, "Stage is not valid or does not exist.");
+ if (!ConfigPackageUtility::ValidateName(stageName)) {
+ HttpUtility::SendJsonError(response, 400, "Invalid stage name.");
+ return true;
+ }
String relativePath = HttpUtility::GetLastParameter(params, "path");
- if (ConfigPackageUtility::ContainsDotDot(relativePath))
- return HttpUtility::SendJsonError(response, 403, "Path contains '..' (not allowed).");
+ if (ConfigPackageUtility::ContainsDotDot(relativePath)) {
+ HttpUtility::SendJsonError(response, 400, "Path contains '..' (not allowed).");
+ return true;
+ }
String path = ConfigPackageUtility::GetPackageDir() + "/" + packageName + "/" + stageName + "/" + relativePath;
- if (!Utility::PathExists(path))
- return HttpUtility::SendJsonError(response, 404, "Path not found.");
+ if (!Utility::PathExists(path)) {
+ HttpUtility::SendJsonError(response, 404, "Path not found.");
+ return true;
+ }
try {
std::ifstream fp(path.CStr(), std::ifstream::in | std::ifstream::binary);
response.AddHeader("Content-Type", "application/octet-stream");
response.WriteBody(content.CStr(), content.GetLength());
} catch (const std::exception& ex) {
- return HttpUtility::SendJsonError(response, 500, "Could not read file.",
+ HttpUtility::SendJsonError(response, 500, "Could not read file.",
request.GetVerboseErrors() ? DiagnosticInformation(ex) : "");
}
-}
-
+ return true;
+}
DECLARE_PTR_TYPEDEFS(ConfigFilesHandler);
virtual bool HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response) override;
-
-private:
- void HandleGet(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response);
};
}
bool ConfigPackagesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestUrl->GetPath().size() > 4) {
- String path = boost::algorithm::join(request.RequestUrl->GetPath(), "/");
- HttpUtility::SendJsonError(response, 404, "The requested path is too long to match any config package requests");
- return true;
- }
+ if (request.RequestUrl->GetPath().size() > 4)
+ return false;
if (request.RequestMethod == "GET")
HandleGet(user, request, response);
else if (request.RequestMethod == "DELETE")
HandleDelete(user, request, response);
else
- HttpUtility::SendJsonError(response, 400, "Invalid request type. Must be GET, POST or DELETE.");
+ return false;
return true;
}
String packageName = HttpUtility::GetLastParameter(params, "package");
if (!ConfigPackageUtility::ValidateName(packageName)) {
- HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ HttpUtility::SendJsonError(response, 400, "Invalid package name.");
return;
}
String packageName = HttpUtility::GetLastParameter(params, "package");
if (!ConfigPackageUtility::ValidateName(packageName)) {
- HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ HttpUtility::SendJsonError(response, 400, "Invalid package name.");
return;
}
bool ConfigStagesHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestUrl->GetPath().size() > 5) {
- String path = boost::algorithm::join(request.RequestUrl->GetPath(), "/");
- HttpUtility::SendJsonError(response, 404, "The requested path is too long to match any config tag requests.");
- return true;
- }
+ if (request.RequestUrl->GetPath().size() > 5)
+ return false;
if (request.RequestMethod == "GET")
HandleGet(user, request, response);
else if (request.RequestMethod == "DELETE")
HandleDelete(user, request, response);
else
- HttpUtility::SendJsonError(response, 400, "Invalid request type. Must be GET, POST or DELETE.");
+ return false;
return true;
}
String stageName = HttpUtility::GetLastParameter(params, "stage");
if (!ConfigPackageUtility::ValidateName(packageName))
- return HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
if (!ConfigPackageUtility::ValidateName(stageName))
- return HttpUtility::SendJsonError(response, 404, "Stage is not valid or does not exist.");
+ return HttpUtility::SendJsonError(response, 400, "Invalid stage name.");
Array::Ptr results = new Array();
String packageName = HttpUtility::GetLastParameter(params, "package");
if (!ConfigPackageUtility::ValidateName(packageName))
- return HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
Dictionary::Ptr files = params->Get("files");
String stageName = HttpUtility::GetLastParameter(params, "stage");
if (!ConfigPackageUtility::ValidateName(packageName))
- return HttpUtility::SendJsonError(response, 404, "Package is not valid or does not exist.");
+ return HttpUtility::SendJsonError(response, 400, "Invalid package name.");
if (!ConfigPackageUtility::ValidateName(stageName))
- return HttpUtility::SendJsonError(response, 404, "Stage is not valid or does not exist.");
+ return HttpUtility::SendJsonError(response, 400, "Invalid stage name.");
try {
ConfigPackageUtility::DeleteStage(packageName, stageName);
Dictionary::Ptr result1 = new Dictionary();
result1->Set("code", 200);
- result1->Set("status", "Stage deleted");
+ result1->Set("status", "Stage deleted.");
Array::Ptr results = new Array();
results->Add(result1);
bool CreateObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod != "PUT") {
- /* there might be other request methods pending */
+ if (request.RequestUrl->GetPath().size() != 4)
return false;
- }
- if (request.RequestUrl->GetPath().size() < 4) {
- HttpUtility::SendJsonError(response, 400, "Object name is missing.");
- return true;
- }
+ if (request.RequestMethod != "PUT")
+ return false;
Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
if (!type) {
- HttpUtility::SendJsonError(response, 403, "Erroneous type was supplied.");
+ HttpUtility::SendJsonError(response, 400, "Invalid type specified.");
return true;
}
bool DeleteObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod != "DELETE") {
- /* there might be other request methods pending */
+ if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
return false;
- }
- if (request.RequestUrl->GetPath().size() < 3) {
- String path = boost::algorithm::join(request.RequestUrl->GetPath(), "/");
- HttpUtility::SendJsonError(response, 404, "The requested path is too long to match any config tag requests.");
- return true;
- }
+ if (request.RequestMethod != "DELETE")
+ return false;
Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
if (!type) {
- HttpUtility::SendJsonError(response, 400, "Erroneous type was supplied.");
+ HttpUtility::SendJsonError(response, 400, "Invalid type specified.");
return true;
}
}
if (!processed) {
String path = boost::algorithm::join(request.RequestUrl->GetPath(), "/");
- HttpUtility::SendJsonError(response, 404, "The requested API '" + path +
- "' could not be found. Please check it for common errors like spelling and consult the docs.");
+ HttpUtility::SendJsonError(response, 404, "The requested path '" + path +
+ "' could not be found.");
return;
}
}
bool ModifyObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod != "POST") {
- /* there might be other request methods pending */
+ if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
return false;
- }
- if (request.RequestUrl->GetPath().size() < 3)
+ if (request.RequestMethod != "POST")
return false;
Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[1]);
- if (!type)
- return false;
+ if (!type) {
+ HttpUtility::SendJsonError(response, 400, "Invalid type specified.");
+ return true;
+ }
QueryDescription qd;
qd.Types.insert(type->GetName());
return true;
}
-
bool ObjectQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- if (request.RequestMethod != "GET")
+ if (request.RequestUrl->GetPath().size() < 3 || request.RequestUrl->GetPath().size() > 4)
return false;
- if (request.RequestUrl->GetPath().size() < 3)
+ if (request.RequestMethod != "GET")
return false;
Type::Ptr type = FilterUtility::TypeFromPluralName(request.RequestUrl->GetPath()[2]);
- if (!type)
- return false;
+ if (!type) {
+ HttpUtility::SendJsonError(response, 400, "Invalid type specified.");
+ return true;
+ }
QueryDescription qd;
qd.Types.insert(type->GetName());
bool StatusHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- Dictionary::Ptr result = new Dictionary();
-
- if (request.RequestMethod != "GET") {
- response.SetStatus(400, "Bad request");
- result->Set("info", "Request must be type GET");
- HttpUtility::SendJsonBody(response, result);
- return true;
- }
+ if (request.RequestUrl->GetPath().size() > 3)
+ return false;
+ if (request.RequestMethod != "GET")
+ return false;
QueryDescription qd;
qd.Types.insert("Status");
Array::Ptr results = Array::FromVector(objs);
+ Dictionary::Ptr result = new Dictionary();
result->Set("results", results);
response.SetStatus(200, "OK");
bool TypeQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
{
- Dictionary::Ptr result = new Dictionary();
+ if (request.RequestUrl->GetPath().size() > 3)
+ return false;
- if (request.RequestMethod != "GET") {
- response.SetStatus(400, "Bad request");
- result->Set("info", "Request must be type GET");
- HttpUtility::SendJsonBody(response, result);
- return true;
- }
+ if (request.RequestMethod != "GET")
+ return false;
QueryDescription qd;
qd.Types.insert("Type");
}
}
+ Dictionary::Ptr result = new Dictionary();
result->Set("results", results);
response.SetStatus(200, "OK");