mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-11 07:17:44 +00:00
esx: Fix and improve the libcurl debug callback
The data passed to the callback is not guaranteed to be zero terminated, take care of that by coping the data and adding a zero terminator. Also dump the data for other types than CURLINFO_TEXT. Set CURLOPT_VERBOSE to 1 so the debug callback is called when enabled.
This commit is contained in:
parent
b0367e86ee
commit
d9734072ff
@ -181,27 +181,50 @@ static int
|
|||||||
esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
|
esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
|
||||||
char *info, size_t size, void *data ATTRIBUTE_UNUSED)
|
char *info, size_t size, void *data ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
|
char *buffer = NULL;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The libcurl documentation says:
|
||||||
|
*
|
||||||
|
* The data pointed to by the char * passed to this function WILL NOT
|
||||||
|
* be zero terminated, but will be exactly of the size as told by the
|
||||||
|
* size_t argument.
|
||||||
|
*
|
||||||
|
* To handle this properly in order to pass the info string to VIR_DEBUG
|
||||||
|
* a zero terminated copy of the info string has to be allocated.
|
||||||
|
*/
|
||||||
|
if (VIR_ALLOC_N(buffer, size + 1) < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (virStrncpy(buffer, info, size, size + 1) == NULL) {
|
||||||
|
VIR_FREE(buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case CURLINFO_TEXT:
|
case CURLINFO_TEXT:
|
||||||
VIR_DEBUG0("CURLINFO_TEXT");
|
if (size > 0 && buffer[size - 1] == '\n') {
|
||||||
fwrite(info, 1, size, stderr);
|
buffer[size - 1] = '\0';
|
||||||
printf("\n\n");
|
}
|
||||||
|
|
||||||
|
VIR_DEBUG("CURLINFO_TEXT [[[[%s]]]]", buffer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CURLINFO_HEADER_IN:
|
case CURLINFO_HEADER_IN:
|
||||||
VIR_DEBUG0("CURLINFO_HEADER_IN");
|
VIR_DEBUG("CURLINFO_HEADER_IN [[[[%s]]]]", buffer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CURLINFO_HEADER_OUT:
|
case CURLINFO_HEADER_OUT:
|
||||||
VIR_DEBUG0("CURLINFO_HEADER_OUT");
|
VIR_DEBUG("CURLINFO_HEADER_OUT [[[[%s]]]]", buffer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CURLINFO_DATA_IN:
|
case CURLINFO_DATA_IN:
|
||||||
VIR_DEBUG0("CURLINFO_DATA_IN");
|
VIR_DEBUG("CURLINFO_DATA_IN [[[[%s]]]]", buffer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CURLINFO_DATA_OUT:
|
case CURLINFO_DATA_OUT:
|
||||||
VIR_DEBUG0("CURLINFO_DATA_OUT");
|
VIR_DEBUG("CURLINFO_DATA_OUT [[[[%s]]]]", buffer);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -209,6 +232,8 @@ esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VIR_FREE(buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -338,8 +363,8 @@ esxVI_Context_Connect(virConnectPtr conn, esxVI_Context *ctx, const char *url,
|
|||||||
curl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEFUNCTION,
|
curl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||||
esxVI_CURL_WriteBuffer);
|
esxVI_CURL_WriteBuffer);
|
||||||
#if ESX_VI__CURL__ENABLE_DEBUG_OUTPUT
|
#if ESX_VI__CURL__ENABLE_DEBUG_OUTPUT
|
||||||
curl_easy_setopt(ctx->curl_handle, CURLOPT_DEBUGFUNCTION,
|
curl_easy_setopt(ctx->curl_handle, CURLOPT_DEBUGFUNCTION, esxVI_CURL_Debug);
|
||||||
esxVI_CURL_Debug);
|
curl_easy_setopt(ctx->curl_handle, CURLOPT_VERBOSE, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (virMutexInit(&ctx->curl_lock) < 0) {
|
if (virMutexInit(&ctx->curl_lock) < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user