Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / libedataserver / e-url.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4  * e-url.c
5  *
6  * Copyright (C) 2001 Ximian, Inc.
7  *
8  * Developed by Jon Trowbridge <trow@ximian.com>
9  *              Rodrigo Moya   <rodrigo@ximian.com>
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of version 2 of the GNU Lesser General Public
15  * License as published by the Free Software Foundation.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25  * USA.
26  */
27
28 #include <config.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "e-url.h"
33
34 /**
35  * e_url_shroud:
36  * @url: The url to shroud.
37  *
38  * Removes the moniker (i.e. mailto:) from a url.
39  *
40  * Returns: The newly-allocated shrouded url.
41  **/
42 char *
43 e_url_shroud (const char *url)
44 {
45         const char *first_colon = NULL;
46         const char *last_at = NULL;
47         const char *p;
48         char *shrouded;
49         
50         if (url == NULL)
51                 return NULL;
52
53         /* Skip past the moniker */
54         for (p = url; *p && *p != ':'; ++p);
55         if (*p)
56                 ++p;
57
58         while (*p) {
59                 if (first_colon == NULL && *p == ':')
60                         first_colon = p;
61                 if (*p == '@')
62                         last_at = p;
63                 ++p;
64         }
65
66         if (first_colon && last_at && first_colon < last_at) {
67                 shrouded = g_malloc(first_colon - url + strlen(last_at)+1);
68                 memcpy(shrouded, url, first_colon-url);
69                 strcpy(shrouded + (first_colon-url), last_at);
70         } else {
71                 shrouded = g_strdup (url);
72         }
73
74         return shrouded;
75 }
76
77 /**
78  * e_url_equal:
79  * @url1: The first url to compare.
80  * @url2: The second url to compare.
81  *
82  * Checks two urls for equality, after first removing any monikers on
83  * the urls.
84  *
85  * Returns: %TRUE if the urls are equal, %FALSE if they are not.
86  **/
87 gboolean
88 e_url_equal (const char *url1, const char *url2)
89 {
90         char *shroud1 = e_url_shroud (url1);
91         char *shroud2 = e_url_shroud (url2);
92         gint len1, len2;
93         gboolean rv;
94
95         if (shroud1 == NULL || shroud2 == NULL) {
96                 rv = (shroud1 == shroud2);
97         } else {
98                 len1 = strlen (shroud1);
99                 len2 = strlen (shroud2);
100
101                 rv = !strncmp (shroud1, shroud2, MIN (len1, len2));
102         }
103
104         g_free (shroud1);
105         g_free (shroud2);
106
107         return rv;
108 }
109
110 #define HEXVAL(c) (isdigit (c) ? (c) - '0' : tolower (c) - 'a' + 10)
111
112 static void
113 uri_decode (char *part)
114 {
115         guchar *s, *d;
116
117         s = d = (guchar *)part;
118         while (*s) {
119                 if (*s == '%') {
120                         if (isxdigit (s[1]) && isxdigit (s[2])) {
121                                 *d++ = HEXVAL (s[1]) * 16 + HEXVAL (s[2]);
122                                 s += 3;
123                         } else
124                                 *d++ = *s++;
125                 } else
126                         *d++ = *s++;
127         }
128         *d = '\0';
129 }
130
131 /**
132  * e_uri_new:
133  * @uri_string: The uri to represent as an #EUri.
134  *
135  * Creates an #EUri representation of the uri given in @uri_string.
136  *
137  * Returns: The newly-allocated #EUri structure.
138  **/
139 EUri *
140 e_uri_new (const char *uri_string)
141 {
142         EUri *uri;
143         const char *end, *hash, *colon, *semi, *at, *slash, *question;
144         const char *p;
145
146         if (!uri_string)
147                 return NULL;
148
149         uri = g_new0 (EUri, 1);
150
151         /* find fragment */
152         end = hash = strchr (uri_string, '#');
153         if (hash && hash[1]) {
154                 uri->fragment = g_strdup (hash + 1);
155                 uri_decode (uri->fragment);
156         }
157         else
158                 end = uri_string + strlen (uri_string);
159
160         /* find protocol: initial [a-z+.-]* substring until ":" */
161         p = uri_string;
162         while (p < end && (isalnum ((unsigned char) *p) ||
163                            *p == '.' || *p == '+' || *p == '-'))
164                 p++;
165
166         if (p > uri_string && *p == ':') {
167                 uri->protocol = g_ascii_strdown (uri_string, p - uri_string);
168                 uri_string = p + 1;
169         }
170         else
171                 uri->protocol = g_strdup ("file");
172
173         if (!*uri_string)
174                 return uri;
175
176         /* check for authority */
177         if (strncmp (uri_string, "//", 2) == 0) {
178                 uri_string += 2;
179
180                 slash = uri_string + strcspn (uri_string, "/#");
181                 at = strchr (uri_string, '@');
182                 if (at && at < slash) {
183                         colon = strchr (uri_string, ':');
184                         if (colon && colon < at) {
185                                 uri->passwd = g_strndup (colon + 1, at - colon - 1);
186                                 uri_decode (uri->passwd);
187                         }
188                         else {
189                                 uri->passwd = NULL;
190                                 colon = at;
191                         }
192
193                         semi = strchr (uri_string, ';');
194                         if (semi && semi < colon &&
195                             !g_ascii_strncasecmp (semi, ";auth=", 6)) {
196                                 uri->authmech = g_strndup (semi + 6, colon - semi - 6);
197                                 uri_decode (uri->authmech);
198                         }
199                         else {
200                                 uri->authmech = NULL;
201                                 semi = colon;
202                         }
203
204                         uri->user = g_strndup (uri_string, semi - uri_string);
205                         uri_decode (uri->user);
206                         uri_string = at + 1;
207                 }
208                 else
209                         uri->user = uri->passwd = uri->authmech = NULL;
210
211                 /* find host and port */
212                 colon = strchr (uri_string, ':');
213                 if (colon && colon < slash) {
214                         uri->host = g_strndup (uri_string, colon - uri_string);
215                         uri->port = strtoul (colon + 1, NULL, 10);
216                 }
217                 else {
218                         uri->host = g_strndup (uri_string, slash - uri_string);
219                         uri_decode (uri->host);
220                         uri->port = 0;
221                 }
222
223                 uri_string = slash;
224         }
225
226         /* find query */
227         question = memchr (uri_string, '?', end - uri_string);
228         if (question) {
229                 if (question[1]) {
230                         uri->query = g_strndup (question + 1, end - (question + 1));
231                         uri_decode (uri->query);
232                 }
233                 end = question;
234         }
235
236         /* find parameters */
237         semi = memchr (uri_string, ';', end - uri_string);
238         if (semi) {
239                 if (semi[1]) {
240                         const char *cur, *ptr, *eq;
241                         char *name, *value;
242
243                         for (cur = semi + 1; cur < end; cur = ptr + 1) {
244                                 ptr = memchr (cur, ';', end - cur);
245                                 if (!ptr)
246                                         ptr = end;
247                                 eq = memchr (cur, '=', ptr - cur);
248                                 if (eq) {
249                                         name = g_strndup (cur, eq - cur);
250                                         value = g_strndup (eq + 1, ptr - (eq + 1));
251                                         uri_decode (value);
252                                 } else {
253                                         name = g_strndup (cur, ptr - cur);
254                                         value = g_strdup ("");
255                                 }
256                                 uri_decode (name);
257                                 g_datalist_set_data_full (&uri->params, name,
258                                                           value, g_free);
259                                 g_free (name);
260                         }
261                 }
262                 end = semi;
263         }
264
265         if (end != uri_string) {
266                 uri->path = g_strndup (uri_string, end - uri_string);
267                 uri_decode (uri->path);
268         }
269
270         return uri;
271 }
272
273 /**
274  * e_uri_free:
275  * @uri: A pointer to the #EUri to free.
276  *
277  * Frees the memory of an #EUri structure.
278  **/
279 void
280 e_uri_free (EUri *uri)
281 {
282         if (uri) {
283                 g_free (uri->protocol);
284                 g_free (uri->user);
285                 g_free (uri->authmech);
286                 g_free (uri->passwd);
287                 g_free (uri->host);
288                 g_free (uri->path);
289                 g_datalist_clear (&uri->params);
290                 g_free (uri->query);
291                 g_free (uri->fragment);
292
293                 g_free (uri);
294         }
295 }
296
297 /**
298  * e_uri_get_param:
299  * @uri: The #EUri to get the parameter from.
300  * @name: The name of the parameter to get.
301  *
302  * Retrieves the value of the parameter associated with @name in @uri.
303  *
304  * Returns: The value of the parameter.
305  **/
306 const char *
307 e_uri_get_param (EUri *uri, const char *name)
308 {
309         return g_datalist_get_data (&uri->params, name);
310 }
311
312 static void
313 copy_param_cb (GQuark key_id, gpointer data, gpointer user_data)
314 {
315         GData *params = (GData *) user_data;
316
317         g_datalist_id_set_data_full (&params, key_id, g_strdup (data), g_free);
318 }
319
320 /**
321  * e_uri_copy:
322  * @uri: The #EUri to copy.
323  *
324  * Makes a copy of @uri.
325  *
326  * Returns: The newly-allocated copy of @uri.
327  **/
328 EUri *
329 e_uri_copy (EUri *uri)
330 {
331         EUri *uri_copy;
332
333         g_return_val_if_fail (uri != NULL, NULL);
334
335         uri_copy = g_new0 (EUri, 1);
336         uri_copy->protocol = g_strdup (uri->protocol);
337         uri_copy->user = g_strdup (uri->user);
338         uri_copy->authmech = g_strdup (uri->authmech);
339         uri_copy->passwd = g_strdup (uri->passwd);
340         uri_copy->host = g_strdup (uri->host);
341         uri_copy->port = uri->port;
342         uri_copy->path = g_strdup (uri->path);
343         uri_copy->query = g_strdup (uri->query);
344         uri_copy->fragment = g_strdup (uri->fragment);
345
346         /* copy uri->params */
347         g_datalist_foreach (&uri->params,
348                             (GDataForeachFunc) copy_param_cb,
349                             &uri_copy->params);
350
351         return uri_copy;
352 }
353
354 /**
355  * e_uri_to_string:
356  * @uri: The #EUri to convert to a string.
357  * @show_password: Whether or not to show the password in the string.
358  *
359  * Creates a string representation of @uri. The password will only be
360  * included in the string if @show_password is set to %TRUE.
361  *
362  * Returns: The string representation of @uri.
363  **/
364 char *
365 e_uri_to_string (EUri *uri, gboolean show_password)
366 {
367         char *str_uri = NULL;
368
369         g_return_val_if_fail (uri != NULL, NULL);
370
371         if (uri->port != 0)
372                 str_uri = g_strdup_printf (
373                         "%s://%s%s%s%s%s%s%s:%d%s%s%s",
374                         uri->protocol,
375                         uri->user ? uri->user : "",
376                         uri->authmech ? ";auth=" : "",
377                         uri->authmech ? uri->authmech : "",
378                         uri->passwd && show_password ? ":" : "",
379                         uri->passwd && show_password ? uri->passwd : "",
380                         uri->user ? "@" : "",
381                         uri->host ? uri->host : "",
382                         uri->port,
383                         uri->path ? uri->path : "",
384                         uri->query ? "?" : "",
385                         uri->query ? uri->query : "");
386         else
387                 str_uri = g_strdup_printf(
388                         "%s://%s%s%s%s%s%s%s%s%s%s",
389                         uri->protocol,
390                         uri->user ? uri->user : "",
391                         uri->authmech ? ";auth=" : "",
392                         uri->authmech ? uri->authmech : "",
393                         uri->passwd && show_password ? ":" : "",
394                         uri->passwd && show_password ? uri->passwd : "",
395                         uri->user ? "@" : "",
396                         uri->host ? uri->host : "",
397                         uri->path ? uri->path : "",
398                         uri->query ? "?" : "",
399                         uri->query ? uri->query : "");
400
401         return str_uri;
402 }