Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[C][Client]Fix the defect of data lost when libcurl write-data callba…
…ck function (configured by CURLOPT_WRITEFUNCTION) is called multiple times.
  • Loading branch information
ityuhui committed Apr 5, 2020
commit f21bdaefdeb9b7c431365b6dfd511723eb355bdd
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ end:
{{/returnTypeIsPrimitive}}
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason to keep this outside of the if check? or can it be moved inside also? as it will be already 0 if the if statement is false. So the line becomes obsolete.

Copy link
Contributor Author

@ityuhui ityuhui Apr 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhemant
Yes. it can be moved inside. I have updated it.
Could you please review the new code change ?

{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
Expand Down Expand Up @@ -463,7 +465,9 @@ end:
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ apiClient_t *apiClient_create() {
apiClient->basePath = strdup("{{{basePath}}}");
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -59,6 +60,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -469,7 +471,7 @@ void apiClient_invoke(apiClient_t *apiClient,
writeDataCallback);
curl_easy_setopt(handle,
CURLOPT_WRITEDATA,
&apiClient->dataReceived);
apiClient);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable

Expand Down Expand Up @@ -559,9 +561,12 @@ void apiClient_invoke(apiClient_t *apiClient,
}

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
*(char **) userp = strdup(buffer);

return size * nmemb;
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
return size_this_time;
}

char *strReplace(char *orig, char *rep, char *with) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct apiClient_t {
char *basePath;
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
long response_code;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/c/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.0-SNAPSHOT
4.3.1-SNAPSHOT
16 changes: 16 additions & 0 deletions samples/client/petstore/c/api/PetAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -176,7 +178,9 @@ PetAPI_deletePet(apiClient_t *apiClient, long petId , char * api_key )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;

list_free(localVarHeaderParameters);

Expand Down Expand Up @@ -256,7 +260,9 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t * status )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;
list_free(localVarQueryParameters);


Expand Down Expand Up @@ -335,7 +341,9 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t * tags )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;
list_free(localVarQueryParameters);


Expand Down Expand Up @@ -415,7 +423,9 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -482,7 +492,9 @@ PetAPI_updatePet(apiClient_t *apiClient, pet_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -569,7 +581,9 @@ PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId , char * name , char
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;


list_free(localVarFormParameters);
Expand Down Expand Up @@ -669,7 +683,9 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId , char * additionalMetadata
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;


list_free(localVarFormParameters);
Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/c/api/StoreAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ StoreAPI_deleteOrder(apiClient_t *apiClient, char * orderId )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -121,7 +123,9 @@ StoreAPI_getInventory(apiClient_t *apiClient)

if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -201,7 +205,9 @@ StoreAPI_getOrderById(apiClient_t *apiClient, long orderId )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -272,7 +278,9 @@ StoreAPI_placeOrder(apiClient_t *apiClient, order_t * body )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down
16 changes: 16 additions & 0 deletions samples/client/petstore/c/api/UserAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ UserAPI_createUser(apiClient_t *apiClient, user_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -136,7 +138,9 @@ UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, list_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -215,7 +219,9 @@ UserAPI_createUsersWithListInput(apiClient_t *apiClient, list_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -280,7 +286,9 @@ UserAPI_deleteUser(apiClient_t *apiClient, char * username )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -352,7 +360,9 @@ UserAPI_getUserByName(apiClient_t *apiClient, char * username )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -432,7 +442,9 @@ UserAPI_loginUser(apiClient_t *apiClient, char * username , char * password )

if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;
list_free(localVarQueryParameters);


Expand Down Expand Up @@ -505,7 +517,9 @@ UserAPI_logoutUser(apiClient_t *apiClient)
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down Expand Up @@ -575,7 +589,9 @@ UserAPI_updateUser(apiClient_t *apiClient, char * username , user_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
}
apiClient->dataReceivedLen = 0;



Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/c/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct apiClient_t {
char *basePath;
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
long response_code;
list_t *apiKeys;
char *accessToken;
Expand Down
18 changes: 12 additions & 6 deletions samples/client/petstore/c/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ apiClient_t *apiClient_create() {
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
apiClient->apiKeys = NULL;
apiClient->accessToken = NULL;
Expand All @@ -41,6 +42,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
if(apiKeys!= NULL) {
apiClient->apiKeys = list_create();
Expand Down Expand Up @@ -417,7 +419,7 @@ void apiClient_invoke(apiClient_t *apiClient,
writeDataCallback);
curl_easy_setopt(handle,
CURLOPT_WRITEDATA,
&apiClient->dataReceived);
apiClient);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable

Expand Down Expand Up @@ -465,9 +467,12 @@ void apiClient_invoke(apiClient_t *apiClient,
}

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
*(char **) userp = strdup(buffer);

return size * nmemb;
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
return size_this_time;
}

char *strReplace(char *orig, char *rep, char *with) {
Expand Down Expand Up @@ -522,7 +527,7 @@ char *strReplace(char *orig, char *rep, char *with) {
return result;
}

char *sbi_base64encode (const void *b64_encode_this, int encode_this_many_bytes){
char *base64encode (const void *b64_encode_this, int encode_this_many_bytes){
#ifdef OPENSSL
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
BUF_MEM *mem_bio_mem_ptr; //Pointer to a "memory BIO" structure holding our base64 data.
Expand All @@ -541,7 +546,7 @@ char *sbi_base64encode (const void *b64_encode_this, int encode_this_many_bytes)
#endif
}

char *sbi_base64decode (const void *b64_decode_this, int decode_this_many_bytes){
char *base64decode (const void *b64_decode_this, int decode_this_many_bytes, int *decoded_bytes){
#ifdef OPENSSL
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
char *base64_decoded = calloc( (decode_this_many_bytes*3)/4+1, sizeof(char) ); //+1 = null.
Expand All @@ -555,6 +560,7 @@ char *sbi_base64decode (const void *b64_decode_this, int decode_this_many_bytes)
decoded_byte_index++; //Increment the index until read of BIO decoded data is complete.
} //Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
BIO_free_all(b64_bio); //Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
*decoded_bytes = decoded_byte_index;
return base64_decoded; //Returns base-64 decoded data with trailing null terminator.
#endif
}