Skip to content

Commit f4185b8

Browse files
authored
[C-libcurl] Support HTTPS/SSL for the C client (#5140)
1 parent e27700c commit f4185b8

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apiClient_t *apiClient_create() {
1313
curl_global_init(CURL_GLOBAL_ALL);
1414
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1515
apiClient->basePath = strdup("{{{basePath}}}");
16+
apiClient->caPath = NULL;
1617
apiClient->dataReceived = NULL;
1718
apiClient->response_code = 0;
1819
{{#hasAuthMethods}}
@@ -34,6 +35,7 @@ apiClient_t *apiClient_create() {
3435
}
3536

3637
apiClient_t *apiClient_create_with_base_path(const char *basePath
38+
, const char *caPath
3739
{{#hasAuthMethods}}
3840
{{#authMethods}}
3941
{{#isApiKey}}
@@ -49,6 +51,13 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
4951
}else{
5052
apiClient->basePath = strdup("{{{basePath}}}");
5153
}
54+
55+
if(caPath){
56+
apiClient->caPath = strdup(caPath);
57+
}else{
58+
apiClient->caPath = NULL;
59+
}
60+
5261
apiClient->dataReceived = NULL;
5362
apiClient->response_code = 0;
5463
{{#hasAuthMethods}}
@@ -83,6 +92,9 @@ void apiClient_free(apiClient_t *apiClient) {
8392
if(apiClient->basePath) {
8493
free(apiClient->basePath);
8594
}
95+
if(apiClient->caPath) {
96+
free(apiClient->caPath);
97+
}
8698
{{#hasAuthMethods}}
8799
{{#authMethods}}
88100
{{#isBasic}}
@@ -375,6 +387,17 @@ void apiClient_invoke(apiClient_t *apiClient,
375387
free(headerValueToWrite);
376388
}
377389
}
390+
391+
if( strstr(apiClient->basePath, "https") != NULL ){
392+
if (apiClient->caPath) {
393+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
394+
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath);
395+
} else {
396+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
397+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
398+
}
399+
}
400+
378401
{{#hasAuthMethods}}
379402
{{#authMethods}}
380403
{{#isApiKey}}

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
typedef struct apiClient_t {
1313
char *basePath;
14+
char *caPath;
1415
void *dataReceived;
1516
long response_code;
1617
{{#hasAuthMethods}}
@@ -38,6 +39,7 @@ typedef struct binary_t
3839
apiClient_t* apiClient_create();
3940

4041
apiClient_t* apiClient_create_with_base_path(const char *basePath
42+
, const char *caPath
4143
{{#hasAuthMethods}}
4244
{{#authMethods}}
4345
{{#isApiKey}}

0 commit comments

Comments
 (0)