Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
Azure.IoT Build
Date:
Fri Jul 01 10:43:23 2016 -0700
Revision:
6:c55b013dfc2a
Parent:
1:9190c0f4d23a
Child:
7:1af47e3a19b6
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 6 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 7 #endif
Azure.IoT Build 0:fa2de1b79154 8 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 9
Azure.IoT Build 0:fa2de1b79154 10 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 11 #include <time.h>
Azure.IoT Build 0:fa2de1b79154 12
Azure.IoT Build 0:fa2de1b79154 13 #include "azure_c_shared_utility/agenttime.h"
Azure.IoT Build 0:fa2de1b79154 14 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 0:fa2de1b79154 15 #include "azure_c_shared_utility/buffer_.h"
Azure.IoT Build 0:fa2de1b79154 16 #include "azure_c_shared_utility/sastoken.h"
Azure.IoT Build 0:fa2de1b79154 17 #include "azure_c_shared_utility/httpheaders.h"
Azure.IoT Build 0:fa2de1b79154 18 #include "azure_c_shared_utility/httpapiex.h"
Azure.IoT Build 0:fa2de1b79154 19 #include "azure_c_shared_utility/httpapiexsas.h"
Azure.IoT Build 6:c55b013dfc2a 20 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 typedef struct HTTPAPIEX_SAS_STATE_TAG
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 STRING_HANDLE key;
Azure.IoT Build 0:fa2de1b79154 25 STRING_HANDLE uriResource;
Azure.IoT Build 0:fa2de1b79154 26 STRING_HANDLE keyName;
Azure.IoT Build 0:fa2de1b79154 27 }HTTPAPIEX_SAS_STATE;
Azure.IoT Build 0:fa2de1b79154 28
Azure.IoT Build 0:fa2de1b79154 29
Azure.IoT Build 0:fa2de1b79154 30 HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriResource, STRING_HANDLE keyName)
Azure.IoT Build 0:fa2de1b79154 31 {
Azure.IoT Build 0:fa2de1b79154 32 HTTPAPIEX_SAS_HANDLE result = NULL;
Azure.IoT Build 0:fa2de1b79154 33 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 34 {
Azure.IoT Build 0:fa2de1b79154 35 /*Codes_SRS_HTTPAPIEXSAS_06_001: [If the parameter key is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 36 LogError("No key passed to HTTPAPIEX_SAS_Create.");
Azure.IoT Build 0:fa2de1b79154 37 }
Azure.IoT Build 0:fa2de1b79154 38 else if (uriResource == NULL)
Azure.IoT Build 0:fa2de1b79154 39 {
Azure.IoT Build 0:fa2de1b79154 40 /*Codes_SRS_HTTPAPIEXSAS_06_002: [If the parameter uriResource is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 41 LogError("No uri resource passed to HTTPAPIEX_SAS_Create.");
Azure.IoT Build 0:fa2de1b79154 42 }
Azure.IoT Build 0:fa2de1b79154 43 else if (keyName == NULL)
Azure.IoT Build 0:fa2de1b79154 44 {
Azure.IoT Build 0:fa2de1b79154 45 /*Codes_SRS_HTTPAPIEXSAS_06_003: [If the parameter keyName is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 46 LogError("No key name passed to HTTPAPIEX_SAS_Create.");
Azure.IoT Build 0:fa2de1b79154 47 }
Azure.IoT Build 0:fa2de1b79154 48 else
Azure.IoT Build 0:fa2de1b79154 49 {
Azure.IoT Build 0:fa2de1b79154 50 HTTPAPIEX_SAS_STATE* state = malloc(sizeof(HTTPAPIEX_SAS_STATE));
Azure.IoT Build 0:fa2de1b79154 51 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 52 if (state != NULL)
Azure.IoT Build 0:fa2de1b79154 53 {
Azure.IoT Build 0:fa2de1b79154 54 state->key = NULL;
Azure.IoT Build 0:fa2de1b79154 55 state->uriResource = NULL;
Azure.IoT Build 0:fa2de1b79154 56 state->keyName = NULL;
Azure.IoT Build 0:fa2de1b79154 57 if (((state->key = STRING_clone(key)) == NULL) ||
Azure.IoT Build 0:fa2de1b79154 58 ((state->uriResource = STRING_clone(uriResource)) == NULL) ||
Azure.IoT Build 0:fa2de1b79154 59 ((state->keyName = STRING_clone(keyName)) == NULL))
Azure.IoT Build 0:fa2de1b79154 60 {
Azure.IoT Build 0:fa2de1b79154 61 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 62 LogError("Unable to clone the arguments.");
Azure.IoT Build 0:fa2de1b79154 63 HTTPAPIEX_SAS_Destroy(state);
Azure.IoT Build 0:fa2de1b79154 64 }
Azure.IoT Build 0:fa2de1b79154 65 else
Azure.IoT Build 0:fa2de1b79154 66 {
Azure.IoT Build 0:fa2de1b79154 67 result = state;
Azure.IoT Build 0:fa2de1b79154 68 }
Azure.IoT Build 0:fa2de1b79154 69 }
Azure.IoT Build 0:fa2de1b79154 70 }
Azure.IoT Build 0:fa2de1b79154 71 return result;
Azure.IoT Build 0:fa2de1b79154 72 }
Azure.IoT Build 0:fa2de1b79154 73
Azure.IoT Build 0:fa2de1b79154 74 void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 75 {
Azure.IoT Build 0:fa2de1b79154 76 /*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/
Azure.IoT Build 0:fa2de1b79154 77 if (handle)
Azure.IoT Build 0:fa2de1b79154 78 {
Azure.IoT Build 0:fa2de1b79154 79 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle;
Azure.IoT Build 0:fa2de1b79154 80 /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/
Azure.IoT Build 0:fa2de1b79154 81 if (state->key)
Azure.IoT Build 0:fa2de1b79154 82 {
Azure.IoT Build 0:fa2de1b79154 83 STRING_delete(state->key);
Azure.IoT Build 0:fa2de1b79154 84 }
Azure.IoT Build 0:fa2de1b79154 85 if (state->uriResource)
Azure.IoT Build 0:fa2de1b79154 86 {
Azure.IoT Build 0:fa2de1b79154 87 STRING_delete(state->uriResource);
Azure.IoT Build 0:fa2de1b79154 88 }
Azure.IoT Build 0:fa2de1b79154 89 if (state->keyName)
Azure.IoT Build 0:fa2de1b79154 90 {
Azure.IoT Build 0:fa2de1b79154 91 STRING_delete(state->keyName);
Azure.IoT Build 0:fa2de1b79154 92 }
Azure.IoT Build 0:fa2de1b79154 93 free(state);
Azure.IoT Build 0:fa2de1b79154 94 }
Azure.IoT Build 0:fa2de1b79154 95 }
Azure.IoT Build 0:fa2de1b79154 96
Azure.IoT Build 0:fa2de1b79154 97 HTTPAPIEX_RESULT HTTPAPIEX_SAS_ExecuteRequest(HTTPAPIEX_SAS_HANDLE sasHandle, HTTPAPIEX_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HTTP_HEADERS_HANDLE requestHttpHeadersHandle, BUFFER_HANDLE requestContent, unsigned int* statusCode, HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent)
Azure.IoT Build 0:fa2de1b79154 98 {
Azure.IoT Build 0:fa2de1b79154 99 /*Codes_SRS_HTTPAPIEXSAS_06_007: [If the parameter sasHandle is NULL then HTTPAPIEX_SAS_ExecuteRequest shall simply invoke HTTPAPIEX_ExecuteRequest with the remaining parameters (following sasHandle) as its arguments and shall return immediately with the result of that call as the result of HTTPAPIEX_SAS_ExecuteRequest.]*/
Azure.IoT Build 0:fa2de1b79154 100 if (sasHandle != NULL)
Azure.IoT Build 0:fa2de1b79154 101 {
Azure.IoT Build 0:fa2de1b79154 102 /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 103 if (requestHttpHeadersHandle != NULL)
Azure.IoT Build 0:fa2de1b79154 104 {
Azure.IoT Build 0:fa2de1b79154 105 /*Codes_SRS_HTTPAPIEXSAS_06_009: [HTTPHeaders_FindHeaderValue shall be invoked with the requestHttpHeadersHandle as its first argument and the string "Authorization" as its second argument.]*/
Azure.IoT Build 0:fa2de1b79154 106 /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 107 if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL)
Azure.IoT Build 0:fa2de1b79154 108 {
Azure.IoT Build 0:fa2de1b79154 109 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle;
Azure.IoT Build 0:fa2de1b79154 110 /*Codes_SRS_HTTPAPIEXSAS_06_018: [A value of type time_t that shall be known as currentTime is obtained from calling get_time.]*/
Azure.IoT Build 0:fa2de1b79154 111 time_t currentTime = get_time(NULL);
Azure.IoT Build 0:fa2de1b79154 112 /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 113 if (currentTime == (time_t)-1)
Azure.IoT Build 0:fa2de1b79154 114 {
AzureIoTClient 1:9190c0f4d23a 115 LogError("Time does not appear to be working.");
Azure.IoT Build 0:fa2de1b79154 116 }
Azure.IoT Build 0:fa2de1b79154 117 else
Azure.IoT Build 0:fa2de1b79154 118 {
Azure.IoT Build 0:fa2de1b79154 119 /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/
Azure.IoT Build 0:fa2de1b79154 120 /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 121 size_t expiry = (size_t)(difftime(currentTime, 0) + 3600);
Azure.IoT Build 0:fa2de1b79154 122 STRING_HANDLE newSASToken = SASToken_Create(state->key, state->uriResource, state->keyName, expiry);
Azure.IoT Build 0:fa2de1b79154 123 if (newSASToken != NULL)
Azure.IoT Build 0:fa2de1b79154 124 {
Azure.IoT Build 0:fa2de1b79154 125 /*Codes_SRS_HTTPAPIEXSAS_06_013: [HTTPHeaders_ReplaceHeaderNameValuePair shall be invoked with "Authorization" as its second argument and STRING_c_str (newSASToken) as its third argument.]*/
Azure.IoT Build 0:fa2de1b79154 126 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK)
Azure.IoT Build 0:fa2de1b79154 127 {
Azure.IoT Build 0:fa2de1b79154 128 /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/
AzureIoTClient 1:9190c0f4d23a 129 LogError("Unable to replace the old SAS Token.");
Azure.IoT Build 0:fa2de1b79154 130 }
Azure.IoT Build 0:fa2de1b79154 131 /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/
Azure.IoT Build 0:fa2de1b79154 132 STRING_delete(newSASToken);
Azure.IoT Build 0:fa2de1b79154 133 }
Azure.IoT Build 0:fa2de1b79154 134 else
Azure.IoT Build 0:fa2de1b79154 135 {
AzureIoTClient 1:9190c0f4d23a 136 LogError("Unable to create a new SAS token.");
Azure.IoT Build 0:fa2de1b79154 137 }
Azure.IoT Build 0:fa2de1b79154 138 }
Azure.IoT Build 0:fa2de1b79154 139 }
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141 }
Azure.IoT Build 0:fa2de1b79154 142 /*Codes_SRS_HTTPAPIEXSAS_06_016: [HTTPAPIEX_ExecuteRequest with the remaining parameters (following sasHandle) as its arguments will be invoked and the result of that call is the result of HTTPAPIEX_SAS_ExecuteRequest.]*/
Azure.IoT Build 0:fa2de1b79154 143 return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent);
Azure.IoT Build 0:fa2de1b79154 144 }