in src/adapters/pnp/AisUtils.c [235:368]
static int SendAisRequest(const char* udsSocketPath, const char* apiUriPath, const char* payload, char** response)
{
size_t payloadLen = 0;
HTTP_CLIENT_RESULT httpResult = HTTP_CLIENT_OK;
HTTP_CLIENT_HANDLE clientHandle = NULL;
HTTP_HEADERS_HANDLE httpHeadersHandle = NULL;
HTTP_CLIENT_REQUEST_TYPE clientRequestType = HTTP_CLIENT_REQUEST_GET;
AIS_HTTP_CONTEXT context = {0};
time_t startTime = 0;
bool timedOut = false;
size_t responseLen = 0;
int bufferSize = 0;
int result = AIS_ERROR;
context.httpStatus = AIS_ERROR;
if ((NULL == udsSocketPath) || (NULL == apiUriPath) || (NULL == response))
{
OsConfigLogError(GetLog(), "SendAisRequest: invalid argument");
return result;
}
context.inProgress = true;
context.httpResponse = NULL;
*response = NULL;
if (NULL == (clientHandle = HttpOpenClient(udsSocketPath, &context)))
{
OsConfigLogError(GetLog(), "SendAisRequest: HttpOpenClient failed");
}
// Send the request and wait for completion
if (NULL != clientHandle)
{
if (NULL != payload)
{
if (NULL != (httpHeadersHandle = HttpCreateHeader()))
{
clientRequestType = HTTP_CLIENT_REQUEST_POST;
payloadLen = strlen(payload);
}
}
OsConfigLogInfo(GetLog(), "SendAisRequest: %s %s to %s, %d long", (HTTP_CLIENT_REQUEST_POST == clientRequestType) ? "POST" : "GET", apiUriPath, udsSocketPath, (int)payloadLen);
OsConfigLogDebug(GetLog(), "SendAisRequest payload: %s", payload);
if (HTTP_CLIENT_OK != (httpResult = uhttp_client_execute_request(clientHandle, clientRequestType, apiUriPath, httpHeadersHandle, (const unsigned char*)payload, payloadLen, HttpReceiveCallback, &context)))
{
OsConfigLogError(GetLog(), "SendAisRequest: uhttp_client_execute_request failed with %d", (int)httpResult);
}
else
{
OsConfigLogInfo(GetLog(), "SendAisRequest: uhttp_client_execute_request sent, entering wait");
startTime = get_time(NULL);
timedOut = false;
do
{
uhttp_client_dowork(clientHandle);
timedOut = ((difftime(get_time(NULL), startTime) > AIS_WAIT_TIMEOUT)) ? true : false;
} while ((true == context.inProgress) && (false == timedOut));
if (true == timedOut)
{
OsConfigLogError(GetLog(), "SendAisRequest: timed out waiting for uhttp_client_execute_request completion");
result = AIS_ERROR;
}
else
{
result = context.httpStatus;
OsConfigLogInfo(GetLog(), "SendAisRequest: uhttp_client_execute_request complete, wait ended with %d", result);
}
}
}
// Get the response
if (AIS_SUCCESS == result)
{
if (0 != (bufferSize = BUFFER_size(context.httpResponse, &responseLen)))
{
OsConfigLogError(GetLog(), "SendAisRequest: BUFFER_size failed returning non-zero %d", bufferSize);
result = AIS_ERROR;
}
else if ((responseLen > AIS_RESPONSE_SIZE_MAX) && (responseLen < AIS_RESPONSE_SIZE_MIN))
{
OsConfigLogError(GetLog(), "SendAisRequest: response size out of supported range (%d, %d)", AIS_RESPONSE_SIZE_MIN, AIS_RESPONSE_SIZE_MAX);
result = AIS_ERROR;
}
else
{
if (NULL != (*response = (char*)malloc(responseLen + 1)))
{
memcpy(&(*response)[0], BUFFER_u_char(context.httpResponse), responseLen);
(*response)[responseLen] = 0;
result = AIS_SUCCESS;
OsConfigLogInfo(GetLog(), "SendAisRequest: ok");
OsConfigLogDebug(GetLog(), "SendAisRequest response: %s", *response);
}
else
{
OsConfigLogError(GetLog(), "SendAisRequest: out of memory allocating %d bytes", (int)(responseLen + 1));
result = AIS_ERROR;
}
}
}
// Clean-up
HTTPHeaders_Free(httpHeadersHandle);
uhttp_client_close(clientHandle, NULL, NULL);
uhttp_client_destroy(clientHandle);
if (NULL != context.httpResponse)
{
BUFFER_delete(context.httpResponse);
}
if (AIS_SUCCESS != result)
{
FREE_MEMORY(*response);
}
if (AIS_SUCCESS == result)
{
OsConfigLogInfo(GetLog(), "SendAisRequest(%s) ok", udsSocketPath);
}
else
{
OsConfigLogError(GetLog(), "SendAisRequest(%s) failed with %d", udsSocketPath, result);
}
return result;
}