2
Copyright (C) 2011 Christian Dywan <christian@twotoasts.de>
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.
9
See the file COPYING for the full license text.
13
extern static string hostname_to_unicode (string hostname);
14
extern static string hostname_to_ascii (string hostname);
18
public class URI : Object {
19
public static string parse (string? uri, out string path) {
21
If there's no hostname, the original URI is returned */
24
unowned string? hostname = uri.chr (-1, '/');
25
if (hostname == null || hostname[1] != '/')
27
hostname = hostname.offset (2);
29
if ((path = hostname.chr (-1, '/')) != null)
30
return hostname.split ("/")[0];
34
public static string to_ascii (string uri) {
35
/* Convert hostname to ASCII. */
37
if (uri.chr (-1, '/') != null && uri.chr (-1, ':') != null)
38
proto = uri.split ("://")[0];
40
string hostname = parse (uri, out path);
41
string encoded = hostname_to_ascii (hostname);
42
if (encoded != null) {
44
+ (proto != null ? "://" : "")
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)
56
return unescaped.replace (" ", "%20");
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)
66
else if (!unescaped.validate ())
69
string hostname = parse (unescaped, out path);
70
string decoded = hostname_to_unicode (hostname);
72
return "http://" + decoded + path;
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. */
83
string escaped = GLib.Uri.escape_string (keywords, ":/", true);
84
if (uri.str ("%s") != null)
85
return uri.printf (escaped);
88
public static bool is_blank (string? uri) {
89
return !(uri != null && uri != "" && !uri.has_prefix ("about:"));
91
public static bool is_resource (string? uri) {
93
&& (uri.has_prefix ("http://")
94
|| (uri.has_prefix ("data:") && uri.chr (-1, ';') != null)
95
|| uri.has_prefix ("https://"));
97
public static bool is_location (string? uri) {
98
/* file:// is not considered a location for security reasons */
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:"));
106
public static bool is_email (string? uri) {
108
&& (uri.chr (-1, '@') != null || uri.has_prefix ("mailto:"))
109
/* :// and @ together would mean login credentials */
110
&& uri.str ("://") == null;
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);
120
public static bool is_valid (string? uri) {
122
&& uri.chr (-1, ' ') == null
123
&& (URI.is_location (uri) || uri.chr (-1, '.') != null);