diff options
author | John-Mark Bell <jmb@netsurf-browser.org> | 2018-04-22 02:59:44 +0000 |
---|---|---|
committer | John-Mark Bell <jmb@netsurf-browser.org> | 2018-04-22 11:39:12 +0000 |
commit | f2121d1c0f36ef3951e0570552e70f9450f0fab5 (patch) | |
tree | 58e875790295199f1c6f84070ebd4bb1a0174d6a /utils | |
parent | a6014cae157107ec868816fcccf8689a5d6cc766 (diff) | |
download | netsurf-f2121d1c0f36ef3951e0570552e70f9450f0fab5.tar.gz netsurf-f2121d1c0f36ef3951e0570552e70f9450f0fab5.tar.bz2 |
NSURL: add ability to create replacement scheme
Diffstat (limited to 'utils')
-rw-r--r-- | utils/corestringlist.h | 1 | ||||
-rw-r--r-- | utils/nsurl.h | 19 | ||||
-rw-r--r-- | utils/nsurl/nsurl.c | 87 |
3 files changed, 107 insertions, 0 deletions
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 @@ -301,6 +301,25 @@ nserror nsurl_replace_query(const nsurl *url, const char *query, /** + * 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. * * \param url A NetSurf URL object to create a filename from 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) { |