Skip to content

Commit

Permalink
fix #1288: correlation id in body for upload to blob (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalton-msft authored Nov 13, 2019
1 parent bb29fc1 commit ccad412
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
87 changes: 59 additions & 28 deletions iothub_client/src/iothub_client_ll_uploadtoblob.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ 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\" }"
static const char* const RESPONSE_BODY_FORMAT = "{\"correlationId\":\"%s\", \"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}";
static const char* const RESPONSE_BODY_ABORTED_MESSAGE = "file upload aborted";
static const char* const RESPONSE_BODY_FAILED_MESSAGE = "client not able to connect with the server";
static const char* const RESPONSE_BODY_ERROR_RETURN_CODE = "-1";
static const char* const RESPONSE_BODY_ERROR_BOOLEAN_STRING = "false";

#define INDEFINITE_TIME ((time_t)-1)

static const char* const EMPTY_STRING = "";
Expand Down Expand Up @@ -538,15 +542,15 @@ static int IoTHubClient_LL_UploadToBlob_step1and2(IOTHUB_CLIENT_LL_UPLOADTOBLOB_
}

/*returns 0 when the IoTHub has been informed about the file upload status*/
static int IoTHubClient_LL_UploadToBlob_step3(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* upload_data, STRING_HANDLE correlationId, HTTPAPIEX_HANDLE iotHubHttpApiExHandle, HTTP_HEADERS_HANDLE requestHttpHeaders, BUFFER_HANDLE messageBody)
static int IoTHubClient_LL_UploadToBlob_step3(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* upload_data, HTTPAPIEX_HANDLE iotHubHttpApiExHandle, HTTP_HEADERS_HANDLE requestHttpHeaders, BUFFER_HANDLE messageBody)
{
int result;
/*here is step 3. depending on the outcome of step 2 it needs to inform IoTHub about the file upload status*/
/*if step 1 failed, there's nothing that step 3 needs to report.*/
/*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);
if (relativePathNotification == NULL)
{
result = MU_FAILURE;
Expand Down Expand Up @@ -788,23 +792,38 @@ 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 = STRING_construct_sprintf(RESPONSE_BODY_FORMAT,
STRING_c_str(correlationId),
RESPONSE_BODY_ERROR_BOOLEAN_STRING,
RESPONSE_BODY_ERROR_RETURN_CODE,
RESPONSE_BODY_ABORTED_MESSAGE);
if(aborted_response == NULL)
{
LogError("STRING_construct_sprintf failed");
result = IOTHUB_CLIENT_ERROR;
}
else
{
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
size_t response_length = STRING_length(aborted_response);
if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(aborted_response), response_length) == 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
result = IOTHUB_CLIENT_ERROR;
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
result = IOTHUB_CLIENT_ERROR;
}
else
{
/*Codes_SRS_IOTHUBCLIENT_LL_99_009: [ If step 2 is aborted by the client and if step 3 succeeds, then `IoTHubClient_LL_UploadMultipleBlocksToBlob(Ex)` shall return `IOTHUB_CLIENT_OK`. ] */
result = IOTHUB_CLIENT_OK;
}
}
else
{
/*Codes_SRS_IOTHUBCLIENT_LL_99_009: [ If step 2 is aborted by the client and if step 3 succeeds, then `IoTHubClient_LL_UploadMultipleBlocksToBlob(Ex)` shall return `IOTHUB_CLIENT_OK`. ] */
result = IOTHUB_CLIENT_OK;
LogError("Unable to BUFFER_build, can't perform IoTHubClient_LL_UploadToBlob_step3");
result = IOTHUB_CLIENT_ERROR;
}
}
else
{
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,28 +833,40 @@ 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 = STRING_construct_sprintf(RESPONSE_BODY_FORMAT,
STRING_c_str(correlationId),
RESPONSE_BODY_ERROR_BOOLEAN_STRING,
RESPONSE_BODY_ERROR_RETURN_CODE,
RESPONSE_BODY_FAILED_MESSAGE);
if(failed_response == NULL)
{
LogError("STRING_construct_sprintf failed");
result = IOTHUB_CLIENT_ERROR;
}
else
{
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
size_t response_length = STRING_length(failed_response);
if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(failed_response), response_length) == 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
}
}
STRING_delete(failed_response);
result = IOTHUB_CLIENT_ERROR;
}
result = IOTHUB_CLIENT_ERROR;
}
else
{
/*must make a json*/
unsigned char * response = BUFFER_u_char(responseToIoTHub);
STRING_HANDLE req_string;
if(response == NULL)
{
req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":""}", ((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(RESPONSE_BODY_FORMAT,
STRING_c_str(correlationId),
((httpResponse < 300) ? "true" : "false"),
httpResponse,
(response == NULL ? (const unsigned char*)"" : response));
if (req_string == NULL)
{
LogError("Failure constructing string");
Expand All @@ -854,7 +885,7 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE
}
else
{
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, toBeTransmitted) != 0)
if (IoTHubClient_LL_UploadToBlob_step3(upload_data, iotHubHttpApiExHandle, requestHttpHeaders, toBeTransmitted) != 0)
{
LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
result = IOTHUB_CLIENT_ERROR;
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 ccad412

Please sign in to comment.