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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-url.c
*
* Copyright (C) 2001 Ximian, Inc.
*
* Developed by Jon Trowbridge <trow@ximian.com>
* Rodrigo Moya <rodrigo@ximian.com>
*/
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
#include <config.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "e-url.h"
char *
e_url_shroud (const char *url)
{
const char *first_colon = NULL;
const char *last_at = NULL;
const char *p;
char *shrouded;
if (url == NULL)
return NULL;
/* Skip past the moniker */
for (p = url; *p && *p != ':'; ++p);
if (*p)
++p;
while (*p) {
if (first_colon == NULL && *p == ':')
first_colon = p;
if (*p == '@')
last_at = p;
++p;
}
if (first_colon && last_at && first_colon < last_at) {
shrouded = g_malloc(first_colon - url + strlen(last_at)+1);
memcpy(shrouded, url, first_colon-url);
strcpy(shrouded + (first_colon-url), last_at);
} else {
shrouded = g_strdup (url);
}
return shrouded;
}
gboolean
e_url_equal (const char *url1, const char *url2)
{
char *shroud1 = e_url_shroud (url1);
char *shroud2 = e_url_shroud (url2);
gint len1, len2;
gboolean rv;
if (shroud1 == NULL || shroud2 == NULL) {
rv = (shroud1 == shroud2);
} else {
len1 = strlen (shroud1);
len2 = strlen (shroud2);
rv = !strncmp (shroud1, shroud2, MIN (len1, len2));
}
g_free (shroud1);
g_free (shroud2);
return rv;
}
#define HEXVAL(c) (isdigit (c) ? (c) - '0' : tolower (c) - 'a' + 10)
static void
uri_decode (char *part)
{
guchar *s, *d;
s = d = (guchar *)part;
while (*s) {
if (*s == '%') {
if (isxdigit (s[1]) && isxdigit (s[2])) {
*d++ = HEXVAL (s[1]) * 16 + HEXVAL (s[2]);
s += 3;
} else
*d++ = *s++;
} else
*d++ = *s++;
}
*d = '\0';
}
EUri *
e_uri_new (const char *uri_string)
{
EUri *uri;
const char *end, *hash, *colon, *semi, *at, *slash, *question;
const char *p;
if (!uri_string)
return NULL;
uri = g_new0 (EUri, 1);
/* find fragment */
end = hash = strchr (uri_string, '#');
if (hash && hash[1]) {
uri->fragment = g_strdup (hash + 1);
uri_decode (uri->fragment);
}
else
end = uri_string + strlen (uri_string);
/* find protocol: initial [a-z+.-]* substring until ":" */
p = uri_string;
while (p < end && (isalnum ((unsigned char) *p) ||
*p == '.' || *p == '+' || *p == '-'))
p++;
if (p > uri_string && *p == ':') {
uri->protocol = g_ascii_strdown (uri_string, p - uri_string);
uri_string = p + 1;
}
else
uri->protocol = g_strdup ("file");
if (!*uri_string)
return uri;
/* check for authority */
if (strncmp (uri_string, "//", 2) == 0) {
uri_string += 2;
slash = uri_string + strcspn (uri_string, "/#");
at = strchr (uri_string, '@');
if (at && at < slash) {
colon = strchr (uri_string, ':');
if (colon && colon < at) {
uri->passwd = g_strndup (colon + 1, at - colon - 1);
uri_decode (uri->passwd);
}
else {
uri->passwd = NULL;
colon = at;
}
semi = strchr (uri_string, ';');
if (semi && semi < colon &&
!strncasecmp (semi, ";auth=", 6)) {
uri->authmech = g_strndup (semi + 6, colon - semi - 6);
uri_decode (uri->authmech);
}
else {
uri->authmech = NULL;
semi = colon;
}
uri->user = g_strndup (uri_string, semi - uri_string);
uri_decode (uri->user);
uri_string = at + 1;
}
else
uri->user = uri->passwd = uri->authmech = NULL;
/* find host and port */
colon = strchr (uri_string, ':');
if (colon && colon < slash) {
uri->host = g_strndup (uri_string, colon - uri_string);
uri->port = strtoul (colon + 1, NULL, 10);
}
else {
uri->host = g_strndup (uri_string, slash - uri_string);
uri_decode (uri->host);
uri->port = 0;
}
}
/* find query */
question = memchr (uri_string, '?', end - uri_string);
if (question) {
if (question[1]) {
uri->query = g_strndup (question + 1, end - (question + 1));
uri_decode (uri->query);
}
end = question;
}
/* find parameters */
semi = memchr (uri_string, ';', end - uri_string);
if (semi) {
if (semi[1]) {
const char *cur, *p, *eq;
char *name, *value;
for (cur = semi + 1; cur < end; cur = p + 1) {
p = memchr (cur, ';', end - cur);
if (!p)
p = end;
eq = memchr (cur, '=', p - cur);
if (eq) {
name = g_strndup (cur, eq - cur);
value = g_strndup (eq + 1, p - (eq + 1));
uri_decode (value);
} else {
name = g_strndup (cur, p - cur);
value = g_strdup ("");
}
uri_decode (name);
g_datalist_set_data_full (&uri->params, name,
value, g_free);
g_free (name);
}
}
end = semi;
}
if (end != uri_string) {
uri->path = g_strndup (uri_string, end - uri_string);
uri_decode (uri->path);
}
return uri;
}
void
e_uri_free (EUri *uri)
{
if (uri) {
g_free (uri->protocol);
g_free (uri->user);
g_free (uri->authmech);
g_free (uri->passwd);
g_free (uri->host);
g_free (uri->path);
g_datalist_clear (&uri->params);
g_free (uri->query);
g_free (uri->fragment);
g_free (uri);
}
}
const char *
e_uri_get_param (EUri *uri, const char *name)
{
return g_datalist_get_data (&uri->params, name);
}
static void
copy_param_cb (GQuark key_id, gpointer data, gpointer user_data)
{
GData *params = (GData *) user_data;
g_datalist_id_set_data_full (¶ms, key_id, g_strdup (data), g_free);
}
EUri *
e_uri_copy (EUri *uri)
{
EUri *uri_copy;
g_return_val_if_fail (uri != NULL, NULL);
uri_copy = g_new0 (EUri, 1);
uri_copy->protocol = g_strdup (uri->protocol);
uri_copy->user = g_strdup (uri->user);
uri_copy->authmech = g_strdup (uri->authmech);
uri_copy->passwd = g_strdup (uri->passwd);
uri_copy->host = g_strdup (uri->host);
uri_copy->port = uri->port;
uri_copy->path = g_strdup (uri->path);
uri_copy->query = g_strdup (uri->query);
uri_copy->fragment = g_strdup (uri->fragment);
/* copy uri->params */
g_datalist_foreach (&uri->params,
(GDataForeachFunc) copy_param_cb,
&uri_copy->params);
return uri_copy;
}
char *
e_uri_to_string (EUri *uri, gboolean show_password)
{
char *str_uri = NULL;
g_return_val_if_fail (uri != NULL, NULL);
if (uri->port != 0)
str_uri = g_strdup_printf (
"%s://%s%s%s%s%s%s%s:%d%s%s%s",
uri->protocol,
uri->user ? uri->user : "",
uri->authmech ? ";auth=" : "",
uri->authmech ? uri->authmech : "",
uri->passwd && show_password ? ":" : "",
uri->passwd && show_password ? uri->passwd : "",
uri->user ? "@" : "",
uri->host ? uri->host : "",
uri->port,
uri->path ? uri->path : "",
uri->query ? "?" : "",
uri->query ? uri->query : "");
else
str_uri = g_strdup_printf(
"%s://%s%s%s%s%s%s%s%s%s%s",
uri->protocol,
uri->user ? uri->user : "",
uri->authmech ? ";auth=" : "",
uri->authmech ? uri->authmech : "",
uri->passwd && show_password ? ":" : "",
uri->passwd && show_password ? uri->passwd : "",
uri->user ? "@" : "",
uri->host ? uri->host : "",
uri->path ? uri->path : "",
uri->query ? "?" : "",
uri->query ? uri->query : "");
return str_uri;
}
|