Skip to content

Commit 7d3913f

Browse files
authored
Add verbose, keepalive, keepidle, keepintvl to c-libcurl (#21613)
* Add keepalive to c-libcurl * Fix manual tests on client/petstore/c * Update Readme of c/libcurl * better curlConfig handling on c-libcurl
1 parent f5da0ea commit 7d3913f

File tree

18 files changed

+200
-49
lines changed

18 files changed

+200
-49
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
1515
{{/infoUrl}}
1616

1717
## Installation
18-
You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
18+
You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
1919

2020
# Prerequisites
2121

22-
## Install the `curl 7.58.0` package with the following command on Linux.
22+
## Install the `curl 7.61.1` package with the following command on Linux.
2323
```bash
2424
sudo apt remove curl
25-
wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
26-
tar -xvf curl-7.58.0.tar.gz
27-
cd curl-7.58.0/
25+
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz
26+
tar -xvf curl-7.61.1.tar.gz
27+
cd curl-7.61.1/
2828
./configure
2929
make
3030
sudo make install

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("{{{basePath}}}");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -60,6 +61,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
6061
apiClient->sslConfig = NULL;
6162
}
6263

64+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
65+
apiClient->curlConfig->verbose = 0;
66+
apiClient->curlConfig->keepalive = 0;
67+
apiClient->curlConfig->keepidle = 120;
68+
apiClient->curlConfig->keepintvl = 60;
69+
6370
apiClient->dataReceived = NULL;
6471
apiClient->dataReceivedLen = 0;
6572
apiClient->data_callback_func = NULL;
@@ -142,6 +149,12 @@ void apiClient_free(apiClient_t *apiClient) {
142149
{{/isApiKey}}
143150
{{/authMethods}}
144151
{{/hasAuthMethods}}
152+
153+
if(apiClient->curlConfig) {
154+
free(apiClient->curlConfig);
155+
apiClient->curlConfig = NULL;
156+
}
157+
145158
free(apiClient);
146159
}
147160

@@ -498,7 +511,6 @@ void apiClient_invoke(apiClient_t *apiClient,
498511
CURLOPT_WRITEDATA,
499512
apiClient);
500513
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
501-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
502514

503515
{{#hasAuthMethods}}
504516
{{#authMethods}}
@@ -543,6 +555,15 @@ void apiClient_invoke(apiClient_t *apiClient,
543555
postData(handle, bodyParameters, bodyParametersLength);
544556
}
545557

558+
if(apiClient->curlConfig != NULL) {
559+
if(apiClient->curlConfig->keepalive == 1) {
560+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
561+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
562+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
563+
}
564+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
565+
}
566+
546567
res = curl_easy_perform(handle);
547568

548569
curl_slist_free_all(headers);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/others/c/bearerAuth/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat
99
- Build package: org.openapitools.codegen.languages.CLibcurlClientCodegen
1010

1111
## Installation
12-
You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
12+
You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
1313

1414
# Prerequisites
1515

16-
## Install the `curl 7.58.0` package with the following command on Linux.
16+
## Install the `curl 7.61.1` package with the following command on Linux.
1717
```bash
1818
sudo apt remove curl
19-
wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
20-
tar -xvf curl-7.58.0.tar.gz
21-
cd curl-7.58.0/
19+
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz
20+
tar -xvf curl-7.61.1.tar.gz
21+
cd curl-7.61.1/
2222
./configure
2323
make
2424
sudo make install

samples/client/others/c/bearerAuth/include/apiClient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/others/c/bearerAuth/src/apiClient.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("http://api.example.com/v1");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -37,6 +38,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3738
apiClient->sslConfig = NULL;
3839
}
3940

41+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
42+
apiClient->curlConfig->verbose = 0;
43+
apiClient->curlConfig->keepalive = 0;
44+
apiClient->curlConfig->keepidle = 120;
45+
apiClient->curlConfig->keepintvl = 60;
46+
4047
apiClient->dataReceived = NULL;
4148
apiClient->dataReceivedLen = 0;
4249
apiClient->data_callback_func = NULL;
@@ -58,6 +65,12 @@ void apiClient_free(apiClient_t *apiClient) {
5865
if(apiClient->accessToken) {
5966
free(apiClient->accessToken);
6067
}
68+
69+
if(apiClient->curlConfig) {
70+
free(apiClient->curlConfig);
71+
apiClient->curlConfig = NULL;
72+
}
73+
6174
free(apiClient);
6275
}
6376

