const char* ConstMap_GetValue(CONSTMAP_HANDLE handle, const char* key) { const char* value = NULL; if (handle == NULL) { /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/ LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); } else { if (key == NULL) { /*Codes_SRS_CONSTMAP_17_040: [If parameter handle or key is NULL then ConstMap_GetValue returns NULL.]*/ LOG_CONSTMAP_ERROR(CONSTMAP_INVALIDARG); } else { /*Codes_SRS_CONSTMAP_17_041: [If the key is not found, then ConstMap_GetValue returns NULL.]*/ /*Codes_SRS_CONSTMAP_17_042: [Otherwise, ConstMap_GetValue returns the key's value.]*/ value = Map_GetValueFromKey(((CONSTMAP_HANDLE_DATA *)handle)->map, key); } } return value; }
const char* HTTPHeaders_FindHeaderValue(HTTP_HEADERS_HANDLE httpHeadersHandle, const char* name) { const char* result; /*Codes_SRS_HTTP_HEADERS_99_022:[ The return value shall be NULL if name parameter is NULL or if httpHeadersHandle is NULL]*/ if ( (httpHeadersHandle == NULL) || (name == NULL) ) { result = NULL; } else { /*Codes_SRS_HTTP_HEADERS_99_018:[ Calling this API shall retrieve the value for a previously stored name.]*/ /*Codes_SRS_HTTP_HEADERS_99_020:[ The return value shall be different than NULL when the name matches the name of a previously stored name:value pair.] */ /*Codes_SRS_HTTP_HEADERS_99_021:[ In this case the return value shall point to a string that shall strcmp equal to the original stored string.]*/ HTTP_HEADERS_HANDLE_DATA* handleData = (HTTP_HEADERS_HANDLE_DATA*)httpHeadersHandle; result = Map_GetValueFromKey(handleData->headers, name); } return result; }
const char* IoTHubMessage_GetProperty(IOTHUB_MESSAGE_HANDLE msg_handle, const char* key) { const char* result; if (msg_handle == NULL || key == NULL) { LogError("invalid parameter (NULL) to IoTHubMessage_GetProperty iotHubMessageHandle=%p, key=%p", msg_handle, key); result = NULL; } else { bool key_exists = false; // The return value is not neccessary, just check the key_exist variable if ((Map_ContainsKey(msg_handle->properties, key, &key_exists) == MAP_OK) && key_exists) { result = Map_GetValueFromKey(msg_handle->properties, key); } else { result = NULL; } } return result; }
PROVISIONING_SERVICE_CLIENT_HANDLE prov_sc_create_from_connection_string(const char* conn_string) { PROV_SERVICE_CLIENT* result; if (conn_string == NULL) { LogError("Input parameter is NULL: conn_string"); result = NULL; } else { STRING_HANDLE cs_string; if ((cs_string = STRING_construct(conn_string)) == NULL) { LogError("STRING_construct failed"); result = NULL; } else { MAP_HANDLE connection_string_values_map; if ((connection_string_values_map = connectionstringparser_parse(cs_string)) == NULL) { LogError("Tokenizing conn_string failed"); result = NULL; } else { const char* hostname = NULL; const char* key_name = NULL; const char* key = NULL; if ((hostname = Map_GetValueFromKey(connection_string_values_map, IOTHUBHOSTNAME)) == NULL) { LogError("Couldn't find %s in conn_string", IOTHUBHOSTNAME); result = NULL; } else if ((key_name = Map_GetValueFromKey(connection_string_values_map, IOTHUBSHAREDACESSKEYNAME)) == NULL) { LogError("Couldn't find %s in conn_string", IOTHUBSHAREDACESSKEYNAME); result = NULL; } else if ((key = Map_GetValueFromKey(connection_string_values_map, IOTHUBSHAREDACESSKEY)) == NULL) { LogError("Couldn't find %s in conn_string", IOTHUBSHAREDACESSKEY); result = NULL; } if (hostname == NULL || key_name == NULL || key == NULL) { LogError("invalid parameter hostname: %p, key_name: %p, key: %p", hostname, key_name, key); result = NULL; } else if ((result = malloc(sizeof(PROV_SERVICE_CLIENT))) == NULL) { LogError("Allocation of provisioning service client failed"); result = NULL; } else { memset(result, 0, sizeof(PROV_SERVICE_CLIENT)); if (mallocAndStrcpy_s(&result->provisioning_service_uri, hostname) != 0) { LogError("Failure allocating of provisioning service uri"); prov_sc_destroy(result); result = NULL; } else if (mallocAndStrcpy_s(&result->key_name, key_name) != 0) { LogError("Failure allocating of keyname"); prov_sc_destroy(result); result = NULL; } else if (mallocAndStrcpy_s(&result->access_key, key) != 0) { LogError("Failure allocating of access key"); prov_sc_destroy(result); result = NULL; } else { result->tracing = TRACING_STATUS_OFF; } } Map_Destroy(connection_string_values_map); } } STRING_delete(cs_string); } return result; }
/*Codes_SRS_HTTP_HEADERS_99_012:[ Calling this API shall record a header from name and value parameters.]*/ static HTTP_HEADERS_RESULT headers_ReplaceHeaderNameValuePair(HTTP_HEADERS_HANDLE handle, const char* name, const char* value, bool replace) { HTTP_HEADERS_RESULT result; /*Codes_SRS_HTTP_HEADERS_99_014:[ The function shall return when the handle is not valid or when name parameter is NULL or when value parameter is NULL.]*/ if ( (handle == NULL) || (name == NULL) || (value == NULL) ) { result = HTTP_HEADERS_INVALID_ARG; LogError("invalid arg (NULL) , result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_036:[ If name contains the characters outside character codes 33 to 126 then the return value shall be HTTP_HEADERS_INVALID_ARG]*/ /*Codes_SRS_HTTP_HEADERS_99_031:[ If name contains the character ":" then the return value shall be HTTP_HEADERS_INVALID_ARG.]*/ size_t i; size_t nameLen = strlen(name); for (i = 0; i < nameLen; i++) { if ((name[i] < 33) || (126 < name[i]) || (name[i] == ':')) { break; } } if (i < nameLen) { result = HTTP_HEADERS_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { HTTP_HEADERS_HANDLE_DATA* handleData = (HTTP_HEADERS_HANDLE_DATA*)handle; const char* existingValue = Map_GetValueFromKey(handleData->headers, name); /*eat up the whitespaces from value, as per RFC 2616, chapter 4.2 "The field value MAY be preceded by any amount of LWS, though a single SP is preferred."*/ /*Codes_SRS_HTTP_HEADERS_02_002: [The LWS from the beginning of the value shall not be stored.] */ while ((value[0] == ' ') || (value[0] == '\t') || (value[0] == '\r') || (value[0] == '\n')) { value++; } if (!replace && (existingValue != NULL)) { char* newValue = (char*)malloc(strlen(existingValue) + COMMA_AND_SPACE_LENGTH + strlen(value) + 1); if (newValue == NULL) { /*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/ result = HTTP_HEADERS_ALLOC_FAILED; LogError("failed to malloc , result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_017:[ If the name already exists in the collection of headers, the function shall concatenate the new value after the existing value, separated by a comma and a space as in: old-value+", "+new-value.]*/ (void)strcpy(newValue, existingValue); (void)strcat(newValue, COMMA_AND_SPACE); (void)strcat(newValue, value); /*Codes_SRS_HTTP_HEADERS_99_016:[ The function shall store the name:value pair in such a way that when later retrieved by a call to GetHeader it will return a string that shall strcmp equal to the name+": "+value.]*/ if (Map_AddOrUpdate(handleData->headers, name, newValue) != MAP_OK) { /*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/ result = HTTP_HEADERS_ERROR; LogError("failed to Map_AddOrUpdate, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_013:[ The function shall return HTTP_HEADERS_OK when execution is successful.]*/ result = HTTP_HEADERS_OK; } free(newValue); } } else { /*Codes_SRS_HTTP_HEADERS_99_016:[ The function shall store the name:value pair in such a way that when later retrieved by a call to GetHeader it will return a string that shall strcmp equal to the name+": "+value.]*/ if (Map_AddOrUpdate(handleData->headers, name, value) != MAP_OK) { /*Codes_SRS_HTTP_HEADERS_99_015:[ The function shall return HTTP_HEADERS_ALLOC_FAILED when an internal request to allocate memory fails.]*/ result = HTTP_HEADERS_ALLOC_FAILED; LogError("failed to Map_AddOrUpdate, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { result = HTTP_HEADERS_OK; } } } } return result; }
IOTHUB_SERVICE_CLIENT_AUTH_HANDLE IoTHubServiceClientAuth_CreateFromConnectionString(const char* connectionString) { IOTHUB_SERVICE_CLIENT_AUTH_HANDLE result; /*Codes_SRS_IOTHUBSERVICECLIENT_12_001: [** IoTHubServiceClientAuth_CreateFromConnectionString shall verify the input parameter and if it is NULL then return NULL **]*/ if (connectionString == NULL) { LogError("Input parameter is NULL: connectionString"); result = NULL; } else { /*Codes_SRS_IOTHUBSERVICECLIENT_12_002: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory for a new service client instance. **] */ result = malloc(sizeof(IOTHUB_SERVICE_CLIENT_AUTH)); if (result == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_003: [** If the allocation failed, IoTHubServiceClientAuth_CreateFromConnectionString shall return NULL **] */ LogError("Malloc failed for IOTHUB_SERVICE_CLIENT_AUTH"); } else { /*Codes_SRS_IOTHUBSERVICECLIENT_12_009: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create a STRING_HANDLE from the given connection string by calling STRING_construct. **] */ STRING_HANDLE connection_string; if ((connection_string = STRING_construct(connectionString)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_010: [** If the STRING_construct fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("STRING_construct failed"); free(result); result = NULL; } else { /*Codes_SRS_IOTHUBSERVICECLIENT_12_004: [** IoTHubServiceClientAuth_CreateFromConnectionString shall populate hostName, iotHubName, iotHubSuffix, sharedAccessKeyName, sharedAccessKeyValue from the given connection string by calling connectionstringparser_parse **] */ MAP_HANDLE connection_string_values_map; if ((connection_string_values_map = connectionstringparser_parse(connection_string)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_005: [** If populating the IOTHUB_SERVICE_CLIENT_AUTH fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL **] */ LogError("Tokenizing failed on connectionString"); free(result); result = NULL; } else { STRING_TOKENIZER_HANDLE tokenizer = NULL; STRING_HANDLE token_key_string = NULL; STRING_HANDLE token_value_string = NULL; STRING_HANDLE host_name_string = NULL; const char* hostName; const char* keyName; const char* sharedAccessKey; const char* iothubName; const char* iothubSuffix; /*Codes_SRS_IOTHUBSERVICECLIENT_12_004: [** IoTHubServiceClientAuth_CreateFromConnectionString shall populate hostName, iotHubName, iotHubSuffix, sharedAccessKeyName, sharedAccessKeyValue from the given connection string by calling connectionstringparser_parse **] */ (void)memset(result, 0, sizeof(IOTHUB_SERVICE_CLIENT_AUTH)); if ((hostName = Map_GetValueFromKey(connection_string_values_map, IOTHUBHOSTNAME)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_011: [** If the populating HostName fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Couldn't find %s in connection string", IOTHUBHOSTNAME); free(result); result = NULL; } else if ((keyName = Map_GetValueFromKey(connection_string_values_map, IOTHUBSHAREDACESSKEYNAME)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_012: [** If the populating SharedAccessKeyName fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Couldn't find %s in connection string", IOTHUBSHAREDACESSKEYNAME); free(result); result = NULL; } else if ((sharedAccessKey = Map_GetValueFromKey(connection_string_values_map, IOTHUBSHAREDACESSKEY)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_013: [** If the populating SharedAccessKey fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Couldn't find %s in connection string", IOTHUBSHAREDACESSKEY); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_038: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create a STRING_handle from hostName by calling STRING_construct. **] */ else if ((host_name_string = STRING_construct(hostName)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_039: [** If the STRING_construct fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("STRING_construct failed"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_014: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create a STRING_TOKENIZER to parse HostName by calling STRING_TOKENIZER_create. **] */ else if ((tokenizer = STRING_TOKENIZER_create(host_name_string)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_015: [** If the STRING_TOKENIZER_create fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Error creating STRING tokenizer"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_016: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create a new STRING_HANDLE for token key string by calling STRING_new. **] */ else if ((token_key_string = STRING_new()) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_017: [** If the STRING_new fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Error creating key token STRING_HANDLE"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_018: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create a new STRING_HANDLE for token value string by calling STRING_new. **] */ else if ((token_value_string = STRING_new()) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_019: [** If the STRING_new fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Error creating value token STRING_HANDLE"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_020: [** IoTHubServiceClientAuth_CreateFromConnectionString shall call STRING_TOKENIZER_get_next_token to get token key string. **] */ else if (STRING_TOKENIZER_get_next_token(tokenizer, token_key_string, ".") != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_021: [** If the STRING_TOKENIZER_get_next_token fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Error reading key token STRING"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_022: [** IoTHubServiceClientAuth_CreateFromConnectionString shall call STRING_TOKENIZER_get_next_token to get token value string. **] */ else if (STRING_TOKENIZER_get_next_token(tokenizer, token_value_string, "0") != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_023: [** If the STRING_TOKENIZER_get_next_token fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("Error reading value token STRING"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_024: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory and copy hostName to result->hostName by calling mallocAndStrcpy_s. **] */ else if (mallocAndStrcpy_s(&result->hostname, hostName) != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_025: [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("mallocAndStrcpy_s failed for hostName"); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_026: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory and copy keyName to result->keyName by calling mallocAndStrcpy_s. **] */ else if (mallocAndStrcpy_s(&result->keyName, keyName) != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_027: [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("mallocAndStrcpy_s failed for keyName"); free(result->hostname); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_028: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory and copy sharedAccessKey to result->sharedAccessKey by calling mallocAndStrcpy_s. **] */ else if (mallocAndStrcpy_s(&result->sharedAccessKey, sharedAccessKey) != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_029: [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("mallocAndStrcpy_s failed for sharedAccessKey"); free(result->hostname); free(result->keyName); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_034: [** IoTHubServiceClientAuth_CreateFromConnectionString shall create C string from token key string handle by calling STRING_c_str. **] */ else if ((iothubName = STRING_c_str(token_key_string)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_035 : [** If the STRING_c_str fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("STRING_c_str failed for iothubName"); free(result->hostname); free(result->keyName); free(result->sharedAccessKey); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_036 : [** IoTHubServiceClientAuth_CreateFromConnectionString shall create C string from token value string handle by calling STRING_c_str. **] */ else if ((iothubSuffix = STRING_c_str(token_value_string)) == NULL) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_037 : [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("STRING_c_str failed for iothubSuffix"); free(result->hostname); free(result->keyName); free(result->sharedAccessKey); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_030: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory and copy iothubName to result->iothubName by calling mallocAndStrcpy_s. **] */ else if (mallocAndStrcpy_s(&result->iothubName, iothubName) != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_031 : [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("mallocAndStrcpy_s failed for sharedAccessKey"); free(result->hostname); free(result->keyName); free(result->sharedAccessKey); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_032: [** IoTHubServiceClientAuth_CreateFromConnectionString shall allocate memory and copy iothubSuffix to result->iothubSuffix by calling mallocAndStrcpy_s. **] */ else if (mallocAndStrcpy_s(&result->iothubSuffix, iothubSuffix) != 0) { /*Codes_SRS_IOTHUBSERVICECLIENT_12_033 : [** If the mallocAndStrcpy_s fails, IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return NULL. **] */ LogError("mallocAndStrcpy_s failed for sharedAccessKey"); free(result->hostname); free(result->keyName); free(result->sharedAccessKey); free(result->iothubName); free(result); result = NULL; } /*Codes_SRS_IOTHUBSERVICECLIENT_12_006: [** If the IOTHUB_SERVICE_CLIENT_AUTH has been populated IoTHubServiceClientAuth_CreateFromConnectionString shall do clean up and return with a IOTHUB_SERVICE_CLIENT_AUTH_HANDLE to it **] */ STRING_delete(token_key_string); STRING_delete(token_value_string); STRING_delete(host_name_string); STRING_TOKENIZER_destroy(tokenizer); Map_Destroy(connection_string_values_map); } STRING_delete(connection_string); } } } return result; }