summaryrefslogtreecommitdiff
path: root/utils/nsurl.c
diff options
context:
space:
mode:
authorChris Young <chris@unsatisfactorysoftware.co.uk>2012-10-11 19:48:49 +0100
committerChris Young <chris@unsatisfactorysoftware.co.uk>2012-10-11 19:48:49 +0100
commite3cbd8aec61b06534ed317f83c5225a36015bef9 (patch)
treec343a157c6453c5d5defa623f5fba2bef0ddd0cd /utils/nsurl.c
parentd4043d801108846b8c7fe0eeae84bc04ade1b6a8 (diff)
parentd84e3fd773e48f13dcfd594b5365fc93dbca93c9 (diff)
downloadnetsurf-e3cbd8aec61b06534ed317f83c5225a36015bef9.tar.gz
netsurf-e3cbd8aec61b06534ed317f83c5225a36015bef9.tar.bz2
Merge branch 'master' of git://git.netsurf-browser.org/netsurf
Diffstat (limited to 'utils/nsurl.c')
-rw-r--r--utils/nsurl.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/utils/nsurl.c b/utils/nsurl.c
index af6581d0d..47b7f5451 100644
--- a/utils/nsurl.c
+++ b/utils/nsurl.c
@@ -1902,6 +1902,89 @@ nserror nsurl_refragment(const nsurl *url, lwc_string *frag, nsurl **new_url)
/* exported interface, documented in nsurl.h */
+nserror nsurl_replace_query(const nsurl *url, const char *query,
+ nsurl **new_url)
+{
+ int query_len;
+ int base_len;
+ char *pos;
+ size_t len;
+ lwc_string *lwc_query;
+
+ assert(url != NULL);
+ assert(query != NULL);
+ assert(query[0] == '?');
+
+ /* Get the length of the new query */
+ query_len = strlen(query);
+
+ /* Find the change in length from url to new_url */
+ base_len = url->length;
+ if (url->components.query != NULL) {
+ base_len -= lwc_string_length(url->components.query);
+ }
+ if (url->components.fragment != NULL) {
+ base_len -= 1 + lwc_string_length(url->components.fragment);
+ }
+
+ /* Set new_url's length */
+ len = base_len + query_len;
+
+ /* Create NetSurf URL object */
+ *new_url = malloc(sizeof(nsurl) + len + 1); /* Add 1 for \0 */
+ if (*new_url == NULL) {
+ return NSERROR_NOMEM;
+ }
+
+ if (lwc_intern_string(query, query_len, &lwc_query) != lwc_error_ok) {
+ free(*new_url);
+ return NSERROR_NOMEM;
+ }
+
+ (*new_url)->length = len;
+
+ /* Set string */
+ pos = (*new_url)->string;
+ memcpy(pos, url->string, base_len);
+ pos += base_len;
+ memcpy(pos, query, query_len);
+ pos += query_len;
+ if (url->components.fragment != NULL) {
+ const char *frag = lwc_string_data(url->components.fragment);
+ size_t frag_len = lwc_string_length(url->components.fragment);
+ *pos = '#';
+ memcpy(++pos, frag, frag_len);
+ pos += frag_len;
+ }
+ *pos = '\0';
+
+ /* Copy components */
+ (*new_url)->components.scheme =
+ nsurl__component_copy(url->components.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 = lwc_query;
+ (*new_url)->components.fragment =
+ nsurl__component_copy(url->components.fragment);
+
+ (*new_url)->components.scheme_type = url->components.scheme_type;
+
+ /* Give the URL a reference */
+ (*new_url)->count = 1;
+
+ return NSERROR_OK;
+}
+
+
+/* exported interface, documented in nsurl.h */
nserror nsurl_parent(const nsurl *url, nsurl **new_url)
{
lwc_string *lwc_path;