1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include "gdummyfile.h"
39 static void g_dummy_file_file_iface_init (GFileIface *iface);
45 int port; /* -1 => not in uri */
53 GObject parent_instance;
55 GDecodedUri *decoded_uri;
59 #define g_dummy_file_get_type _g_dummy_file_get_type
60 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
62 g_dummy_file_file_iface_init))
64 #define SUB_DELIM_CHARS "!$&'()*+,;="
66 static char * _g_encode_uri (GDecodedUri *decoded);
67 static void _g_decoded_uri_free (GDecodedUri *decoded);
68 static GDecodedUri *_g_decode_uri (const char *uri);
69 static GDecodedUri *_g_decoded_uri_new (void);
71 static char * unescape_string (const gchar *escaped_string,
72 const gchar *escaped_string_end,
73 const gchar *illegal_characters);
75 static void g_string_append_encoded (GString *string,
77 const char *reserved_chars_allowed);
80 g_dummy_file_finalize (GObject *object)
84 dummy = G_DUMMY_FILE (object);
86 if (dummy->decoded_uri)
87 _g_decoded_uri_free (dummy->decoded_uri);
89 g_free (dummy->text_uri);
91 G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
95 g_dummy_file_class_init (GDummyFileClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = g_dummy_file_finalize;
103 g_dummy_file_init (GDummyFile *dummy)
108 _g_dummy_file_new (const char *uri)
112 g_return_val_if_fail (uri != NULL, NULL);
114 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
115 dummy->text_uri = g_strdup (uri);
116 dummy->decoded_uri = _g_decode_uri (uri);
118 return G_FILE (dummy);
122 g_dummy_file_is_native (GFile *file)
128 g_dummy_file_get_basename (GFile *file)
130 GDummyFile *dummy = G_DUMMY_FILE (file);
132 if (dummy->decoded_uri)
133 return g_path_get_basename (dummy->decoded_uri->path);
134 return g_strdup (dummy->text_uri);
138 g_dummy_file_get_path (GFile *file)
144 g_dummy_file_get_uri (GFile *file)
146 return g_strdup (G_DUMMY_FILE (file)->text_uri);
150 g_dummy_file_get_parse_name (GFile *file)
152 return g_strdup (G_DUMMY_FILE (file)->text_uri);
156 g_dummy_file_get_parent (GFile *file)
158 GDummyFile *dummy = G_DUMMY_FILE (file);
162 GDecodedUri new_decoded_uri;
164 if (dummy->decoded_uri == NULL ||
165 g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
168 dirname = g_path_get_dirname (dummy->decoded_uri->path);
170 if (strcmp (dirname, ".") == 0)
176 new_decoded_uri = *dummy->decoded_uri;
177 new_decoded_uri.path = dirname;
178 uri = _g_encode_uri (&new_decoded_uri);
181 parent = _g_dummy_file_new (uri);
188 g_dummy_file_dup (GFile *file)
190 GDummyFile *dummy = G_DUMMY_FILE (file);
192 return _g_dummy_file_new (dummy->text_uri);
196 g_dummy_file_hash (GFile *file)
198 GDummyFile *dummy = G_DUMMY_FILE (file);
200 return g_str_hash (dummy->text_uri);
204 g_dummy_file_equal (GFile *file1,
207 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
208 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
210 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
214 safe_strcmp (const char *a,
222 return strcmp (a, b);
226 uri_same_except_path (GDecodedUri *a,
229 if (safe_strcmp (a->scheme, b->scheme) != 0)
231 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
233 if (safe_strcmp (a->host, b->host) != 0)
235 if (a->port != b->port)
242 match_prefix (const char *path,
247 prefix_len = strlen (prefix);
248 if (strncmp (path, prefix, prefix_len) != 0)
250 return path + prefix_len;
254 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
256 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
257 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
258 const char *remainder;
260 if (parent_dummy->decoded_uri != NULL &&
261 descendant_dummy->decoded_uri != NULL)
263 if (uri_same_except_path (parent_dummy->decoded_uri,
264 descendant_dummy->decoded_uri))
266 remainder = match_prefix (descendant_dummy->decoded_uri->path,
267 parent_dummy->decoded_uri->path);
268 if (remainder != NULL && *remainder == '/')
270 while (*remainder == '/')
279 remainder = match_prefix (descendant_dummy->text_uri,
280 parent_dummy->text_uri);
281 if (remainder != NULL && *remainder == '/')
283 while (*remainder == '/')
294 g_dummy_file_get_relative_path (GFile *parent,
297 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
298 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
299 const char *remainder;
301 if (parent_dummy->decoded_uri != NULL &&
302 descendant_dummy->decoded_uri != NULL)
304 if (uri_same_except_path (parent_dummy->decoded_uri,
305 descendant_dummy->decoded_uri))
307 remainder = match_prefix (descendant_dummy->decoded_uri->path,
308 parent_dummy->decoded_uri->path);
309 if (remainder != NULL && *remainder == '/')
311 while (*remainder == '/')
314 return g_strdup (remainder);
320 remainder = match_prefix (descendant_dummy->text_uri,
321 parent_dummy->text_uri);
322 if (remainder != NULL && *remainder == '/')
324 while (*remainder == '/')
327 return unescape_string (remainder, NULL, "/");
336 g_dummy_file_resolve_relative_path (GFile *file,
337 const char *relative_path)
339 GDummyFile *dummy = G_DUMMY_FILE (file);
342 GDecodedUri new_decoded_uri;
345 if (dummy->decoded_uri == NULL)
347 str = g_string_new (dummy->text_uri);
348 g_string_append (str, "/");
349 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
350 child = _g_dummy_file_new (str->str);
351 g_string_free (str, TRUE);
355 new_decoded_uri = *dummy->decoded_uri;
357 if (g_path_is_absolute (relative_path))
358 new_decoded_uri.path = g_strdup (relative_path);
360 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
362 uri = _g_encode_uri (&new_decoded_uri);
363 g_free (new_decoded_uri.path);
365 child = _g_dummy_file_new (uri);
373 g_dummy_file_get_child_for_display_name (GFile *file,
374 const char *display_name,
377 return g_file_get_child (file, display_name);
381 g_dummy_file_has_uri_scheme (GFile *file,
382 const char *uri_scheme)
384 GDummyFile *dummy = G_DUMMY_FILE (file);
386 if (dummy->decoded_uri)
387 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
392 g_dummy_file_get_uri_scheme (GFile *file)
394 GDummyFile *dummy = G_DUMMY_FILE (file);
396 if (dummy->decoded_uri)
397 return g_strdup (dummy->decoded_uri->scheme);
404 g_dummy_file_file_iface_init (GFileIface *iface)
406 iface->dup = g_dummy_file_dup;
407 iface->hash = g_dummy_file_hash;
408 iface->equal = g_dummy_file_equal;
409 iface->is_native = g_dummy_file_is_native;
410 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
411 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
412 iface->get_basename = g_dummy_file_get_basename;
413 iface->get_path = g_dummy_file_get_path;
414 iface->get_uri = g_dummy_file_get_uri;
415 iface->get_parse_name = g_dummy_file_get_parse_name;
416 iface->get_parent = g_dummy_file_get_parent;
417 iface->prefix_matches = g_dummy_file_prefix_matches;
418 iface->get_relative_path = g_dummy_file_get_relative_path;
419 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
420 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
422 iface->supports_thread_contexts = TRUE;
425 /* Uri handling helper functions: */
428 unescape_character (const char *scanner)
433 first_digit = g_ascii_xdigit_value (*scanner++);
437 second_digit = g_ascii_xdigit_value (*scanner++);
438 if (second_digit < 0)
441 return (first_digit << 4) | second_digit;
445 unescape_string (const gchar *escaped_string,
446 const gchar *escaped_string_end,
447 const gchar *illegal_characters)
453 if (escaped_string == NULL)
456 if (escaped_string_end == NULL)
457 escaped_string_end = escaped_string + strlen (escaped_string);
459 result = g_malloc (escaped_string_end - escaped_string + 1);
462 for (in = escaped_string; in < escaped_string_end; in++)
468 if (escaped_string_end - in < 2)
474 character = unescape_character (in);
476 /* Check for an illegal character. We consider '\0' illegal here. */
477 if (character <= 0 ||
478 (illegal_characters != NULL &&
479 strchr (illegal_characters, (char)character) != NULL))
484 in++; /* The other char will be eaten in the loop header */
486 *out++ = (char)character;
490 g_warn_if_fail (out - result <= strlen (escaped_string));
495 _g_decoded_uri_free (GDecodedUri *decoded)
500 g_free (decoded->scheme);
501 g_free (decoded->query);
502 g_free (decoded->fragment);
503 g_free (decoded->userinfo);
504 g_free (decoded->host);
505 g_free (decoded->path);
510 _g_decoded_uri_new (void)
514 uri = g_new0 (GDecodedUri, 1);
521 _g_decode_uri (const char *uri)
523 GDecodedUri *decoded;
524 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
528 /* From RFC 3986 Decodes:
529 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
535 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
538 if (!g_ascii_isalpha (*p))
548 if (!(g_ascii_isalnum(c) ||
555 decoded = _g_decoded_uri_new ();
557 decoded->scheme = g_malloc (p - uri);
558 out = decoded->scheme;
559 for (in = uri; in < p - 1; in++)
560 *out++ = g_ascii_tolower (*in);
565 query_start = strchr (p, '?');
568 hier_part_end = query_start++;
569 fragment_start = strchr (query_start, '#');
572 decoded->query = g_strndup (query_start, fragment_start - query_start);
573 decoded->fragment = g_strdup (fragment_start+1);
577 decoded->query = g_strdup (query_start);
578 decoded->fragment = NULL;
584 decoded->query = NULL;
585 fragment_start = strchr (p, '#');
588 hier_part_end = fragment_start++;
589 decoded->fragment = g_strdup (fragment_start);
593 hier_part_end = p + strlen (p);
594 decoded->fragment = NULL;
599 hier-part = "//" authority path-abempty
606 if (hier_part_start[0] == '/' &&
607 hier_part_start[1] == '/')
609 const char *authority_start, *authority_end;
610 const char *userinfo_start, *userinfo_end;
611 const char *host_start, *host_end;
612 const char *port_start;
614 authority_start = hier_part_start + 2;
615 /* authority is always followed by / or nothing */
616 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
617 if (authority_end == NULL)
618 authority_end = hier_part_end;
621 authority = [ userinfo "@" ] host [ ":" port ]
624 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
627 userinfo_start = authority_start;
628 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
629 if (decoded->userinfo == NULL)
631 _g_decoded_uri_free (decoded);
634 host_start = userinfo_end + 1;
637 host_start = authority_start;
639 port_start = memchr (host_start, ':', authority_end - host_start);
642 host_end = port_start++;
644 decoded->port = atoi(port_start);
648 host_end = authority_end;
652 decoded->host = g_strndup (host_start, host_end - host_start);
654 hier_part_start = authority_end;
657 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
659 if (decoded->path == NULL)
661 _g_decoded_uri_free (decoded);
669 is_valid (char c, const char *reserved_chars_allowed)
671 if (g_ascii_isalnum (c) ||
678 if (reserved_chars_allowed &&
679 strchr (reserved_chars_allowed, c) != NULL)
686 g_string_append_encoded (GString *string,
688 const char *reserved_chars_allowed)
691 static const gchar hex[16] = "0123456789ABCDEF";
693 while ((c = *encoded) != 0)
695 if (is_valid (c, reserved_chars_allowed))
697 g_string_append_c (string, c);
702 g_string_append_c (string, '%');
703 g_string_append_c (string, hex[((guchar)c) >> 4]);
704 g_string_append_c (string, hex[((guchar)c) & 0xf]);
711 _g_encode_uri (GDecodedUri *decoded)
715 uri = g_string_new (NULL);
717 g_string_append (uri, decoded->scheme);
718 g_string_append (uri, "://");
720 if (decoded->host != NULL)
722 if (decoded->userinfo)
724 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
725 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
726 g_string_append_c (uri, '@');
729 g_string_append (uri, decoded->host);
731 if (decoded->port != -1)
733 g_string_append_c (uri, ':');
734 g_string_append_printf (uri, "%d", decoded->port);
738 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
742 g_string_append_c (uri, '?');
743 g_string_append (uri, decoded->query);
746 if (decoded->fragment)
748 g_string_append_c (uri, '#');
749 g_string_append (uri, decoded->fragment);
752 return g_string_free (uri, FALSE);