source: azure_iot_hub/trunk/azure_iohub/c-utility/src/httpapiexsas.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 9.3 KB
Line 
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#include <stdlib.h>
5#include "azure_c_shared_utility/gballoc.h"
6#include <stddef.h>
7#include <time.h>
8#include "azure_c_shared_utility/agenttime.h"
9#include "azure_c_shared_utility/strings.h"
10#include "azure_c_shared_utility/buffer_.h"
11#include "azure_c_shared_utility/sastoken.h"
12#include "azure_c_shared_utility/httpheaders.h"
13#include "azure_c_shared_utility/httpapiex.h"
14#include "azure_c_shared_utility/httpapiexsas.h"
15#include "azure_c_shared_utility/xlogging.h"
16#include "azure_c_shared_utility/crt_abstractions.h"
17
18#define SHARED_ACCESS_SIGNATURE_PREFIX "sas="
19
20typedef struct HTTPAPIEX_SAS_STATE_TAG
21{
22 char* key;
23 char* uriResource;
24 char* keyName;
25} HTTPAPIEX_SAS_STATE;
26
27static HTTPAPIEX_SAS_STATE* construct_httpex_sas(const char* key, const char* uriResource, const char* keyName)
28{
29 HTTPAPIEX_SAS_STATE* result;
30
31 result = (HTTPAPIEX_SAS_STATE*)malloc(sizeof(HTTPAPIEX_SAS_STATE));
32 if (result == NULL)
33 {
34 LogError("Failure allocating HTTPAPIEX_SAS_Create.");
35 }
36 else
37 {
38 (void)memset(result, 0, sizeof(HTTPAPIEX_SAS_STATE));
39 if (mallocAndStrcpy_s(&result->key, key) != 0)
40 {
41 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/
42 LogError("Failure allocating sas key.");
43 HTTPAPIEX_SAS_Destroy(result);
44 result = NULL;
45 }
46 else if (mallocAndStrcpy_s(&result->uriResource, uriResource) != 0)
47 {
48 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/
49 LogError("Failure allocating sas uriResource.");
50 HTTPAPIEX_SAS_Destroy(result);
51 result = NULL;
52 }
53 else if (keyName != NULL && mallocAndStrcpy_s(&result->keyName, keyName) != 0)
54 {
55 /*Codes_SRS_HTTPAPIEXSAS_06_004: [If there are any other errors in the instantiation of this handle then HTTPAPIEX_SAS_Create shall return NULL.]*/
56 LogError("Failure allocating sas keyName.");
57 HTTPAPIEX_SAS_Destroy(result);
58 result = NULL;
59 }
60 }
61 return result;
62}
63
64HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create_From_String(const char* key, const char* uriResource, const char* keyName)
65{
66 HTTPAPIEX_SAS_HANDLE result = NULL;
67 if (key == NULL || uriResource == NULL)
68 {
69 /* Codes_SRS_HTTPAPIEXSAS_07_001: [ If the parameter key or uriResource is NULL then HTTPAPIEX_SAS_Create_From_String shall return NULL. ] */
70 LogError("Invalid parameter key: %p, uriResource: %p", key, uriResource);
71 result = NULL;
72 }
73 else
74 {
75 /* 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. ] */
76 result = construct_httpex_sas(key, uriResource, keyName);
77 }
78 /* 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 ] */
79 return result;
80}
81
82HTTPAPIEX_SAS_HANDLE HTTPAPIEX_SAS_Create(STRING_HANDLE key, STRING_HANDLE uriResource, STRING_HANDLE keyName)
83{
84 HTTPAPIEX_SAS_HANDLE result = NULL;
85 if (key == NULL)
86 {
87 /*Codes_SRS_HTTPAPIEXSAS_06_001: [If the parameter key is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
88 LogError("No key passed to HTTPAPIEX_SAS_Create.");
89 }
90 else if (uriResource == NULL)
91 {
92 /*Codes_SRS_HTTPAPIEXSAS_06_002: [If the parameter uriResource is NULL then HTTPAPIEX_SAS_Create shall return NULL.]*/
93 LogError("No uri resource passed to HTTPAPIEX_SAS_Create.");
94 }
95 else
96 {
97 /*Codes_SRS_HTTPAPIEXSAS_06_003: [The parameter keyName for HTTPAPIEX_SAS_Create is optional and can be NULL.]*/
98 /*Codes_SRS_HTTPAPIEXSAS_01_001: [ HTTPAPIEX_SAS_Create shall create a new instance of HTTPAPIEX_SAS and return a non-NULL handle to it. ]*/
99 const char* keyStr = STRING_c_str(key);
100 const char* uriStr = STRING_c_str(uriResource);
101 const char* keyNameStr = STRING_c_str(keyName);
102 result = construct_httpex_sas(keyStr, uriStr, keyNameStr);
103 }
104 return result;
105}
106
107void HTTPAPIEX_SAS_Destroy(HTTPAPIEX_SAS_HANDLE handle)
108{
109 /*Codes_SRS_HTTPAPIEXSAS_06_005: [If the parameter handle is NULL then HTTAPIEX_SAS_Destroy shall do nothing and return.]*/
110 if (handle)
111 {
112 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)handle;
113 /*Codes_SRS_HTTPAPIEXSAS_06_006: [HTTAPIEX_SAS_Destroy shall deallocate any structures denoted by the parameter handle.]*/
114 if (state->key)
115 {
116 free(state->key);
117 }
118 if (state->uriResource)
119 {
120 free(state->uriResource);
121 }
122 if (state->keyName)
123 {
124 free(state->keyName);
125 }
126 free(state);
127 }
128}
129
130HTTPAPIEX_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)
131{
132 /*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.]*/
133 if (sasHandle != NULL)
134 {
135 /*Codes_SRS_HTTPAPIEXSAS_06_008: [if the parameter requestHttpHeadersHandle is NULL then fallthrough.]*/
136 if (requestHttpHeadersHandle != NULL)
137 {
138 /*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.]*/
139 /*Codes_SRS_HTTPAPIEXSAS_06_010: [If the return result of the invocation of HTTPHeaders_FindHeaderValue is NULL then fallthrough.]*/
140 if (HTTPHeaders_FindHeaderValue(requestHttpHeadersHandle, "Authorization") != NULL)
141 {
142 HTTPAPIEX_SAS_STATE* state = (HTTPAPIEX_SAS_STATE*)sasHandle;
143 /*Codes_SRS_HTTPAPIEXSAS_06_018: [A value of type time_t that shall be known as currentTime is obtained from calling get_time.]*/
144 time_t currentTime = get_time(NULL);
145 /*Codes_SRS_HTTPAPIEXSAS_06_019: [If the value of currentTime is (time_t)-1 is then fallthrough.]*/
146 if (currentTime == (time_t)-1)
147 {
148 LogError("Time does not appear to be working.");
149 }
150 else
151 {
152 STRING_HANDLE newSASToken = NULL;
153 if (strncmp(state->key, SHARED_ACCESS_SIGNATURE_PREFIX, 4) == 0)
154 {
155 /*Codes_SRS_HTTPAPIEXSAS_06_017: [If state->key is prefixed with "sas=", SharedAccessSignature will be used rather than created. STRING_construct will be invoked.]*/
156 newSASToken = STRING_construct(&state->key[4]);
157 }
158 else
159 {
160 /*Codes_SRS_HTTPAPIEXSAS_06_011: [SASToken_Create shall be invoked.]*/
161 /*Codes_SRS_HTTPAPIEXSAS_06_012: [If the return result of SASToken_Create is NULL then fallthrough.]*/
162 size_t expiry = (size_t)(difftime(currentTime, 0) + 3600);
163 newSASToken = SASToken_CreateString(state->key, state->uriResource, state->keyName, expiry);
164 }
165
166 if (newSASToken != NULL)
167 {
168 /*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.]*/
169 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeadersHandle, "Authorization", STRING_c_str(newSASToken)) != HTTP_HEADERS_OK)
170 {
171 /*Codes_SRS_HTTPAPIEXSAS_06_014: [If the result of the invocation of HTTPHeaders_ReplaceHeaderNameValuePair is NOT HTTP_HEADERS_OK then fallthrough.]*/
172 LogError("Unable to replace the old SAS Token.");
173 }
174 /*Codes_SRS_HTTPAPIEXSAS_06_015: [STRING_delete(newSASToken) will be invoked.]*/
175 STRING_delete(newSASToken);
176 }
177 else
178 {
179 LogError("Unable to create a new SAS token.");
180 }
181 }
182 }
183 }
184 }
185 /*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.]*/
186 return HTTPAPIEX_ExecuteRequest(handle,requestType,relativePath,requestHttpHeadersHandle,requestContent,statusCode,responseHeadersHandle,responseContent);
187}
Note: See TracBrowser for help on using the repository browser.