Mercurial > audlegacy
annotate Plugins/Input/wma/libffwma/uri.c @ 1458:f12d7e208b43 trunk
[svn] Update FSF address in copyright notices. Update autotools templates.
author | chainsaw |
---|---|
date | Wed, 02 Aug 2006 15:44:07 -0700 |
parents | 93c0af9bb521 |
children | 705d4c089fce |
rev | line source |
---|---|
1410 | 1 /* GNet - Networking library |
2 * Copyright (C) 2000-2003 David Helder, David Bolcsfoldi, Eric Williams | |
3 * | |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public | |
15 * License along with this library; if not, write to the | |
1458
f12d7e208b43
[svn] Update FSF address in copyright notices. Update autotools templates.
chainsaw
parents:
1410
diff
changeset
|
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
1410 | 17 * Boston, MA 02111-1307, USA. |
18 */ | |
19 | |
20 /* FIXME: #include "gnet-private.h" */ | |
21 #include <glib.h> | |
22 #include <string.h> | |
23 #include <malloc.h> | |
24 #include <ctype.h> | |
25 | |
26 #include "uri.h" | |
27 #include <string.h> | |
28 | |
29 static void field_unescape (char *str); | |
30 static char* field_escape (char* str, unsigned char mask); | |
31 | |
32 #define USERINFO_ESCAPE_MASK 0x01 | |
33 #define PATH_ESCAPE_MASK 0x02 | |
34 #define QUERY_ESCAPE_MASK 0x04 | |
35 #define FRAGMENT_ESCAPE_MASK 0x08 | |
36 | |
37 /* #define FALSE 0 */ | |
38 /* #define TRUE (!FALSE) */ | |
39 | |
40 static unsigned char neednt_escape_table[] = | |
41 { | |
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
46 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0x0f, 0x0f, | |
47 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, | |
48 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
49 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x0f, 0x00, 0x0c, | |
50 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
51 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
52 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
53 0x0f, 0x0f, 0x0f, 0x00, 0x0f, 0x00, 0x00, 0x0f, | |
54 0x00, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
55 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
56 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, | |
57 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x0f, 0x00, | |
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
74 }; | |
75 | |
76 | |
77 /* | |
78 Perl code to generate above table: | |
79 | |
80 #!/usr/bin/perl | |
81 | |
82 $ok = "abcdefghijklmnopqrstuvwxyz" . | |
83 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . | |
84 "0123456789" . | |
85 "-_.!~*'()"; | |
86 $userinfo_ok = ';:&=+\$,'; | |
87 $path_ok = ':\@&=+\$,;/'; | |
88 $query_ok = ';/?:\@&=+\$,'; | |
89 $fragment_ok = ';/?:\@&=+\$,'; | |
90 | |
91 for ($i = 0; $i < 32; $i++) | |
92 { | |
93 print " "; | |
94 for ($j = 0; $j < 8; $j++) | |
95 { | |
96 $num = 0; | |
97 $letter = chr(($i * 8) + $j); | |
98 | |
99 $num |= 0b0001 if (index($userinfo_ok, $letter) != -1); | |
100 $num |= 0b0010 if (index($path_ok, $letter) != -1); | |
101 $num |= 0b0100 if (index($query_ok, $letter) != -1); | |
102 $num |= 0b1000 if (index($fragment_ok, $letter) != -1); | |
103 $num |= 0b1111 if (index($ok, $letter) != -1); | |
104 | |
105 printf "0x%02x, ", $num; | |
106 } | |
107 print "\n"; | |
108 } | |
109 */ | |
110 | |
111 | |
112 /* our own ISSPACE. ANSI isspace is local dependent */ | |
113 #define ISSPACE(C) (((C) >= 9 && (C) <= 13) || (C) == ' ') | |
114 | |
115 | |
116 static int split_user_passwd(const char* in, char** user, char** passwd) | |
117 { | |
118 char *tmp = strdup(in); | |
119 | |
120 if(!tmp) | |
121 return 0; | |
122 *passwd = strchr(tmp, ':'); | |
123 if(!(*passwd)) | |
124 { | |
125 free(tmp); | |
126 return 0; | |
127 } | |
128 *((*passwd)++) = '\0'; // don't you love C? :) | |
129 | |
130 *user = strdup(tmp); | |
131 if(!*user) | |
132 return 0; | |
133 *passwd = strdup(*passwd); | |
134 if(!*passwd) | |
135 return 0; | |
136 | |
137 free(tmp); | |
138 return 1; | |
139 } | |
140 | |
141 /** | |
142 * gnet_uri_new | |
143 * @uri: URI string | |
144 * | |
145 * Creates a #GURI from a string. Empty fields are set to NULL. The | |
146 * parser does not validate the URI -- it will accept some malformed | |
147 * URI. URIs are usually in the form | |
148 * scheme://userinfo@hostname:port/path?query#fragment | |
149 * | |
150 * URIs created from user input are typically unescaped. URIs | |
151 * created from machine input (e.g. received over the internet) are | |
152 * typically escaped. | |
153 * | |
154 * Returns: a new #GURI, or NULL if there was a failure. | |
155 * | |
156 **/ | |
157 GURI* | |
158 gnet_uri_new (const char* uri) | |
159 { | |
160 GURI* guri = NULL; | |
161 const char* p; | |
162 const char* temp; | |
163 | |
164 g_return_val_if_fail (uri, NULL); | |
165 | |
166 /* Skip initial whitespace */ | |
167 p = uri; | |
168 while (*p && ISSPACE((int)*p)) | |
169 ++p; | |
170 if (!*p) /* Error if it's just a string of space */ | |
171 return NULL; | |
172 | |
173 guri = g_new0 (GURI, 1); | |
174 | |
175 /* Scheme */ | |
176 temp = p; | |
177 while (*p && *p != ':' && *p != '/' && *p != '?' && *p != '#') | |
178 ++p; | |
179 if (*p == ':') | |
180 { | |
181 guri->scheme = g_strndup (temp, p - temp); | |
182 ++p; | |
183 } | |
184 else /* This char is NUL, /, ?, or # */ | |
185 p = temp; | |
186 | |
187 /* Authority */ | |
188 if (*p == '/' && p[1] == '/') | |
189 { | |
190 char *userinfo; | |
191 p += 2; | |
192 | |
193 /* Userinfo */ | |
194 temp = p; | |
195 while (*p && *p != '@' && *p != '/' ) /* Look for @ or / */ | |
196 ++p; | |
197 if (*p == '@') /* Found userinfo */ | |
198 { | |
199 userinfo = g_strndup (temp, p - temp); | |
200 if(!split_user_passwd(userinfo, &guri->user, &guri->passwd)) | |
201 { | |
202 free(userinfo); | |
203 goto error; | |
204 } | |
205 free(userinfo); | |
206 ++p; | |
207 } | |
208 else | |
209 p = temp; | |
210 | |
211 /* Hostname */ | |
212 | |
213 /* Check for IPv6 canonical hostname in brackets */ | |
214 if (*p == '[') | |
215 { | |
216 p++; /* Skip [ */ | |
217 temp = p; | |
218 while (*p && *p != ']') ++p; | |
219 if ((p - temp) == 0) | |
220 goto error; | |
221 guri->hostname = g_strndup (temp, p - temp); | |
222 if (*p) | |
223 p++; /* Skip ] (if there) */ | |
224 } | |
225 else | |
226 { | |
227 temp = p; | |
228 while (*p && *p != '/' && *p != '?' && *p != '#' && *p != ':') ++p; | |
229 if ((p - temp) == 0) | |
230 goto error; | |
231 guri->hostname = g_strndup (temp, p - temp); | |
232 } | |
233 | |
234 /* Port */ | |
235 if (*p == ':') | |
236 { | |
237 for (++p; isdigit((int)*p); ++p) | |
238 guri->port = guri->port * 10 + (*p - '0'); | |
239 } | |
240 | |
241 } | |
242 | |
243 /* Path (we are liberal and won't check if it starts with /) */ | |
244 temp = p; | |
245 while (*p && *p != '?' && *p != '#') | |
246 ++p; | |
247 if (p != temp) | |
248 guri->path = g_strndup(temp, p - temp); | |
249 | |
250 /* Query */ | |
251 if (*p == '?') | |
252 { | |
253 temp = p + 1; | |
254 while (*p && *p != '#') | |
255 ++p; | |
256 guri->query = g_strndup (temp, p - temp); | |
257 } | |
258 | |
259 /* Fragment */ | |
260 if (*p == '#') | |
261 { | |
262 ++p; | |
263 guri->fragment = g_strdup (p); | |
264 } | |
265 | |
266 return guri; | |
267 | |
268 error: | |
269 gnet_uri_delete (guri); | |
270 return NULL; | |
271 } | |
272 | |
273 | |
274 /** | |
275 * gnet_uri_new_fields | |
276 * @scheme: scheme | |
277 * @hostname: host name | |
278 * @port: port | |
279 * @path: path | |
280 * | |
281 * Creates a #GURI from the fields. This function uses the most | |
282 * common fields. Use gnet_uri_new_fields_all() to specify all | |
283 * fields. | |
284 * | |
285 * Returns: a new #GURI. | |
286 * | |
287 **/ | |
288 GURI* | |
289 gnet_uri_new_fields (const char* scheme, const char* hostname, | |
290 const gint port, const char* path) | |
291 { | |
292 GURI* uri = NULL; | |
293 | |
294 uri = g_new0 (GURI, 1); | |
295 if (scheme) uri->scheme = g_strdup (scheme); | |
296 if (hostname) uri->hostname = g_strdup (hostname); | |
297 uri->port = port; | |
298 if (path) uri->path = g_strdup (path); | |
299 | |
300 return uri; | |
301 } | |
302 | |
303 | |
304 /** | |
305 * gnet_uri_new_fields_all | |
306 * @scheme: scheme | |
307 * @userinfo: user info | |
308 * @hostname: host name | |
309 * @port: port | |
310 * @path: path | |
311 * @query: query | |
312 * @fragment: fragment | |
313 * | |
314 * Creates a #GURI from all fields. | |
315 * | |
316 * Returns: a new #GURI. | |
317 * | |
318 **/ | |
319 GURI* | |
320 gnet_uri_new_fields_all (const char* scheme, const char* user, | |
321 const char* passwd, const char* hostname, | |
322 const gint port, const char* path, | |
323 const char* query, const char* fragment) | |
324 { | |
325 GURI* uri = NULL; | |
326 | |
327 uri = g_new0 (GURI, 1); | |
328 if (scheme) uri->scheme = g_strdup (scheme); | |
329 if (user) uri->user = g_strdup (user); | |
330 if (passwd) uri->passwd = g_strdup (passwd); | |
331 if (hostname) uri->hostname = g_strdup (hostname); | |
332 uri->port = port; | |
333 if (path) uri->path = g_strdup (path); | |
334 if (query) uri->query = g_strdup (query); | |
335 if (fragment) uri->fragment = g_strdup (fragment); | |
336 | |
337 return uri; | |
338 } | |
339 | |
340 | |
341 /** | |
342 * gnet_uri_clone: | |
343 * @uri: a #GURI | |
344 * | |
345 * Copies a #GURI. | |
346 * | |
347 * Returns: a copy of @uri. | |
348 * | |
349 **/ | |
350 GURI* | |
351 gnet_uri_clone (const GURI* uri) | |
352 { | |
353 GURI* uri2; | |
354 | |
355 g_return_val_if_fail (uri, NULL); | |
356 | |
357 uri2 = g_new0 (GURI, 1); | |
358 uri2->scheme = g_strdup (uri->scheme); | |
359 uri2->user = g_strdup (uri->user); | |
360 uri2->passwd = g_strdup (uri->passwd); | |
361 uri2->hostname = g_strdup (uri->hostname); | |
362 uri2->port = uri->port; | |
363 uri2->path = g_strdup (uri->path); | |
364 uri2->query = g_strdup (uri->query); | |
365 uri2->fragment = g_strdup (uri->fragment); | |
366 | |
367 return uri2; | |
368 } | |
369 | |
370 | |
371 /** | |
372 * gnet_uri_delete: | |
373 * @uri: a #GURI | |
374 * | |
375 * Deletes a #GURI. | |
376 * | |
377 **/ | |
378 void | |
379 gnet_uri_delete (GURI* uri) | |
380 { | |
381 if (uri) | |
382 { | |
383 g_free (uri->scheme); | |
384 g_free (uri->user); | |
385 g_free (uri->passwd); | |
386 g_free (uri->hostname); | |
387 g_free (uri->path); | |
388 g_free (uri->query); | |
389 g_free (uri->fragment); | |
390 g_free (uri); | |
391 } | |
392 } | |
393 | |
394 | |
395 | |
396 | |
397 #define SAFESTRCMP(A,B) (((A)&&(B))?(strcmp((A),(B))):((A)||(B))) | |
398 | |
399 /** | |
400 * gnet_uri_equal | |
401 * @p1: a #GURI | |
402 * @p2: another #GURI | |
403 * | |
404 * Compares two #GURI's for equality. | |
405 * | |
406 * Returns: TRUE if they are equal; FALSE otherwise. | |
407 * | |
408 **/ | |
409 int | |
410 gnet_uri_equal (gconstpointer p1, gconstpointer p2) | |
411 { | |
412 const GURI* uri1 = (const GURI*) p1; | |
413 const GURI* uri2 = (const GURI*) p2; | |
414 | |
415 g_return_val_if_fail (uri1, FALSE); | |
416 g_return_val_if_fail (uri2, FALSE); | |
417 | |
418 if (uri1->port == uri2->port && | |
419 !SAFESTRCMP(uri1->scheme, uri2->scheme) && | |
420 !SAFESTRCMP(uri1->user, uri2->user) && | |
421 !SAFESTRCMP(uri1->passwd, uri2->passwd) && | |
422 !SAFESTRCMP(uri1->hostname, uri2->hostname) && | |
423 !SAFESTRCMP(uri1->path, uri2->path) && | |
424 !SAFESTRCMP(uri1->query, uri2->query) && | |
425 !SAFESTRCMP(uri1->fragment, uri2->fragment)) | |
426 return TRUE; | |
427 | |
428 return FALSE; | |
429 } | |
430 | |
431 | |
432 /** | |
433 * gnet_uri_hash | |
434 * @p: a #GURI | |
435 * | |
436 * Creates a hash code for @p for use with GHashTable. | |
437 * | |
438 * Returns: hash code for @p. | |
439 * | |
440 **/ | |
441 guint | |
442 gnet_uri_hash (gconstpointer p) | |
443 { | |
444 const GURI* uri = (const GURI*) p; | |
445 guint h = 0; | |
446 | |
447 g_return_val_if_fail (uri, 0); | |
448 | |
449 if (uri->scheme) h = g_str_hash (uri->scheme); | |
450 if (uri->user) h ^= g_str_hash (uri->user); | |
451 if (uri->passwd) h ^= g_str_hash (uri->passwd); | |
452 if (uri->hostname) h ^= g_str_hash (uri->hostname); | |
453 h ^= uri->port; | |
454 if (uri->path) h ^= g_str_hash (uri->path); | |
455 if (uri->query) h ^= g_str_hash (uri->query); | |
456 if (uri->fragment) h ^= g_str_hash (uri->fragment); | |
457 | |
458 return h; | |
459 } | |
460 | |
461 | |
462 /** | |
463 * gnet_uri_escape | |
464 * @uri: a #GURI | |
465 * | |
466 * Escapes the fields in a #GURI. Network protocols use escaped | |
467 * URIs. People use unescaped URIs. | |
468 * | |
469 **/ | |
470 void | |
471 gnet_uri_escape (GURI* uri) | |
472 { | |
473 g_return_if_fail (uri); | |
474 | |
475 uri->user = field_escape (uri->user, USERINFO_ESCAPE_MASK); | |
476 uri->passwd = field_escape (uri->passwd, USERINFO_ESCAPE_MASK); | |
477 uri->path = field_escape (uri->path, PATH_ESCAPE_MASK); | |
478 uri->query = field_escape (uri->query, QUERY_ESCAPE_MASK); | |
479 uri->fragment = field_escape (uri->fragment, FRAGMENT_ESCAPE_MASK); | |
480 } | |
481 | |
482 | |
483 /** | |
484 * gnet_uri_unescape | |
485 * @uri: a #GURI | |
486 * | |
487 * Unescapes the fields in the URI. Network protocols use escaped | |
488 * URIs. People use unescaped URIs. | |
489 * | |
490 **/ | |
491 void | |
492 gnet_uri_unescape (GURI* uri) | |
493 { | |
494 g_return_if_fail (uri); | |
495 | |
496 if (uri->user) | |
497 field_unescape (uri->user); | |
498 if (uri->passwd) | |
499 field_unescape (uri->passwd); | |
500 if (uri->path) | |
501 field_unescape (uri->path); | |
502 if (uri->query) | |
503 field_unescape (uri->query); | |
504 if (uri->fragment) | |
505 field_unescape (uri->fragment); | |
506 } | |
507 | |
508 | |
509 static char* | |
510 field_escape (char* str, unsigned char mask) | |
511 { | |
512 gint len; | |
513 gint i; | |
514 int must_escape = FALSE; | |
515 char* dst; | |
516 gint j; | |
517 | |
518 if (str == NULL) | |
519 return NULL; | |
520 | |
521 /* Roughly calculate buffer size */ | |
522 len = 0; | |
523 for (i = 0; str[i]; i++) | |
524 { | |
525 if (neednt_escape_table[(guint) str[i]] & mask) | |
526 len++; | |
527 else | |
528 { | |
529 len += 3; | |
530 must_escape = TRUE; | |
531 } | |
532 } | |
533 | |
534 /* Don't escape if unnecessary */ | |
535 if (must_escape == FALSE) | |
536 return str; | |
537 | |
538 /* Allocate buffer */ | |
539 dst = (char*) g_malloc(len + 1); | |
540 | |
541 /* Copy */ | |
542 for (i = j = 0; str[i]; i++, j++) | |
543 { | |
544 /* Unescaped character */ | |
545 if (neednt_escape_table[(guint) str[i]] & mask) | |
546 { | |
547 dst[j] = str[i]; | |
548 } | |
549 | |
550 /* Escaped character */ | |
551 else | |
552 { | |
553 dst[j] = '%'; | |
554 | |
555 if (((str[i] & 0xf0) >> 4) < 10) | |
556 dst[j+1] = ((str[i] & 0xf0) >> 4) + '0'; | |
557 else | |
558 dst[j+1] = ((str[i] & 0xf0) >> 4) + 'a' - 10; | |
559 | |
560 if ((str[i] & 0x0f) < 10) | |
561 dst[j+2] = (str[i] & 0x0f) + '0'; | |
562 else | |
563 dst[j+2] = (str[i] & 0x0f) + 'a' - 10; | |
564 | |
565 j += 2; /* and j is incremented in loop too */ | |
566 } | |
567 } | |
568 dst[j] = '\0'; | |
569 | |
570 g_free (str); | |
571 return dst; | |
572 } | |
573 | |
574 | |
575 | |
576 static void | |
577 field_unescape (char* s) | |
578 { | |
579 char* src; | |
580 char* dst; | |
581 | |
582 for (src = dst = s; *src; ++src, ++dst) | |
583 { | |
584 if (src[0] == '%' && src[1] != '\0' && src[2] != '\0') | |
585 { | |
586 gint high, low; | |
587 | |
588 if ('a' <= src[1] && src[1] <= 'f') | |
589 high = src[1] - 'a' + 10; | |
590 else if ('A' <= src[1] && src[1] <= 'F') | |
591 high = src[1] - 'A' + 10; | |
592 else if ('0' <= src[1] && src[1] <= '9') | |
593 high = src[1] - '0'; | |
594 else /* malformed */ | |
595 goto regular_copy; | |
596 | |
597 if ('a' <= src[2] && src[2] <= 'f') | |
598 low = src[2] - 'a' + 10; | |
599 else if ('A' <= src[2] && src[2] <= 'F') | |
600 low = src[2] - 'A' + 10; | |
601 else if ('0' <= src[2] && src[2] <= '9') | |
602 low = src[2] - '0'; | |
603 else /* malformed */ | |
604 goto regular_copy; | |
605 | |
606 *dst = (char)((high << 4) + low); | |
607 src += 2; | |
608 } | |
609 else | |
610 { | |
611 regular_copy: | |
612 *dst = *src; | |
613 } | |
614 } | |
615 | |
616 *dst = '\0'; | |
617 } | |
618 | |
619 | |
620 | |
621 /** | |
622 * gnet_uri_get_string | |
623 * @uri: a #GURI | |
624 * | |
625 * Gets a string representation of a #GURI. This function does not | |
626 * escape or unescape the fields first. Call gnet_uri_escape() or | |
627 * gnet_uri_unescape first if necessary. | |
628 * | |
629 * Returns: a string. | |
630 * | |
631 **/ | |
632 char* | |
633 gnet_uri_get_string (const GURI* uri) | |
634 { | |
635 char* rv = NULL; | |
636 GString* buffer = NULL; | |
637 | |
638 g_return_val_if_fail (uri, NULL); | |
639 | |
640 buffer = g_string_sized_new (16); | |
641 | |
642 if (uri->scheme) | |
643 g_string_sprintfa (buffer, "%s:", uri->scheme); | |
644 | |
645 if (uri->user || uri->passwd || uri->hostname || uri->port) | |
646 g_string_append (buffer, "//"); | |
647 | |
648 if (uri->user) | |
649 { | |
650 buffer = g_string_append (buffer, uri->user); | |
651 buffer = g_string_append_c (buffer, '@'); | |
652 } | |
653 if (uri->passwd) | |
654 { | |
655 buffer = g_string_append (buffer, uri->passwd); | |
656 buffer = g_string_append_c (buffer, '@'); | |
657 } | |
658 | |
659 /* Add brackets around the hostname if it's IPv6 */ | |
660 if (uri->hostname) | |
661 { | |
662 if (strchr(uri->hostname, ':') == NULL) | |
663 buffer = g_string_append (buffer, uri->hostname); | |
664 else | |
665 g_string_sprintfa (buffer, "[%s]", uri->hostname); | |
666 } | |
667 | |
668 if (uri->port) | |
669 g_string_sprintfa (buffer, ":%d", uri->port); | |
670 | |
671 if (uri->path) | |
672 { | |
673 if (*uri->path == '/' || | |
674 !(uri->user || uri->passwd || uri->hostname || uri->port)) | |
675 g_string_append (buffer, uri->path); | |
676 else | |
677 g_string_sprintfa (buffer, "/%s", uri->path); | |
678 } | |
679 | |
680 if (uri->query) | |
681 g_string_sprintfa (buffer, "?%s", uri->query); | |
682 | |
683 if (uri->fragment) | |
684 g_string_sprintfa (buffer, "#%s", uri->fragment); | |
685 | |
686 /* Free only GString not data contained, return the data instead */ | |
687 rv = buffer->str; | |
688 g_string_free (buffer, FALSE); | |
689 return rv; | |
690 } | |
691 | |
692 | |
693 /** | |
694 * gnet_uri_set_scheme | |
695 * @uri: a #GURI | |
696 * @scheme: scheme | |
697 * | |
698 * Sets a #GURI's scheme. | |
699 * | |
700 **/ | |
701 void | |
702 gnet_uri_set_scheme (GURI* uri, const char* scheme) | |
703 { | |
704 g_return_if_fail (uri); | |
705 | |
706 if (uri->scheme) | |
707 { | |
708 g_free (uri->scheme); | |
709 uri->scheme = NULL; | |
710 } | |
711 | |
712 if (scheme) | |
713 uri->scheme = g_strdup (scheme); | |
714 } | |
715 | |
716 | |
717 /** | |
718 * gnet_uri_set_userinfo | |
719 * @uri: a #GURI | |
720 * @userinfo: user info | |
721 * | |
722 * Sets a #GURI's user info. | |
723 * | |
724 **/ | |
725 void | |
726 gnet_uri_set_userinfo (GURI* uri, const char* user, const char* passwd) | |
727 { | |
728 g_return_if_fail (uri); | |
729 | |
730 if (uri->user) | |
731 { | |
732 g_free (uri->user); | |
733 uri->user = NULL; | |
734 } | |
735 if (uri->passwd) | |
736 { | |
737 g_free (uri->passwd); | |
738 uri->passwd = NULL; | |
739 } | |
740 | |
741 if (user) | |
742 uri->user = g_strdup (user); | |
743 if (passwd) | |
744 uri->passwd = g_strdup (passwd); | |
745 } | |
746 | |
747 | |
748 /** | |
749 * gnet_uri_set_hostname | |
750 * @uri: a #GURI | |
751 * @hostname: host name | |
752 * | |
753 * Sets a #GURI's host name. | |
754 * | |
755 **/ | |
756 void | |
757 gnet_uri_set_hostname (GURI* uri, const char* hostname) | |
758 { | |
759 g_return_if_fail (uri); | |
760 | |
761 if (uri->hostname) | |
762 { | |
763 g_free (uri->hostname); | |
764 uri->hostname = NULL; | |
765 } | |
766 | |
767 if (hostname) | |
768 uri->hostname = g_strdup (hostname); | |
769 } | |
770 | |
771 | |
772 /** | |
773 * gnet_uri_set_port | |
774 * @uri: a #GURI | |
775 * @port: port | |
776 * | |
777 * Set a #GURI's port. | |
778 * | |
779 **/ | |
780 void | |
781 gnet_uri_set_port (GURI* uri, gint port) | |
782 { | |
783 uri->port = port; | |
784 } | |
785 | |
786 | |
787 /** | |
788 * gnet_uri_set_path | |
789 * @uri: a #GURI | |
790 * @path: path | |
791 * | |
792 * Set a #GURI's path. | |
793 * | |
794 **/ | |
795 void | |
796 gnet_uri_set_path (GURI* uri, const char* path) | |
797 { | |
798 g_return_if_fail (uri); | |
799 | |
800 if (uri->path) | |
801 { | |
802 g_free (uri->path); | |
803 uri->path = NULL; | |
804 } | |
805 | |
806 if (path) | |
807 uri->path = g_strdup (path); | |
808 } | |
809 | |
810 | |
811 | |
812 /** | |
813 * gnet_uri_set_query | |
814 * @uri: a #GURI | |
815 * @query: query | |
816 * | |
817 * Set a #GURI's query. | |
818 * | |
819 **/ | |
820 void | |
821 gnet_uri_set_query (GURI* uri, const char* query) | |
822 { | |
823 g_return_if_fail (uri); | |
824 | |
825 if (uri->query) | |
826 { | |
827 g_free (uri->query); | |
828 uri->query = NULL; | |
829 } | |
830 | |
831 if (query) | |
832 uri->query = g_strdup (query); | |
833 } | |
834 | |
835 | |
836 /** | |
837 * gnet_uri_set_fragment | |
838 * @uri: a #GURI | |
839 * @fragment: fragment | |
840 * | |
841 * Set a #GURI's fragment. | |
842 * | |
843 **/ | |
844 void | |
845 gnet_uri_set_fragment (GURI* uri, const char* fragment) | |
846 { | |
847 g_return_if_fail (uri); | |
848 | |
849 if (uri->fragment) | |
850 { | |
851 g_free (uri->fragment); | |
852 uri->fragment = NULL; | |
853 } | |
854 | |
855 if (fragment) | |
856 uri->fragment = g_strdup (fragment); | |
857 } |