Re: [PATCH] esx: Move CURL handling code to it's own type

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2011/4/10 Matthias Bolte <matthias.bolte@xxxxxxxxxxxxxx>:
> ---
> Âsrc/esx/esx_driver.c | Â Â4 +-
> Âsrc/esx/esx_vi.c   | Â348 ++++++++++++++++++++++++++------------------------
> Âsrc/esx/esx_vi.h   |  29 +++-
> Â3 files changed, 205 insertions(+), 176 deletions(-)
>
> diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
> index deda372..ef76350 100644
> --- a/src/esx/esx_driver.c
> +++ b/src/esx/esx_driver.c
> @@ -2642,7 +2642,7 @@ esxDomainDumpXML(virDomainPtr domain, int flags)
>
> Â Â url = virBufferContentAndReset(&buffer);
>
> - Â Âif (esxVI_Context_DownloadFile(priv->primary, url, &vmx) < 0) {
> + Â Âif (esxVI_CURL_Download(priv->primary->curl, url, &vmx) < 0) {
> Â Â Â Â goto cleanup;
> Â Â }
>
> @@ -3111,7 +3111,7 @@ esxDomainDefineXML(virConnectPtr conn, const char *xml)
> Â Â /* Upload VMX file */
> Â Â VIR_DEBUG("Uploading .vmx config, url='%s' vmx='%s'", url, vmx);
>
> - Â Âif (esxVI_Context_UploadFile(priv->primary, url, vmx) < 0) {
> + Â Âif (esxVI_CURL_Upload(priv->primary->curl, url, vmx) < 0) {
> Â Â Â Â goto cleanup;
> Â Â }
>
> diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
> index fbab347..2fd09cd 100644
> --- a/src/esx/esx_vi.c
> +++ b/src/esx/esx_vi.c
> @@ -76,42 +76,25 @@
>
>
> Â/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> - * Context
> + * CURL
> Â*/
>
> -/* esxVI_Context_Alloc */
> -ESX_VI__TEMPLATE__ALLOC(Context);
> +/* esxVI_CURL_Alloc */
> +ESX_VI__TEMPLATE__ALLOC(CURL)
>
> -/* esxVI_Context_Free */
> -ESX_VI__TEMPLATE__FREE(Context,
> +/* esxVI_CURL_Free */
> +ESX_VI__TEMPLATE__FREE(CURL,
> Â{
> - Â ÂVIR_FREE(item->url);
> - Â ÂVIR_FREE(item->ipAddress);
> -
> - Â Âif (item->curl_handle != NULL) {
> - Â Â Â Âcurl_easy_cleanup(item->curl_handle);
> + Â Âif (item->handle != NULL) {
> + Â Â Â Âcurl_easy_cleanup(item->handle);
> Â Â }
>
> - Â Âif (item->curl_headers != NULL) {
> - Â Â Â Âcurl_slist_free_all(item->curl_headers);
> + Â Âif (item->headers != NULL) {
> + Â Â Â Âcurl_slist_free_all(item->headers);
> Â Â }
>
> - Â ÂvirMutexDestroy(&item->curl_lock);
> -
> - Â ÂVIR_FREE(item->username);
> - Â ÂVIR_FREE(item->password);
> - Â ÂesxVI_ServiceContent_Free(&item->service);
> - Â ÂesxVI_UserSession_Free(&item->session);
> - Â ÂesxVI_Datacenter_Free(&item->datacenter);
> - Â ÂesxVI_ComputeResource_Free(&item->computeResource);
> - Â ÂesxVI_HostSystem_Free(&item->hostSystem);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_folderToChildEntity);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToParent);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToVm);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToDatastore);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_computeResourceToHost);
> - Â ÂesxVI_SelectionSpec_Free(&item->selectSet_computeResourceToParentToParent);
> -});
> + Â ÂvirMutexDestroy(&item->lock);
> +})
>
> Âstatic size_t
> ÂesxVI_CURL_ReadString(char *data, size_t size, size_t nmemb, void *ptrptr)
> @@ -218,7 +201,7 @@ esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
> Â#endif
>
> Âstatic int
> -esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
> +esxVI_CURL_Perform(esxVI_CURL *curl, const char *url)
> Â{
> Â Â CURLcode errorCode;
> Â Â long responseCode = 0;
> @@ -226,23 +209,23 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
> Â Â const char *redirectUrl = NULL;
> Â#endif
>
> - Â ÂerrorCode = curl_easy_perform(ctx->curl_handle);
> + Â ÂerrorCode = curl_easy_perform(curl->handle);
>
> Â Â if (errorCode != CURLE_OK) {
> Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> Â Â Â Â Â Â Â Â Â Â Â_("curl_easy_perform() returned an error: %s (%d) : %s"),
> - Â Â Â Â Â Â Â Â Â Â curl_easy_strerror(errorCode), errorCode, ctx->curl_error);
> + Â Â Â Â Â Â Â Â Â Â curl_easy_strerror(errorCode), errorCode, curl->error);
> Â Â Â Â return -1;
> Â Â }
>
> - Â ÂerrorCode = curl_easy_getinfo(ctx->curl_handle, CURLINFO_RESPONSE_CODE,
> + Â ÂerrorCode = curl_easy_getinfo(curl->handle, CURLINFO_RESPONSE_CODE,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &responseCode);
>
> Â Â if (errorCode != CURLE_OK) {
> Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> Â Â Â Â Â Â Â Â Â Â Â_("curl_easy_getinfo(CURLINFO_RESPONSE_CODE) returned an "
> Â Â Â Â Â Â Â Â Â Â Â Â"error: %s (%d) : %s"), curl_easy_strerror(errorCode),
> - Â Â Â Â Â Â Â Â Â Â errorCode, ctx->curl_error);
> + Â Â Â Â Â Â Â Â Â Â errorCode, curl->error);
> Â Â Â Â return -1;
> Â Â }
>
> @@ -255,7 +238,7 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
>
> Â Â if (responseCode == 301) {
> Â#if LIBCURL_VERSION_NUM >= 0x071202 /* 7.18.2 */
> - Â Â Â ÂerrorCode = curl_easy_getinfo(ctx->curl_handle, CURLINFO_REDIRECT_URL,
> + Â Â Â ÂerrorCode = curl_easy_getinfo(curl->handle, CURLINFO_REDIRECT_URL,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &redirectUrl);
>
> Â Â Â Â if (errorCode != CURLE_OK) {
> @@ -263,7 +246,7 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
> Â Â Â Â Â Â Â Â Â Â Â Â Â_("curl_easy_getinfo(CURLINFO_REDIRECT_URL) returned "
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â"an error: %s (%d) : %s"),
> Â Â Â Â Â Â Â Â Â Â Â Â Âcurl_easy_strerror(errorCode),
> - Â Â Â Â Â Â Â Â Â Â Â Â errorCode, ctx->curl_error);
> + Â Â Â Â Â Â Â Â Â Â Â Â errorCode, curl->error);
> Â Â Â Â } else {
> Â Â Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> Â Â Â Â Â Â Â Â Â Â Â Â Â_("The server redirects from '%s' to '%s'"), url,
> @@ -281,32 +264,23 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
> Â}
>
> Âint
> -esxVI_Context_Connect(esxVI_Context *ctx, const char *url,
> - Â Â Â Â Â Â Â Â Â Â Âconst char *ipAddress, const char *username,
> - Â Â Â Â Â Â Â Â Â Â Âconst char *password, esxUtil_ParsedUri *parsedUri)
> +esxVI_CURL_Connect(esxVI_CURL *curl, esxUtil_ParsedUri *parsedUri)
> Â{
> - Â Âif (ctx == NULL || url == NULL || ipAddress == NULL || username == NULL ||
> - Â Â Â Âpassword == NULL || ctx->url != NULL || ctx->service != NULL ||
> - Â Â Â Âctx->curl_handle != NULL || ctx->curl_headers != NULL) {
> - Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> - Â Â Â Âreturn -1;
> - Â Â}
> -
> - Â Âif (esxVI_String_DeepCopyValue(&ctx->url, url) < 0 ||
> - Â Â Â ÂesxVI_String_DeepCopyValue(&ctx->ipAddress, ipAddress) < 0) {
> + Â Âif (curl->handle != NULL) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid call"));
> Â Â Â Â return -1;
> Â Â }
>
> - Â Âctx->curl_handle = curl_easy_init();
> + Â Âcurl->handle = curl_easy_init();
>
> - Â Âif (ctx->curl_handle == NULL) {
> + Â Âif (curl->handle == NULL) {
> Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
> Â Â Â Â Â Â Â Â Â Â Â_("Could not initialize CURL"));
> Â Â Â Â return -1;
> Â Â }
>
> - Â Âctx->curl_headers = curl_slist_append(ctx->curl_headers, "Content-Type: "
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"text/xml; charset=UTF-8");
> + Â Âcurl->headers = curl_slist_append(curl->headers,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"Content-Type: text/xml; charset=UTF-8");
>
> Â Â /*
> Â Â Â* Add a dummy expect header to stop CURL from waiting for a response code
> @@ -316,56 +290,179 @@ esxVI_Context_Connect(esxVI_Context *ctx, const char *url,
> Â Â Â* 100 (Continue) response and the wait times out resulting in wasting
> Â Â Â* approx. 2 sec per POST operation.
> Â Â Â*/
> - Â Âctx->curl_headers = curl_slist_append(ctx->curl_headers,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"Expect: nothing");
> + Â Âcurl->headers = curl_slist_append(curl->headers, "Expect: nothing");
>
> - Â Âif (ctx->curl_headers == NULL) {
> + Â Âif (curl->headers == NULL) {
> Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
> Â Â Â Â Â Â Â Â Â Â Â_("Could not build CURL header list"));
> Â Â Â Â return -1;
> Â Â }
>
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_URL, ctx->url);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_USERAGENT, "libvirt-esx");
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_HEADER, 0);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_FOLLOWLOCATION, 0);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_SSL_VERIFYPEER,
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_USERAGENT, "libvirt-esx");
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_HEADER, 0);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_FOLLOWLOCATION, 0);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_SSL_VERIFYPEER,
> Â Â Â Â Â Â Â Â Â Â ÂparsedUri->noVerify ? 0 : 1);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_SSL_VERIFYHOST,
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_SSL_VERIFYHOST,
> Â Â Â Â Â Â Â Â Â Â ÂparsedUri->noVerify ? 0 : 2);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_COOKIEFILE, "");
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_HTTPHEADER, ctx->curl_headers);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_READFUNCTION,
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "");
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->headers);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_READFUNCTION,
> Â Â Â Â Â Â Â Â Â Â ÂesxVI_CURL_ReadString);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEFUNCTION,
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_WRITEFUNCTION,
> Â Â Â Â Â Â Â Â Â Â ÂesxVI_CURL_WriteBuffer);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_ERRORBUFFER,
> - Â Â Â Â Â Â Â Â Â Â ctx->curl_error);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_ERRORBUFFER, curl->error);
> Â#if ESX_VI__CURL__ENABLE_DEBUG_OUTPUT
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_DEBUGFUNCTION, esxVI_CURL_Debug);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_VERBOSE, 1);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_DEBUGFUNCTION, esxVI_CURL_Debug);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_VERBOSE, 1);
> Â#endif
>
> Â Â if (parsedUri->proxy) {
> - Â Â Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_PROXY,
> + Â Â Â Âcurl_easy_setopt(curl->handle, CURLOPT_PROXY,
> Â Â Â Â Â Â Â Â Â Â Â Â ÂparsedUri->proxy_hostname);
> - Â Â Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_PROXYTYPE,
> + Â Â Â Âcurl_easy_setopt(curl->handle, CURLOPT_PROXYTYPE,
> Â Â Â Â Â Â Â Â Â Â Â Â ÂparsedUri->proxy_type);
> - Â Â Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_PROXYPORT,
> + Â Â Â Âcurl_easy_setopt(curl->handle, CURLOPT_PROXYPORT,
> Â Â Â Â Â Â Â Â Â Â Â Â ÂparsedUri->proxy_port);
> Â Â }
>
> - Â Âif (virMutexInit(&ctx->curl_lock) < 0) {
> + Â Âif (virMutexInit(&curl->lock) < 0) {
> Â Â Â Â ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
> Â Â Â Â Â Â Â Â Â Â Â_("Could not initialize CURL mutex"));
> Â Â Â Â return -1;
> Â Â }
>
> - Â Âctx->username = strdup(username);
> - Â Âctx->password = strdup(password);
> + Â Âreturn 0;
> +}
> +
> +int
> +esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content)
> +{
> + Â ÂvirBuffer buffer = VIR_BUFFER_INITIALIZER;
> + Â Âint responseCode = 0;
>
> - Â Âif (ctx->username == NULL || ctx->password == NULL) {
> + Â Âif (content == NULL || *content != NULL) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> + Â Â Â Âreturn -1;
> + Â Â}
> +
> + Â ÂvirMutexLock(&curl->lock);
> +
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_URL, url);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_WRITEDATA, &buffer);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 0);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1);
> +
> + Â ÂresponseCode = esxVI_CURL_Perform(curl, url);
> +
> + Â ÂvirMutexUnlock(&curl->lock);
> +
> + Â Âif (responseCode < 0) {
> + Â Â Â Âgoto cleanup;
> + Â Â} else if (responseCode != 200) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> + Â Â Â Â Â Â Â Â Â Â _("HTTP response code %d for download from '%s'"),
> + Â Â Â Â Â Â Â Â Â Â responseCode, url);
> + Â Â Â Âgoto cleanup;
> + Â Â}
> +
> + Â Âif (virBufferError(&buffer)) {
> Â Â Â Â virReportOOMError();
> + Â Â Â Âgoto cleanup;
> + Â Â}
> +
> + Â Â*content = virBufferContentAndReset(&buffer);
> +
> + Âcleanup:
> + Â Âif (*content == NULL) {
> + Â Â Â ÂvirBufferFreeAndReset(&buffer);
> + Â Â Â Âreturn -1;
> + Â Â}
> +
> + Â Âreturn 0;
> +}
> +
> +int
> +esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content)
> +{
> + Â Âint responseCode = 0;
> +
> + Â Âif (content == NULL) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> + Â Â Â Âreturn -1;
> + Â Â}
> +
> + Â ÂvirMutexLock(&curl->lock);
> +
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_URL, url);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_READDATA, &content);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1);
> + Â Âcurl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, strlen(content));
> +
> + Â ÂresponseCode = esxVI_CURL_Perform(curl, url);
> +
> + Â ÂvirMutexUnlock(&curl->lock);
> +
> + Â Âif (responseCode < 0) {
> + Â Â Â Âreturn -1;
> + Â Â} else if (responseCode != 200 && responseCode != 201) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> + Â Â Â Â Â Â Â Â Â Â _("HTTP response code %d for upload to '%s'"),
> + Â Â Â Â Â Â Â Â Â Â responseCode, url);
> + Â Â Â Âreturn -1;
> + Â Â}
> +
> + Â Âreturn 0;
> +}
> +
> +
> +
> +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> + * Context
> + */
> +
> +/* esxVI_Context_Alloc */
> +ESX_VI__TEMPLATE__ALLOC(Context)
> +
> +/* esxVI_Context_Free */
> +ESX_VI__TEMPLATE__FREE(Context,
> +{
> + Â ÂesxVI_CURL_Free(&item->curl);
> + Â ÂVIR_FREE(item->url);
> + Â ÂVIR_FREE(item->ipAddress);
> + Â ÂVIR_FREE(item->username);
> + Â ÂVIR_FREE(item->password);
> + Â ÂesxVI_ServiceContent_Free(&item->service);
> + Â ÂesxVI_UserSession_Free(&item->session);
> + Â ÂesxVI_Datacenter_Free(&item->datacenter);
> + Â ÂesxVI_ComputeResource_Free(&item->computeResource);
> + Â ÂesxVI_HostSystem_Free(&item->hostSystem);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_folderToChildEntity);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToParent);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToVm);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_hostSystemToDatastore);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_computeResourceToHost);
> + Â ÂesxVI_SelectionSpec_Free(&item->selectSet_computeResourceToParentToParent);
> +})
> +
> +int
> +esxVI_Context_Connect(esxVI_Context *ctx, const char *url,
> + Â Â Â Â Â Â Â Â Â Â Âconst char *ipAddress, const char *username,
> + Â Â Â Â Â Â Â Â Â Â Âconst char *password, esxUtil_ParsedUri *parsedUri)
> +{
> + Â Âif (ctx == NULL || url == NULL || ipAddress == NULL || username == NULL ||
> + Â Â Â Âpassword == NULL || ctx->url != NULL || ctx->service != NULL ||
> + Â Â Â Âctx->curl != NULL) {
> + Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> + Â Â Â Âreturn -1;
> + Â Â}
> +
> + Â Âif (esxVI_CURL_Alloc(&ctx->curl) < 0 ||
> + Â Â Â ÂesxVI_CURL_Connect(ctx->curl, parsedUri) < 0 ||
> + Â Â Â ÂesxVI_String_DeepCopyValue(&ctx->url, url) < 0 ||
> + Â Â Â ÂesxVI_String_DeepCopyValue(&ctx->ipAddress, ipAddress) < 0 ||
> + Â Â Â ÂesxVI_String_DeepCopyValue(&ctx->username, username) < 0 ||
> + Â Â Â ÂesxVI_String_DeepCopyValue(&ctx->password, password) < 0) {
> Â Â Â Â return -1;
> Â Â }
>
> @@ -577,87 +674,6 @@ esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
> Â}
>
> Âint
> -esxVI_Context_DownloadFile(esxVI_Context *ctx, const char *url, char **content)
> -{
> - Â ÂvirBuffer buffer = VIR_BUFFER_INITIALIZER;
> - Â Âint responseCode = 0;
> -
> - Â Âif (content == NULL || *content != NULL) {
> - Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> - Â Â Â Âreturn -1;
> - Â Â}
> -
> - Â ÂvirMutexLock(&ctx->curl_lock);
> -
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_URL, url);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEDATA, &buffer);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_UPLOAD, 0);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_HTTPGET, 1);
> -
> - Â ÂresponseCode = esxVI_CURL_Perform(ctx, url);
> -
> - Â ÂvirMutexUnlock(&ctx->curl_lock);
> -
> - Â Âif (responseCode < 0) {
> - Â Â Â Âgoto cleanup;
> - Â Â} else if (responseCode != 200) {
> - Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> - Â Â Â Â Â Â Â Â Â Â _("HTTP response code %d for download from '%s'"),
> - Â Â Â Â Â Â Â Â Â Â responseCode, url);
> - Â Â Â Âgoto cleanup;
> - Â Â}
> -
> - Â Âif (virBufferError(&buffer)) {
> - Â Â Â ÂvirReportOOMError();
> - Â Â Â Âgoto cleanup;
> - Â Â}
> -
> - Â Â*content = virBufferContentAndReset(&buffer);
> -
> - Âcleanup:
> - Â Âif (*content == NULL) {
> - Â Â Â ÂvirBufferFreeAndReset(&buffer);
> - Â Â Â Âreturn -1;
> - Â Â}
> -
> - Â Âreturn 0;
> -}
> -
> -int
> -esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url,
> - Â Â Â Â Â Â Â Â Â Â Â Â const char *content)
> -{
> - Â Âint responseCode = 0;
> -
> - Â Âif (content == NULL) {
> - Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
> - Â Â Â Âreturn -1;
> - Â Â}
> -
> - Â ÂvirMutexLock(&ctx->curl_lock);
> -
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_URL, url);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_READDATA, &content);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_UPLOAD, 1);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_INFILESIZE, strlen(content));
> -
> - Â ÂresponseCode = esxVI_CURL_Perform(ctx, url);
> -
> - Â ÂvirMutexUnlock(&ctx->curl_lock);
> -
> - Â Âif (responseCode < 0) {
> - Â Â Â Âreturn -1;
> - Â Â} else if (responseCode != 200 && responseCode != 201) {
> - Â Â Â ÂESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
> - Â Â Â Â Â Â Â Â Â Â _("HTTP response code %d for upload to '%s'"),
> - Â Â Â Â Â Â Â Â Â Â responseCode, url);
> - Â Â Â Âreturn -1;
> - Â Â}
> -
> - Â Âreturn 0;
> -}
> -
> -int
> ÂesxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
> Â Â Â Â Â Â Â Â Â Â Â const char *request, esxVI_Response **response,
> Â Â Â Â Â Â Â Â Â Â Â esxVI_Occurrence occurrence)
> @@ -678,17 +694,17 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
> Â Â Â Â return -1;
> Â Â }
>
> - Â ÂvirMutexLock(&ctx->curl_lock);
> + Â ÂvirMutexLock(&ctx->curl->lock);
>
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_URL, ctx->url);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEDATA, &buffer);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_UPLOAD, 0);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_POSTFIELDS, request);
> - Â Âcurl_easy_setopt(ctx->curl_handle, CURLOPT_POSTFIELDSIZE, strlen(request));
> + Â Âcurl_easy_setopt(ctx->curl->handle, CURLOPT_URL, ctx->url);
> + Â Âcurl_easy_setopt(ctx->curl->handle, CURLOPT_WRITEDATA, &buffer);
> + Â Âcurl_easy_setopt(ctx->curl->handle, CURLOPT_UPLOAD, 0);
> + Â Âcurl_easy_setopt(ctx->curl->handle, CURLOPT_POSTFIELDS, request);
> + Â Âcurl_easy_setopt(ctx->curl->handle, CURLOPT_POSTFIELDSIZE, strlen(request));
>
> - Â Â(*response)->responseCode = esxVI_CURL_Perform(ctx, ctx->url);
> + Â Â(*response)->responseCode = esxVI_CURL_Perform(ctx->curl, ctx->url);
>
> - Â ÂvirMutexUnlock(&ctx->curl_lock);
> + Â ÂvirMutexUnlock(&ctx->curl->lock);
>
> Â Â if ((*response)->responseCode < 0) {
> Â Â Â Â goto cleanup;
> @@ -869,7 +885,7 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
> Â*/
>
> Â/* esxVI_Response_Alloc */
> -ESX_VI__TEMPLATE__ALLOC(Response);
> +ESX_VI__TEMPLATE__ALLOC(Response)
>
> Â/* esxVI_Response_Free */
> ÂESX_VI__TEMPLATE__FREE(Response,
> @@ -879,7 +895,7 @@ ESX_VI__TEMPLATE__FREE(Response,
> Â Â if (item->document != NULL) {
> Â Â Â Â xmlFreeDoc(item->document);
> Â Â }
> -});
> +})
>
>
>
> diff --git a/src/esx/esx_vi.h b/src/esx/esx_vi.h
> index d046bf9..57788ac 100644
> --- a/src/esx/esx_vi.h
> +++ b/src/esx/esx_vi.h
> @@ -82,6 +82,7 @@ typedef enum _esxVI_APIVersion esxVI_APIVersion;
> Âtypedef enum _esxVI_ProductVersion esxVI_ProductVersion;
> Âtypedef enum _esxVI_Occurrence esxVI_Occurrence;
> Âtypedef struct _esxVI_ParsedHostCpuIdInfo esxVI_ParsedHostCpuIdInfo;
> +typedef struct _esxVI_CURL esxVI_CURL;
> Âtypedef struct _esxVI_Context esxVI_Context;
> Âtypedef struct _esxVI_Response esxVI_Response;
> Âtypedef struct _esxVI_Enumeration esxVI_Enumeration;
> @@ -142,16 +143,32 @@ struct _esxVI_ParsedHostCpuIdInfo {
>
>
> Â/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> + * CURL
> + */
> +
> +struct _esxVI_CURL {
> + Â ÂCURL *handle;
> + Â ÂvirMutex lock;
> + Â Âstruct curl_slist *headers;
> + Â Âchar error[CURL_ERROR_SIZE];
> +};
> +
> +int esxVI_CURL_Alloc(esxVI_CURL **curl);
> +void esxVI_CURL_Free(esxVI_CURL **curl);
> +int esxVI_CURL_Connect(esxVI_CURL *curl, esxUtil_ParsedUri *parsedUri);
> +int esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content);
> +int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content);
> +
> +
> +
> +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> Â* Context
> Â*/
>
> Âstruct _esxVI_Context {
> + Â ÂesxVI_CURL *curl;
> Â Â char *url;
> Â Â char *ipAddress;
> - Â ÂCURL *curl_handle;
> - Â Âstruct curl_slist *curl_headers;
> - Â Âchar curl_error[CURL_ERROR_SIZE];
> - Â ÂvirMutex curl_lock;
> Â Â char *username;
> Â Â char *password;
> Â Â esxVI_ServiceContent *service;
> @@ -180,10 +197,6 @@ int esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â esxUtil_ParsedUri *parsedUri);
> Âint esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â const char *hostSystemIpAddress);
> -int esxVI_Context_DownloadFile(esxVI_Context *ctx, const char *url,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â char **content);
> -int esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â const char *content);
> Âint esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
> Â Â Â Â Â Â Â Â Â Â Â Â Â const char *request, esxVI_Response **response,
> Â Â Â Â Â Â Â Â Â Â Â Â Â esxVI_Occurrence occurrence);
> --
> 1.7.0.4
>

Ping :)

Matthias

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]