diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/url.c | 59 | ||||
-rw-r--r-- | utils/url.h | 1 |
2 files changed, 60 insertions, 0 deletions
diff --git a/utils/url.c b/utils/url.c index c696659f4..352241e30 100644 --- a/utils/url.c +++ b/utils/url.c @@ -618,6 +618,65 @@ url_func_result url_strip_lqf(const char *url, char **result) /** + * Extract path, leafname and query segments 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_plq(const char *url, char **result) +{ + int m, path_len = 0, query_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; + if (match[URL_RE_QUERY].rm_so != -1) + query_len = match[URL_RE_QUERY].rm_eo - + match[URL_RE_QUERY].rm_so; + + (*result) = malloc((path_len ? path_len : 1) + query_len + 1 + 1); + if (!(*result)) { + LOG(("malloc failed")); + return URL_FUNC_NOMEM; + } + + m = 0; + if (path_len) { + strncpy((*result), url + match[URL_RE_PATH].rm_so, + path_len); + m += path_len; + } + else + (*result)[m++] = '/'; + + if (query_len) { + (*result)[m++] = '?'; + strncpy((*result) + m, url + match[URL_RE_QUERY].rm_so, + query_len); + m += query_len; + } + + (*result)[m] = '\0'; + + return URL_FUNC_OK; +} + + +/** * Attempt to find a nice filename for a URL. * * \param url an absolute URL diff --git a/utils/url.h b/utils/url.h index 3bda22969..7dc0b12ca 100644 --- a/utils/url.h +++ b/utils/url.h @@ -29,6 +29,7 @@ url_func_result url_nice(const char *url, char **result, url_func_result url_escape(const char *unescaped, char **result); url_func_result url_canonical_root(const char *url, char **result); url_func_result url_strip_lqf(const char *url, char **result); +url_func_result url_plq(const char *url, char **result); char *path_to_url(const char *path); char *url_to_path(const char *url); |