viruri.c 7.9 KB
Newer Older
M
Martin Kletzander 已提交
1 2 3
/*
 * viruri.c: URI parsing wrappers for libxml2 functions
 *
4
 * Copyright (C) 2012-2014 Red Hat, Inc.
M
Martin Kletzander 已提交
5
 *
O
Osier Yang 已提交
6 7 8 9 10 11 12 13 14 15 16
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
M
Martin Kletzander 已提交
19 20 21 22 23 24
 */

#include <config.h>

#include "viruri.h"

25
#include "viralloc.h"
26
#include "virerror.h"
27
#include "virbuffer.h"
28
#include "virstring.h"
29 30 31

#define VIR_FROM_THIS VIR_FROM_URI

32 33 34 35 36 37 38 39
static int
virURIParamAppend(virURIPtr uri,
                  const char *name,
                  const char *value)
{
    char *pname = NULL;
    char *pvalue = NULL;

40 41
    if (VIR_STRDUP(pname, name) < 0 || VIR_STRDUP(pvalue, value) < 0)
        goto error;
42

43
    if (VIR_RESIZE_N(uri->params, uri->paramsAlloc, uri->paramsCount, 1) < 0)
44
        goto error;
45 46 47 48 49 50 51 52

    uri->params[uri->paramsCount].name = pname;
    uri->params[uri->paramsCount].value = pvalue;
    uri->params[uri->paramsCount].ignore = 0;
    uri->paramsCount++;

    return 0;

53
 error:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    VIR_FREE(pname);
    VIR_FREE(pvalue);
    return -1;
}


static int
virURIParseParams(virURIPtr uri)
{
    const char *end, *eq;
    const char *query = uri->query;

    if (!query || query[0] == '\0')
        return 0;

    while (*query) {
        char *name = NULL, *value = NULL;

        /* Find the next separator, or end of the string. */
73
        end = strchr(query, '&');
74
        if (!end)
75
            end = strchr(query, ';');
76
        if (!end)
77
            end = query + strlen(query);
78 79

        /* Find the first '=' character between here and end. */
80
        eq = strchr(query, '=');
81 82
        if (eq && eq >= end) eq = NULL;

83 84
        if (end == query) {
            /* Empty section (eg. "&&"). */
85
            goto next;
86 87 88 89
        } else if (!eq) {
            /* If there is no '=' character, then we have just "name"
             * and consistent with CGI.pm we assume value is "".
             */
90
            name = xmlURIUnescapeString(query, end - query, NULL);
91
            if (!name) goto no_memory;
92 93 94 95
        } else if (eq+1 == end) {
            /* Or if we have "name=" here (works around annoying
             * problem when calling xmlURIUnescapeString with len = 0).
             */
96
            name = xmlURIUnescapeString(query, eq - query, NULL);
97
            if (!name) goto no_memory;
98 99 100 101
        } else if (query == eq) {
            /* If the '=' character is at the beginning then we have
             * "=value" and consistent with CGI.pm we _ignore_ this.
             */
102
            goto next;
103 104
        } else {
            /* Otherwise it's "name=value". */
105
            name = xmlURIUnescapeString(query, eq - query, NULL);
106 107
            if (!name)
                goto no_memory;
108
            value = xmlURIUnescapeString(eq+1, end - (eq+1), NULL);
109 110 111 112 113 114 115 116 117 118
            if (!value) {
                VIR_FREE(name);
                goto no_memory;
            }
        }

        /* Append to the parameter set. */
        if (virURIParamAppend(uri, name, value ? value : "") < 0) {
            VIR_FREE(name);
            VIR_FREE(value);
J
Ján Tomko 已提交
119
            return -1;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }
        VIR_FREE(name);
        VIR_FREE(value);

    next:
        query = end;
        if (*query) query ++; /* skip '&' separator */
    }

    return 0;

 no_memory:
    virReportOOMError();
    return -1;
}

M
Martin Kletzander 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
 * virURIParse:
 * @uri: URI to parse
 *
 * Wrapper for xmlParseURI
 *
 * Unfortunately there are few things that should be managed after
 * parsing the URI. Fortunately there is only one thing now and its
 * removing of square brackets around IPv6 addresses.
 *
 * @returns the parsed uri object with some fixes
 */
