Microsoft Azure IoTHub client libraries
Dependents: sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more
This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks
Diff: iothub_client_ll_uploadtoblob.c
- Revision:
- 69:b6f532f8c608
- Parent:
- 62:5a4cdacf5090
- Child:
- 74:ea0021abecf7
diff -r 0a75918666d7 -r b6f532f8c608 iothub_client_ll_uploadtoblob.c --- a/iothub_client_ll_uploadtoblob.c Fri Jun 02 15:52:53 2017 -0700 +++ b/iothub_client_ll_uploadtoblob.c Fri Jun 16 16:16:27 2017 -0700 @@ -157,7 +157,7 @@ { int result; - /*Codes_SRS_IOTHUBCLIENT_LL_02_066: [ IoTHubClient_LL_UploadToBlob shall create an HTTP relative path formed from "/devices/" + deviceId + "/files/" + destinationFileName + "?api-version=API_VERSION". ]*/ + /*Codes_SRS_IOTHUBCLIENT_LL_02_066: [ IoTHubClient_LL_UploadToBlob shall create an HTTP relative path formed from "/devices/" + deviceId + "/files/" + "?api-version=API_VERSION". ]*/ STRING_HANDLE relativePath = STRING_construct("/devices/"); if (relativePath == NULL) { @@ -165,354 +165,389 @@ LogError("unable to STRING_construct"); result = __FAILURE__; } - else - { - if (!( - (STRING_concat_with_STRING(relativePath, handleData->deviceId) == 0) && - (STRING_concat(relativePath, "/files/") == 0) && - (STRING_concat(relativePath, destinationFileName) == 0) && - (STRING_concat(relativePath, API_VERSION) == 0) - )) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_067: [ If creating the relativePath fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to concatenate STRING"); - result = __FAILURE__; - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_068: [ IoTHubClient_LL_UploadToBlob shall create an HTTP responseContent BUFFER_HANDLE. ]*/ - BUFFER_HANDLE responseContent = BUFFER_new(); - if (responseContent == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_069: [ If creating the HTTP response buffer handle fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - result = __FAILURE__; - LogError("unable to BUFFER_new"); - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_072: [ IoTHubClient_LL_UploadToBlob shall add the following name:value to request HTTP headers: ] "Content-Type": "application/json" "Accept": "application/json" "User-Agent": "iothubclient/" IOTHUB_SDK_VERSION*/ - /*Codes_SRS_IOTHUBCLIENT_LL_02_107: [ - "Authorization" header shall not be build. ]*/ - if (!( - (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Content-Type", "application/json") == HTTP_HEADERS_OK) && - (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) && - (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "User-Agent", "iothubclient/" IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) && - (handleData->authorizationScheme==X509 || (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Authorization", "") == HTTP_HEADERS_OK)) - )) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_071: [ If creating the HTTP headers fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to HTTPHeaders_AddHeaderNameValuePair"); - result = __FAILURE__; - } - else - { - int wasIoTHubRequestSuccess = 0; /*!=0 means responseContent has a buffer that should be parsed by parson after executing the below switch*/ - /* set the result to error by default */ - result = __FAILURE__; - switch (handleData->authorizationScheme) - { - default: - { - /*wasIoTHubRequestSuccess takes care of the return value*/ - LogError("Internal Error: unexpected value in handleData->authorizationScheme = %d", handleData->authorizationScheme); - result = __FAILURE__; - break; - } - case(X509): - { - unsigned int statusCode; - /*Codes_SRS_IOTHUBCLIENT_LL_02_108: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/ - if (HTTPAPIEX_ExecuteRequest( - iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/ - HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/ - STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ - requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ - NULL, /*BUFFER_HANDLE requestContent - NULL*/ - &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ - NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandl - NULL*/ - responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/ - ) != HTTPAPIEX_OK) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - result = __FAILURE__; - LogError("unable to HTTPAPIEX_ExecuteRequest"); - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - if (statusCode >= 300) - { - result = __FAILURE__; - LogError("HTTP code was %u", statusCode); - } - else - { - wasIoTHubRequestSuccess = 1; - } - } - break; - } - case (SAS_TOKEN): - { - const char* sasToken = STRING_c_str(handleData->credentials.sas); - /*Codes_SRS_IOTHUBCLIENT_LL_02_073: [ If the credentials used to create handle have "sasToken" then IoTHubClient_LL_UploadToBlob shall add the following HTTP request headers: ]*/ - if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeaders, "Authorization", sasToken) != HTTP_HEADERS_OK) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_074: [ If adding "Authorization" fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR ]*/ - result = __FAILURE__; - LogError("unable to HTTPHeaders_AddHeaderNameValuePair"); - } - else - { - unsigned int statusCode; - /*Codes_SRS_IOTHUBCLIENT_LL_02_075: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/ - if (HTTPAPIEX_ExecuteRequest( - iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/ - HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/ - STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ - requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ - NULL, /*BUFFER_HANDLE requestContent - NULL*/ - &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ - NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandl - NULL*/ - responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/ - ) != HTTPAPIEX_OK) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - result = __FAILURE__; - LogError("unable to HTTPAPIEX_ExecuteRequest"); - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - if (statusCode >= 300) - { - result = __FAILURE__; - LogError("HTTP code was %u", statusCode); - } - else - { - wasIoTHubRequestSuccess = 1; - } - } - } - break; - } - case(DEVICE_KEY): - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_078: [ If the credentials used to create handle have "deviceKey" then IoTHubClient_LL_UploadToBlob shall create an HTTPAPIEX_SAS_HANDLE passing as arguments: ]*/ - STRING_HANDLE uriResource = STRING_construct(handleData->hostname); - if (uriResource == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - result = __FAILURE__; - LogError("unable to STRING_construct"); - } - else - { - if (!( - (STRING_concat(uriResource, "/devices/") == 0) && - (STRING_concat_with_STRING(uriResource, handleData->deviceId) == 0) - )) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to STRING_concat_with_STRING"); - result = __FAILURE__; - } - else - { - STRING_HANDLE empty = STRING_new(); - if (empty == NULL) - { - LogError("unable to STRING_new"); - result = __FAILURE__; - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - HTTPAPIEX_SAS_HANDLE sasHandle = HTTPAPIEX_SAS_Create(handleData->credentials.deviceKey, uriResource, empty); - if (sasHandle == NULL) - { - LogError("unable to HTTPAPIEX_SAS_Create"); - result = __FAILURE__; - } - else - { - unsigned int statusCode; - /*Codes_SRS_IOTHUBCLIENT_LL_02_090: [ IoTHubClient_LL_UploadToBlob shall call HTTPAPIEX_SAS_ExecuteRequest passing as arguments: ]*/ - if (HTTPAPIEX_SAS_ExecuteRequest( - sasHandle, /*HTTPAPIEX_SAS_HANDLE sasHandle - the created HTTPAPIEX_SAS_HANDLE*/ - iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the created HTTPAPIEX_HANDLE*/ - HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/ - STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ - requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ - NULL, /*BUFFER_HANDLE requestContent - NULL*/ - &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ - NULL, /*HTTP_HEADERS_HANDLE responseHeadersHandle - NULL*/ - responseContent - ) != HTTPAPIEX_OK) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_079: [ If HTTPAPIEX_SAS_ExecuteRequest fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to HTTPAPIEX_SAS_ExecuteRequest"); - result = __FAILURE__; - } - else - { - if (statusCode >= 300) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_080: [ If status code is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - result = __FAILURE__; - LogError("HTTP code was %u", statusCode); - } - else - { - wasIoTHubRequestSuccess = 1; - } - } - HTTPAPIEX_SAS_Destroy(sasHandle); - } - STRING_delete(empty); - } - } - STRING_delete(uriResource); - } - } - } /*switch*/ + else + { + if (!( + (STRING_concat_with_STRING(relativePath, handleData->deviceId) == 0) && + (STRING_concat(relativePath, "/files") == 0) && + (STRING_concat(relativePath, API_VERSION) == 0) + )) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_067: [ If creating the relativePath fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to concatenate STRING"); + result = __FAILURE__; + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_32_001: [ IoTHubClient_LL_UploadToBlob shall create a JSON string formed from "{ \"blobName\": \" + destinationFileName + "\" }" */ + STRING_HANDLE blobName = STRING_construct("{ \"blobName\": \""); + if (blobName == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_32_002: [ If creating the JSON string fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to STRING_construct"); + result = __FAILURE__; + } + else + { + if (!( + (STRING_concat(blobName, destinationFileName) == 0) && + (STRING_concat(blobName, "\" }") == 0) + )) + { + /*Codes_SRS_IOTHUBCLIENT_LL_32_002: [ If creating the JSON string fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to concatenate STRING"); + result = __FAILURE__; + } + else + { + size_t len = STRING_length(blobName); + BUFFER_HANDLE blobBuffer = BUFFER_create((const unsigned char *)STRING_c_str(blobName), len); - if (wasIoTHubRequestSuccess == 0) - { - /*do nothing, shall be reported as an error*/ - } - else - { - 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) - { - result = __FAILURE__; - LogError("unable to get the response as string"); - } - else - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_081: [ Otherwise, IoTHubClient_LL_UploadToBlob shall use parson to extract and save the following information from the response buffer: correlationID and SasUri. ]*/ - JSON_Value* allJson = json_parse_string(STRING_c_str(responseAsString)); - if (allJson == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_parse_string"); - result = __FAILURE__; - } - else - { - JSON_Object* jsonObject = json_value_get_object(allJson); - if (jsonObject == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_value_get_object"); - result = __FAILURE__; - } - else - { - const char* json_correlationId; - json_correlationId = json_object_get_string(jsonObject, "correlationId"); - if (json_correlationId == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_object_get_string(jsonObject, \"correlationId\")"); - result = __FAILURE__; - } - else - { - if (STRING_copy(correlationId, json_correlationId) != 0) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to copy json_correlationId"); - result = __FAILURE__; - } - else - { - const char* json_hostName = json_object_get_string(jsonObject, "hostName"); - if (json_hostName == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_object_get_string(jsonObject, \"hostName\")"); - result = __FAILURE__; - } - else - { - const char* json_containerName = json_object_get_string(jsonObject, "containerName"); - if (json_containerName == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_object_get_string(jsonObject, \"containerName\")"); - result = __FAILURE__; - } - else - { - const char* json_blobName = json_object_get_string(jsonObject, "blobName"); - if (json_blobName == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_object_get_string(jsonObject, \"blobName\")"); - result = __FAILURE__; - } - else - { - const char* json_sasToken = json_object_get_string(jsonObject, "sasToken"); - if (json_sasToken == NULL) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to json_object_get_string(jsonObject, \"sasToken\")"); - result = __FAILURE__; - } - else - { - /*good JSON received from the service*/ + if (blobBuffer == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_32_002: [ If creating the JSON string fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to create BUFFER"); + result = __FAILURE__; + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_068: [ IoTHubClient_LL_UploadToBlob shall create an HTTP responseContent BUFFER_HANDLE. ]*/ + BUFFER_HANDLE responseContent = BUFFER_new(); + if (responseContent == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_069: [ If creating the HTTP response buffer handle fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + result = __FAILURE__; + LogError("unable to BUFFER_new"); + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_072: [ IoTHubClient_LL_UploadToBlob shall add the following name:value to request HTTP headers: ] "Content-Type": "application/json" "Accept": "application/json" "User-Agent": "iothubclient/" IOTHUB_SDK_VERSION*/ + /*Codes_SRS_IOTHUBCLIENT_LL_02_107: [ - "Authorization" header shall not be build. ]*/ + if (!( + (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Content-Type", "application/json") == HTTP_HEADERS_OK) && + (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) && + (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "User-Agent", "iothubclient/" IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) && + (handleData->authorizationScheme == X509 || (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Authorization", "") == HTTP_HEADERS_OK)) + )) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_071: [ If creating the HTTP headers fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to HTTPHeaders_AddHeaderNameValuePair"); + result = __FAILURE__; + } + else + { + int wasIoTHubRequestSuccess = 0; /*!=0 means responseContent has a buffer that should be parsed by parson after executing the below switch*/ + /* set the result to error by default */ + result = __FAILURE__; + switch (handleData->authorizationScheme) + { + default: + { + /*wasIoTHubRequestSuccess takes care of the return value*/ + LogError("Internal Error: unexpected value in handleData->authorizationScheme = %d", handleData->authorizationScheme); + result = __FAILURE__; + break; + } + case(X509): + { + unsigned int statusCode; + /*Codes_SRS_IOTHUBCLIENT_LL_32_003: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/ + if (HTTPAPIEX_ExecuteRequest( + iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/ + HTTPAPI_REQUEST_POST, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_POST*/ + STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ + requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ + blobBuffer, /*BUFFER_HANDLE requestContent - address of JSON with optional/directory/tree/filename*/ + &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ + NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandle - NULL*/ + responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/ + ) != HTTPAPIEX_OK) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + result = __FAILURE__; + LogError("unable to HTTPAPIEX_ExecuteRequest"); + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + if (statusCode >= 300) + { + result = __FAILURE__; + LogError("HTTP code was %u", statusCode); + } + else + { + wasIoTHubRequestSuccess = 1; + } + } + break; + } + case (SAS_TOKEN): + { + const char* sasToken = STRING_c_str(handleData->credentials.sas); + /*Codes_SRS_IOTHUBCLIENT_LL_02_073: [ If the credentials used to create handle have "sasToken" then IoTHubClient_LL_UploadToBlob shall add the following HTTP request headers: ]*/ + if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeaders, "Authorization", sasToken) != HTTP_HEADERS_OK) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_074: [ If adding "Authorization" fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR ]*/ + result = __FAILURE__; + LogError("unable to HTTPHeaders_AddHeaderNameValuePair"); + } + else + { + unsigned int statusCode; + /*Codes_SRS_IOTHUBCLIENT_LL_32_004: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/ + if (HTTPAPIEX_ExecuteRequest( + iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/ + HTTPAPI_REQUEST_POST, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_POST*/ + STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ + requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ + blobBuffer, /*BUFFER_HANDLE requestContent - address of JSON with optional/directory/tree/filename*/ + &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ + NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandle - NULL*/ + responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/ + ) != HTTPAPIEX_OK) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + result = __FAILURE__; + LogError("unable to HTTPAPIEX_ExecuteRequest"); + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + if (statusCode >= 300) + { + result = __FAILURE__; + LogError("HTTP code was %u", statusCode); + } + else + { + wasIoTHubRequestSuccess = 1; + } + } + } + break; + } + case(DEVICE_KEY): + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_078: [ If the credentials used to create handle have "deviceKey" then IoTHubClient_LL_UploadToBlob shall create an HTTPAPIEX_SAS_HANDLE passing as arguments: ]*/ + STRING_HANDLE uriResource = STRING_construct(handleData->hostname); + if (uriResource == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + result = __FAILURE__; + LogError("unable to STRING_construct"); + } + else + { + if (!( + (STRING_concat(uriResource, "/devices/") == 0) && + (STRING_concat_with_STRING(uriResource, handleData->deviceId) == 0) + )) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to STRING_concat_with_STRING"); + result = __FAILURE__; + } + else + { + STRING_HANDLE empty = STRING_new(); + if (empty == NULL) + { + LogError("unable to STRING_new"); + result = __FAILURE__; + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + HTTPAPIEX_SAS_HANDLE sasHandle = HTTPAPIEX_SAS_Create(handleData->credentials.deviceKey, uriResource, empty); + if (sasHandle == NULL) + { + LogError("unable to HTTPAPIEX_SAS_Create"); + result = __FAILURE__; + } + else + { + unsigned int statusCode; + /*Codes_SRS_IOTHUBCLIENT_LL_32_005: [ IoTHubClient_LL_UploadToBlob shall call HTTPAPIEX_SAS_ExecuteRequest passing as arguments: ]*/ + if (HTTPAPIEX_SAS_ExecuteRequest( + sasHandle, /*HTTPAPIEX_SAS_HANDLE sasHandle - the created HTTPAPIEX_SAS_HANDLE*/ + iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the created HTTPAPIEX_HANDLE*/ + HTTPAPI_REQUEST_POST, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_POST*/ + STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/ + requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/ + blobBuffer, /*BUFFER_HANDLE requestContent - address of JSON with optional/directory/tree/filename*/ + &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/ + NULL, /*HTTP_HEADERS_HANDLE responseHeadersHandle - NULL*/ + responseContent + ) != HTTPAPIEX_OK) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_079: [ If HTTPAPIEX_SAS_ExecuteRequest fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to HTTPAPIEX_SAS_ExecuteRequest"); + result = __FAILURE__; + } + else + { + if (statusCode >= 300) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_080: [ If status code is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + result = __FAILURE__; + LogError("HTTP code was %u", statusCode); + } + else + { + wasIoTHubRequestSuccess = 1; + } + } + HTTPAPIEX_SAS_Destroy(sasHandle); + } + STRING_delete(empty); + } + } + STRING_delete(uriResource); + } + } + } /*switch*/ - if (STRING_copy(sasUri, "https://") != 0) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to STRING_copy"); - result = __FAILURE__; - } - else - { - if (!( - (STRING_concat(sasUri, json_hostName) == 0) && - (STRING_concat(sasUri, "/") == 0) && - (STRING_concat(sasUri, json_containerName) == 0) && - (STRING_concat(sasUri, "/") == 0) && - (STRING_concat(sasUri, json_blobName) == 0) && - (STRING_concat(sasUri, json_sasToken) == 0) - )) - { - /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ - LogError("unable to STRING_concat"); - result = __FAILURE__; - } - else - { - result = 0; /*success in step 1*/ - } - } - } - } - } - } - } - } - } - json_value_free(allJson); - } - STRING_delete(responseAsString); - } - } - } - BUFFER_delete(responseContent); - } - } - STRING_delete(relativePath); - } + if (wasIoTHubRequestSuccess == 0) + { + /*do nothing, shall be reported as an error*/ + } + else + { + 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) + { + result = __FAILURE__; + LogError("unable to get the response as string"); + } + else + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_081: [ Otherwise, IoTHubClient_LL_UploadToBlob shall use parson to extract and save the following information from the response buffer: correlationID and SasUri. ]*/ + JSON_Value* allJson = json_parse_string(STRING_c_str(responseAsString)); + if (allJson == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_parse_string"); + result = __FAILURE__; + } + else + { + JSON_Object* jsonObject = json_value_get_object(allJson); + if (jsonObject == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_value_get_object"); + result = __FAILURE__; + } + else + { + const char* json_correlationId; + json_correlationId = json_object_get_string(jsonObject, "correlationId"); + if (json_correlationId == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_object_get_string(jsonObject, \"correlationId\")"); + result = __FAILURE__; + } + else + { + if (STRING_copy(correlationId, json_correlationId) != 0) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to copy json_correlationId"); + result = __FAILURE__; + } + else + { + const char* json_hostName = json_object_get_string(jsonObject, "hostName"); + if (json_hostName == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_object_get_string(jsonObject, \"hostName\")"); + result = __FAILURE__; + } + else + { + const char* json_containerName = json_object_get_string(jsonObject, "containerName"); + if (json_containerName == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_object_get_string(jsonObject, \"containerName\")"); + result = __FAILURE__; + } + else + { + const char* json_blobName = json_object_get_string(jsonObject, "blobName"); + if (json_blobName == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_object_get_string(jsonObject, \"blobName\")"); + result = __FAILURE__; + } + else + { + const char* json_sasToken = json_object_get_string(jsonObject, "sasToken"); + if (json_sasToken == NULL) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to json_object_get_string(jsonObject, \"sasToken\")"); + result = __FAILURE__; + } + else + { + /*good JSON received from the service*/ + if (STRING_copy(sasUri, "https://") != 0) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to STRING_copy"); + result = __FAILURE__; + } + else + { + if (!( + (STRING_concat(sasUri, json_hostName) == 0) && + (STRING_concat(sasUri, "/") == 0) && + (STRING_concat(sasUri, json_containerName) == 0) && + (STRING_concat(sasUri, "/") == 0) && + (STRING_concat(sasUri, json_blobName) == 0) && + (STRING_concat(sasUri, json_sasToken) == 0) + )) + { + /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/ + LogError("unable to STRING_concat"); + result = __FAILURE__; + } + else + { + result = 0; /*success in step 1*/ + } + } + } + } + } + } + } + } + } + json_value_free(allJson); + } + STRING_delete(responseAsString); + } + } + } + BUFFER_delete(responseContent); + } + BUFFER_delete(blobBuffer); + } + } + STRING_delete(blobName); + } + } + STRING_delete(relativePath); + } return result; }