nsIURI.idl (11608B)
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 45 namespace mozilla { 46 class Encoding; 47 namespace ipc { 48 class URIParams; 49 } // namespace ipc 50 } // namespace mozilla 51 %} 52 53 [ptr] native Encoding(const mozilla::Encoding); 54 [ref] native URIParams(mozilla::ipc::URIParams); 55 interface nsIURIMutator; 56 native MallocSizeOf(mozilla::MallocSizeOf); 57 58 /** 59 * nsIURI - interface for an uniform resource identifier w/ i18n support. 60 * 61 * AUTF8String attributes may contain unescaped UTF-8 characters. 62 * Consumers should be careful to escape the UTF-8 strings as necessary, but 63 * should always try to "display" the UTF-8 version as provided by this 64 * interface. 65 * 66 * AUTF8String attributes may also contain escaped characters. 67 * 68 * Unescaping URI segments is unadvised unless there is intimate 69 * knowledge of the underlying charset or there is no plan to display (or 70 * otherwise enforce a charset on) the resulting URI substring. 71 * 72 * The correct way to create an nsIURI from a string is via 73 * nsIIOService.newURI. 74 * 75 * NOTE: nsBinaryInputStream::ReadObject contains a hackaround to intercept the 76 * old (pre-gecko6) nsIURI IID and swap in the current IID instead, in order 77 * for sessionstore to work after an upgrade. If this IID is revved further, 78 * we will need to add additional checks there for all intermediate IIDs, until 79 * ContentPrincipal is fixed to serialize its URIs as nsISupports (bug 662693). 80 */ 81 [scriptable, builtinclass, uuid(92073a54-6d78-4f30-913a-b871813208c6)] 82 interface nsIURI : nsISupports 83 { 84 /************************************************************************ 85 * The URI is broken down into the following principal components: 86 */ 87 88 /** 89 * Returns a string representation of the URI. 90 * 91 * Some characters may be escaped. 92 */ 93 readonly attribute AUTF8String spec; 94 95 %{ C++ 96 // An infallible wrapper for GetSpec() that returns a failure indication 97 // string if GetSpec() fails. It is most useful for creating 98 // logging/warning/error messages produced for human consumption, and when 99 // matching a URI spec against a fixed spec such as about:blank. 100 nsCString GetSpecOrDefault() 101 { 102 nsCString spec; 103 nsresult rv = GetSpec(spec); 104 if (NS_FAILED(rv)) { 105 spec.AssignLiteral("[nsIURI::GetSpec failed]"); 106 } 107 return spec; 108 } 109 %} 110 111 /** 112 * The prePath (eg. scheme://user:password@host:port) returns the string 113 * before the path. This is useful for authentication or managing sessions. 114 * 115 * Some characters may be escaped. 116 */ 117 readonly attribute AUTF8String prePath; 118 119 /** 120 * The Scheme is the protocol to which this URI refers. The scheme is 121 * restricted to the US-ASCII charset per RFC3986. 122 */ 123 readonly attribute ACString scheme; 124 125 /** 126 * The username:password (or username only if value doesn't contain a ':') 127 * 128 * Some characters may be escaped. 129 */ 130 readonly attribute AUTF8String userPass; 131 132 /** 133 * The optional username and password, assuming the preHost consists of 134 * username:password. 135 * 136 * Some characters may be escaped. 137 */ 138 readonly attribute AUTF8String username; 139 readonly attribute AUTF8String password; 140 141 /** 142 * The host:port (or simply the host, if port == -1). 143 */ 144 readonly attribute AUTF8String hostPort; 145 146 /** 147 * The host is the internet domain name to which this URI refers. It could 148 * be an IPv4 (or IPv6) address literal. Otherwise it is an ASCII or punycode 149 * encoded string. 150 */ 151 readonly attribute AUTF8String host; 152 153 /** 154 * A port value of -1 corresponds to the protocol's default port (eg. -1 155 * implies port 80 for http URIs). 156 */ 157 readonly attribute long port; 158 159 /** 160 * The path, typically including at least a leading '/' (but may also be 161 * empty, depending on the protocol). 162 * 163 * Some characters may be escaped. 164 * 165 * This attribute contains query and ref parts for historical reasons. 166 * Use the 'filePath' attribute if you do not want those parts included. 167 */ 168 readonly attribute AUTF8String pathQueryRef; 169 170 171 /************************************************************************ 172 * An URI supports the following methods: 173 */ 174 175 /** 176 * URI equivalence test (not a strict string comparison). 177 * 178 * eg. http://foo.com:80/ == http://foo.com/ 179 */ 180 boolean equals(in nsIURI other); 181 182 /** 183 * An optimization to do scheme checks without requiring the users of nsIURI 184 * to GetScheme, thereby saving extra allocating and freeing. Returns true if 185 * the schemes match (case ignored). 186 */ 187 [infallible] boolean schemeIs(in string scheme); 188 189 /** 190 * This method resolves a relative string into an absolute URI string, 191 * using this URI as the base. 192 * 193 * NOTE: some implementations may have no concept of a relative URI. 194 */ 195 AUTF8String resolve(in AUTF8String relativePath); 196 197 198 /************************************************************************ 199 * Additional attributes: 200 */ 201 202 /** 203 * The URI spec with an ASCII compatible encoding. Host portion follows 204 * the IDNA draft spec. Other parts are URL-escaped per the rules of 205 * RFC2396. The result is strictly ASCII. 206 */ 207 readonly attribute ACString asciiSpec; 208 209 /** 210 * The host:port (or simply the host, if port == -1), with an ASCII compatible 211 * encoding. Host portion follows the IDNA draft spec. The result is strictly 212 * ASCII. 213 */ 214 readonly attribute ACString asciiHostPort; 215 216 /** 217 * The URI host with an ASCII compatible encoding. Follows the IDNA 218 * draft spec for converting internationalized domain names (UTF-8) to 219 * ASCII for compatibility with existing internet infrasture. 220 */ 221 readonly attribute ACString asciiHost; 222 223 /************************************************************************ 224 * Additional attribute & methods added for .ref support: 225 */ 226 227 /** 228 * Returns the reference portion (the part after the "#") of the URI. 229 * If there isn't one, an empty string is returned. 230 * 231 * Some characters may be escaped. 232 */ 233 readonly attribute AUTF8String ref; 234 235 /** 236 * URI equivalence test (not a strict string comparison), ignoring 237 * the value of the .ref member. 238 * 239 * eg. http://foo.com/# == http://foo.com/ 240 * http://foo.com/#aaa == http://foo.com/#bbb 241 */ 242 boolean equalsExceptRef(in nsIURI other); 243 244 /** 245 * returns a string for the current URI with the ref element cleared. 246 */ 247 readonly attribute AUTF8String specIgnoringRef; 248 249 /** 250 * Returns if there is a reference portion (the part after the "#") of the URI. 251 */ 252 readonly attribute boolean hasRef; 253 254 /** 255 * Returns if there is user and pass in the URI. 256 */ 257 readonly attribute boolean hasUserPass; 258 259 /************************************************************************ 260 * Additional attributes added for .query support: 261 */ 262 263 /** 264 * Returns a path including the directory and file portions of a 265 * URL. For example, the filePath of "http://host/foo/bar.html#baz" 266 * is "/foo/bar.html". 267 * 268 * Some characters may be escaped. 269 */ 270 readonly attribute AUTF8String filePath; 271 272 /** 273 * Returns the query portion (the part after the "?") of the URL. 274 * If there isn't one, an empty string is returned. 275 * 276 * Some characters may be escaped. 277 */ 278 readonly attribute AUTF8String query; 279 280 /** 281 * Returns if there is a query portion (the part after the "?") of the URI. 282 */ 283 readonly attribute boolean hasQuery; 284 285 /** 286 * Returns the host in a form suitable for display in the UI (with Punycode 287 * potentially decoded). 288 */ 289 readonly attribute AUTF8String displayHost; 290 291 /** 292 * The displayHost:port (or simply the displayHost, if port == -1). 293 */ 294 readonly attribute AUTF8String displayHostPort; 295 296 /** 297 * Returns the same as calling .spec, but with displayHost in the host position. 298 */ 299 readonly attribute AUTF8String displaySpec; 300 301 /** 302 * Returns the same as calling .prePath, but with displayHost in the host position. 303 */ 304 readonly attribute AUTF8String displayPrePath; 305 306 /** 307 * Returns an nsIURIMutator that can be used to make changes to the URI. 308 * After performing the setter operations on the mutator, one may call 309 * mutator.finalize() to get a new immutable URI with the desired 310 * properties. 311 */ 312 nsIURIMutator mutate(); 313 314 /** 315 * Serializes a URI object to a URIParams data structure in order for being 316 * passed over IPC. For deserialization, see nsIURIMutator. 317 */ 318 [noscript, notxpcom] void serialize(in URIParams aParams); 319 320 /** 321 * Measures the size of the object and the things that it points to. 322 * 323 * WARNING: Don't call this more than once on a particular object or you 324 * will end up with overcounting. Having an nsCOMPtr<nsIURI> is not 325 * sufficient to know that you are the only one measuring this object. 326 * 327 * SizeOfExcludingThis does not make sense here because this is a 328 * refcounted object, so it will never be embedded in something else. 329 */ 330 [notxpcom, nostdcall] size_t SizeOfIncludingThis(in MallocSizeOf aMallocSizeOf); 331 332 %{C++ 333 // MOZ_DBG support 334 friend std::ostream& operator<<(std::ostream& aOut, const nsIURI& aURI) { 335 nsIURI* uri = const_cast<nsIURI*>(&aURI); 336 return aOut << "nsIURI { " << uri->GetSpecOrDefault() << " }"; 337 } 338 %} 339 }; 340 341 %{C++ 342 // fmt support 343 template <> 344 struct fmt::formatter<nsIURI> : ostream_formatter {}; 345 %}