mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
194 lines
6.0 KiB
C++
194 lines
6.0 KiB
C++
#include "Request.h"
|
|
#include "../../plugin-macros.generated.h"
|
|
|
|
json GetDefaultJsonObject(json requestData)
|
|
{
|
|
// Always provide an object to prevent exceptions while running checks in requests
|
|
if (!requestData.is_object())
|
|
return json::object();
|
|
else
|
|
return requestData;
|
|
}
|
|
|
|
Request::Request(SessionPtr session, std::string requestType, json requestData) :
|
|
Session(session),
|
|
RpcVersion(session->RpcVersion()),
|
|
IgnoreNonFatalRequestChecks(session->IgnoreNonFatalRequestChecks()),
|
|
RequestType(requestType),
|
|
RequestData(GetDefaultJsonObject(requestData))
|
|
{
|
|
}
|
|
|
|
const bool Request::ValidateBasic(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment) const
|
|
{
|
|
if (!HasRequestData()) {
|
|
statusCode = RequestStatus::MissingRequestData;
|
|
comment = "Your request data is missing or invalid (non-object)";
|
|
return false;
|
|
}
|
|
|
|
if (!RequestData.contains(keyName) || RequestData[keyName].is_null()) {
|
|
statusCode = RequestStatus::MissingRequestParameter;
|
|
comment = std::string("Your request is missing the `") + keyName + "` parameter.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const bool Request::ValidateNumber(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment, const double minValue, const double maxValue) const
|
|
{
|
|
if (!ValidateBasic(keyName, statusCode, comment))
|
|
return false;
|
|
|
|
if (!RequestData[keyName].is_number()) {
|
|
statusCode = RequestStatus::InvalidRequestParameterDataType;
|
|
comment = std::string("The parameter `") + keyName + "` must be a number.";
|
|
return false;
|
|
}
|
|
|
|
double value = RequestData[keyName];
|
|
if (value < minValue) {
|
|
statusCode = RequestStatus::RequestParameterOutOfRange;
|
|
comment = std::string("The parameter `") + keyName + "` is below the minimum of `" + std::to_string(minValue) + "`";
|
|
return false;
|
|
}
|
|
if (value > maxValue) {
|
|
statusCode = RequestStatus::RequestParameterOutOfRange;
|
|
comment = std::string("The parameter `") + keyName + "` is above the maximum of `" + std::to_string(maxValue) + "`";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const bool Request::ValidateString(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment, const bool allowEmpty) const
|
|
{
|
|
if (!ValidateBasic(keyName, statusCode, comment))
|
|
return false;
|
|
|
|
if (!RequestData[keyName].is_string()) {
|
|
statusCode = RequestStatus::InvalidRequestParameterDataType;
|
|
comment = std::string("The parameter `") + keyName + "` must be a string.";
|
|
return false;
|
|
}
|
|
|
|
if (RequestData[keyName].get<std::string>().empty() && !allowEmpty) {
|
|
statusCode = RequestStatus::RequestParameterEmpty;
|
|
comment = std::string("The parameter `") + keyName + "` must not be empty.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const bool Request::ValidateBoolean(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment) const
|
|
{
|
|
if (!ValidateBasic(keyName, statusCode, comment))
|
|
return false;
|
|
|
|
if (!RequestData[keyName].is_boolean()) {
|
|
statusCode = RequestStatus::InvalidRequestParameterDataType;
|
|
comment = std::string("The parameter `") + keyName + "` must be boolean.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const bool Request::ValidateObject(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment, const bool allowEmpty) const
|
|
{
|
|
if (!ValidateBasic(keyName, statusCode, comment))
|
|
return false;
|
|
|
|
if (!RequestData[keyName].is_object()) {
|
|
statusCode = RequestStatus::InvalidRequestParameterDataType;
|
|
comment = std::string("The parameter `") + keyName + "` must be an object.";
|
|
return false;
|
|
}
|
|
|
|
if (RequestData[keyName].empty() && !allowEmpty) {
|
|
statusCode = RequestStatus::RequestParameterEmpty;
|
|
comment = std::string("The parameter `") + keyName + "` must not be empty.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const bool Request::ValidateArray(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment, const bool allowEmpty) const
|
|
{
|
|
if (!ValidateBasic(keyName, statusCode, comment))
|
|
return false;
|
|
|
|
if (!RequestData[keyName].is_array()) {
|
|
statusCode = RequestStatus::InvalidRequestParameterDataType;
|
|
comment = std::string("The parameter `") + keyName + "` must be an array.";
|
|
return false;
|
|
}
|
|
|
|
if (RequestData[keyName].empty() && !allowEmpty) {
|
|
statusCode = RequestStatus::RequestParameterEmpty;
|
|
comment = std::string("The parameter `") + keyName + "` must not be empty.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
obs_source_t *Request::ValidateScene(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment) const
|
|
{
|
|
if (!ValidateString(keyName, statusCode, comment))
|
|
return nullptr;
|
|
|
|
std::string sceneName = RequestData[keyName];
|
|
|
|
obs_source_t *ret = obs_get_source_by_name(sceneName.c_str());
|
|
if (!ret) {
|
|
statusCode = RequestStatus::SceneNotFound;
|
|
comment = std::string("No scene was found by the name of `") + sceneName + "`.";
|
|
return nullptr;
|
|
}
|
|
|
|
if (obs_source_get_type(ret) != OBS_SOURCE_TYPE_SCENE) {
|
|
obs_source_release(ret);
|
|
statusCode = RequestStatus::InvalidSourceType;
|
|
comment = "The specified source is not a scene.";
|
|
return nullptr;
|
|
}
|
|
|
|
OBSScene scene = obs_scene_from_source(ret);
|
|
if (obs_scene_is_group(scene)) {
|
|
obs_source_release(ret);
|
|
statusCode = RequestStatus::InvalidSourceType;
|
|
comment = "The specified source is not a scene.";
|
|
return nullptr;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
obs_source_t *Request::ValidateInput(const std::string keyName, RequestStatus::RequestStatus &statusCode, std::string &comment) const
|
|
{
|
|
if (!ValidateString(keyName, statusCode, comment))
|
|
return nullptr;
|
|
|
|
std::string inputName = RequestData[keyName];
|
|
|
|
obs_source_t *ret = obs_get_source_by_name(inputName.c_str());
|
|
if (!ret) {
|
|
statusCode = RequestStatus::InputNotFound;
|
|
comment = std::string("No input was found by the name of `") + inputName + "`.";
|
|
return nullptr;
|
|
}
|
|
|
|
if (obs_source_get_type(ret) != OBS_SOURCE_TYPE_INPUT) {
|
|
obs_source_release(ret);
|
|
statusCode = RequestStatus::InvalidSourceType;
|
|
comment = "The specified source is not an input.";
|
|
return nullptr;
|
|
}
|
|
|
|
return ret;
|
|
}
|