summaryrefslogtreecommitdiff
path: root/utils/utils.c
diff options
context:
space:
mode:
authorJames Bursa <james@netsurf-browser.org>2003-12-26 00:17:55 +0000
committerJames Bursa <james@netsurf-browser.org>2003-12-26 00:17:55 +0000
commitf1b59814f8da2f3e235d47adfb332edf8a093b31 (patch)
tree760327fafeb70a0ed342abba3455da00a684eabc /utils/utils.c
parent3b4de07169777f6f820f08ac1422e9af901b3ee2 (diff)
downloadnetsurf-f1b59814f8da2f3e235d47adfb332edf8a093b31.tar.gz
netsurf-f1b59814f8da2f3e235d47adfb332edf8a093b31.tar.bz2
[project @ 2003-12-26 00:17:55 by bursa]
New url_join using liburi, <base href=...>. svn path=/import/netsurf/; revision=441
Diffstat (limited to 'utils/utils.c')
-rw-r--r--utils/utils.c136
1 files changed, 49 insertions, 87 deletions
diff --git a/utils/utils.c b/utils/utils.c
index 004fa3fa7..1487ac1c7 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -12,6 +12,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
+#include <uri.h>
#include "libxml/encoding.h"
#include "libxml/uri.h"
#include "netsurf/utils/log.h"
@@ -165,98 +166,59 @@ char *squash_tolat1(xmlChar *s)
return squash;
}
-char *url_join(const char* new, const char* base)
+
+/**
+ * Calculate a URL from a relative and base URL.
+ *
+ * base may be 0 for a new URL, in which case the URL is cannonicalized and
+ * returned. Returns 0 in case of error.
+ */
+
+char *url_join(char *rel_url, char *base_url)
{
- char* ret, *nn;
- int i,j,k;
-
- LOG(("new = %s, base = %s", new, base));
-
- /* deal with spaces and quotation marks in URLs etc.
- also removes spaces from end of links.
- There's definitely a better way to do this */
- nn = xcalloc(strlen(new) * 3 + 40, sizeof(char));
- j=0;
- for(i=0;i<strlen(new);i++){
-
- if(new[i] == ' '){ /* space */
-
- nn[j] = '%';
- nn[j+1] = '2';
- nn[j+2] = '0';
- j+=2;
- }
- else if(new[i] == '"'){ /* quotes */
-
- nn[j] = '%';
- nn[j+1] = '2';
- nn[j+2] = '2';
- j+=2;
- k = j;
- }
- else{
-
- nn[j] = new[i];
- k = j;
- }
-
- j++;
- }
- if(k < j){
- nn[k+1] = '\0';
- LOG(("before: %s after: %s", new, nn));
- }
+ char *res;
+ uri_t *base = 0, *rel = 0, *abs;
+
+ LOG(("rel_url = %s, base_url = %s", rel_url, base_url));
+
+ if (!base_url) {
+ res = uri_cannonicalize_string(rel_url, strlen(rel_url),
+ URI_STRING_URI_STYLE);
+ LOG(("res = %s", res));
+ if (res)
+ return xstrdup(res);
+ return 0;
+ }
- new = nn;
-
- if (base == 0)
- {
- /* no base, so make an absolute URL */
- ret = xcalloc(strlen(new) + 10, sizeof(char));
-
- /* check if a scheme is present */
- i = strspn(new, "abcdefghijklmnopqrstuvwxyz");
- if (new[i] == ':')
- {
- strcpy(ret, new);
- i += 3;
- }
- else
- {
- strcpy(ret, "http://");
- strcat(ret, new);
- i = 7;
- }
-
- /* make server name lower case */
- for (; ret[i] != 0 && ret[i] != '/'; i++)
- ret[i] = tolower(ret[i]);
-
- xmlNormalizeURIPath(ret + i);
-
- /* http://www.example.com -> http://www.example.com/ */
- if (ret[i] == 0)
- {
- ret[i] = '/';
- ret[i+1] = 0;
- }
- }
- else
- {
- /* relative url */
- ret = xmlBuildURI(new, base);
- }
+ base = uri_alloc(base_url, strlen(base_url));
+ rel = uri_alloc(rel_url, strlen(rel_url));
+ if (!base || !rel)
+ goto fail;
+ if (!base->scheme)
+ goto fail;
- LOG(("ret = %s", ret));
- if (ret == NULL)
- {
- ret = xcalloc(strlen(new) + 10, sizeof(char));
- strcpy(ret, new);
- }
+ abs = uri_abs_1(base, rel);
+
+ res = xstrdup(uri_uri(abs));
+
+ uri_free(base);
+ uri_free(rel);
+
+ LOG(("res = %s", res));
+ return res;
+
+fail:
+ if (base)
+ uri_free(base);
+ if (rel)
+ uri_free(rel);
+
+ LOG(("error"));
- xfree(nn);
- return ret;
+ return 0;
}
+
+
char *get_host_from_url (char *url) {