Skip to content

Commit

Permalink
fix #1288: correlation id in body for upload to blob
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalton-msft committed Nov 8, 2019
1 parent b740871 commit 96f64f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
25 changes: 18 additions & 7 deletions iothub_client/src/iothub_client_ll_uploadtoblob.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ int snprintf(char * s, size_t n, const char * format, ...)
#endif

/*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadToBlob shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/
#define FILE_UPLOAD_FAILED_BODY "{ \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"client not able to connect with the server\" }"
#define FILE_UPLOAD_ABORTED_BODY "{ \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"file upload aborted\" }"
#define FILE_UPLOAD_FAILED_BODY "{ \"correlationId\":\"%s\", \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"client not able to connect with the server\" }"
#define FILE_UPLOAD_ABORTED_BODY "{ \"correlationId\":\"%s\", \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"file upload aborted\" }"
#define INDEFINITE_TIME ((time_t)-1)

static const char* const EMPTY_STRING = "";
Expand Down Expand Up @@ -546,7 +546,8 @@ static int IoTHubClient_LL_UploadToBlob_step3(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HAND
/*this POST "tries" to happen*/

/*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadMultipleBlocksToBlob(Ex) shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/
STRING_HANDLE relativePathNotification = STRING_construct_sprintf("/devices/%s/files/notifications/%s%s", upload_data->deviceId, STRING_c_str(correlationId), API_VERSION);
STRING_HANDLE relativePathNotification = STRING_construct_sprintf("/devices/%s/files/notifications/%s", upload_data->deviceId, API_VERSION);
(void)correlationId;
if (relativePathNotification == NULL)
{
result = MU_FAILURE;
Expand Down Expand Up @@ -788,7 +789,10 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE
/*Codes_SRS_IOTHUBCLIENT_LL_99_008: [ If step 2 is aborted by the client, then the HTTP message body shall look like: ]*/
LogInfo("Blob_UploadFromSasUri aborted file upload");

if (BUFFER_build(responseToIoTHub, (const unsigned char*)FILE_UPLOAD_ABORTED_BODY, sizeof(FILE_UPLOAD_ABORTED_BODY) / sizeof(FILE_UPLOAD_ABORTED_BODY[0])) == 0)
STRING_HANDLE aborted_response;
aborted_response = STRING_construct_sprintf(FILE_UPLOAD_ABORTED_BODY, STRING_c_str(correlationId));
size_t response_length = STRING_length(aborted_response);
if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(aborted_response), response_length) == 0)
{
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
{
Expand All @@ -806,6 +810,7 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE
LogError("Unable to BUFFER_build, can't perform IoTHubClient_LL_UploadToBlob_step3");
result = IOTHUB_CLIENT_ERROR;
}
STRING_delete(aborted_response);
}
else if (uploadMultipleBlocksResult != BLOB_OK)
{
Expand All @@ -814,13 +819,17 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE

/*do step 3*/ /*try*/
/*Codes_SRS_IOTHUBCLIENT_LL_02_091: [ If step 2 fails without establishing an HTTP dialogue, then the HTTP message body shall look like: ]*/
if (BUFFER_build(responseToIoTHub, (const unsigned char*)FILE_UPLOAD_FAILED_BODY, sizeof(FILE_UPLOAD_FAILED_BODY) / sizeof(FILE_UPLOAD_FAILED_BODY[0])) == 0)
STRING_HANDLE failed_response;
failed_response = STRING_construct_sprintf(FILE_UPLOAD_FAILED_BODY, STRING_c_str(correlationId));
size_t response_length = STRING_length(failed_response);
if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(failed_response), response_length) == 0)
{
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
}
}
STRING_delete(failed_response);
result = IOTHUB_CLIENT_ERROR;
}
else
Expand All @@ -830,11 +839,13 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE
STRING_HANDLE req_string;
if(response == NULL)
{
req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":""}", ((httpResponse < 300) ? "true" : "false"), httpResponse);
req_string = STRING_construct_sprintf("{\"correlationId\":\"%s\", \"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":""}",
STRING_c_str(correlationId), ((httpResponse < 300) ? "true" : "false"), httpResponse);
}
else
{
req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", ((httpResponse < 300) ? "true" : "false"), httpResponse, response);
req_string = STRING_construct_sprintf("{\"correlationId\":\"%s\", \"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}",
STRING_c_str(correlationId), ((httpResponse < 300) ? "true" : "false"), httpResponse, response);
}
if (req_string == NULL)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ static void setup_steps_3(IOTHUB_CREDENTIAL_TYPE cred_type)

if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY)
{
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(IoTHubClient_Auth_Get_DeviceKey(TEST_AUTH_HANDLE)).CallCannotFail();
Expand All @@ -714,7 +713,6 @@ static void setup_steps_3(IOTHUB_CREDENTIAL_TYPE cred_type)
}
else
{
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(HTTPAPIEX_ExecuteRequest(IGNORED_PTR_ARG, HTTPAPI_REQUEST_POST, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, NULL, IGNORED_PTR_ARG))
.CopyOutArgumentBuffer_statusCode(&status_code, sizeof(status_code));
Expand All @@ -734,9 +732,13 @@ static void setup_Blob_UploadMultipleBlocksFromSasUri_mocks(IOTHUB_CREDENTIAL_TY
STRICT_EXPECTED_CALL(Blob_UploadMultipleBlocksFromSasUri(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
.CopyOutArgumentBuffer_httpStatus(&status_code, sizeof(status_code))
.SetReturn(blob_result);
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG));
STRICT_EXPECTED_CALL(STRING_length(IGNORED_PTR_ARG));
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG));
STRICT_EXPECTED_CALL(BUFFER_build(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG));

setup_steps_3(cred_type);
STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG));
}
else
{
Expand All @@ -752,6 +754,7 @@ static void setup_Blob_UploadMultipleBlocksFromSasUri_mocks(IOTHUB_CREDENTIAL_TY
{
STRICT_EXPECTED_CALL(BUFFER_u_char(IGNORED_PTR_ARG)).CallCannotFail();
}
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(STRING_length(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail();
STRICT_EXPECTED_CALL(BUFFER_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_RESULT uploadToBlobGetDataEx(IOTHUB_CLIENT_FI

UPLOADTOBLOB_CALLBACK_STATUS* callbackStatus = (UPLOADTOBLOB_CALLBACK_STATUS*)context;
ASSERT_ARE_EQUAL(UPLOADTOBLOB_CALLBACK_STATUS, *callbackStatus, UPLOADTOBLOB_CALLBACK_PENDING);
ASSERT_ARE_EQUAL(int, (int)result, (int)FILE_UPLOAD_OK);
ASSERT_ARE_EQUAL(int, (int)FILE_UPLOAD_OK, (int)result);

static char* uploadData0 = "AAA-";
static char* uploadData1 = "BBBBB-";
static char* uploadData2 = "CCCCCC-";

ASSERT_ARE_EQUAL(int, (int)Lock(updateBlobTestLock), (int)LOCK_OK);
ASSERT_ARE_EQUAL(int, (int)LOCK_OK, (int)Lock(updateBlobTestLock));

if (data == NULL)
{
Expand Down

0 comments on commit 96f64f5

Please sign in to comment.