~kalikiana/midori/beetle

« back to all changes in this revision

Viewing changes to katze/midori-uri.vala

  • Committer: Christian Dywan
  • Date: 2011-10-20 23:00:13 UTC
  • Revision ID: git-v1:77428fe44cece896019be5ce5c6f409f21298931
Introduce Midori.URI to unitfy all URI logic

sokoke_hostname_from_uri, sokoke_search_uri,
sokoke_hostname_to_ascii and sokoke_unescape_uri_string
are merged into Midori.URI.
midori-core.h is the official API header for Vala now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2011 Christian Dywan <christian@twotoasts.de>
 
3
 
 
4
 This library is free software; you can redistribute it and/or
 
5
 modify it under the terms of the GNU Lesser General Public
 
6
 License as published by the Free Software Foundation; either
 
7
 version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
 See the file COPYING for the full license text.
 
10
*/
 
11
 
 
12
namespace GLib {
 
13
    extern static string hostname_to_unicode (string hostname);
 
14
    extern static string hostname_to_ascii (string hostname);
 
15
}
 
16
 
 
17
namespace Midori {
 
18
    public class URI : Object {
 
19
        public static string parse (string? uri, out string path) {
 
20
            /* path may be null.
 
21
               If there's no hostname, the original URI is returned */
 
22
            if (uri == null)
 
23
                return uri;
 
24
            unowned string? hostname = uri.chr (-1, '/');
 
25
            if (hostname == null || hostname[1] != '/')
 
26
                return uri;
 
27
            hostname = hostname.offset (2);
 
28
            if (&path != null) {
 
29
                if ((path = hostname.chr (-1, '/')) != null)
 
30
                    return hostname.split ("/")[0];
 
31
            }
 
32
            return hostname;
 
33
        }
 
34
        public static string to_ascii (string uri) {
 
35
            /* Convert hostname to ASCII. */
 
36
            string? proto = null;
 
37
            if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null)
 
38
                proto = uri.split ("://")[0];
 
39
            string? path = null;
 
40
            string hostname = parse (uri, out path);
 
41
            string encoded = hostname_to_ascii (hostname);
 
42
            if (encoded != null) {
 
43
                return (proto ?? "")
 
44
                     + (proto != null ? "://" : "")
 
45
                     + encoded + path;
 
46
            }
 
47
            return uri;
 
48
        }
 
49
        public static string unescape (string uri) {
 
50
            /* Unescape, pass through + and %20 */
 
51
            if (uri.chr (-1, '%') != null || uri.chr (-1, ' ') != null) {
 
52
                /* Preserve %20 for pasting URLs into other windows */
 
53
                string? unescaped = GLib.Uri.unescape_string (uri, "+");
 
54
                if (unescaped == null)
 
55
                    return uri;
 
56
                return unescaped.replace (" ", "%20");
 
57
            }
 
58
            return uri;
 
59
        }
 
60
        public static string format_for_display (string? uri) {
 
61
            /* Percent-decode and decode puniycode for user display */
 
62
            if (uri != null && uri.has_prefix ("http://")) {
 
63
                string unescaped = unescape (uri);
 
64
                if (unescaped == null)
 
65
                    return uri;
 
66
                else if (!unescaped.validate ())
 
67
                    return uri;
 
68
                string path;
 
69
                string hostname = parse (unescaped, out path);
 
70
                string decoded = hostname_to_unicode (hostname);
 
71
                if (decoded != null)
 
72
                    return "http://" + decoded + path;
 
73
                return unescaped;
 
74
            }
 
75
            return uri;
 
76
        }
 
77
        public static string for_search (string? uri, string keywords) {
 
78
            /* Take a search engine URI and insert specified keywords.
 
79
               Keywords are percent-encoded. If the uri contains a %s
 
80
               the keywords are inserted there, otherwise appended. */
 
81
            if (uri == null)
 
82
                return keywords;
 
83
            string escaped = GLib.Uri.escape_string (keywords, ":/", true);
 
84
            if (uri.str ("%s") != null)
 
85
                return uri.printf (escaped);
 
86
            return uri + escaped;
 
87
        }
 
88
        public static bool is_blank (string? uri) {
 
89
            return !(uri != null && uri != "" && !uri.has_prefix ("about:"));
 
90
        }
 
91
        public static bool is_resource (string? uri) {
 
92
            return uri != null
 
93
              && (uri.has_prefix ("http://")
 
94
               || (uri.has_prefix ("data:") && uri.chr (-1, ';') != null)
 
95
               || uri.has_prefix ("https://"));
 
96
        }
 
97
        public static bool is_location (string? uri) {
 
98
            /* file:// is not considered a location for security reasons */
 
99
            return uri != null
 
100
             && ((uri.str ("://") != null && uri.chr (-1, ' ') == null)
 
101
              || uri.has_prefix ("about:")
 
102
              || (uri.has_prefix ("data:") && uri.chr (-1, ';') != null)
 
103
              || (uri.has_prefix ("geo:") && uri.chr (-1, ',') != null)
 
104
              || uri.has_prefix ("javascript:"));
 
105
        }
 
106
        public static bool is_email (string? uri) {
 
107
            return uri != null
 
108
             && (uri.chr (-1, '@') != null || uri.has_prefix ("mailto:"))
 
109
            /* :// and @ together would mean login credentials */
 
110
             && uri.str ("://") == null;
 
111
        }
 
112
        public static bool is_ip_address (string? uri) {
 
113
            /* Quick check for IPv4 or IPv6, no validation.
 
114
               FIXME: Schemes are not handled
 
115
               hostname_is_ip_address () is not used because
 
116
               we'd have to separate the path from the URI first. */
 
117
            return uri != null && uri[0].isdigit ()
 
118
             && (uri.chr (4, '.') != null || uri.chr (4, ':') != null);
 
119
        }
 
120
        public static bool is_valid (string? uri) {
 
121
            return uri != null
 
122
             && uri.chr (-1, ' ') == null
 
123
             && (URI.is_location (uri) || uri.chr (-1, '.') != null);
 
124
        }
 
125
    }
 
126
}