virURIPtr
virURIParse(const char *uri)
{
151 152
    xmlURIPtr xmluri;
    virURIPtr ret = NULL;
M
Martin Kletzander 已提交
153

154 155 156
    xmluri = xmlParseURI(uri);

    if (!xmluri) {
157
        /* libxml2 does not tell us what failed. Grr :-( */
158 159
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse URI %s"), uri);
160 161 162
        return NULL;
    }

163
    if (VIR_ALLOC(ret) < 0)
164
        goto error;
165

166 167 168 169
    if (VIR_STRDUP(ret->scheme, xmluri->scheme) < 0)
        goto error;
    if (VIR_STRDUP(ret->server, xmluri->server) < 0)
        goto error;
170
    ret->port = xmluri->port;
171 172
    if (VIR_STRDUP(ret->path, xmluri->path) < 0)
        goto error;
173
#ifdef HAVE_XMLURI_QUERY_RAW
174 175
    if (VIR_STRDUP(ret->query, xmluri->query_raw) < 0)
        goto error;
176
#else
177 178
    if (VIR_STRDUP(ret->query, xmluri->query) < 0)
        goto error;
179
#endif
180 181 182 183
    if (VIR_STRDUP(ret->fragment, xmluri->fragment) < 0)
        goto error;
    if (VIR_STRDUP(ret->user, xmluri->user) < 0)
        goto error;
184

M
Martin Kletzander 已提交
185
    /* First check: does it even make sense to jump inside */
186
    if (ret->server != NULL &&
M
Martin Kletzander 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        ret->server[0] == '[') {
        size_t length = strlen(ret->server);

        /* We want to modify the server string only if there are
         * square brackets on both ends and inside there is IPv6
         * address. Otherwise we could make a mistake by modifying
         * something other than an IPv6 address. */
        if (ret->server[length - 1] == ']' && strchr(ret->server, ':')) {
            memmove(&ret->server[0], &ret->server[1], length - 2);
            ret->server[length - 2] = '\0';
        }
        /* Even after such modification, it is completely ok to free
         * the uri with xmlFreeURI() */
    }

202 203 204
    if (virURIParseParams(ret) < 0)
        goto error;

205 206
    xmlFreeURI(xmluri);

M
Martin Kletzander 已提交
207
    return ret;
208

209
 error:
210 211 212
    xmlFreeURI(xmluri);
    virURIFree(ret);
    return NULL;
M
Martin Kletzander 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

/**
 * virURIFormat:
 * @uri: URI to format
 *
 * Wrapper for xmlSaveUri
 *
 * This function constructs back everything that @ref virURIParse
 * changes after parsing
 *
 * @returns the constructed uri as a string
 */
char *
227
virURIFormat(virURIPtr uri)
M
Martin Kletzander 已提交
228
{
229
    xmlURI xmluri;
M
Martin Kletzander 已提交
230 231 232
    char *tmpserver = NULL;
    char *ret;

233 234 235 236 237 238
    memset(&xmluri, 0, sizeof(xmluri));

    xmluri.scheme = uri->scheme;
    xmluri.server = uri->server;
    xmluri.port = uri->port;
    xmluri.path = uri->path;
239 240 241
#ifdef HAVE_XMLURI_QUERY_RAW
    xmluri.query_raw = uri->query;
#else
242
    xmluri.query = uri->query;
243
#endif
244
    xmluri.fragment = uri->fragment;
245
    xmluri.user = uri->user;
246

M
Martin Kletzander 已提交
247
    /* First check: does it make sense to do anything */
248 249
    if (xmluri.server != NULL &&
        strchr(xmluri.server, ':') != NULL) {
M
Martin Kletzander 已提交
250

251
        if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0)
M
Martin Kletzander 已提交
252 253
            return NULL;

254
        xmluri.server = tmpserver;
M
Martin Kletzander 已提交
255 256
    }

257
    ret = (char *)xmlSaveUri(&xmluri);
258 259 260 261
    if (!ret) {
        virReportOOMError();
        goto cleanup;
    }
M
Martin Kletzander 已提交
262

263
 cleanup:
264
    VIR_FREE(tmpserver);
M
Martin Kletzander 已提交
265 266 267

    return ret;
}
268 269


270 271 272
char *virURIFormatParams(virURIPtr uri)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
273
    size_t i;
274
    bool amp = false;
275 276 277

    for (i = 0; i < uri->paramsCount; ++i) {
        if (!uri->params[i].ignore) {
278 279 280
            if (amp) virBufferAddChar(&buf, '&');
            virBufferStrcat(&buf, uri->params[i].name, "=", NULL);
            virBufferURIEncodeString(&buf, uri->params[i].value);
281
            amp = true;
282 283 284
        }
    }

285
    if (virBufferCheckError(&buf) < 0)
286 287 288 289 290
        return NULL;

    return virBufferContentAndReset(&buf);
}

291 292 293 294 295 296 297 298
/**
 * virURIFree:
 * @uri: uri to free
 *
 * Frees the URI
 */
void virURIFree(virURIPtr uri)
{
299 300
    size_t i;

301 302 303 304 305
    if (!uri)
        return;

    VIR_FREE(uri->scheme);
    VIR_FREE(uri->server);
306
    VIR_FREE(uri->user);
307 308
    VIR_FREE(uri->path);
    VIR_FREE(uri->query);
309 310
    VIR_FREE(uri->fragment);

311
    for (i = 0; i < uri->paramsCount; i++) {
312 313 314 315 316
        VIR_FREE(uri->params[i].name);
        VIR_FREE(uri->params[i].value);
    }
    VIR_FREE(uri->params);

317
    VIR_FREE(uri);
318
}