Skip to content

Commit

Permalink
fix custom iot user agent (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalton authored Nov 30, 2021
1 parent e329335 commit cca74f2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- [[#2027]](https://github.com/Azure/azure-sdk-for-c/pull/2027) Update IoT user agent to append property name before property value in cases where a custom user agent was specified.

### Other Changes

## 1.3.0-beta.1 (2021-11-09)
Expand Down
13 changes: 9 additions & 4 deletions sdk/src/azure/iot/az_iot_hub_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ static const az_span hub_client_param_equals_span = AZ_SPAN_LITERAL_FROM_STR("="

static const az_span hub_digital_twin_model_id = AZ_SPAN_LITERAL_FROM_STR("model-id");
static const az_span hub_service_api_version = AZ_SPAN_LITERAL_FROM_STR("/?api-version=2020-09-30");
static const az_span client_sdk_version
= AZ_SPAN_LITERAL_FROM_STR("DeviceClientType=c%2F" AZ_SDK_VERSION_STRING);
static const az_span client_sdk_device_client_type_name = AZ_SPAN_LITERAL_FROM_STR("dct");
static const az_span client_sdk_version_default_value
= AZ_SPAN_LITERAL_FROM_STR("azsdk-c%2F" AZ_SDK_VERSION_STRING);

AZ_NODISCARD az_iot_hub_client_options az_iot_hub_client_options_default()
{
return (az_iot_hub_client_options){ .module_id = AZ_SPAN_EMPTY,
.user_agent = client_sdk_version,
.user_agent = client_sdk_version_default_value,
.model_id = AZ_SPAN_EMPTY,
.component_names = NULL,
.component_names_length = 0 };
Expand Down Expand Up @@ -76,7 +77,9 @@ AZ_NODISCARD az_result az_iot_hub_client_get_user_name(
}
if (az_span_size(*user_agent) > 0)
{
required_length += az_span_size(*user_agent) + az_span_size(hub_client_param_separator_span);
required_length += az_span_size(hub_client_param_separator_span)
+ az_span_size(client_sdk_device_client_type_name)
+ az_span_size(hub_client_param_equals_span) + az_span_size(*user_agent);
}
// Note we skip the length of the model id since we have to url encode it. Bound checking is done
// later.
Expand Down Expand Up @@ -104,6 +107,8 @@ AZ_NODISCARD az_result az_iot_hub_client_get_user_name(
if (az_span_size(*user_agent) > 0)
{
remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_separator_span));
remainder = az_span_copy(remainder, client_sdk_device_client_type_name);
remainder = az_span_copy_u8(remainder, *az_span_ptr(hub_client_param_equals_span));
remainder = az_span_copy(remainder, *user_agent);
}

Expand Down
12 changes: 6 additions & 6 deletions sdk/tests/iot/hub/test_az_iot_hub_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@
#define TEST_DEVICE_ID_STR "my_device"
#define TEST_HUB_HOSTNAME_STR "myiothub.azure-devices.net"
#define TEST_MODULE_ID "my_module_id"
#define TEST_USER_AGENT "os=azrtos"
#define TEST_USER_AGENT "azrtos"
#define TEST_MODEL_ID "dtmi:YOUR_COMPANY_NAME_HERE:sample_device;1"
#define PLATFORM_USER_AGENT "DeviceClientType=c%2F" AZ_SDK_VERSION_STRING
#define PLATFORM_USER_AGENT "azsdk-c%2F" AZ_SDK_VERSION_STRING

static const az_span test_device_id = AZ_SPAN_LITERAL_FROM_STR(TEST_DEVICE_ID_STR);
static const az_span test_hub_hostname = AZ_SPAN_LITERAL_FROM_STR(TEST_HUB_HOSTNAME_STR);

static const char test_correct_user_name[]
= "myiothub.azure-devices.net/my_device/?api-version=2020-09-30&" PLATFORM_USER_AGENT;
= "myiothub.azure-devices.net/my_device/?api-version=2020-09-30&dct=" PLATFORM_USER_AGENT;
static const char test_correct_user_name_with_model_id[]
= "myiothub.azure-devices.net/my_device/?api-version=2020-09-30&" PLATFORM_USER_AGENT
= "myiothub.azure-devices.net/my_device/?api-version=2020-09-30&dct=" PLATFORM_USER_AGENT
"&model-id=dtmi%3AYOUR_COMPANY_NAME_HERE%3Asample_device%3B1";
static const char test_correct_user_name_with_model_id_with_module_id[]
= "myiothub.azure-devices.net/my_device/my_module_id/"
"?api-version=2020-09-30&os=azrtos&model-id=dtmi%3AYOUR_COMPANY_NAME_HERE%"
"?api-version=2020-09-30&dct=azrtos&model-id=dtmi%3AYOUR_COMPANY_NAME_HERE%"
"3Asample_device%3B1";
static const char test_correct_user_name_with_module_id[]
= "myiothub.azure-devices.net/my_device/my_module_id/?api-version=2020-09-30&os=azrtos";
= "myiothub.azure-devices.net/my_device/my_module_id/?api-version=2020-09-30&dct=azrtos";
static const char test_correct_client_id[] = "my_device";
static const char test_correct_client_id_with_module_id[] = "my_device/my_module_id";

Expand Down

0 comments on commit cca74f2

Please sign in to comment.