From f2121d1c0f36ef3951e0570552e70f9450f0fab5 Mon Sep 17 00:00:00 2001 From: John-Mark Bell Date: Sun, 22 Apr 2018 02:59:44 +0000 Subject: NSURL: add ability to create replacement scheme --- utils/corestringlist.h | 1 + utils/nsurl.h | 19 +++++++++++ utils/nsurl/nsurl.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/utils/corestringlist.h b/utils/corestringlist.h index 8d15eebf2..90dd796a7 100644 --- a/utils/corestringlist.h +++ b/utils/corestringlist.h @@ -70,6 +70,7 @@ CORESTRING_LWC_STRING(filename); CORESTRING_LWC_STRING(font); CORESTRING_LWC_STRING(frame); CORESTRING_LWC_STRING(frameset); +CORESTRING_LWC_STRING(ftp); CORESTRING_LWC_STRING(h1); CORESTRING_LWC_STRING(h2); CORESTRING_LWC_STRING(h3); diff --git a/utils/nsurl.h b/utils/nsurl.h index f97562bf0..054baf26b 100644 --- a/utils/nsurl.h +++ b/utils/nsurl.h @@ -300,6 +300,25 @@ nserror nsurl_replace_query(const nsurl *url, const char *query, nsurl **new_url); +/** + * Create a NetSurf URL object, with scheme replaced + * + * \param url NetSurf URL to create new NetSurf URL from + * \param scheme Scheme to use + * \param new_url Returns new NetSurf URL with scheme provided + * \return NSERROR_OK on success, appropriate error otherwise + * + * If return value != NSERROR_OK, nothing will be returned in new_url. + * + * It is up to the client to call nsurl_unref when they are finished with + * the created object. + * + * Any scheme component in url is replaced with scheme in new_url. + */ +nserror nsurl_replace_scheme(const nsurl *url, lwc_string *scheme, + nsurl **new_url); + + /** * Attempt to find a nice filename for a URL. * diff --git a/utils/nsurl/nsurl.c b/utils/nsurl/nsurl.c index 3b0af9328..8c769cfff 100644 --- a/utils/nsurl/nsurl.c +++ b/utils/nsurl/nsurl.c @@ -648,6 +648,93 @@ nserror nsurl_replace_query(const nsurl *url, const char *query, } +/* exported interface, documented in nsurl.h */ +nserror nsurl_replace_scheme(const nsurl *url, lwc_string *scheme, + nsurl **new_url) +{ + int scheme_len; + int base_len; + char *pos; + size_t len; + bool match; + + assert(url != NULL); + assert(scheme != NULL); + + /* Get the length of the new scheme */ + scheme_len = lwc_string_length(scheme); + + /* Find the change in length from url to new_url */ + base_len = url->length; + if (url->components.scheme != NULL) { + base_len -= lwc_string_length(url->components.scheme); + } + + /* Set new_url's length */ + len = base_len + scheme_len; + + /* Create NetSurf URL object */ + *new_url = malloc(sizeof(nsurl) + len + 1); /* Add 1 for \0 */ + if (*new_url == NULL) { + return NSERROR_NOMEM; + } + + (*new_url)->length = len; + + /* Set string */ + pos = (*new_url)->string; + memcpy(pos, lwc_string_data(scheme), scheme_len); + memcpy(pos + scheme_len, + url->string + url->length - base_len, base_len); + pos[len] = '\0'; + + /* Copy components */ + (*new_url)->components.scheme = lwc_string_ref(scheme); + (*new_url)->components.username = + nsurl__component_copy(url->components.username); + (*new_url)->components.password = + nsurl__component_copy(url->components.password); + (*new_url)->components.host = + nsurl__component_copy(url->components.host); + (*new_url)->components.port = + nsurl__component_copy(url->components.port); + (*new_url)->components.path = + nsurl__component_copy(url->components.path); + (*new_url)->components.query = + nsurl__component_copy(url->components.query); + (*new_url)->components.fragment = + nsurl__component_copy(url->components.fragment); + + /* Compute new scheme type */ + if (lwc_string_caseless_isequal(scheme, corestring_lwc_http, + &match) == lwc_error_ok && match == true) { + (*new_url)->components.scheme_type = NSURL_SCHEME_HTTP; + } else if (lwc_string_caseless_isequal(scheme, corestring_lwc_https, + &match) == lwc_error_ok && match == true) { + (*new_url)->components.scheme_type = NSURL_SCHEME_HTTPS; + } else if (lwc_string_caseless_isequal(scheme, corestring_lwc_file, + &match) == lwc_error_ok && match == true) { + (*new_url)->components.scheme_type = NSURL_SCHEME_FILE; + } else if (lwc_string_caseless_isequal(scheme, corestring_lwc_ftp, + &match) == lwc_error_ok && match == true) { + (*new_url)->components.scheme_type = NSURL_SCHEME_FTP; + } else if (lwc_string_caseless_isequal(scheme, corestring_lwc_mailto, + &match) == lwc_error_ok && match == true) { + (*new_url)->components.scheme_type = NSURL_SCHEME_MAILTO; + } else { + (*new_url)->components.scheme_type = NSURL_SCHEME_OTHER; + } + + /* Get the nsurl's hash */ + nsurl__calc_hash(*new_url); + + /* Give the URL a reference */ + (*new_url)->count = 1; + + return NSERROR_OK; +} + + /* exported interface documented in utils/nsurl.h */ nserror nsurl_nice(const nsurl *url, char **result, bool remove_extensions) { -- cgit v1.2.3