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"
40 static void g_dummy_file_file_iface_init (GFileIface *iface);
46 int port; /* -1 => not in uri */
54 GObject parent_instance;
56 GDecodedUri *decoded_uri;
60 #define g_dummy_file_get_type _g_dummy_file_get_type
61 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
62 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
63 g_dummy_file_file_iface_init))
65 #define SUB_DELIM_CHARS "!$&'()*+,;="
67 static char * _g_encode_uri (GDecodedUri *decoded);
68 static void _g_decoded_uri_free (GDecodedUri *decoded);
69 static GDecodedUri *_g_decode_uri (const char *uri);
70 static GDecodedUri *_g_decoded_uri_new (void);
72 static char * unescape_string (const gchar *escaped_string,
73 const gchar *escaped_string_end,
74 const gchar *illegal_characters);
76 static void g_string_append_encoded (GString *string,
78 const char *reserved_chars_allowed);
81 g_dummy_file_finalize (GObject *object)
85 dummy = G_DUMMY_FILE (object);
87 if (dummy->decoded_uri)
88 _g_decoded_uri_free (dummy->decoded_uri);
90 g_free (dummy->text_uri);
92 G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
96 g_dummy_file_class_init (GDummyFileClass *klass)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 gobject_class->finalize = g_dummy_file_finalize;
104 g_dummy_file_init (GDummyFile *dummy)
109 _g_dummy_file_new (const char *uri)
113 g_return_val_if_fail (uri != NULL, NULL);
115 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
116 dummy->text_uri = g_strdup (uri);
117 dummy->decoded_uri = _g_decode_uri (uri);
119 return G_FILE (dummy);
123 g_dummy_file_is_native (GFile *file)
129 g_dummy_file_get_basename (GFile *file)
131 GDummyFile *dummy = G_DUMMY_FILE (file);
133 if (dummy->decoded_uri)
134 return g_path_get_basename (dummy->decoded_uri->path);
135 return g_strdup (dummy->text_uri);
139 g_dummy_file_get_path (GFile *file)
145 g_dummy_file_get_uri (GFile *file)
147 return g_strdup (G_DUMMY_FILE (file)->text_uri);
151 g_dummy_file_get_parse_name (GFile *file)
153 return g_strdup (G_DUMMY_FILE (file)->text_uri);
157 g_dummy_file_get_parent (GFile *file)
159 GDummyFile *dummy = G_DUMMY_FILE (file);
163 GDecodedUri new_decoded_uri;
165 if (dummy->decoded_uri == NULL ||
166 g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
169 dirname = g_path_get_dirname (dummy->decoded_uri->path);
171 if (strcmp (dirname, ".") == 0)
177 new_decoded_uri = *dummy->decoded_uri;
178 new_decoded_uri.path = dirname;
179 uri = _g_encode_uri (&new_decoded_uri);
182 parent = _g_dummy_file_new (uri);
189 g_dummy_file_dup (GFile *file)
191 GDummyFile *dummy = G_DUMMY_FILE (file);
193 return _g_dummy_file_new (dummy->text_uri);
197 g_dummy_file_hash (GFile *file)
199 GDummyFile *dummy = G_DUMMY_FILE (file);
201 return g_str_hash (dummy->text_uri);
205 g_dummy_file_equal (GFile *file1,
208 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
209 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
211 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
215 safe_strcmp (const char *a,
223 return strcmp (a, b);
227 uri_same_except_path (GDecodedUri *a,
230 if (safe_strcmp (a->scheme, b->scheme) != 0)
232 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
234 if (safe_strcmp (a->host, b->host) != 0)
236 if (a->port != b->port)
243 match_prefix (const char *path,
248 prefix_len = strlen (prefix);
249 if (strncmp (path, prefix, prefix_len) != 0)
251 return path + prefix_len;
255 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
257 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
258 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
259 const char *remainder;
261 if (parent_dummy->decoded_uri != NULL &&
262 descendant_dummy->decoded_uri != NULL)
264 if (uri_same_except_path (parent_dummy->decoded_uri,
265 descendant_dummy->decoded_uri))
267 remainder = match_prefix (descendant_dummy->decoded_uri->path,
268 parent_dummy->decoded_uri->path);
269 if (remainder != NULL && *remainder == '/')
271 while (*remainder == '/')
280 remainder = match_prefix (descendant_dummy->text_uri,
281 parent_dummy->text_uri);
282 if (remainder != NULL && *remainder == '/')
284 while (*remainder == '/')
295 g_dummy_file_get_relative_path (GFile *parent,
298 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
299 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
300 const char *remainder;
302 if (parent_dummy->decoded_uri != NULL &&
303 descendant_dummy->decoded_uri != NULL)
305 if (uri_same_except_path (parent_dummy->decoded_uri,
306 descendant_dummy->decoded_uri))
308 remainder = match_prefix (descendant_dummy->decoded_uri->path,
309 parent_dummy->decoded_uri->path);
310 if (remainder != NULL && *remainder == '/')
312 while (*remainder == '/')
315 return g_strdup (remainder);
321 remainder = match_prefix (descendant_dummy->text_uri,
322 parent_dummy->text_uri);
323 if (remainder != NULL && *remainder == '/')
325 while (*remainder == '/')
328 return unescape_string (remainder, NULL, "/");
337 g_dummy_file_resolve_relative_path (GFile *file,
338 const char *relative_path)
340 GDummyFile *dummy = G_DUMMY_FILE (file);
343 GDecodedUri new_decoded_uri;
346 if (dummy->decoded_uri == NULL)
348 str = g_string_new (dummy->text_uri);
349 g_string_append (str, "/");
350 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
351 child = _g_dummy_file_new (str->str);
352 g_string_free (str, TRUE);
356 new_decoded_uri = *dummy->decoded_uri;
358 if (g_path_is_absolute (relative_path))
359 new_decoded_uri.path = g_strdup (relative_path);
361 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
363 uri = _g_encode_uri (&new_decoded_uri);
364 g_free (new_decoded_uri.path);
366 child = _g_dummy_file_new (uri);
374 g_dummy_file_get_child_for_display_name (GFile *file,
375 const char *display_name,
378 return g_file_get_child (file, display_name);
382 g_dummy_file_has_uri_scheme (GFile *file,
383 const char *uri_scheme)
385 GDummyFile *dummy = G_DUMMY_FILE (file);
387 if (dummy->decoded_uri)
388 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
393 g_dummy_file_get_uri_scheme (GFile *file)
395 GDummyFile *dummy = G_DUMMY_FILE (file);
397 if (dummy->decoded_uri)
398 return g_strdup (dummy->decoded_uri->scheme);
405 g_dummy_file_file_iface_init (GFileIface *iface)
407 iface->dup = g_dummy_file_dup;
408 iface->hash = g_dummy_file_hash;
409 iface->equal = g_dummy_file_equal;
410 iface->is_native = g_dummy_file_is_native;
411 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
412 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
413 iface->get_basename = g_dummy_file_get_basename;
414 iface->get_path = g_dummy_file_get_path;
415 iface->get_uri = g_dummy_file_get_uri;
416 iface->get_parse_name = g_dummy_file_get_parse_name;
417 iface->get_parent = g_dummy_file_get_parent;
418 iface->prefix_matches = g_dummy_file_prefix_matches;
419 iface->get_relative_path = g_dummy_file_get_relative_path;
420 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
421 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
423 iface->supports_thread_contexts = TRUE;
426 /* Uri handling helper functions: */
429 unescape_character (const char *scanner)
434 first_digit = g_ascii_xdigit_value (*scanner++);
438 second_digit = g_ascii_xdigit_value (*scanner++);
439 if (second_digit < 0)
442 return (first_digit << 4) | second_digit;
446 unescape_string (const gchar *escaped_string,
447 const gchar *escaped_string_end,
448 const gchar *illegal_characters)
454 if (escaped_string == NULL)
457 if (escaped_string_end == NULL)
458 escaped_string_end = escaped_string + strlen (escaped_string);
460 result = g_malloc (escaped_string_end - escaped_string + 1);
463 for (in = escaped_string; in < escaped_string_end; in++)
469 if (escaped_string_end - in < 2)
475 character = unescape_character (in);
477 /* Check for an illegal character. We consider '\0' illegal here. */
478 if (character <= 0 ||
479 (illegal_characters != NULL &&
480 strchr (illegal_characters, (char)character) != NULL))
485 in++; /* The other char will be eaten in the loop header */
487 *out++ = (char)character;
491 g_warn_if_fail (out - result <= strlen (escaped_string));
496 _g_decoded_uri_free (GDecodedUri *decoded)
501 g_free (decoded->scheme);
502 g_free (decoded->query);
503 g_free (decoded->fragment);
504 g_free (decoded->userinfo);
505 g_free (decoded->host);
506 g_free (decoded->path);
511 _g_decoded_uri_new (void)
515 uri = g_new0 (GDecodedUri, 1);
522 _g_decode_uri (const char *uri)
524 GDecodedUri *decoded;
525 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
529 /* From RFC 3986 Decodes:
530 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
536 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
539 if (!g_ascii_isalpha (*p))
549 if (!(g_ascii_isalnum(c) ||
556 decoded = _g_decoded_uri_new ();
558 decoded->scheme = g_malloc (p - uri);
559 out = decoded->scheme;
560 for (in = uri; in < p - 1; in++)
561 *out++ = g_ascii_tolower (*in);
566 query_start = strchr (p, '?');
569 hier_part_end = query_start++;
570 fragment_start = strchr (query_start, '#');
573 decoded->query = g_strndup (query_start, fragment_start - query_start);
574 decoded->fragment = g_strdup (fragment_start+1);
578 decoded->query = g_strdup (query_start);
579 decoded->fragment = NULL;
585 decoded->query = NULL;
586 fragment_start = strchr (p, '#');
589 hier_part_end = fragment_start++;
590 decoded->fragment = g_strdup (fragment_start);
594 hier_part_end = p + strlen (p);
595 decoded->fragment = NULL;
600 hier-part = "//" authority path-abempty
607 if (hier_part_start[0] == '/' &&
608 hier_part_start[1] == '/')
610 const char *authority_start, *authority_end;
611 const char *userinfo_start, *userinfo_end;
612 const char *host_start, *host_end;
613 const char *port_start;
615 authority_start = hier_part_start + 2;
616 /* authority is always followed by / or nothing */
617 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
618 if (authority_end == NULL)
619 authority_end = hier_part_end;
622 authority = [ userinfo "@" ] host [ ":" port ]
625 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
628 userinfo_start = authority_start;
629 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
630 if (decoded->userinfo == NULL)
632 _g_decoded_uri_free (decoded);
635 host_start = userinfo_end + 1;
638 host_start = authority_start;
640 port_start = memchr (host_start, ':', authority_end - host_start);
643 host_end = port_start++;
645 decoded->port = atoi(port_start);
649 host_end = authority_end;
653 decoded->host = g_strndup (host_start, host_end - host_start);
655 hier_part_start = authority_end;
658 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
660 if (decoded->path == NULL)
662 _g_decoded_uri_free (decoded);
670 is_valid (char c, const char *reserved_chars_allowed)
672 if (g_ascii_isalnum (c) ||
679 if (reserved_chars_allowed &&
680 strchr (reserved_chars_allowed, c) != NULL)
687 g_string_append_encoded (GString *string,
689 const char *reserved_chars_allowed)
692 static const gchar hex[16] = "0123456789ABCDEF";
694 while ((c = *encoded) != 0)
696 if (is_valid (c, reserved_chars_allowed))
698 g_string_append_c (string, c);
703 g_string_append_c (string, '%');
704 g_string_append_c (string, hex[((guchar)c) >> 4]);
705 g_string_append_c (string, hex[((guchar)c) & 0xf]);
712 _g_encode_uri (GDecodedUri *decoded)
716 uri = g_string_new (NULL);
718 g_string_append (uri, decoded->scheme);
719 g_string_append (uri, "://");
721 if (decoded->host != NULL)
723 if (decoded->userinfo)
725 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
726 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
727 g_string_append_c (uri, '@');
730 g_string_append (uri, decoded->host);
732 if (decoded->port != -1)
734 g_string_append_c (uri, ':');
735 g_string_append_printf (uri, "%d", decoded->port);
739 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
743 g_string_append_c (uri, '?');
744 g_string_append (uri, decoded->query);
747 if (decoded->fragment)
749 g_string_append_c (uri, '#');
750 g_string_append (uri, decoded->fragment);
753 return g_string_free (uri, FALSE);