From 4f249f9d0ab7701876cc5bd6be2338de8dae8e35 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Mon, 19 Jun 2006 21:49:25 +0000 Subject: Merge cookies changes into head - unvalidated transactions and a UI still need implementing. svn path=/trunk/netsurf/; revision=2632 --- utils/url.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'utils/url.c') diff --git a/utils/url.c b/utils/url.c index 126bbcebd..897faab77 100644 --- a/utils/url.c +++ b/utils/url.c @@ -676,6 +676,58 @@ url_func_result url_plq(const char *url, char **result) } +/** + * Extract path segment from an URL + * + * \param url an absolute URL + * \param result pointer to pointer to buffer to hold result + * \return URL_FUNC_OK on success + */ + +url_func_result url_path(const char *url, char **result) +{ + int m, path_len = 0; + regmatch_t match[10]; + + (*result) = 0; + + m = regexec(&url_re, url, 10, match, 0); + if (m) { + LOG(("url '%s' failed to match regex", url)); + return URL_FUNC_FAILED; + } + if (match[URL_RE_SCHEME].rm_so == -1 || + match[URL_RE_AUTHORITY].rm_so == -1) + return URL_FUNC_FAILED; + + if (match[URL_RE_PATH].rm_so != -1) + path_len = match[URL_RE_PATH].rm_eo - + match[URL_RE_PATH].rm_so; + + (*result) = malloc((path_len ? path_len : 1) + 1); + if (!(*result)) { + LOG(("malloc failed")); + return URL_FUNC_NOMEM; + } + + m = 0; + if (path_len > 1) { + strncpy((*result), url + match[URL_RE_PATH].rm_so, + path_len); + for (; path_len != 0 && (*result)[m + path_len - 1] != '/'; + path_len--) + /* do nothing */; + m += path_len; + } + else + (*result)[m++] = '/'; + + (*result)[m] = '\0'; + + return URL_FUNC_OK; +} + + /** * Attempt to find a nice filename for a URL. * -- cgit v1.2.3