diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/nsurl.c | 53 | ||||
-rw-r--r-- | utils/nsurl.h | 16 |
2 files changed, 37 insertions, 32 deletions
diff --git a/utils/nsurl.c b/utils/nsurl.c index 78647b4ae..ed6d896b6 100644 --- a/utils/nsurl.c +++ b/utils/nsurl.c @@ -1698,8 +1698,11 @@ const char *nsurl_access(const nsurl *url) return url->string; } -char *nsurl_access_utf8(const nsurl *url) + +/* exported interface, documented in nsurl.h */ +nserror nsurl_access_utf8(const nsurl *url, char **url_s, size_t *url_l) { + nserror err; lwc_string *host; char *idna_host; size_t idna_host_len; @@ -1707,45 +1710,43 @@ char *nsurl_access_utf8(const nsurl *url) size_t scheme_len; char *path; size_t path_len; - char *idna_url; - size_t idna_url_len; assert(url != NULL); host = nsurl_get_component(url, NSURL_HOST); - if(host == NULL) { - return strdup(url->string); - } + if(host == NULL) + return NSERROR_BAD_URL; - if (idna_decode(lwc_string_data(host), lwc_string_length(host), - &idna_host, &idna_host_len) != NSERROR_OK) { - lwc_string_unref(host); - return strdup(url->string); - } + err = idna_decode(lwc_string_data(host), lwc_string_length(host), + &idna_host, &idna_host_len); lwc_string_unref(host); - if (nsurl_get(url, NSURL_SCHEME | NSURL_CREDENTIALS, - &scheme, &scheme_len) != NSERROR_OK) { - return strdup(url->string); - } + if (err != NSERROR_OK) + return err; - if (nsurl_get(url, NSURL_PORT | NSURL_PATH | NSURL_QUERY | NSURL_FRAGMENT, - &path, &path_len) != NSERROR_OK) { - return strdup(url->string); - } + err = nsurl_get(url, NSURL_SCHEME | NSURL_CREDENTIALS, + &scheme, &scheme_len); - idna_url_len = scheme_len + idna_host_len + path_len + 1; /* +1 for \0 */ - idna_url = malloc(idna_url_len); + if (err != NSERROR_OK) + return err; - if (idna_url == NULL) { - return strdup(url->string); - } + err = nsurl_get(url, NSURL_PORT | NSURL_PATH | NSURL_QUERY | NSURL_FRAGMENT, + &path, &path_len); + + if (err != NSERROR_OK) + return err; + + *url_l = scheme_len + idna_host_len + path_len + 1; /* +1 for \0 */ + *url_s = malloc(*url_l); - snprintf(idna_url, idna_url_len, "%s%s%s", scheme, idna_host, path); + if (*url_s == NULL) + return NSERROR_NOMEM; + + snprintf(*url_s, *url_l, "%s%s%s", scheme, idna_host, path); - return(idna_url); + return NSERROR_OK; } diff --git a/utils/nsurl.h b/utils/nsurl.h index 07d73f17f..383b35711 100644 --- a/utils/nsurl.h +++ b/utils/nsurl.h @@ -181,17 +181,21 @@ const char *nsurl_access(const nsurl *url); /** - * Access a NetSurf URL object as a UTF-8 string (for human readable IDNA) + * Access a NetSurf URL object as a UTF-8 string (for human readable IDNs) * * \param url NetSurf URL to retrieve a string pointer for. - * \return the required string + * \param url_s Returns a url string + * \param url_l Returns length of url_s + * \return NSERROR_OK on success, appropriate error otherwise + * + * If return value != NSERROR_OK, nothing will be returned in url_s or url_l. * - * It is up to the client to free the returned string when they have - * finished with it. + * The string returned in url_s is owned by the client and it is up to them + * to free it. It includes a trailing '\0'. * - * The returned string has a trailing '\0'. + * The length returned in url_l excludes the trailing '\0'. */ -char *nsurl_access_utf8(const nsurl *url); +nserror nsurl_access_utf8(const nsurl *url, char **url_s, size_t *url_l); /** |