static int IoTHubClient_LL_UploadToBlob_GetBlobCredentialsFromIoTHub()

in iothub_client/src/iothub_client_ll_uploadtoblob.c [468:618]


static int IoTHubClient_LL_UploadToBlob_GetBlobCredentialsFromIoTHub(
    IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* upload_data, const char* destinationFileName, char** correlationId, char** sasUri)
{
    int result;
    STRING_HANDLE relativePath = STRING_construct_sprintf("/devices/%s/files/%s", upload_data->deviceId, API_VERSION);

    if (relativePath == NULL)
    {
        LogError("Failure constructing string");
        result = MU_FAILURE;
    }
    else
    {
        STRING_HANDLE blobName = STRING_construct_sprintf("{ \"blobName\": \"%s\" }", destinationFileName);

        if (blobName == NULL)
        {
            LogError("Failure constructing string");
            result = MU_FAILURE;
        }
        else
        {
            // Keep STRING_length separate in an separate call to avoid messing up with unit tests.
            // For details, see function parameter evaluation order (C99 6.5.2.2p10).
            size_t blobNameLength = STRING_length(blobName);
            BUFFER_HANDLE blobBuffer = BUFFER_create(
                (const unsigned char *)STRING_c_str(blobName), blobNameLength);

            if (blobBuffer == NULL)
            {
                LogError("unable to create BUFFER");
                result = MU_FAILURE;
            }
            else
            {
                BUFFER_HANDLE responseContent;

                if ((responseContent = BUFFER_new()) == NULL)
                {
                    result = MU_FAILURE;
                    LogError("unable to BUFFER_new");
                }
                else
                {
                    HTTPAPIEX_HANDLE iotHubHttpApiExHandle = createIotHubHttpApiExHandle(upload_data);

                    if (iotHubHttpApiExHandle == NULL)
                    {
                        LogError("Failed to create the HTTPAPIEX_HANDLE for Azure IoT Hub");
                        result = MU_FAILURE;
                    }
                    else
                    {
                        HTTP_HEADERS_HANDLE iotHubRequestHttpHeaders;

                        if ((iotHubRequestHttpHeaders = createIotHubRequestHttpHeaders(upload_data)) == NULL)
                        {
                            LogError("Failed to allocate HTTP headers for IoT Hub connection");
                            result = MU_FAILURE;
                        }
                        else
                        {
                            bool wasIoTHubRequestSuccess = false;

                            switch (upload_data->cred_type)
                            {
                                default:
                                {
                                    LogError("Internal Error: unexpected value in auth schema = %d", upload_data->cred_type);
                                    break;
                                }
                                case IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN:
                                case IOTHUB_CREDENTIAL_TYPE_DEVICE_AUTH:
                                case IOTHUB_CREDENTIAL_TYPE_X509_ECC:
                                case IOTHUB_CREDENTIAL_TYPE_X509:
                                {
                                    if (send_http_request(iotHubHttpApiExHandle, STRING_c_str(relativePath), iotHubRequestHttpHeaders, blobBuffer, responseContent) != 0)
                                    {
                                        LogError("unable to HTTPAPIEX_ExecuteRequest");
                                    }
                                    else
                                    {
                                        wasIoTHubRequestSuccess = true;
                                    }
                                    break;
                                }
                                case IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY:
                                {
                                    if (send_http_sas_request(upload_data, iotHubHttpApiExHandle, STRING_c_str(relativePath), iotHubRequestHttpHeaders, blobBuffer, responseContent) != 0)
                                    {
                                        LogError("unable to HTTPAPIEX_SAS_ExecuteRequest");
                                    }
                                    else
                                    {
                                        wasIoTHubRequestSuccess = true;
                                    }

                                    break;
                                }
                            }

                            if (wasIoTHubRequestSuccess)
                            {
                                const unsigned char* responseContent_u_char = BUFFER_u_char(responseContent);
                                size_t responseContent_length = BUFFER_length(responseContent);
                                STRING_HANDLE responseAsString = STRING_from_byte_array(responseContent_u_char, responseContent_length);
                                if (responseAsString == NULL)
                                {
                                    LogError("unable to get the response as string");
                                    result = MU_FAILURE;
                                }
                                else
                                {
                                    if (parseResultFromIoTHub(STRING_c_str(responseAsString), correlationId, sasUri) != 0)
                                    {
                                        LogError("unable to parse json result");
                                        result = MU_FAILURE;
                                    }
                                    else
                                    {
                                        result = 0;
                                    }

                                    STRING_delete(responseAsString);
                                }
                            }
                            else
                            {
                                result = MU_FAILURE;
                            }

                            HTTPHeaders_Free(iotHubRequestHttpHeaders);
                        }

                        HTTPAPIEX_Destroy(iotHubHttpApiExHandle);
                    }

                    BUFFER_delete(responseContent);
                }

                BUFFER_delete(blobBuffer);
            }

            STRING_delete(blobName);
        }

        STRING_delete(relativePath);
    }

    return result;
}