Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Fri Nov 03 13:20:36 2017 -0700
Revision:
35:98add15351f3
Parent:
25:8507bf644fdf
Child:
38:ed9c888e5e12
1.1.27

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 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 6 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 7 #include <time.h>
Azure.IoT Build 0:fa2de1b79154 8 #include "azure_c_shared_utility/agenttime.h"
Azure.IoT Build 0:fa2de1b79154 9 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 0:fa2de1b79154 10 #include "azure_c_shared_utility/buffer_.h"
Azure.IoT Build 0:fa2de1b79154 11 #include "azure_c_shared_utility/sastoken.h"
Azure.IoT Build 0:fa2de1b79154 12 #include "azure_c_shared_utility/httpheaders.h"
Azure.IoT Build 0:fa2de1b79154 13 #include "azure_c_shared_utility/httpapiex.h"
Azure.IoT Build 0:fa2de1b79154 14 #include "azure_c_shared_utility/httpapiexsas.h"
Azure.IoT Build 6:c55b013dfc2a 15 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 16
Azure.IoT Build 0:fa2de1b79154 17 typedef struct HTTPAPIEX_SAS_STATE_TAG
Azure.IoT Build 0:fa2de1b79154 18 {
Azure.IoT Build 0:fa2de1b79154 19 STRING_HANDLE key;
Azure.IoT Build 0:fa2de1b79154 20 STRING_HANDLE uriResource;
Azure.IoT Build 0:fa2de1b79154 21 STRING_HANDLE keyName;
Azure.IoT Build 0:fa2de1b79154 22 }HTTPAPIEX_SAS_STATE;
Azure.IoT Build 0:fa2de1b79154 23
Azure.IoT Build 0:fa2de1b79154 24
Azure.IoT Build 0:fa2de1b79154 25 HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriResource, STRING_HANDLE keyName)
Azure.IoT Build 0:fa2de1b79154 26 {
Azure.IoT Build 0:fa2de1b79154 27 HTTPAPIEX_SAS_HANDLE result = NULL;
Azure.IoT Build 0:fa2de1b79154 28 if (key == NULL)
Azure.IoT Build 0:fa2de1b79154 29 {
Azure.IoT Build 0:fa2de1b79154 30 /*Codes_SRS_HTTPAPIEXSAS_06_001: [If the parameter key is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 31 LogError("No key passed to HTTPAPIEX_SAS_Create.");
Azure.IoT Build 0:fa2de1b79154 32 }
Azure.IoT Build 0:fa2de1b79154 33 else if (uriResource == NULL)
Azure.IoT Build 0:fa2de1b79154 34 {
Azure.IoT Build 0:fa2de1b79154 35 /*Codes_SRS_HTTPAPIEXSAS_06_002: [If the parameter uriResource is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
AzureIoTClient 1:9190c0f4d23a 36 LogError("No uri resource passed to HTTPAPIEX_SAS_Create.");
Azure.IoT Build 0:fa2de1b79154 37 }
Azure.IoT Build 0:fa2de1b79154 38 else
Azure.IoT Build 0:fa2de1b79154 39 {
AzureIoTClient 7:1af47e3a19b6 40 /*Codes_SRS_HTTPAPIEXSAS_01_001: [ HTTPAPIEX_SAS_Create shall create a new instance of HTTPAPIEX_SAS and return a non-NULL handle to it. ]*/
AzureIoTClient 25:8507bf644fdf 41 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)malloc(sizeof(HTTPAPIEX_SAS_STATE));
AzureIoTClient 35:98add15351f3 42 /*Codes_SRS_HTTPAPIEXSAS_06_003: [The parameter keyName for HTTPAPIEX_SAS_Create is optional and can be NULL.]*/
Azure.IoT Build 0:fa2de1b79154 43 if (state != NULL)
Azure.IoT Build 0:fa2de1b79154 44 {
Azure.IoT Build 0:fa2de1b79154 45 state->key = NULL;
Azure.IoT Build 0:fa2de1b79154 46 state->uriResource = NULL;
Azure.IoT Build 0:fa2de1b79154 47 state->keyName = NULL;
Azure.IoT Build 0:fa2de1b79154 48 if (((state->key = STRING_clone(key)) == NULL) ||
Azure.IoT Build 0:fa2de1b79154 49 ((state->uriResource = STRING_clone(uriResource)) == NULL) ||
AzureIoTClient 35:98add15351f3 50 ((keyName != NULL) && ((state->keyName = STRING_clone(keyName)) == NULL)))
Azure.IoT Build 0:fa2de1b79154 51 {
Azure.IoT Build 0:fa2de1b79154 52 /*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 53 LogError("Unable to clone the arguments.");
Azure.IoT Build 0:fa2de1b79154 54 HTTPAPIEX_SAS_Destroy(state);
Azure.IoT Build 0:fa2de1b79154 55 }
Azure.IoT Build 0:fa2de1b79154 56 else
Azure.IoT Build 0:fa2de1b79154 57 {
Azure.IoT Build 0:fa2de1b79154 58 result = state;
Azure.IoT Build 0:fa2de1b79154 59 }
Azure.IoT Build 0:fa2de1b79154 60 }
Azure.IoT Build 0:fa2de1b79154 61 }
Azure.IoT Build 0:fa2de1b79154 62 return result;
Azure.IoT Build 0:fa2de1b79154 63 }
Azure.IoT Build 0:fa2de1b79154 64
Azure.IoT Build 0:fa2de1b79154 65 void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle)
Azure.IoT Build 0:fa2de1b79154 66 {
Azure.IoT Build 0:fa2de1b79154 67 /*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 68 if (handle)
Azure.IoT Build 0:fa2de1b79154 69 {
Azure.IoT Build 0:fa2de1b79154 70 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle;
Azure.IoT Build 0:fa2de1b79154 71 /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/
Azure.IoT Build 0:fa2de1b79154 72 if (state->key)
Azure.IoT Build 0:fa2de1b79154 73 {
Azure.IoT Build 0:fa2de1b79154 74 STRING_delete(state->key);
Azure.IoT Build 0:fa2de1b79154 75 }
Azure.IoT Build 0:fa2de1b79154 76 if (state->uriResource)
Azure.IoT Build 0:fa2de1b79154 77 {
Azure.IoT Build 0:fa2de1b79154 78 STRING_delete(state->uriResource);
Azure.IoT Build 0:fa2de1b79154 79 }
Azure.IoT Build 0:fa2de1b79154 80 if (state->keyName)
Azure.IoT Build 0:fa2de1b79154 81 {
Azure.IoT Build 0:fa2de1b79154 82 STRING_delete(state->keyName);
Azure.IoT Build 0:fa2de1b79154 83 }
Azure.IoT Build 0:fa2de1b79154 84 free(state);
Azure.IoT Build 0:fa2de1b79154 85 }
Azure.IoT Build 0:fa2de1b79154 86 }
Azure.IoT Build 0:fa2de1b79154 87
Azure.IoT Build 0:fa2de1b79154 88 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 89 {
Azure.IoT Build 0:fa2de1b79154 90 /*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 91 if (sasHandle != NULL)
Azure.IoT Build 0:fa2de1b79154 92 {
Azure.IoT Build 0:fa2de1b79154 93 /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 94 if (requestHttpHeadersHandle != NULL)
Azure.IoT Build 0:fa2de1b79154 95 {
Azure.IoT Build 0:fa2de1b79154 96 /*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 97 /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 98 if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL)
Azure.IoT Build 0:fa2de1b79154 99 {
Azure.IoT Build 0:fa2de1b79154 100 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle;
Azure.IoT Build 0:fa2de1b79154 101 /*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 102 time_t currentTime = get_time(NULL);
Azure.IoT Build 0:fa2de1b79154 103 /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 104 if (currentTime == (time_t)-1)
Azure.IoT Build 0:fa2de1b79154 105 {
AzureIoTClient 1:9190c0f4d23a 106 LogError("Time does not appear to be working.");
Azure.IoT Build 0:fa2de1b79154 107 }
Azure.IoT Build 0:fa2de1b79154 108 else
Azure.IoT Build 0:fa2de1b79154 109 {
Azure.IoT Build 0:fa2de1b79154 110 /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/
Azure.IoT Build 0:fa2de1b79154 111 /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/
Azure.IoT Build 0:fa2de1b79154 112 size_t expiry = (size_t)(difftime(currentTime, 0) + 3600);
Azure.IoT Build 0:fa2de1b79154 113 STRING_HANDLE newSASToken = SASToken_Create(state->key, state->uriResource, state->keyName, expiry);
Azure.IoT Build 0:fa2de1b79154 114 if (newSASToken != NULL)
Azure.IoT Build 0:fa2de1b79154 115 {
Azure.IoT Build 0:fa2de1b79154 116 /*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 117 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK)
Azure.IoT Build 0:fa2de1b79154 118 {
Azure.IoT Build 0:fa2de1b79154 119 /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/
AzureIoTClient 1:9190c0f4d23a 120 LogError("Unable to replace the old SAS Token.");
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122 /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/
Azure.IoT Build 0:fa2de1b79154 123 STRING_delete(newSASToken);
Azure.IoT Build 0:fa2de1b79154 124 }
Azure.IoT Build 0:fa2de1b79154 125 else
Azure.IoT Build 0:fa2de1b79154 126 {
AzureIoTClient 1:9190c0f4d23a 127 LogError("Unable to create a new SAS token.");
Azure.IoT Build 0:fa2de1b79154 128 }
Azure.IoT Build 0:fa2de1b79154 129 }
Azure.IoT Build 0:fa2de1b79154 130 }
Azure.IoT Build 0:fa2de1b79154 131 }
Azure.IoT Build 0:fa2de1b79154 132 }
Azure.IoT Build 0:fa2de1b79154 133 /*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 134 return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent);
Azure.IoT Build 0:fa2de1b79154 135 }