1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6#include "nsISupports.idl"
7
8/**
9 * URIs are essentially structured names for things -- anything. This interface
10 * provides accessors to get the most basic components of an URI.
11 * If you need to change some parts of the URI use nsIURIMutator.
12 * Subclasses, including nsIURL, impose greater structure on the URI.
13 *
14 * This interface follows Tim Berners-Lee's URI spec (RFC3986) [1], where the
15 * basic URI components are defined as such:
16 * <pre>
17 *      ftp://username:password@hostname:portnumber/pathname?query#ref
18 *      \ /   \               / \      / \        /\       / \   / \ /
19 *       -     ---------------   ------   --------  -------   ---   -
20 *       |            |             |        |         |       |    |
21 *       |            |             |        |      FilePath Query Ref
22 *       |            |             |       Port       \            /
23 *       |            |            Host      /          ------------
24 *       |         UserPass                 /                |
25 *     Scheme                              /                Path
26 *       \                                /
27 *        --------------------------------
28 *                       |
29 *                    PrePath
30 * </pre>
31 * The definition of the URI components has been extended to allow for
32 * internationalized domain names [2] and the more generic IRI structure [3].
33 *
34 * [1] https://tools.ietf.org/html/rfc3986
35 * [2] https://tools.ietf.org/html/rfc5890
36 * [3] https://tools.ietf.org/html/rfc3987
37 */
38
39%{C++
40#include "nsString.h"
41
42#undef GetPort  // XXX Windows!
43#undef SetPort  // XXX Windows!
44
45namespace mozilla {
46class Encoding;
47namespace ipc {
48class URIParams;
49}  // namespace ipc
50}  // namespace mozilla
51%}
52
53[ptr] native Encoding(const mozilla::Encoding);
54[ref] native URIParams(mozilla::ipc::URIParams);
55interface nsIURIMutator;
56
57/**
58 * nsIURI - interface for an uniform resource identifier w/ i18n support.
59 *
60 * AUTF8String attributes may contain unescaped UTF-8 characters.
61 * Consumers should be careful to escape the UTF-8 strings as necessary, but
62 * should always try to "display" the UTF-8 version as provided by this
63 * interface.
64 *
65 * AUTF8String attributes may also contain escaped characters.
66 *
67 * Unescaping URI segments is unadvised unless there is intimate
68 * knowledge of the underlying charset or there is no plan to display (or
69 * otherwise enforce a charset on) the resulting URI substring.
70 *
71 * The correct way to create an nsIURI from a string is via
72 * nsIIOService.newURI.
73 *
74 * NOTE: nsBinaryInputStream::ReadObject contains a hackaround to intercept the
75 * old (pre-gecko6) nsIURI IID and swap in the current IID instead, in order
76 * for sessionstore to work after an upgrade.  If this IID is revved further,
77 * we will need to add additional checks there for all intermediate IIDs, until
78 * ContentPrincipal is fixed to serialize its URIs as nsISupports (bug 662693).
79 */
80[scriptable, builtinclass, uuid(92073a54-6d78-4f30-913a-b871813208c6)]
81interface nsIURI : nsISupports
82{
83    /************************************************************************
84     * The URI is broken down into the following principal components:
85     */
86
87    /**
88     * Returns a string representation of the URI.
89     *
90     * Some characters may be escaped.
91     */
92    readonly attribute AUTF8String spec;
93
94%{ C++
95    // An infallible wrapper for GetSpec() that returns a failure indication
96    // string if GetSpec() fails. It is most useful for creating
97    // logging/warning/error messages produced for human consumption, and when
98    // matching a URI spec against a fixed spec such as about:blank.
99    nsCString GetSpecOrDefault()
100    {
101        nsCString spec;
102        nsresult rv = GetSpec(spec);
103        if (NS_FAILED(rv)) {
104            spec.AssignLiteral("[nsIURI::GetSpec failed]");
105        }
106        return spec;
107    }
108%}
109
110    /**
111     * The prePath (eg. scheme://user:password@host:port) returns the string
112     * before the path.  This is useful for authentication or managing sessions.
113     *
114     * Some characters may be escaped.
115     */
116    readonly attribute AUTF8String prePath;
117
118    /**
119     * The Scheme is the protocol to which this URI refers.  The scheme is
120     * restricted to the US-ASCII charset per RFC3986.
121     */
122    readonly attribute ACString scheme;
123
124    /**
125     * The username:password (or username only if value doesn't contain a ':')
126     *
127     * Some characters may be escaped.
128     */
129    readonly attribute AUTF8String userPass;
130
131    /**
132     * The optional username and password, assuming the preHost consists of
133     * username:password.
134     *
135     * Some characters may be escaped.
136     */
137    readonly attribute AUTF8String username;
138    readonly attribute AUTF8String password;
139
140    /**
141     * The host:port (or simply the host, if port == -1).
142     */
143    readonly attribute AUTF8String hostPort;
144
145    /**
146     * The host is the internet domain name to which this URI refers.  It could
147     * be an IPv4 (or IPv6) address literal. Otherwise it is an ASCII or punycode
148     * encoded string.
149     */
150    readonly attribute AUTF8String host;
151
152    /**
153     * A port value of -1 corresponds to the protocol's default port (eg. -1
154     * implies port 80 for http URIs).
155     */
156    readonly attribute long port;
157
158    /**
159     * The path, typically including at least a leading '/' (but may also be
160     * empty, depending on the protocol).
161     *
162     * Some characters may be escaped.
163     *
164     * This attribute contains query and ref parts for historical reasons.
165     * Use the 'filePath' attribute if you do not want those parts included.
166     */
167    readonly attribute AUTF8String pathQueryRef;
168
169
170    /************************************************************************
171     * An URI supports the following methods:
172     */
173
174    /**
175     * URI equivalence test (not a strict string comparison).
176     *
177     * eg. http://foo.com:80/ == http://foo.com/
178     */
179    boolean equals(in nsIURI other);
180
181    /**
182     * An optimization to do scheme checks without requiring the users of nsIURI
183     * to GetScheme, thereby saving extra allocating and freeing. Returns true if
184     * the schemes match (case ignored).
185     */
186    [infallible] boolean schemeIs(in string scheme);
187
188    /**
189     * This method resolves a relative string into an absolute URI string,
190     * using this URI as the base.
191     *
192     * NOTE: some implementations may have no concept of a relative URI.
193     */
194    AUTF8String resolve(in AUTF8String relativePath);
195
196
197    /************************************************************************
198     * Additional attributes:
199     */
200
201    /**
202     * The URI spec with an ASCII compatible encoding.  Host portion follows
203     * the IDNA draft spec.  Other parts are URL-escaped per the rules of
204     * RFC2396.  The result is strictly ASCII.
205     */
206    readonly attribute ACString asciiSpec;
207
208    /**
209     * The host:port (or simply the host, if port == -1), with an ASCII compatible
210     * encoding.  Host portion follows the IDNA draft spec.  The result is strictly
211     * ASCII.
212     */
213    readonly attribute ACString asciiHostPort;
214
215    /**
216     * The URI host with an ASCII compatible encoding.  Follows the IDNA
217     * draft spec for converting internationalized domain names (UTF-8) to
218     * ASCII for compatibility with existing internet infrasture.
219     */
220    readonly attribute ACString asciiHost;
221
222    /************************************************************************
223     * Additional attribute & methods added for .ref support:
224     */
225
226    /**
227     * Returns the reference portion (the part after the "#") of the URI.
228     * If there isn't one, an empty string is returned.
229     *
230     * Some characters may be escaped.
231     */
232    readonly attribute AUTF8String ref;
233
234    /**
235     * URI equivalence test (not a strict string comparison), ignoring
236     * the value of the .ref member.
237     *
238     * eg. http://foo.com/# == http://foo.com/
239     *     http://foo.com/#aaa == http://foo.com/#bbb
240     */
241    boolean equalsExceptRef(in nsIURI other);
242
243    /**
244     * returns a string for the current URI with the ref element cleared.
245     */
246    readonly attribute AUTF8String specIgnoringRef;
247
248    /**
249     * Returns if there is a reference portion (the part after the "#") of the URI.
250     */
251    readonly attribute boolean hasRef;
252
253    /************************************************************************
254     * Additional attributes added for .query support:
255     */
256
257    /**
258     * Returns a path including the directory and file portions of a
259     * URL.  For example, the filePath of "http://host/foo/bar.html#baz"
260     * is "/foo/bar.html".
261     *
262     * Some characters may be escaped.
263     */
264    readonly attribute AUTF8String filePath;
265
266    /**
267     * Returns the query portion (the part after the "?") of the URL.
268     * If there isn't one, an empty string is returned.
269     *
270     * Some characters may be escaped.
271     */
272    readonly attribute AUTF8String query;
273
274    /**
275     * If the URI has a punycode encoded hostname, this will hold the UTF8
276     * representation of that hostname (if that representation doesn't contain
277     * blacklisted characters, and the network.IDN_show_punycode pref is false)
278     * Otherwise, if the hostname is ASCII, it will return the same as .asciiHost
279     */
280    readonly attribute AUTF8String displayHost;
281
282    /**
283     * The displayHost:port (or simply the displayHost, if port == -1).
284     */
285    readonly attribute AUTF8String displayHostPort;
286
287    /**
288     * Returns the same as calling .spec, only with a UTF8 encoded hostname
289     * (if that hostname doesn't contain blacklisted characters, and
290     * the network.IDN_show_punycode pref is false)
291     */
292    readonly attribute AUTF8String displaySpec;
293
294    /**
295     * Returns the same as calling .prePath, only with a UTF8 encoded hostname
296     * (if that hostname doesn't contain blacklisted characters, and
297     * the network.IDN_show_punycode pref is false)
298     */
299    readonly attribute AUTF8String displayPrePath;
300
301    /**
302     * Returns an nsIURIMutator that can be used to make changes to the URI.
303     * After performing the setter operations on the mutator, one may call
304     * mutator.finalize() to get a new immutable URI with the desired
305     * properties.
306     */
307    nsIURIMutator mutate();
308
309    /**
310     * Serializes a URI object to a URIParams data structure in order for being
311     * passed over IPC.  For deserialization, see nsIURIMutator.
312     */
313    [noscript, notxpcom] void serialize(in URIParams aParams);
314
315    %{C++
316    // MOZ_DBG support
317    friend std::ostream& operator<<(std::ostream& aOut, const nsIURI& aURI) {
318      nsIURI* uri = const_cast<nsIURI*>(&aURI);
319      return aOut << "nsIURI { " << uri->GetSpecOrDefault() << " }";
320    }
321    %}
322};
323