IURL-XS

 view release on metacpan or  search on metacpan

src/yuarel.c  view on Meta::CPAN

{
	return find_and_terminate(str, '#');
}

static inline char *
find_query(char *str)
{
	return find_and_terminate(str, '?');
}

static inline char *
find_path(char *str)
{
	return find_and_terminate(str, '/');
}

/**
 * Parse a URL string to a struct.
 *
 * url: pointer to the struct where to store the parsed URL parts.
 * u:   the string containing the URL to be parsed.
 *
 * Returns 0 on success, otherwise -1.
 */
int
yuarel_parse(struct yuarel *url, char *u)
{
	if (NULL == url || NULL == u) {
		return -1;
	}

	memset(url, 0, sizeof (struct yuarel));

	/* (Fragment) */
	url->fragment = find_fragment(u);

	/* (Query) */
	url->query = find_query(u);

	/* Relative URL? Parse scheme and hostname */
	if (!is_relative(u)) {
		/* Scheme */
		url->scheme = u;
		u = parse_scheme(u);
		if (u == NULL) {
			return -1;
		}

		/* Host */
		if ('\0' == *u) {
			return -1;
		}
		url->host = u;

		/* (Path) */
		url->path = find_path(u);

		/* (Credentials) */
		u = strchr(url->host, '@');
		if (NULL != u) {
			/* Missing credentials? */
			if (u == url->host) {
				return -1;
			}

			url->username = url->host;
			url->host = u + 1;
			*u = '\0';

			u = strchr(url->username, ':');
			if (NULL == u) {
				return -1;
			}

			url->password = u + 1;
			*u = '\0';
		}

		/* Missing hostname? */
		if ('\0' == *url->host) {
			return -1;
		}

		/* (Port) */
		u = strchr(url->host, ':');
		if (NULL != u && (NULL == url->path || u < url->path)) {
			*(u++) = '\0';
			if ('\0' == *u) {
				return -1;
			}

			if (url->path) {
				url->port = natoi(u, url->path - u - 1);
			} else {
				url->port = atoi(u);
			}
		}

		/* Missing hostname? */
		if ('\0' == *url->host) {
			return -1;
		}
	} else {
		/* (Path) */
		url->path = find_path(u);
	}

	return 0;
}

/**
 * Split a path into several strings.
 *
 * No data is copied, the slashed are used as null terminators and then
 * pointers to each path part will be stored in **parts. Double slashes will be
 * treated as one.
 *
 * path: the path to split.
 * parts: a pointer to an array of (char *) where to store the result.
 * max_parts: max number of parts to parse.
 */



( run in 0.857 second using v1.01-cache-2.11-cpan-39bf76dae61 )