From ad6fcea6b008638ca532436ecf8ba9fe7e41ffdd Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 3 Feb 2008 12:04:48 +0000 Subject: Add url_fragment to extract fragment from URL Optionally allow url_compare to ignore fragments in comparison Fix handling of url_compare result in a few places Fix redirects which contain fragments in the Location header svn path=/trunk/netsurf/; revision=3826 --- utils/url.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ utils/url.h | 3 ++- 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'utils') diff --git a/utils/url.c b/utils/url.c index 3a43e1765..ac46944eb 100644 --- a/utils/url.c +++ b/utils/url.c @@ -30,6 +30,7 @@ #include #include #include +#include "curl/curl.h" #include "utils/log.h" #include "utils/url.h" #include "utils/utils.h" @@ -718,6 +719,44 @@ url_func_result url_leafname(const char *url, char **result) return status; } +/** + * Extract fragment from an URL + * This will unescape any %xx entities in the fragment + * + * \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_fragment(const char *url, char **result) +{ + url_func_result status; + struct url_components components; + + assert(url); + + status = url_get_components(url, &components); + if (status == URL_FUNC_OK) { + if (!components.fragment) { + status = URL_FUNC_FAILED; + } else { + char *frag = curl_unescape(components.fragment, + strlen(components.fragment)); + if (!frag) { + status = URL_FUNC_NOMEM; + } else { + *result = strdup(frag); + if (!(*result)) + status = URL_FUNC_NOMEM; + curl_free(frag); + } + } + } + + url_destroy_components(&components); + return status; +} + /** * Attempt to find a nice filename for a URL. * @@ -897,12 +936,14 @@ url_func_result url_escape(const char *unescaped, bool sptoplus, /** * Compare two absolute, normalized URLs * - * \param url1 URL 1 - * \param url2 URL 2 - * \param result Pointer to location to store result (true if URLs match) + * \param url1 URL 1 + * \param url2 URL 2 + * \param nofrag Ignore fragment part in comparison + * \param result Pointer to location to store result (true if URLs match) * \return URL_FUNC_OK on success */ -url_func_result url_compare(const char *url1, const char *url2, bool *result) +url_func_result url_compare(const char *url1, const char *url2, + bool nofrag, bool *result) { url_func_result status; struct url_components c1, c2; @@ -930,7 +971,7 @@ url_func_result url_compare(const char *url1, const char *url2, bool *result) ((c1.path && c2.path) || (!c1.path && !c2.path)) && ((c1.query && c2.query) || (!c1.query && !c2.query)) && - ((c1.fragment && c2.fragment) || + (nofrag || (c1.fragment && c2.fragment) || (!c1.fragment && !c2.fragment))) { if (c1.scheme) @@ -946,7 +987,7 @@ url_func_result url_compare(const char *url1, const char *url2, bool *result) if (c1.query) res &= strcmp(c1.query, c2.query) == 0; - if (c1.fragment) + if (!nofrag && c1.fragment) res &= strcmp(c1.fragment, c2.fragment) == 0; } else { /* Can't match */ diff --git a/utils/url.h b/utils/url.h index b35b6e376..9a1b934a5 100644 --- a/utils/url.h +++ b/utils/url.h @@ -54,8 +54,9 @@ url_func_result url_parent(const char *url, char **result); url_func_result url_plq(const char *url, char **result); url_func_result url_path(const char *url, char **result); url_func_result url_leafname(const char *url, char **result); +url_func_result url_fragment(const char *url, char **result); url_func_result url_compare(const char *url1, const char *url2, - bool *result); + bool nofrag, bool *result); url_func_result url_get_components(const char *url, struct url_components *result); -- cgit v1.2.1