Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
httpapiexsas.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 #include <stdlib.h> 00005 #include "azure_c_shared_utility/gballoc.h" 00006 #include <stddef.h> 00007 #include <time.h> 00008 #include "azure_c_shared_utility/agenttime.h" 00009 #include "azure_c_shared_utility/strings.h" 00010 #include "azure_c_shared_utility/buffer_.h" 00011 #include "azure_c_shared_utility/sastoken.h" 00012 #include "azure_c_shared_utility/httpheaders.h" 00013 #include "azure_c_shared_utility/httpapiex.h" 00014 #include "azure_c_shared_utility/httpapiexsas.h" 00015 #include "azure_c_shared_utility/xlogging.h" 00016 #include "azure_c_shared_utility/crt_abstractions.h" 00017 00018 typedef struct HTTPAPIEX_SAS_STATE_TAG 00019 { 00020 char* key; 00021 char* uriResource; 00022 char* keyName; 00023 } HTTPAPIEX_SAS_STATE; 00024 00025 static HTTPAPIEX_SAS_STATE* construct_httpex_sas(const char* key, const char* uriResource, const char* keyName) 00026 { 00027 HTTPAPIEX_SAS_STATE* result; 00028 00029 result = (HTTPAPIEX_SAS_STATE*)malloc(sizeof(HTTPAPIEX_SAS_STATE)); 00030 if (result == NULL) 00031 { 00032 LogError("Failure allocating HTTPAPIEX_SAS_Create."); 00033 } 00034 else 00035 { 00036 (void)memset(result, 0, sizeof(HTTPAPIEX_SAS_STATE)); 00037 if (mallocAndStrcpy_s(&result->key, key) != 0) 00038 { 00039 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00040 LogError("Failure allocating sas key."); 00041 HTTPAPIEX_SAS_Destroy(result); 00042 result = NULL; 00043 } 00044 else if (mallocAndStrcpy_s(&result->uriResource, uriResource) != 0) 00045 { 00046 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00047 LogError("Failure allocating sas uriResource."); 00048 HTTPAPIEX_SAS_Destroy(result); 00049 result = NULL; 00050 } 00051 else if (keyName != NULL && mallocAndStrcpy_s(&result->keyName, keyName) != 0) 00052 { 00053 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00054 LogError("Failure allocating sas keyName."); 00055 HTTPAPIEX_SAS_Destroy(result); 00056 result = NULL; 00057 } 00058 } 00059 return result; 00060 } 00061 00062 HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create_From_String(const char* key, const char* uriResource, const char* keyName) 00063 { 00064 HTTPAPIEX_SAS_HANDLE result = NULL; 00065 if (key == NULL || uriResource == NULL) 00066 { 00067 /* Codes_SRS_HTTPAPIEXSAS_07_001: [ If the parameter key or uriResource is NULL then HTTPAPIEX_SAS_Create_From_String shall return NULL. ] */ 00068 LogError("Invalid parameter key: %p, uriResource: %p", key, uriResource); 00069 result = NULL; 00070 } 00071 else 00072 { 00073 /* Codes_SRS_HTTPAPIEXSAS_07_002: [ If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create_From_String shall return NULL. ] */ 00074 result = construct_httpex_sas(key, uriResource, keyName); 00075 } 00076 /* Codes_SRS_HTTPAPIEXSAS_07_003: [ HTTPAPIEX_SAS_Create_From_String shall create a new instance of HTTPAPIEX_SAS and return a non-NULL handle to it ] */ 00077 return result; 00078 } 00079 00080 HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriResource, STRING_HANDLE keyName) 00081 { 00082 HTTPAPIEX_SAS_HANDLE result = NULL; 00083 if (key == NULL) 00084 { 00085 /*Codes_SRS_HTTPAPIEXSAS_06_001: [If the parameter key is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00086 LogError("No key passed to HTTPAPIEX_SAS_Create."); 00087 } 00088 else if (uriResource == NULL) 00089 { 00090 /*Codes_SRS_HTTPAPIEXSAS_06_002: [If the parameter uriResource is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00091 LogError("No uri resource passed to HTTPAPIEX_SAS_Create."); 00092 } 00093 else 00094 { 00095 /*Codes_SRS_HTTPAPIEXSAS_06_003: [The parameter keyName for HTTPAPIEX_SAS_Create is optional and can be NULL.]*/ 00096 /*Codes_SRS_HTTPAPIEXSAS_01_001: [ HTTPAPIEX_SAS_Create shall create a new instance of HTTPAPIEX_SAS and return a non-NULL handle to it. ]*/ 00097 result = construct_httpex_sas(STRING_c_str(key), STRING_c_str(uriResource), STRING_c_str(keyName) ); 00098 } 00099 return result; 00100 } 00101 00102 void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle) 00103 { 00104 /*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/ 00105 if (handle) 00106 { 00107 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle; 00108 /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/ 00109 if (state->key) 00110 { 00111 free(state->key); 00112 } 00113 if (state->uriResource) 00114 { 00115 free(state->uriResource); 00116 } 00117 if (state->keyName) 00118 { 00119 free(state->keyName); 00120 } 00121 free(state); 00122 } 00123 } 00124 00125 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) 00126 { 00127 /*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.]*/ 00128 if (sasHandle != NULL) 00129 { 00130 /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/ 00131 if (requestHttpHeadersHandle != NULL) 00132 { 00133 /*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.]*/ 00134 /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/ 00135 if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL) 00136 { 00137 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle; 00138 /*Codes_SRS_HTTPAPIEXSAS_06_018: [A value of type time_t that shall be known as currentTime is obtained from calling get_time.]*/ 00139 time_t currentTime = get_time(NULL); 00140 /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/ 00141 if (currentTime == (time_t)-1) 00142 { 00143 LogError("Time does not appear to be working."); 00144 } 00145 else 00146 { 00147 /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/ 00148 /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/ 00149 size_t expiry = (size_t)(difftime(currentTime, 0) + 3600); 00150 STRING_HANDLE newSASToken = SASToken_CreateString(state->key, state->uriResource, state->keyName, expiry); 00151 if (newSASToken != NULL) 00152 { 00153 /*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.]*/ 00154 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK) 00155 { 00156 /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/ 00157 LogError("Unable to replace the old SAS Token."); 00158 } 00159 /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/ 00160 STRING_delete(newSASToken); 00161 } 00162 else 00163 { 00164 LogError("Unable to create a new SAS token."); 00165 } 00166 } 00167 } 00168 } 00169 } 00170 /*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.]*/ 00171 return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent); 00172 }
Generated on Wed Jul 13 2022 23:38:02 by
1.7.2