Skip to content

Commit 3a52784

Browse files
committed
Use enum instead and address comments
1 parent 2b9bf00 commit 3a52784

File tree

4 files changed

+112
-69
lines changed

4 files changed

+112
-69
lines changed

src/source/CurlApiCallbacks.c

+53-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ STATUS createCurlApiCallbacks(PCallbacksProvider pCallbacksProvider, PCHAR regio
1414
ENTERS();
1515
STATUS retStatus = STATUS_SUCCESS, status;
1616
PCurlApiCallbacks pCurlApiCallbacks = NULL;
17-
BOOL useDualStackEndpoint;
17+
KvsControlPlaneEndpointType controlPlaneEndpointType;
1818

1919
CHK(pCallbacksProvider != NULL && ppCurlApiCallbacks != NULL, STATUS_NULL_ARG);
2020
CHK(certPath == NULL || STRNLEN(certPath, MAX_PATH_LEN + 1) <= MAX_PATH_LEN, STATUS_INVALID_CERT_PATH_LENGTH);
@@ -80,19 +80,8 @@ STATUS createCurlApiCallbacks(PCallbacksProvider pCallbacksProvider, PCHAR regio
8080

8181
// Construct Control Plane URL
8282
if (IS_NULL_OR_EMPTY_STRING(controlPlaneUrl)) {
83-
#if defined(AWS_KVS_USE_LEGACY_ENDPOINT_ONLY)
84-
useDualStackEndpoint = FALSE;
85-
DLOGI("Using legacy endpoint from AWS_KVS_USE_LEGACY_ENDPOINT_ONLY")
86-
#elif defined(AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY)
87-
useDualStackEndpoint = TRUE;
88-
DLOGI("Using dual stack endpoint from AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY")
89-
#else
90-
useDualStackEndpoint = (GETENV(CONTROL_PLANE_USE_DUAL_STACK_ENDPOINT_ENV_VAR) != NULL) &&
91-
(0 == STRNCMPI(GETENV(CONTROL_PLANE_USE_DUAL_STACK_ENDPOINT_ENV_VAR), "true", 4));
92-
DLOGI("Using %s endpoint from environment", useDualStackEndpoint ? "dual stack" : "legacy");
93-
#endif
94-
95-
constructControlPlaneUrl(pCurlApiCallbacks->controlPlaneUrl, MAX_URI_CHAR_LEN, pCurlApiCallbacks->region, !useDualStackEndpoint);
83+
CHK_STATUS(determineKvsControlPlaneEndpointType(&controlPlaneEndpointType));
84+
CHK_STATUS(constructControlPlaneUrl(pCurlApiCallbacks->controlPlaneUrl, MAX_URI_CHAR_LEN, pCurlApiCallbacks->region, controlPlaneEndpointType));
9685
} else {
9786
STRNCPY(pCurlApiCallbacks->controlPlaneUrl, controlPlaneUrl, MAX_URI_CHAR_LEN);
9887
}
@@ -190,7 +179,51 @@ STATUS createCurlApiCallbacks(PCallbacksProvider pCallbacksProvider, PCHAR regio
190179
return retStatus;
191180
}
192181

193-
STATUS constructControlPlaneUrl(const char* buffer, SIZE_T bufferSize, const char* region, BOOL useLegacyEndpoint)
182+
STATUS determineKvsControlPlaneEndpointType(PKvsControlPlaneEndpointType pEndpointType)
183+
{
184+
ENTERS();
185+
STATUS retStatus = STATUS_SUCCESS;
186+
BOOL isDualStack = FALSE;
187+
const char* envValue;
188+
189+
CHK(pEndpointType != NULL, STATUS_NULL_ARG);
190+
191+
#if defined(AWS_KVS_USE_LEGACY_ENDPOINT_ONLY)
192+
*pEndpointType = ENDPOINT_TYPE_LEGACY;
193+
DLOGI("Using legacy endpoint from AWS_KVS_USE_LEGACY_ENDPOINT_ONLY");
194+
#elif defined(AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY)
195+
*pEndpointType = ENDPOINT_TYPE_DUAL_STACK;
196+
DLOGI("Using dual stack endpoint from AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY");
197+
#else
198+
envValue = GETENV(CONTROL_PLANE_USE_DUAL_STACK_ENDPOINT_ENV_VAR);
199+
200+
// Check for "true" to make sure "false" actually disables it
201+
// https://docs.aws.amazon.com/sdkref/latest/guide/feature-endpoints.html
202+
isDualStack = (envValue != NULL) && (0 == STRNCMPI(envValue, "true", 4));
203+
*pEndpointType = isDualStack ? ENDPOINT_TYPE_DUAL_STACK : ENDPOINT_TYPE_LEGACY;
204+
205+
DLOGI("Using %s endpoint from environment", endpointTypeToString(*pEndpointType));
206+
#endif
207+
208+
CleanUp:
209+
CHK_LOG_ERR(retStatus);
210+
211+
LEAVES();
212+
return retStatus;
213+
}
214+
215+
const char* endpointTypeToString(const KvsControlPlaneEndpointType type) {
216+
switch (type) {
217+
case ENDPOINT_TYPE_LEGACY:
218+
return "LEGACY";
219+
case ENDPOINT_TYPE_DUAL_STACK:
220+
return "DUAL_STACK";
221+
default:
222+
return "UNKNOWN";
223+
}
224+
}
225+
226+
STATUS constructControlPlaneUrl(const char* buffer, SIZE_T bufferSize, const char* region, KvsControlPlaneEndpointType endpointType)
194227
{
195228
ENTERS();
196229
STATUS retStatus = STATUS_SUCCESS;
@@ -203,21 +236,21 @@ STATUS constructControlPlaneUrl(const char* buffer, SIZE_T bufferSize, const cha
203236
if (0 == STRNCMP(region, AWS_ISO_B_REGION_PREFIX, STRLEN(AWS_ISO_B_REGION_PREFIX))) {
204237
// Top Secret Cloud regions: https://kinesisvideo-fips.us-isob-east-1.sc2s.sgov.gov
205238
serviceNamePostfix = AWS_KVS_FIPS_ENDPOINT_POSTFIX;
206-
postfix = useLegacyEndpoint ? CONTROL_PLANE_URI_POSTFIX_ISO_B : CONTROL_PLANE_URI_POSTFIX_ISO_B_DUAL_STACK;
239+
postfix = (endpointType == ENDPOINT_TYPE_LEGACY) ? CONTROL_PLANE_URI_POSTFIX_ISO_B : CONTROL_PLANE_URI_POSTFIX_ISO_B_DUAL_STACK;
207240
} else if (0 == STRNCMP(region, AWS_ISO_REGION_PREFIX, STRLEN(AWS_ISO_REGION_PREFIX))) {
208241
// Secret Cloud regions: https://kinesisvideo-fips.us-iso-east-1.c2s.ic.gov
209242
serviceNamePostfix = AWS_KVS_FIPS_ENDPOINT_POSTFIX;
210-
postfix = useLegacyEndpoint ? CONTROL_PLANE_URI_POSTFIX_ISO : CONTROL_PLANE_URI_POSTFIX_ISO_DUAL_STACK;
243+
postfix = (endpointType == ENDPOINT_TYPE_LEGACY) ? CONTROL_PLANE_URI_POSTFIX_ISO : CONTROL_PLANE_URI_POSTFIX_ISO_DUAL_STACK;
211244
} else if (0 == STRNCMP(region, AWS_GOV_CLOUD_REGION_PREFIX, STRLEN(AWS_GOV_CLOUD_REGION_PREFIX))) {
212245
// US Govcloud regions: https://kinesisvideo-fips.us-gov-east-1.amazonaws.com
213246
serviceNamePostfix = AWS_KVS_FIPS_ENDPOINT_POSTFIX;
214-
postfix = useLegacyEndpoint ? CONTROL_PLANE_URI_POSTFIX : CONTROL_PLANE_URI_POSTFIX_DUAL_STACK;
247+
postfix = (endpointType == ENDPOINT_TYPE_LEGACY) ? CONTROL_PLANE_URI_POSTFIX : CONTROL_PLANE_URI_POSTFIX_DUAL_STACK;
215248
} else if (0 == STRNCMP(region, AWS_CN_REGION_PREFIX, STRLEN(AWS_CN_REGION_PREFIX))) {
216249
// China regions: https://kinesisvideo.cn-north-1.amazonaws.com.cn"
217-
postfix = useLegacyEndpoint ? CONTROL_PLANE_URI_POSTFIX_CN : CONTROL_PLANE_URI_POSTFIX_CN_DUAL_STACK;
250+
postfix = (endpointType == ENDPOINT_TYPE_LEGACY) ? CONTROL_PLANE_URI_POSTFIX_CN : CONTROL_PLANE_URI_POSTFIX_CN_DUAL_STACK;
218251
} else {
219252
// Standard regions: https://kinesisvideo.us-west-2.amazonaws.com
220-
postfix = useLegacyEndpoint ? CONTROL_PLANE_URI_POSTFIX : CONTROL_PLANE_URI_POSTFIX_DUAL_STACK;
253+
postfix = (endpointType == ENDPOINT_TYPE_LEGACY) ? CONTROL_PLANE_URI_POSTFIX : CONTROL_PLANE_URI_POSTFIX_DUAL_STACK;
221254
}
222255

223256
// Create a fully qualified URI

src/source/CurlApiCallbacks.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ extern "C" {
2222

2323
#define CONTROL_PLANE_USE_DUAL_STACK_ENDPOINT_ENV_VAR "AWS_USE_DUALSTACK_ENDPOINT"
2424

25+
typedef enum {
26+
ENDPOINT_TYPE_LEGACY,
27+
ENDPOINT_TYPE_DUAL_STACK
28+
} KvsControlPlaneEndpointType;
29+
typedef KvsControlPlaneEndpointType* PKvsControlPlaneEndpointType;
30+
2531
#if defined(AWS_KVS_USE_LEGACY_ENDPOINT_ONLY) && defined(AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY)
2632
#error "Only one of AWS_KVS_USE_LEGACY_ENDPOINT_ONLY or AWS_KVS_USE_DUAL_STACK_ENDPOINT_ONLY can be defined"
2733
#endif
@@ -183,14 +189,16 @@ typedef struct __CurlApiCallbacks* PCurlApiCallbacks;
183189
// Curl API Callbacks main functionality
184190
//////////////////////////////////////////////////////////////////////
185191
STATUS createCurlApiCallbacks(struct __CallbacksProvider*, PCHAR, API_CALL_CACHE_TYPE, UINT64, PCHAR, PCHAR, PCHAR, PCHAR, PCurlApiCallbacks*);
186-
STATUS constructControlPlaneUrl(const char*, SIZE_T, const char*, BOOL);
192+
STATUS constructControlPlaneUrl(const char*, SIZE_T, const char*, KvsControlPlaneEndpointType);
193+
STATUS determineKvsControlPlaneEndpointType(PKvsControlPlaneEndpointType);
187194
STATUS freeCurlApiCallbacks(PCurlApiCallbacks*);
188195
STATUS curlApiCallbacksShutdownActiveRequests(PCurlApiCallbacks, STREAM_HANDLE, UINT64, BOOL, BOOL);
189196
STATUS curlApiCallbacksShutdownCachedEndpoints(PCurlApiCallbacks, STREAM_HANDLE, BOOL);
190197
STATUS curlApiCallbacksShutdownActiveUploads(PCurlApiCallbacks, STREAM_HANDLE, UPLOAD_HANDLE, UINT64, BOOL, BOOL);
191198
STATUS curlApiCallbacksShutdown(PCurlApiCallbacks, UINT64);
192199
STATUS freeApiCallbacksCurl(PUINT64);
193200
STATUS findRequestWithUploadHandle(UPLOAD_HANDLE, PCurlApiCallbacks, PCurlRequest*);
201+
const char* endpointTypeToString(KvsControlPlaneEndpointType type);
194202

195203
//////////////////////////////////////////////////////////////////////
196204
// Auxiliary functionality

src/source/Response.c

+2
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ STATUS initializeCurlSession(PRequestInfo pRequestInfo, PCallInfo pCallInfo, CUR
204204

205205
#ifndef NDEBUG
206206
// Only available in debug build
207+
// This setting will have curl print out the IP address that it resolved and connected to
208+
// More info: https://everything.curl.dev/usingcurl/verbose/index.html
207209
if (!IS_NULL_OR_EMPTY_STRING(GETENV(KVS_CURL_DEBUG_ENV_VAR))) {
208210
curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L);
209211
}

0 commit comments

Comments
 (0)