@@ -391,13 +404,21 @@ void apiClient_invoke(apiClient_t *apiClient,
391404
CURLOPT_WRITEDATA,
392405
apiClient);
393406
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
394-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
395407

396408

397409
if(bodyParameters != NULL) {
398410
postData(handle, bodyParameters, bodyParametersLength);
399411
}
400412

413+
if(apiClient->curlConfig != NULL) {
414+
if(apiClient->curlConfig->keepalive == 1) {
415+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
416+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
417+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
418+
}
419+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
420+
}
421+
401422
res = curl_easy_perform(handle);
402423

403424
curl_slist_free_all(headers);

samples/client/petstore/c-useJsonUnformatted/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat
99
- Build package: org.openapitools.codegen.languages.CLibcurlClientCodegen
1010

1111
## Installation
12-
You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
12+
You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
1313

1414
# Prerequisites
1515

16-
## Install the `curl 7.58.0` package with the following command on Linux.
16+
## Install the `curl 7.61.1` package with the following command on Linux.
1717
```bash
1818
sudo apt remove curl
19-
wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
20-
tar -xvf curl-7.58.0.tar.gz
21-
cd curl-7.58.0/
19+
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz
20+
tar -xvf curl-7.61.1.tar.gz
21+
cd curl-7.61.1/
2222
./configure
2323
make
2424
sudo make install

samples/client/petstore/c-useJsonUnformatted/include/apiClient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/petstore/c-useJsonUnformatted/src/apiClient.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -39,6 +40,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3940
apiClient->sslConfig = NULL;
4041
}
4142

43+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
44+
apiClient->curlConfig->verbose = 0;
45+
apiClient->curlConfig->keepalive = 0;
46+
apiClient->curlConfig->keepidle = 120;
47+
apiClient->curlConfig->keepintvl = 60;
48+
4249
apiClient->dataReceived = NULL;
4350
apiClient->dataReceivedLen = 0;
4451
apiClient->data_callback_func = NULL;
@@ -85,6 +92,12 @@ void apiClient_free(apiClient_t *apiClient) {
8592
}
8693
list_freeList(apiClient->apiKeys_api_key);
8794
}
95+
96+
if(apiClient->curlConfig) {
97+
free(apiClient->curlConfig);
98+
apiClient->curlConfig = NULL;
99+
}
100+
88101
free(apiClient);
89102
}
90103

@@ -421,7 +434,6 @@ void apiClient_invoke(apiClient_t *apiClient,
421434
CURLOPT_WRITEDATA,
422435
apiClient);
423436
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
424-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
425437

426438
// this would only be generated for OAuth2 authentication
427439
if(apiClient->accessToken != NULL) {
@@ -435,6 +447,15 @@ void apiClient_invoke(apiClient_t *apiClient,
435447
postData(handle, bodyParameters, bodyParametersLength);
436448
}
437449

450+
if(apiClient->curlConfig != NULL) {
451+
if(apiClient->curlConfig->keepalive == 1) {
452+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
453+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
454+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
455+
}
456+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
457+
}
458+
438459
res = curl_easy_perform(handle);
439460

440461
curl_slist_free_all(headers);

samples/client/petstore/c/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat
99
- Build package: org.openapitools.codegen.languages.CLibcurlClientCodegen
1010

1111
## Installation
12-
You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
12+
You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later.
1313

1414
# Prerequisites
1515

16-
## Install the `curl 7.58.0` package with the following command on Linux.
16+
## Install the `curl 7.61.1` package with the following command on Linux.
1717
```bash
1818
sudo apt remove curl
19-
wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
20-
tar -xvf curl-7.58.0.tar.gz
21-
cd curl-7.58.0/
19+
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz
20+
tar -xvf curl-7.61.1.tar.gz
21+
cd curl-7.61.1/
2222
./configure
2323
make
2424
sudo make install

0 commit comments

Comments
 (0)