示例#1
0
bool uri_scope_push_uri(UriScope *u, char const *uri)
{
	UriParserStateA state;
	UriUriA a;

	state.uri = &a;
	if (URI_SUCCESS != uriParseUriA(&state, uri))
	{
		uriFreeUriMembersA(&a);
		return false;
	}

	UriUriA *result = g_new0(UriUriA, 1);
	UriUriA *base = uri_scope_get_uri(u);
	if (!base)
	{
		//TODO check that URI is absolute
		memcpy(result, &a, sizeof(a));

		u->uri_stack = g_slist_prepend(u->uri_stack, result);
		return true;
	}

	if (URI_SUCCESS != uriAddBaseUriA(result, &a, base))
	{
		memcpy(result, &a, sizeof(a));

		u->uri_stack = g_slist_prepend(u->uri_stack, result);
		return true;
	}

	u->uri_stack = g_slist_prepend(u->uri_stack, result);
	uriFreeUriMembersA(&a);
	return true;
}
示例#2
0
struct PP_Var
ppb_url_util_dev_resolve_relative_to_url(struct PP_Var base_url, struct PP_Var relative_string,
                                         struct PP_URLComponents_Dev *components)
{
    reset_components(components);
    struct PP_Var var = PP_MakeNull();

    if (base_url.type != PP_VARTYPE_STRING) {
        trace_warning("%s, base_url is not a string\n", __func__);
        return PP_MakeNull();
    }

    if (relative_string.type != PP_VARTYPE_STRING) {
        trace_warning("%s, relative_string is not a string\n", __func__);
        return PP_MakeNull();
    }

    const char *s_base_url = ppb_var_var_to_utf8(base_url, NULL);
    const char *s_relative_string = ppb_var_var_to_utf8(relative_string, NULL);

    UriParserStateA ups;
    UriUriA uri_base, uri_rel, uri_result;

    ups.uri = &uri_base;
    if (uriParseUriA(&ups, s_base_url) != URI_SUCCESS) {
        trace_warning("%s, can't parse s_base_url\n", __func__);
        goto err_1;
    }

    ups.uri = &uri_rel;
    if (uriParseUriA(&ups, s_relative_string) != URI_SUCCESS) {
        trace_warning("%s, can't parse s_relative_string\n", __func__);
        goto err_2;
    }

    if (uriAddBaseUriA(&uri_result, &uri_rel, &uri_base) != URI_SUCCESS) {
        trace_warning("%s, can't merge base and rel\n", __func__);
        goto err_3;
    }

    int len;
    uriToStringCharsRequiredA(&uri_result, &len);
    len++;
    char *str = malloc(len);
    uriToStringA(str, &uri_result, len, NULL);
    var = ppb_var_var_from_utf8_z(str);
    free(str);

    if (components)
        parse_url_string(str, components);

err_3:  uriFreeUriMembersA(&uri_result);
err_2:  uriFreeUriMembersA(&uri_rel);
err_1:  uriFreeUriMembersA(&uri_base);
    return var;
}
示例#3
0
文件: uri_parser.c 项目: ASpade/mulk
UriUriA *create_absolute_uri(const UriUriA *base_uri, const char *url)
{
	UriParserStateA state;
	UriUriA *abs_dest, rel_source;
	char *newurl;

	if (!url || !*url)
		return NULL;

	if ((abs_dest = uri_alloc_1()) == NULL)
		return NULL;

	if (base_uri) {
		state.uri = &rel_source;
		if (uriParseUriA(&state, url) != URI_SUCCESS) {
			uri_free(abs_dest); 
			uriFreeUriMembersA(&rel_source);
			return NULL;
		}

		if (uriAddBaseUriA(abs_dest, &rel_source, base_uri) != URI_SUCCESS) {
			uri_free(abs_dest);
			uriFreeUriMembersA(&rel_source);
			return NULL;
		}

		uriFreeUriMembersA(&rel_source);
	}
	else {
		state.uri = abs_dest;
		if (uriParseUriA(&state, url) != URI_SUCCESS) {
			uri_free(abs_dest); 
			return NULL;
		}
	}

	if (uriNormalizeSyntaxA(abs_dest) != URI_SUCCESS) {
		uri_free(abs_dest);
		return NULL;
	}

	/* http://www.example.com and http://www.example.com/ have to generate the same object */
	if (!base_uri && (!abs_dest->pathHead || !abs_dest->pathHead->text.first)
		&& !abs_dest->query.first) {
		newurl = string_new(url);
		string_cat(&newurl, "/");

		uriFreeUriMembersA(abs_dest);

		state.uri = abs_dest;
		if (uriParseUriA(&state, newurl) != URI_SUCCESS) {
			uri_free(abs_dest);
			string_free(newurl);
			return NULL;
		}

		if (uriNormalizeSyntaxA(abs_dest) != URI_SUCCESS) {
			uri_free(abs_dest);
			string_free(newurl);
			return NULL;
		}
		string_free(newurl);
	}

	return abs_dest;
}