EISErr SendEISRequest()

in src/utils/eis_utils/src/eis_coms.c [264:423]


EISErr SendEISRequest(
    const char* udsSocketPath,
    const char* apiUriPath,
    const char* payload,
    unsigned int timeoutMS,
    char** responseBuff)
{
    EISErr result = EISErr_Failed;

    if (udsSocketPath == NULL || apiUriPath == NULL || responseBuff == NULL)
    {
        return EISErr_InvalidArg;
    }

    *responseBuff = NULL;
    char* response = NULL;
    const int port = 80;
    size_t payloadLen = 0;

    HTTP_HEADERS_HANDLE httpHeadersHandle = NULL;

    HTTP_HEADERS_RESULT httpHeadersResult;

    HTTP_CLIENT_RESULT clientResult;
    HTTP_CLIENT_REQUEST_TYPE clientRequestType = HTTP_CLIENT_REQUEST_GET;

    SOCKETIO_CONFIG config;
    memset(&config, 0, sizeof(config));
    config.accepted_socket = NULL;
    config.hostname = udsSocketPath;
    config.port = port; // Check on this

    EIS_HTTP_WORKLOAD_CONTEXT workloadCtx;
    workloadCtx.continue_running = true;
    workloadCtx.http_response = NULL;
    workloadCtx.status = EISErr_Failed;

    HTTP_CLIENT_HANDLE clientHandle =
        uhttp_client_create(socketio_get_interface_description(), &config, on_eis_http_error, &workloadCtx);

    if (clientHandle == NULL)
    {
        goto done;
    }

    if (uhttp_client_set_option(clientHandle, OPTION_ADDRESS_TYPE, OPTION_ADDRESS_TYPE_DOMAIN_SOCKET)
        != HTTP_CLIENT_OK)
    {
        goto done;
    }

    if (uhttp_client_open(clientHandle, udsSocketPath, 0, on_eis_http_connected, &workloadCtx) != HTTP_CLIENT_OK)
    {
        result = workloadCtx.status;
        goto done;
    }

    if (payload != NULL)
    {
        httpHeadersHandle = HTTPHeaders_Alloc();
        if (httpHeadersHandle == NULL)
        {
            goto done;
        }

        httpHeadersResult = HTTPHeaders_AddHeaderNameValuePair(httpHeadersHandle, "Content-Type", "application/json");

        if (httpHeadersResult != HTTP_HEADERS_OK)
        {
            goto done;
        }

        clientRequestType = HTTP_CLIENT_REQUEST_POST;
        payloadLen = strlen(payload);
    }

    clientResult = uhttp_client_execute_request(
        clientHandle,
        clientRequestType,
        apiUriPath,
        httpHeadersHandle,
        (const unsigned char*)payload,
        payloadLen,
        on_eis_http_recv,
        &workloadCtx);

    if (clientResult != HTTP_CLIENT_OK)
    {
        goto done;
    }

    time_t startTime = get_time(NULL);
    bool timedOut = false;

    do
    {
        uhttp_client_dowork(clientHandle);
        timedOut = (difftime(get_time(NULL), startTime) > timeoutMS);

    } while (workloadCtx.continue_running == true && !timedOut);

    if (timedOut)
    {
        result = EISErr_TimeoutErr;
        goto done;
    }

    if (workloadCtx.status != EISErr_Ok)
    {
        result = workloadCtx.status;
        goto done;
    }

    size_t responseLen = 0;
    if (BUFFER_size(workloadCtx.http_response, &responseLen) != 0)
    {
        goto done;
    }

    if (responseLen > EIS_RESP_SIZE_MAX || responseLen < EIS_RESP_SIZE_MIN)
    {
        result = EISErr_RecvRespOutOfLimitsErr;
        goto done;
    }

    response = (char*)malloc(responseLen + 1);

    if (response == NULL)
    {
        goto done;
    }

    memcpy(response, BUFFER_u_char(workloadCtx.http_response), responseLen);
    response[responseLen] = '\0';

    result = EISErr_Ok;

done:

    // Cleanup

    HTTPHeaders_Free(httpHeadersHandle);
    uhttp_client_close(clientHandle, NULL, NULL);
    uhttp_client_destroy(clientHandle);

    if (workloadCtx.http_response != NULL)
    {
        BUFFER_delete(workloadCtx.http_response);
    }

    if (result != EISErr_Ok)
    {
        free(response);
        response = NULL;
    }

    *responseBuff = response;

    return result;
}