in src/platform/MpiServer.c [185:425]
HTTP_STATUS HandleMpiCall(const char* uri, const char* requestBody, char** response, int* responseSize, MPI_CALLS handlers)
{
JSON_Value* rootValue = NULL;
JSON_Value* clientValue = NULL;
JSON_Value* componentValue = NULL;
JSON_Value* objectValue = NULL;
JSON_Value* payloadValue = NULL;
JSON_Value* maxPayloadSizeValue = NULL;
JSON_Object* rootObject = NULL;
int mpiStatus = MPI_OK;
char* uuid = NULL;
const char* client = NULL;
const char* component = NULL;
const char* object = NULL;
char* payload = NULL;
int maxPayloadSizeBytes = 0;
int estimatedSize = 0;
const char* responseFormat = "\"%s\"";
HTTP_STATUS status = HTTP_OK;
if (NULL == uri)
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall: called with invalid null URI");
status = HTTP_BAD_REQUEST;
}
else if (NULL == requestBody)
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall(%s): called with invalid null request body", uri);
status = HTTP_BAD_REQUEST;
}
else if (NULL == response)
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall(%s): called with invalid null response", uri);
status = HTTP_BAD_REQUEST;
}
else if (NULL == responseSize)
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall(%s): called with invalid null response size", uri);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (rootValue = json_parse_string(requestBody)))
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall(%s): failed to parse request body", uri);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (rootObject = json_value_get_object(rootValue)))
{
OsConfigLogError(GetPlatformLog(), "HandleMpiCall(%s): failed to get object from request body", uri);
status = HTTP_BAD_REQUEST;
}
else
{
if (0 == strcmp(uri, MPI_OPEN_URI))
{
if (NULL == (clientValue = json_object_get_value(rootObject, g_clientName)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_clientName);
status = HTTP_BAD_REQUEST;
}
else if (JSONString != json_value_get_type(clientValue))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is not a string", uri, g_clientName);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (client = json_value_get_string(clientValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get string from '%s'", uri, g_clientName);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (maxPayloadSizeValue = json_object_get_value(rootObject, g_maxPayloadSizeBytes)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_maxPayloadSizeBytes);
status = HTTP_BAD_REQUEST;
}
else if (JSONNumber != json_value_get_type(maxPayloadSizeValue))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is not a number", uri, g_maxPayloadSizeBytes);
status = HTTP_BAD_REQUEST;
}
else if (0 > (maxPayloadSizeBytes = (int)json_value_get_number(maxPayloadSizeValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is negative: %d", uri, g_maxPayloadSizeBytes, maxPayloadSizeBytes);
status = HTTP_BAD_REQUEST;
}
else
{
uuid = (char*)handlers.mpiOpen(client, maxPayloadSizeBytes);
if (uuid)
{
estimatedSize = strlen(responseFormat) + strlen(uuid) + 1;
if (NULL != (*response = (char*)malloc(estimatedSize)))
{
snprintf(*response, estimatedSize, responseFormat, uuid);
*responseSize = strlen(*response);
}
else
{
OsConfigLogError(GetPlatformLog(), "%s: failed to allocate memory for response", uri);
status = HTTP_INTERNAL_SERVER_ERROR;
}
FREE_MEMORY(uuid);
}
else
{
OsConfigLogError(GetPlatformLog(), "%s: failed to open client '%s'", uri, client);
status = HTTP_INTERNAL_SERVER_ERROR;
}
}
}
else if ((0 == strcmp(uri, MPI_CLOSE_URI)) ||
(0 == strcmp(uri, MPI_SET_URI)) ||
(0 == strcmp(uri, MPI_GET_URI)) ||
(0 == strcmp(uri, MPI_SET_DESIRED_URI)) ||
(0 == strcmp(uri, MPI_GET_REPORTED_URI)))
{
if (NULL == (clientValue = json_object_get_value(rootObject, g_clientSession)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_clientSession);
status = HTTP_BAD_REQUEST;
}
else if (JSONString != json_value_get_type(clientValue))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is not a string", uri, g_clientSession);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (client = json_value_get_string(clientValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get string from '%s'", uri, g_clientSession);
status = HTTP_BAD_REQUEST;
}
else if (0 == strcmp(uri, MPI_CLOSE_URI))
{
handlers.mpiClose((MPI_HANDLE)client);
status = HTTP_OK;
}
else if ((0 == strcmp(uri, MPI_SET_URI)) || (0 == strcmp(uri, MPI_GET_URI)))
{
if (NULL == (componentValue = json_object_get_value(rootObject, g_componentName)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_componentName);
status = HTTP_BAD_REQUEST;
}
else if (JSONString != json_value_get_type(componentValue))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is not a string", uri, g_componentName);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (component = json_value_get_string(componentValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get string from '%s'", uri, g_componentName);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (objectValue = json_object_get_value(rootObject, g_objectName)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_objectName);
status = HTTP_BAD_REQUEST;
}
else if (JSONString != json_value_get_type(objectValue))
{
OsConfigLogError(GetPlatformLog(), "%s: '%s' is not a string", uri, g_objectName);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (object = json_value_get_string(objectValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get string from '%s'", uri, g_objectName);
status = HTTP_BAD_REQUEST;
}
else
{
if (0 == strcmp(uri, MPI_SET_URI))
{
if (NULL == (payloadValue = json_object_get_value(rootObject, g_payload)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_payload);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (payload = json_serialize_to_string(payloadValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get payload string", uri);
status = HTTP_BAD_REQUEST;
}
else
{
if (MPI_OK != (mpiStatus = handlers.mpiSet((MPI_HANDLE)client, component, object, (MPI_JSON_STRING)payload, strlen(payload))))
{
status = SetErrorResponse(uri, mpiStatus, response, responseSize);
if (IsDebugLoggingEnabled())
{
OsConfigLogError(GetPlatformLog(), "%s(%s, %s): failed for client '%s' with %d (returning %d)", uri, component, object, client, mpiStatus, status);
}
}
}
}
else if (MPI_OK != (mpiStatus = handlers.mpiGet((MPI_HANDLE)client, component, object, response, responseSize)))
{
status = SetErrorResponse(uri, mpiStatus, response, responseSize);
OsConfigLogDebug(GetPlatformLog(), "%s(%s, %s): failed for client '%s' with %d (returning %d)", uri, component, object, client, mpiStatus, status);
}
}
}
else if (0 == strcmp(uri, MPI_SET_DESIRED_URI))
{
if (NULL == (payloadValue = json_object_get_value(rootObject, g_payload)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to parse '%s' from request body", uri, g_payload);
status = HTTP_BAD_REQUEST;
}
else if (NULL == (payload = json_serialize_to_string(payloadValue)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed to get payload string", uri);
status = HTTP_BAD_REQUEST;
}
else if (MPI_OK != (mpiStatus = handlers.mpiSetDesired((MPI_HANDLE)client, (MPI_JSON_STRING)payload, strlen(payload))))
{
OsConfigLogError(GetPlatformLog(), "%s: failed for client '%s' with %d (returning %d)", uri, client, mpiStatus, status);
status = SetErrorResponse(uri, mpiStatus, response, responseSize);
}
}
else if (0 == strcmp(uri, MPI_GET_REPORTED_URI))
{
if (MPI_OK != (mpiStatus = handlers.mpiGetReported((MPI_HANDLE)client, response, responseSize)))
{
OsConfigLogError(GetPlatformLog(), "%s: failed for client '%s' with %d (returning %d)", uri, client, mpiStatus, status);
status = SetErrorResponse(uri, mpiStatus, response, responseSize);
}
}
}
else
{
OsConfigLogError(GetPlatformLog(), "%s: invalid request URI", uri);
status = HTTP_NOT_FOUND;
}
}
json_free_serialized_string(payload);
json_value_free(rootValue);
return status;
}