1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
#include <string.h>
#include "urlutils.h"
static const gchar *skip_scheme(const char *url);
static gboolean is_scheme_character(gchar c);
gchar *relative_url_to_absolute(const gchar *baseurl, const gchar *href) {
gchar *absolute;
gchar *prefix;
const gchar *postfix = href;
if ((href[0] == '/') && (href[1] == '/')) {
gchar *scheme = url_scheme(baseurl);
prefix = g_strconcat(scheme, ":", NULL);
g_free(scheme);
} else if (href[0] == '/') {
prefix = url_root(baseurl);
if (g_str_has_suffix(prefix, "/")) {
postfix = href+1;
}
} else if (href[0] == '?') {
prefix = url_path_including_file(baseurl);
} else if (href[0] =='#') {
prefix = url_path_and_query(baseurl);
} else if (strstr(href, "://") == NULL) {
prefix = url_path_dirname(baseurl);
} else {
// href is absolute
prefix = NULL;
}
if (prefix) {
absolute = g_strconcat(prefix, postfix, NULL);
g_free(prefix);
} else {
absolute = g_strdup(href);
}
return absolute;
}
gchar *url_scheme(const gchar *url) {
if (!url)
return NULL;
const gchar *scheme_end = skip_scheme(url);
if (scheme_end == url) {
// no scheme
return g_strdup("");
} else {
// scheme found
// Do not include :// in the return value
g_assert(scheme_end >= url+3);
return g_strndup(url, scheme_end-3 - url);
}
}
gchar *url_root(const gchar *url) {
if (!url)
return NULL;
const gchar *authority = skip_scheme(url);
size_t authority_len = strcspn(authority, "/?#");
const gchar *authority_end = authority + authority_len;
gchar *root_without_slash = g_strndup(url, authority_end - url);
gchar *root = g_strconcat(root_without_slash, "/", NULL);
g_free(root_without_slash);
return root;
}
gchar *url_path_including_file(const gchar *url) {
if (!url)
return NULL;
const gchar *scheme_end = skip_scheme(url);
size_t path_len = strcspn(scheme_end, "?#");
const gchar *end = scheme_end + path_len;
gchar *path = g_strndup(url, end - url);
if (memchr(scheme_end, '/', path_len) == NULL) {
gchar *path2 = g_strconcat(path, "/", NULL);
g_free(path);
path = path2;
}
return path;
}
gchar *url_path_dirname(const gchar *url) {
if (!url)
return NULL;
const gchar *scheme_end = skip_scheme(url);
size_t path_len = strcspn(scheme_end, "?#");
const gchar *p = scheme_end + path_len;
while ((p >= url) && (*p != '/')) {
p--;
}
if (*p == '/') {
return g_strndup(url, (p+1) - url);
} else {
return g_strdup("/");
}
}
gchar *url_path_and_query(const gchar *url) {
if (!url)
return NULL;
const gchar *scheme_end = skip_scheme(url);
size_t path_len = strcspn(scheme_end, "#");
const gchar *end = scheme_end + path_len;
return g_strndup(url, end - url);
}
const gchar *skip_scheme(const char *url) {
const gchar *c = url;
while (is_scheme_character(*c)) {
c++;
}
if (strncmp(c, "://", 3) == 0) {
// scheme found
return c + 3;
} else {
// schemeless url
return url;
}
}
gboolean is_scheme_character(gchar c) {
return g_ascii_isalnum(c) || (c == '+') || (c == '-') || (c == '.');
}
|