Libcurl. Cancel request while continuing
What you need to understand is that CURL is a C library. In general, you cannot pass pointers to C ++ objects or functions, because C knows nothing about C ++ classes and the purpose of the call.
For example, this line is incorrect:
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
CURL setopt CURLOPT_ERRORBUFFER
expects the third parameter to be a pointer to a C-style string (C-style array char
), with a space for characters CURL_ERROR_SIZE
. Instead, you are passing a pointer to an object std::string
. Since CURL, written in C, doesn't know what it is std::string
, it just overwrites the byte representation that has sizeof (std::string)
error data bytes because it thinks the pointer is a char
C-style array .
Use this instead:
char errorBuffer[CURL_ERROR_SIZE + 1]; errorBuffer[CURL_ERROR_SIZE] = '\0';
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
errorBuffer
populated with only a valid string if it curl_easy_perform()
returns an error code.
Also Uploader::WriteResponce
is a C ++ feature. With this line:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteResponce);
CURL expects the third parameter to be a C pointer function. Here you are passing a pointer-to-C ++ function. You need to pass the call WriteResponce
to a function extern "C"
that calls the C ++ function:
extern "C" size_t call_uploader_writeresponce(char *ptr, size_t size, size_t nmemb, void *user_data) {
return Uploader::WriteResponce(ptr, size, nmemb, static_cast<std::string *>(user_data));
}
// ...
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &call_uploader_writeresponce);
The "write function" should return size_t
, not int
; Uploader::WriteResponce
must be returned size_t
.
source to share