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.
Fork of azure_c_shared_utility by
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 00017 typedef struct HTTPAPIEX_SAS_STATE_TAG 00018 { 00019 STRING_HANDLE key; 00020 STRING_HANDLE uriResource; 00021 STRING_HANDLE keyName; 00022 }HTTPAPIEX_SAS_STATE; 00023 00024 00025 HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriResource, STRING_HANDLE keyName) 00026 { 00027 HTTPAPIEX_SAS_HANDLE result = NULL; 00028 if (key == NULL) 00029 { 00030 /*Codes_SRS_HTTPAPIEXSAS_06_001: [If the parameter key is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00031 LogError("No key passed to HTTPAPIEX_SAS_Create."); 00032 } 00033 else if (uriResource == NULL) 00034 { 00035 /*Codes_SRS_HTTPAPIEXSAS_06_002: [If the parameter uriResource is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00036 LogError("No uri resource passed to HTTPAPIEX_SAS_Create."); 00037 } 00038 else if (keyName == NULL) 00039 { 00040 /*Codes_SRS_HTTPAPIEXSAS_06_003: [If the parameter keyName is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00041 LogError("No key name passed to HTTPAPIEX_SAS_Create."); 00042 } 00043 else 00044 { 00045 /*Codes_SRS_HTTPAPIEXSAS_01_001: [ HTTPAPIEX_SAS_Create shall create a new instance of HTTPAPIEX_SAS and return a non-NULL handle to it. ]*/ 00046 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)malloc(sizeof(HTTPAPIEX_SAS_STATE)); 00047 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00048 if (state != NULL) 00049 { 00050 state->key = NULL; 00051 state->uriResource = NULL; 00052 state->keyName = NULL; 00053 if (((state->key = STRING_clone(key)) == NULL) || 00054 ((state->uriResource = STRING_clone(uriResource)) == NULL) || 00055 ((state->keyName = STRING_clone(keyName)) == NULL)) 00056 { 00057 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/ 00058 LogError("Unable to clone the arguments."); 00059 HTTPAPIEX_SAS_Destroy(state); 00060 } 00061 else 00062 { 00063 result = state; 00064 } 00065 } 00066 } 00067 return result; 00068 } 00069 00070 void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle) 00071 { 00072 /*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/ 00073 if (handle) 00074 { 00075 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle; 00076 /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/ 00077 if (state->key) 00078 { 00079 STRING_delete(state->key); 00080 } 00081 if (state->uriResource) 00082 { 00083 STRING_delete(state->uriResource); 00084 } 00085 if (state->keyName) 00086 { 00087 STRING_delete(state->keyName); 00088 } 00089 free(state); 00090 } 00091 } 00092 00093 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) 00094 { 00095 /*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.]*/ 00096 if (sasHandle != NULL) 00097 { 00098 /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/ 00099 if (requestHttpHeadersHandle != NULL) 00100 { 00101 /*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.]*/ 00102 /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/ 00103 if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL) 00104 { 00105 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle; 00106 /*Codes_SRS_HTTPAPIEXSAS_06_018: [A value of type time_t that shall be known as currentTime is obtained from calling get_time.]*/ 00107 time_t currentTime = get_time(NULL); 00108 /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/ 00109 if (currentTime == (time_t)-1) 00110 { 00111 LogError("Time does not appear to be working."); 00112 } 00113 else 00114 { 00115 /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/ 00116 /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/ 00117 size_t expiry = (size_t)(difftime(currentTime, 0) + 3600); 00118 STRING_HANDLE newSASToken = SASToken_Create(state->key, state->uriResource, state->keyName, expiry); 00119 if (newSASToken != NULL) 00120 { 00121 /*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.]*/ 00122 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK) 00123 { 00124 /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/ 00125 LogError("Unable to replace the old SAS Token."); 00126 } 00127 /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/ 00128 STRING_delete(newSASToken); 00129 } 00130 else 00131 { 00132 LogError("Unable to create a new SAS token."); 00133 } 00134 } 00135 } 00136 } 00137 } 00138 /*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.]*/ 00139 return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent); 00140 }
Generated on Tue Jul 12 2022 19:14:38 by
1.7.2
