Use a libvirt custom struct for virURIPtr

Instead of just typedef'ing the xmlURIPtr struct for virURIPtr,
use a custom libvirt struct. This allows us to fix various
problems with libxml2. This initially just fixes the query vs
query_raw handling problems.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-03-20 13:33:41 +00:00
parent 1f66c18f79
commit 300e60e15b
6 changed files with 79 additions and 45 deletions

View File

@ -62,11 +62,7 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
return -1; return -1;
} }
#ifdef HAVE_XMLURI_QUERY_RAW
queryParamSet = qparam_query_parse(uri->query_raw);
#else
queryParamSet = qparam_query_parse(uri->query); queryParamSet = qparam_query_parse(uri->query);
#endif
if (queryParamSet == NULL) { if (queryParamSet == NULL) {
goto cleanup; goto cleanup;

View File

@ -54,11 +54,7 @@ hypervParseUri(hypervParsedUri **parsedUri, virURIPtr uri)
return -1; return -1;
} }
#ifdef HAVE_XMLURI_QUERY_RAW
queryParamSet = qparam_query_parse(uri->query_raw);
#else
queryParamSet = qparam_query_parse(uri->query); queryParamSet = qparam_query_parse(uri->query);
#endif
if (queryParamSet == NULL) { if (queryParamSet == NULL) {
goto cleanup; goto cleanup;

View File

@ -1172,15 +1172,12 @@ do_open (const char *name,
VIR_DEBUG("name \"%s\" to URI components:\n" VIR_DEBUG("name \"%s\" to URI components:\n"
" scheme %s\n" " scheme %s\n"
" opaque %s\n"
" authority %s\n"
" server %s\n" " server %s\n"
" user %s\n" " user %s\n"
" port %d\n" " port %d\n"
" path %s\n", " path %s\n",
alias ? alias : name, alias ? alias : name,
NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->opaque), NULLSTR(ret->uri->scheme), NULLSTR(ret->uri->server),
NULLSTR(ret->uri->authority), NULLSTR(ret->uri->server),
NULLSTR(ret->uri->user), ret->uri->port, NULLSTR(ret->uri->user), ret->uri->port,
NULLSTR(ret->uri->path)); NULLSTR(ret->uri->path));

View File

@ -417,15 +417,9 @@ doRemoteOpen (virConnectPtr conn,
*/ */
struct qparam *var; struct qparam *var;
int i; int i;
char *query;
if (conn->uri) { if (conn->uri) {
#ifdef HAVE_XMLURI_QUERY_RAW vars = qparam_query_parse (conn->uri->query);
query = conn->uri->query_raw;
#else
query = conn->uri->query;
#endif
vars = qparam_query_parse (query);
if (vars == NULL) goto failed; if (vars == NULL) goto failed;
for (i = 0; i < vars->n; i++) { for (i = 0; i < vars->n; i++) {
@ -490,11 +484,7 @@ doRemoteOpen (virConnectPtr conn,
} else { } else {
virURI tmpuri = { virURI tmpuri = {
.scheme = conn->uri->scheme, .scheme = conn->uri->scheme,
#ifdef HAVE_XMLURI_QUERY_RAW
.query_raw = qparam_get_query (vars),
#else
.query = qparam_get_query (vars), .query = qparam_get_query (vars),
#endif
.path = conn->uri->path, .path = conn->uri->path,
.fragment = conn->uri->fragment, .fragment = conn->uri->fragment,
}; };
@ -507,11 +497,7 @@ doRemoteOpen (virConnectPtr conn,
name = virURIFormat(&tmpuri); name = virURIFormat(&tmpuri);
#ifdef HAVE_XMLURI_QUERY_RAW
VIR_FREE(tmpuri.query_raw);
#else
VIR_FREE(tmpuri.query); VIR_FREE(tmpuri.query);
#endif
/* Restore transport scheme */ /* Restore transport scheme */
if (transport_str) if (transport_str)

View File

@ -36,15 +36,45 @@
virURIPtr virURIPtr
virURIParse(const char *uri) virURIParse(const char *uri)
{ {
virURIPtr ret = xmlParseURI(uri); xmlURIPtr xmluri;
virURIPtr ret = NULL;
if (!ret) { xmluri = xmlParseURI(uri);
if (!xmluri) {
/* libxml2 does not tell us what failed. Grr :-( */ /* libxml2 does not tell us what failed. Grr :-( */
virURIReportError(VIR_ERR_INTERNAL_ERROR, virURIReportError(VIR_ERR_INTERNAL_ERROR,
"Unable to parse URI %s", uri); "Unable to parse URI %s", uri);
return NULL; return NULL;
} }
if (VIR_ALLOC(ret) < 0)
goto no_memory;
if (xmluri->scheme &&
!(ret->scheme = strdup(xmluri->scheme)))
goto no_memory;
if (xmluri->server &&
!(ret->server = strdup(xmluri->server)))
goto no_memory;
ret->port = xmluri->port;
if (xmluri->path &&
!(ret->path = strdup(xmluri->path)))
goto no_memory;
#ifdef HAVE_XMLURI_QUERY_RAW
if (xmluri->query_raw &&
!(ret->query = strdup(xmluri->query_raw)))
goto no_memory;
#else
if (xmluri->query &&
!(ret->query = strdup(xmluri->query)))
goto no_memory;
#endif
if (xmluri->fragment &&
!(ret->fragment = strdup(xmluri->fragment)))
goto no_memory;
/* First check: does it even make sense to jump inside */ /* First check: does it even make sense to jump inside */
if (ret->server != NULL && if (ret->server != NULL &&
ret->server[0] == '[') { ret->server[0] == '[') {
@ -62,7 +92,15 @@ virURIParse(const char *uri)
* the uri with xmlFreeURI() */ * the uri with xmlFreeURI() */
} }
xmlFreeURI(xmluri);
return ret; return ret;
no_memory:
virReportOOMError();
xmlFreeURI(xmluri);
virURIFree(ret);
return NULL;
} }
/** /**
@ -79,33 +117,37 @@ virURIParse(const char *uri)
char * char *
virURIFormat(virURIPtr uri) virURIFormat(virURIPtr uri)
{ {
char *backupserver = NULL; xmlURI xmluri;
char *tmpserver = NULL; char *tmpserver = NULL;
char *ret; char *ret;
/* First check: does it make sense to do anything */ memset(&xmluri, 0, sizeof(xmluri));
if (uri->server != NULL &&
strchr(uri->server, ':') != NULL) {
backupserver = uri->server; xmluri.scheme = uri->scheme;
if (virAsprintf(&tmpserver, "[%s]", uri->server) < 0) xmluri.server = uri->server;
xmluri.port = uri->port;
xmluri.path = uri->path;
xmluri.query = uri->query;
xmluri.fragment = uri->fragment;
/* First check: does it make sense to do anything */
if (xmluri.server != NULL &&
strchr(xmluri.server, ':') != NULL) {
if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0)
return NULL; return NULL;
uri->server = tmpserver; xmluri.server = tmpserver;
} }
ret = (char *) xmlSaveUri(uri); ret = (char *)xmlSaveUri(&xmluri);
if (!ret) { if (!ret) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
/* Put the fixed version back */ VIR_FREE(tmpserver);
if (tmpserver) {
uri->server = backupserver;
VIR_FREE(tmpserver);
}
return ret; return ret;
} }
@ -119,5 +161,12 @@ cleanup:
*/ */
void virURIFree(virURIPtr uri) void virURIFree(virURIPtr uri)
{ {
xmlFreeURI(uri); if (!uri)
return;
VIR_FREE(uri->scheme);
VIR_FREE(uri->server);
VIR_FREE(uri->path);
VIR_FREE(uri->query);
VIR_FREE(uri);
} }

View File

@ -13,8 +13,18 @@
# include "internal.h" # include "internal.h"
typedef xmlURI virURI; typedef struct _virURI virURI;
typedef xmlURIPtr virURIPtr; typedef virURI *virURIPtr;
struct _virURI {
char *scheme; /* the URI scheme */
char *server; /* the server part */
char *user; /* the user part */
int port; /* the port number */
char *path; /* the path string */
char *query; /* the query string */
char *fragment; /* the fragment string */
};
virURIPtr virURIParse(const char *uri) virURIPtr virURIParse(const char *uri)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_NONNULL(1);