[C-libcurl] Support HTTPS/SSL for the C client (#5140)

This commit is contained in:
Hui Yu 2020-01-29 16:57:31 +08:00 committed by GitHub
parent e27700cfee
commit f4185b8d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
apiClient->basePath = strdup("{{{basePath}}}");
apiClient->caPath = NULL;
apiClient->dataReceived = NULL;
apiClient->response_code = 0;
{{#hasAuthMethods}}
@ -34,6 +35,7 @@ apiClient_t *apiClient_create() {
}
apiClient_t *apiClient_create_with_base_path(const char *basePath
, const char *caPath
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}
@ -49,6 +51,13 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}else{
apiClient->basePath = strdup("{{{basePath}}}");
}
if(caPath){
apiClient->caPath = strdup(caPath);
}else{
apiClient->caPath = NULL;
}
apiClient->dataReceived = NULL;
apiClient->response_code = 0;
{{#hasAuthMethods}}
@ -83,6 +92,9 @@ void apiClient_free(apiClient_t *apiClient) {
if(apiClient->basePath) {
free(apiClient->basePath);
}
if(apiClient->caPath) {
free(apiClient->caPath);
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
@ -375,6 +387,17 @@ void apiClient_invoke(apiClient_t *apiClient,
free(headerValueToWrite);
}
}
if( strstr(apiClient->basePath, "https") != NULL ){
if (apiClient->caPath) {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath);
} else {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
}
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}

View File

@ -11,6 +11,7 @@
typedef struct apiClient_t {
char *basePath;
char *caPath;
void *dataReceived;
long response_code;
{{#hasAuthMethods}}
@ -38,6 +39,7 @@ typedef struct binary_t
apiClient_t* apiClient_create();
apiClient_t* apiClient_create_with_base_path(const char *basePath
, const char *caPath
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}