glib/auto/uri.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Bytes, Error, UriFlags, UriHideFlags, ffi, translate::*};
6
7crate::wrapper! {
8 /// err);
9 /// g_assert_error (err, G_URI_ERROR, G_URI_ERROR_BAD_QUERY);
10 /// ```text
11 ///
12 /// You should pass `G_URI_FLAGS_ENCODED` or `G_URI_FLAGS_ENCODED_QUERY` if you
13 /// need to handle that case manually. In particular, if the query string
14 /// contains `=` characters that are `%`-encoded, you should let
15 /// `GLib::Uri::parse_params()` do the decoding once of the query.
16 ///
17 /// `GUri` is immutable once constructed, and can safely be accessed from
18 /// multiple threads. Its reference counting is atomic.
19 ///
20 /// Note that the scope of `GUri` is to help manipulate URIs in various applications,
21 /// following [RFC 3986](https://tools.ietf.org/html/rfc3986). In particular,
22 /// it doesn't intend to cover web browser needs, and doesn’t implement the
23 /// [WHATWG URL](https://url.spec.whatwg.org/) standard. No APIs are provided to
24 /// help prevent
25 /// [homograph attacks](https://en.wikipedia.org/wiki/IDN_homograph_attack), so
26 /// `GUri` is not suitable for formatting URIs for display to the user for making
27 /// security-sensitive decisions.
28 ///
29 /// ## Relative and absolute URIs
30 ///
31 /// As defined in [RFC 3986](https://tools.ietf.org/html/rfc3986#section-4), the
32 /// hierarchical nature of URIs means that they can either be ‘relative
33 /// references’ (sometimes referred to as ‘relative URIs’) or ‘URIs’ (for
34 /// clarity, ‘URIs’ are referred to in this documentation as
35 /// ‘absolute URIs’ — although
36 /// [in contrast to RFC 3986](https://tools.ietf.org/html/rfc3986#section-4.3),
37 /// fragment identifiers are always allowed).
38 ///
39 /// Relative references have one or more components of the URI missing. In
40 /// particular, they have no scheme. Any other component, such as hostname,
41 /// query, etc. may be missing, apart from a path, which has to be specified (but
42 /// may be empty). The path may be relative, starting with `./` rather than `/`.
43 ///
44 /// For example, a valid relative reference is `./path?query`,
45 /// `/?query#fragment` or `//example.com`.
46 ///
47 /// Absolute URIs have a scheme specified. Any other components of the URI which
48 /// are missing are specified as explicitly unset in the URI, rather than being
49 /// resolved relative to a base URI using [`parse_relative()`][Self::parse_relative()].
50 ///
51 /// For example, a valid absolute URI is `file:///home/bob` or
52 /// `https://search.com?query=string`.
53 ///
54 /// A `GUri` instance is always an absolute URI. A string may be an absolute URI
55 /// or a relative reference; see the documentation for individual functions as to
56 /// what forms they accept.
57 ///
58 /// ## Parsing URIs
59 ///
60 /// The most minimalist APIs for parsing URIs are [`split()`][Self::split()] and
61 /// [`split_with_user()`][Self::split_with_user()]. These split a URI into its component
62 /// parts, and return the parts; the difference between the two is that
63 /// [`split()`][Self::split()] treats the ‘userinfo’ component of the URI as a
64 /// single element, while [`split_with_user()`][Self::split_with_user()] can (depending on the
65 /// [`UriFlags`][crate::UriFlags] you pass) treat it as containing a username, password,
66 /// and authentication parameters. Alternatively, [`split_network()`][Self::split_network()]
67 /// can be used when you are only interested in the components that are
68 /// needed to initiate a network connection to the service (scheme,
69 /// host, and port).
70 ///
71 /// [`parse()`][Self::parse()] is similar to [`split()`][Self::split()], but instead of
72 /// returning individual strings, it returns a `GUri` structure (and it requires
73 /// that the URI be an absolute URI).
74 ///
75 /// [`resolve_relative()`][Self::resolve_relative()] and [`parse_relative()`][Self::parse_relative()] allow
76 /// you to resolve a relative URI relative to a base URI.
77 /// [`resolve_relative()`][Self::resolve_relative()] takes two strings and returns a string,
78 /// and [`parse_relative()`][Self::parse_relative()] takes a `GUri` and a string and returns a
79 /// `GUri`.
80 ///
81 /// All of the parsing functions take a [`UriFlags`][crate::UriFlags] argument describing
82 /// exactly how to parse the URI; see the documentation for that type
83 /// for more details on the specific flags that you can pass. If you
84 /// need to choose different flags based on the type of URI, you can
85 /// use [`peek_scheme()`][Self::peek_scheme()] on the URI string to check the scheme
86 /// first, and use that to decide what flags to parse it with.
87 ///
88 /// For example, you might want to use `G_URI_PARAMS_WWW_FORM` when parsing the
89 /// params for a web URI, so compare the result of [`peek_scheme()`][Self::peek_scheme()]
90 /// against `http` and `https`.
91 ///
92 /// ## Building URIs
93 ///
94 /// [`join()`][Self::join()] and [`join_with_user()`][Self::join_with_user()] can be used to construct
95 /// valid URI strings from a set of component strings. They are the
96 /// inverse of [`split()`][Self::split()] and [`split_with_user()`][Self::split_with_user()].
97 ///
98 /// Similarly, [`build()`][Self::build()] and [`build_with_user()`][Self::build_with_user()] can be
99 /// used to construct a `GUri` from a set of component strings.
100 ///
101 /// As with the parsing functions, the building functions take a
102 /// [`UriFlags`][crate::UriFlags] argument. In particular, it is important to keep in mind
103 /// whether the URI components you are using are already `%`-encoded. If so,
104 /// you must pass the `G_URI_FLAGS_ENCODED` flag.
105 ///
106 /// ## `file://` URIs
107 ///
108 /// Note that Windows and Unix both define special rules for parsing
109 /// `file://` URIs (involving non-UTF-8 character sets on Unix, and the
110 /// interpretation of path separators on Windows). `GUri` does not
111 /// implement these rules. Use [`filename_from_uri()`][crate::filename_from_uri()] and
112 /// [`filename_to_uri()`][crate::filename_to_uri()] if you want to properly convert between
113 /// `file://` URIs and local filenames.
114 ///
115 /// ## URI Equality
116 ///
117 /// Note that there is no `g_uri_equal ()` function, because comparing
118 /// URIs usefully requires scheme-specific knowledge that `GUri` does
119 /// not have. `GUri` can help with normalization if you use the various
120 /// encoded [`UriFlags`][crate::UriFlags] as well as `G_URI_FLAGS_SCHEME_NORMALIZE`
121 /// however it is not comprehensive.
122 /// For example, `data:,foo` and `data:;base64,Zm9v` resolve to the same
123 /// thing according to the `data:` URI specification which GLib does not
124 /// handle.
125 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
126 pub struct Uri(Shared<ffi::GUri>);
127
128 match fn {
129 ref => |ptr| ffi::g_uri_ref(ptr),
130 unref => |ptr| ffi::g_uri_unref(ptr),
131 type_ => || ffi::g_uri_get_type(),
132 }
133}
134
135impl Uri {
136 /// Gets @self's authentication parameters, which may contain
137 /// `%`-encoding, depending on the flags with which @self was created.
138 /// (If @self was not created with [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS] then this will
139 /// be [`None`].)
140 ///
141 /// Depending on the URI scheme, g_uri_parse_params() may be useful for
142 /// further parsing this information.
143 ///
144 /// # Returns
145 ///
146 /// @self's authentication parameters.
147 #[doc(alias = "g_uri_get_auth_params")]
148 #[doc(alias = "get_auth_params")]
149 pub fn auth_params(&self) -> Option<crate::GString> {
150 unsafe { from_glib_none(ffi::g_uri_get_auth_params(self.to_glib_none().0)) }
151 }
152
153 /// Gets @self's flags set upon construction.
154 ///
155 /// # Returns
156 ///
157 /// @self's flags.
158 #[doc(alias = "g_uri_get_flags")]
159 #[doc(alias = "get_flags")]
160 pub fn flags(&self) -> UriFlags {
161 unsafe { from_glib(ffi::g_uri_get_flags(self.to_glib_none().0)) }
162 }
163
164 /// Gets @self's fragment, which may contain `%`-encoding, depending on
165 /// the flags with which @self was created.
166 ///
167 /// # Returns
168 ///
169 /// @self's fragment.
170 #[doc(alias = "g_uri_get_fragment")]
171 #[doc(alias = "get_fragment")]
172 pub fn fragment(&self) -> Option<crate::GString> {
173 unsafe { from_glib_none(ffi::g_uri_get_fragment(self.to_glib_none().0)) }
174 }
175
176 /// Gets @self's host. This will never have `%`-encoded characters,
177 /// unless it is non-UTF-8 (which can only be the case if @self was
178 /// created with [`UriFlags::NON_DNS`][crate::UriFlags::NON_DNS]).
179 ///
180 /// If @self contained an IPv6 address literal, this value will be just
181 /// that address, without the brackets around it that are necessary in
182 /// the string form of the URI. Note that in this case there may also
183 /// be a scope ID attached to the address. Eg, `fe80::1234%``em1` (or
184 /// `fe80::1234%``25em1` if the string is still encoded).
185 ///
186 /// # Returns
187 ///
188 /// @self's host.
189 #[doc(alias = "g_uri_get_host")]
190 #[doc(alias = "get_host")]
191 pub fn host(&self) -> Option<crate::GString> {
192 unsafe { from_glib_none(ffi::g_uri_get_host(self.to_glib_none().0)) }
193 }
194
195 /// Gets @self's password, which may contain `%`-encoding, depending on
196 /// the flags with which @self was created. (If @self was not created
197 /// with [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] then this will be [`None`].)
198 ///
199 /// # Returns
200 ///
201 /// @self's password.
202 #[doc(alias = "g_uri_get_password")]
203 #[doc(alias = "get_password")]
204 pub fn password(&self) -> Option<crate::GString> {
205 unsafe { from_glib_none(ffi::g_uri_get_password(self.to_glib_none().0)) }
206 }
207
208 /// Gets @self's path, which may contain `%`-encoding, depending on the
209 /// flags with which @self was created.
210 ///
211 /// # Returns
212 ///
213 /// @self's path.
214 #[doc(alias = "g_uri_get_path")]
215 #[doc(alias = "get_path")]
216 pub fn path(&self) -> crate::GString {
217 unsafe { from_glib_none(ffi::g_uri_get_path(self.to_glib_none().0)) }
218 }
219
220 /// Gets @self's port.
221 ///
222 /// # Returns
223 ///
224 /// @self's port, or `-1` if no port was specified.
225 #[doc(alias = "g_uri_get_port")]
226 #[doc(alias = "get_port")]
227 pub fn port(&self) -> i32 {
228 unsafe { ffi::g_uri_get_port(self.to_glib_none().0) }
229 }
230
231 /// Gets @self's query, which may contain `%`-encoding, depending on the
232 /// flags with which @self was created.
233 ///
234 /// For queries consisting of a series of `name=value` parameters,
235 /// #GUriParamsIter or g_uri_parse_params() may be useful.
236 ///
237 /// # Returns
238 ///
239 /// @self's query.
240 #[doc(alias = "g_uri_get_query")]
241 #[doc(alias = "get_query")]
242 pub fn query(&self) -> Option<crate::GString> {
243 unsafe { from_glib_none(ffi::g_uri_get_query(self.to_glib_none().0)) }
244 }
245
246 /// Gets @self's scheme. Note that this will always be all-lowercase,
247 /// regardless of the string or strings that @self was created from.
248 ///
249 /// # Returns
250 ///
251 /// @self's scheme.
252 #[doc(alias = "g_uri_get_scheme")]
253 #[doc(alias = "get_scheme")]
254 pub fn scheme(&self) -> crate::GString {
255 unsafe { from_glib_none(ffi::g_uri_get_scheme(self.to_glib_none().0)) }
256 }
257
258 /// Gets the ‘username’ component of @self's userinfo, which may contain
259 /// `%`-encoding, depending on the flags with which @self was created.
260 /// If @self was not created with [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] or
261 /// [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS], this is the same as g_uri_get_userinfo().
262 ///
263 /// # Returns
264 ///
265 /// @self's user.
266 #[doc(alias = "g_uri_get_user")]
267 #[doc(alias = "get_user")]
268 pub fn user(&self) -> Option<crate::GString> {
269 unsafe { from_glib_none(ffi::g_uri_get_user(self.to_glib_none().0)) }
270 }
271
272 /// Gets @self's userinfo, which may contain `%`-encoding, depending on
273 /// the flags with which @self was created.
274 ///
275 /// # Returns
276 ///
277 /// @self's userinfo.
278 #[doc(alias = "g_uri_get_userinfo")]
279 #[doc(alias = "get_userinfo")]
280 pub fn userinfo(&self) -> Option<crate::GString> {
281 unsafe { from_glib_none(ffi::g_uri_get_userinfo(self.to_glib_none().0)) }
282 }
283
284 /// Parses @uri_ref according to @flags and, if it is a
285 /// [relative URI](#relative-and-absolute-uris), resolves it relative to @self.
286 /// If the result is not a valid absolute URI, it will be discarded, and an error
287 /// returned.
288 /// ## `uri_ref`
289 /// a string representing a relative or absolute URI
290 /// ## `flags`
291 /// flags describing how to parse @uri_ref
292 ///
293 /// # Returns
294 ///
295 /// a new #GUri, or NULL on error.
296 #[doc(alias = "g_uri_parse_relative")]
297 pub fn parse_relative(&self, uri_ref: &str, flags: UriFlags) -> Result<Uri, crate::Error> {
298 unsafe {
299 let mut error = std::ptr::null_mut();
300 let ret = ffi::g_uri_parse_relative(
301 self.to_glib_none().0,
302 uri_ref.to_glib_none().0,
303 flags.into_glib(),
304 &mut error,
305 );
306 if error.is_null() {
307 Ok(from_glib_full(ret))
308 } else {
309 Err(from_glib_full(error))
310 }
311 }
312 }
313
314 /// Returns a string representing @self.
315 ///
316 /// This is not guaranteed to return a string which is identical to the
317 /// string that @self was parsed from. However, if the source URI was
318 /// syntactically correct (according to RFC 3986), and it was parsed
319 /// with [`UriFlags::ENCODED`][crate::UriFlags::ENCODED], then g_uri_to_string() is guaranteed to return
320 /// a string which is at least semantically equivalent to the source
321 /// URI (according to RFC 3986).
322 ///
323 /// If @self might contain sensitive details, such as authentication parameters,
324 /// or private data in its query string, and the returned string is going to be
325 /// logged, then consider using g_uri_to_string_partial() to redact parts.
326 ///
327 /// # Returns
328 ///
329 /// a string representing @self,
330 /// which the caller must free.
331 #[doc(alias = "g_uri_to_string")]
332 #[doc(alias = "to_string")]
333 pub fn to_str(&self) -> crate::GString {
334 unsafe { from_glib_full(ffi::g_uri_to_string(self.to_glib_none().0)) }
335 }
336
337 /// Returns a string representing @self, subject to the options in
338 /// @flags. See g_uri_to_string() and #GUriHideFlags for more details.
339 /// ## `flags`
340 /// flags describing what parts of @self to hide
341 ///
342 /// # Returns
343 ///
344 /// a string representing
345 /// @self, which the caller must free.
346 #[doc(alias = "g_uri_to_string_partial")]
347 pub fn to_string_partial(&self, flags: UriHideFlags) -> crate::GString {
348 unsafe {
349 from_glib_full(ffi::g_uri_to_string_partial(
350 self.to_glib_none().0,
351 flags.into_glib(),
352 ))
353 }
354 }
355
356 /// Creates a new #GUri from the given components according to @flags.
357 ///
358 /// See also g_uri_build_with_user(), which allows specifying the
359 /// components of the "userinfo" separately.
360 /// ## `flags`
361 /// flags describing how to build the #GUri
362 /// ## `scheme`
363 /// the URI scheme
364 /// ## `userinfo`
365 /// the userinfo component, or [`None`]
366 /// ## `host`
367 /// the host component, or [`None`]
368 /// ## `port`
369 /// the port, or `-1`
370 /// ## `path`
371 /// the path component
372 /// ## `query`
373 /// the query component, or [`None`]
374 /// ## `fragment`
375 /// the fragment, or [`None`]
376 ///
377 /// # Returns
378 ///
379 /// a new #GUri
380 #[doc(alias = "g_uri_build")]
381 pub fn build(
382 flags: UriFlags,
383 scheme: &str,
384 userinfo: Option<&str>,
385 host: Option<&str>,
386 port: i32,
387 path: &str,
388 query: Option<&str>,
389 fragment: Option<&str>,
390 ) -> Uri {
391 unsafe {
392 from_glib_full(ffi::g_uri_build(
393 flags.into_glib(),
394 scheme.to_glib_none().0,
395 userinfo.to_glib_none().0,
396 host.to_glib_none().0,
397 port,
398 path.to_glib_none().0,
399 query.to_glib_none().0,
400 fragment.to_glib_none().0,
401 ))
402 }
403 }
404
405 /// Creates a new #GUri from the given components according to @flags
406 /// ([`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] is added unconditionally). The @flags must be
407 /// coherent with the passed values, in particular use `%`-encoded values with
408 /// [`UriFlags::ENCODED`][crate::UriFlags::ENCODED].
409 ///
410 /// In contrast to g_uri_build(), this allows specifying the components
411 /// of the ‘userinfo’ field separately. Note that @user must be non-[`None`]
412 /// if either @password or @auth_params is non-[`None`].
413 /// ## `flags`
414 /// flags describing how to build the #GUri
415 /// ## `scheme`
416 /// the URI scheme
417 /// ## `user`
418 /// the user component of the userinfo, or [`None`]
419 /// ## `password`
420 /// the password component of the userinfo, or [`None`]
421 /// ## `auth_params`
422 /// the auth params of the userinfo, or [`None`]
423 /// ## `host`
424 /// the host component, or [`None`]
425 /// ## `port`
426 /// the port, or `-1`
427 /// ## `path`
428 /// the path component
429 /// ## `query`
430 /// the query component, or [`None`]
431 /// ## `fragment`
432 /// the fragment, or [`None`]
433 ///
434 /// # Returns
435 ///
436 /// a new #GUri
437 #[doc(alias = "g_uri_build_with_user")]
438 pub fn build_with_user(
439 flags: UriFlags,
440 scheme: &str,
441 user: Option<&str>,
442 password: Option<&str>,
443 auth_params: Option<&str>,
444 host: Option<&str>,
445 port: i32,
446 path: &str,
447 query: Option<&str>,
448 fragment: Option<&str>,
449 ) -> Uri {
450 unsafe {
451 from_glib_full(ffi::g_uri_build_with_user(
452 flags.into_glib(),
453 scheme.to_glib_none().0,
454 user.to_glib_none().0,
455 password.to_glib_none().0,
456 auth_params.to_glib_none().0,
457 host.to_glib_none().0,
458 port,
459 path.to_glib_none().0,
460 query.to_glib_none().0,
461 fragment.to_glib_none().0,
462 ))
463 }
464 }
465
466 /// Escapes arbitrary data for use in a URI.
467 ///
468 /// Normally all characters that are not ‘unreserved’ (i.e. ASCII
469 /// alphanumerical characters plus dash, dot, underscore and tilde) are
470 /// escaped. But if you specify characters in @reserved_chars_allowed
471 /// they are not escaped. This is useful for the ‘reserved’ characters
472 /// in the URI specification, since those are allowed unescaped in some
473 /// portions of a URI.
474 ///
475 /// Though technically incorrect, this will also allow escaping nul
476 /// bytes as `%``00`.
477 /// ## `unescaped`
478 /// the unescaped input data.
479 /// ## `reserved_chars_allowed`
480 /// a string of reserved
481 /// characters that are allowed to be used, or [`None`].
482 ///
483 /// # Returns
484 ///
485 /// an escaped version of @unescaped.
486 /// The returned string should be freed when no longer needed.
487 #[doc(alias = "g_uri_escape_bytes")]
488 pub fn escape_bytes(unescaped: &[u8], reserved_chars_allowed: Option<&str>) -> crate::GString {
489 let length = unescaped.len() as _;
490 unsafe {
491 from_glib_full(ffi::g_uri_escape_bytes(
492 unescaped.to_glib_none().0,
493 length,
494 reserved_chars_allowed.to_glib_none().0,
495 ))
496 }
497 }
498
499 /// Escapes a string for use in a URI.
500 ///
501 /// Normally all characters that are not "unreserved" (i.e. ASCII
502 /// alphanumerical characters plus dash, dot, underscore and tilde) are
503 /// escaped. But if you specify characters in @reserved_chars_allowed
504 /// they are not escaped. This is useful for the "reserved" characters
505 /// in the URI specification, since those are allowed unescaped in some
506 /// portions of a URI.
507 /// ## `unescaped`
508 /// the unescaped input string.
509 /// ## `reserved_chars_allowed`
510 /// a string of reserved
511 /// characters that are allowed to be used, or [`None`].
512 /// ## `allow_utf8`
513 /// [`true`] if the result can include UTF-8 characters.
514 ///
515 /// # Returns
516 ///
517 /// an escaped version of @unescaped. The
518 /// returned string should be freed when no longer needed.
519 #[doc(alias = "g_uri_escape_string")]
520 pub fn escape_string(
521 unescaped: &str,
522 reserved_chars_allowed: Option<&str>,
523 allow_utf8: bool,
524 ) -> crate::GString {
525 unsafe {
526 from_glib_full(ffi::g_uri_escape_string(
527 unescaped.to_glib_none().0,
528 reserved_chars_allowed.to_glib_none().0,
529 allow_utf8.into_glib(),
530 ))
531 }
532 }
533
534 /// Parses @uri_string according to @flags, to determine whether it is a valid
535 /// [absolute URI](#relative-and-absolute-uris), i.e. it does not need to be resolved
536 /// relative to another URI using g_uri_parse_relative().
537 ///
538 /// If it’s not a valid URI, an error is returned explaining how it’s invalid.
539 ///
540 /// See g_uri_split(), and the definition of #GUriFlags, for more
541 /// information on the effect of @flags.
542 /// ## `uri_string`
543 /// a string containing an absolute URI
544 /// ## `flags`
545 /// flags for parsing @uri_string
546 ///
547 /// # Returns
548 ///
549 /// [`true`] if @uri_string is a valid absolute URI, [`false`] on error.
550 #[doc(alias = "g_uri_is_valid")]
551 pub fn is_valid(uri_string: &str, flags: UriFlags) -> Result<(), crate::Error> {
552 unsafe {
553 let mut error = std::ptr::null_mut();
554 let is_ok =
555 ffi::g_uri_is_valid(uri_string.to_glib_none().0, flags.into_glib(), &mut error);
556 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
557 if error.is_null() {
558 Ok(())
559 } else {
560 Err(from_glib_full(error))
561 }
562 }
563 }
564
565 /// Joins the given components together according to @flags to create
566 /// an absolute URI string. @path may not be [`None`] (though it may be the empty
567 /// string).
568 ///
569 /// When @host is present, @path must either be empty or begin with a slash (`/`)
570 /// character. When @host is not present, @path cannot begin with two slash
571 /// characters (`//`). See
572 /// [RFC 3986, section 3](https://tools.ietf.org/html/rfc3986#section-3).
573 ///
574 /// See also g_uri_join_with_user(), which allows specifying the
575 /// components of the ‘userinfo’ separately.
576 ///
577 /// [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] and [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS] are ignored if set
578 /// in @flags.
579 /// ## `flags`
580 /// flags describing how to build the URI string
581 /// ## `scheme`
582 /// the URI scheme, or [`None`]
583 /// ## `userinfo`
584 /// the userinfo component, or [`None`]
585 /// ## `host`
586 /// the host component, or [`None`]
587 /// ## `port`
588 /// the port, or `-1`
589 /// ## `path`
590 /// the path component
591 /// ## `query`
592 /// the query component, or [`None`]
593 /// ## `fragment`
594 /// the fragment, or [`None`]
595 ///
596 /// # Returns
597 ///
598 /// an absolute URI string
599 #[doc(alias = "g_uri_join")]
600 pub fn join(
601 flags: UriFlags,
602 scheme: Option<&str>,
603 userinfo: Option<&str>,
604 host: Option<&str>,
605 port: i32,
606 path: &str,
607 query: Option<&str>,
608 fragment: Option<&str>,
609 ) -> crate::GString {
610 unsafe {
611 from_glib_full(ffi::g_uri_join(
612 flags.into_glib(),
613 scheme.to_glib_none().0,
614 userinfo.to_glib_none().0,
615 host.to_glib_none().0,
616 port,
617 path.to_glib_none().0,
618 query.to_glib_none().0,
619 fragment.to_glib_none().0,
620 ))
621 }
622 }
623
624 /// Joins the given components together according to @flags to create
625 /// an absolute URI string. @path may not be [`None`] (though it may be the empty
626 /// string).
627 ///
628 /// In contrast to g_uri_join(), this allows specifying the components
629 /// of the ‘userinfo’ separately. It otherwise behaves the same.
630 ///
631 /// [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] and [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS] are ignored if set
632 /// in @flags.
633 /// ## `flags`
634 /// flags describing how to build the URI string
635 /// ## `scheme`
636 /// the URI scheme, or [`None`]
637 /// ## `user`
638 /// the user component of the userinfo, or [`None`]
639 /// ## `password`
640 /// the password component of the userinfo, or
641 /// [`None`]
642 /// ## `auth_params`
643 /// the auth params of the userinfo, or
644 /// [`None`]
645 /// ## `host`
646 /// the host component, or [`None`]
647 /// ## `port`
648 /// the port, or `-1`
649 /// ## `path`
650 /// the path component
651 /// ## `query`
652 /// the query component, or [`None`]
653 /// ## `fragment`
654 /// the fragment, or [`None`]
655 ///
656 /// # Returns
657 ///
658 /// an absolute URI string
659 #[doc(alias = "g_uri_join_with_user")]
660 pub fn join_with_user(
661 flags: UriFlags,
662 scheme: Option<&str>,
663 user: Option<&str>,
664 password: Option<&str>,
665 auth_params: Option<&str>,
666 host: Option<&str>,
667 port: i32,
668 path: &str,
669 query: Option<&str>,
670 fragment: Option<&str>,
671 ) -> crate::GString {
672 unsafe {
673 from_glib_full(ffi::g_uri_join_with_user(
674 flags.into_glib(),
675 scheme.to_glib_none().0,
676 user.to_glib_none().0,
677 password.to_glib_none().0,
678 auth_params.to_glib_none().0,
679 host.to_glib_none().0,
680 port,
681 path.to_glib_none().0,
682 query.to_glib_none().0,
683 fragment.to_glib_none().0,
684 ))
685 }
686 }
687
688 /// Splits an URI list conforming to the text/uri-list
689 /// mime type defined in RFC 2483 into individual URIs,
690 /// discarding any comments. The URIs are not validated.
691 /// ## `uri_list`
692 /// an URI list
693 ///
694 /// # Returns
695 ///
696 /// a newly allocated [`None`]-terminated list
697 /// of strings holding the individual URIs. The array should be freed
698 /// with g_strfreev().
699 #[doc(alias = "g_uri_list_extract_uris")]
700 pub fn list_extract_uris(uri_list: &str) -> Vec<crate::GString> {
701 unsafe {
702 FromGlibPtrContainer::from_glib_full(ffi::g_uri_list_extract_uris(
703 uri_list.to_glib_none().0,
704 ))
705 }
706 }
707
708 /// Parses @uri_string according to @flags. If the result is not a
709 /// valid [absolute URI](#relative-and-absolute-uris), it will be discarded, and an
710 /// error returned.
711 /// ## `uri_string`
712 /// a string representing an absolute URI
713 /// ## `flags`
714 /// flags describing how to parse @uri_string
715 ///
716 /// # Returns
717 ///
718 /// a new #GUri, or NULL on error.
719 #[doc(alias = "g_uri_parse")]
720 pub fn parse(uri_string: &str, flags: UriFlags) -> Result<Uri, crate::Error> {
721 unsafe {
722 let mut error = std::ptr::null_mut();
723 let ret = ffi::g_uri_parse(uri_string.to_glib_none().0, flags.into_glib(), &mut error);
724 if error.is_null() {
725 Ok(from_glib_full(ret))
726 } else {
727 Err(from_glib_full(error))
728 }
729 }
730 }
731
732 //#[doc(alias = "g_uri_parse_params")]
733 //pub fn parse_params(params: &str, separators: &str, flags: UriParamsFlags) -> Result</*Unknown conversion*//*Unimplemented*/HashTable TypeId { ns_id: 0, id: 28 }/TypeId { ns_id: 0, id: 28 }, crate::Error> {
734 // unsafe { TODO: call ffi:g_uri_parse_params() }
735 //}
736
737 /// Gets the scheme portion of a URI string.
738 /// [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3) decodes the scheme
739 /// as:
740 ///
741 /// ```text
742 /// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
743 /// ```
744 /// Common schemes include `file`, `https`, `svn+ssh`, etc.
745 /// ## `uri`
746 /// a valid URI.
747 ///
748 /// # Returns
749 ///
750 /// The ‘scheme’ component of the URI, or
751 /// [`None`] on error. The returned string should be freed when no longer needed.
752 #[doc(alias = "g_uri_parse_scheme")]
753 pub fn parse_scheme(uri: &str) -> Option<crate::GString> {
754 unsafe { from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) }
755 }
756
757 /// Gets the scheme portion of a URI string.
758 /// [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3) decodes the scheme
759 /// as:
760 ///
761 /// ```text
762 /// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
763 /// ```
764 /// Common schemes include `file`, `https`, `svn+ssh`, etc.
765 ///
766 /// Unlike g_uri_parse_scheme(), the returned scheme is normalized to
767 /// all-lowercase and does not need to be freed.
768 /// ## `uri`
769 /// a valid URI.
770 ///
771 /// # Returns
772 ///
773 /// The ‘scheme’ component of the URI, or
774 /// [`None`] on error. The returned string is normalized to all-lowercase, and
775 /// interned via g_intern_string(), so it does not need to be freed.
776 #[doc(alias = "g_uri_peek_scheme")]
777 pub fn peek_scheme(uri: &str) -> Option<crate::GString> {
778 unsafe { from_glib_none(ffi::g_uri_peek_scheme(uri.to_glib_none().0)) }
779 }
780
781 /// Parses @uri_ref according to @flags and, if it is a
782 /// [relative URI](#relative-and-absolute-uris), resolves it relative to
783 /// @base_uri_string. If the result is not a valid absolute URI, it will be
784 /// discarded, and an error returned.
785 ///
786 /// (If @base_uri_string is [`None`], this just returns @uri_ref, or
787 /// [`None`] if @uri_ref is invalid or not absolute.)
788 /// ## `base_uri_string`
789 /// a string representing a base URI
790 /// ## `uri_ref`
791 /// a string representing a relative or absolute URI
792 /// ## `flags`
793 /// flags describing how to parse @uri_ref
794 ///
795 /// # Returns
796 ///
797 /// the resolved URI string,
798 /// or NULL on error.
799 #[doc(alias = "g_uri_resolve_relative")]
800 pub fn resolve_relative(
801 base_uri_string: Option<&str>,
802 uri_ref: &str,
803 flags: UriFlags,
804 ) -> Result<crate::GString, crate::Error> {
805 unsafe {
806 let mut error = std::ptr::null_mut();
807 let ret = ffi::g_uri_resolve_relative(
808 base_uri_string.to_glib_none().0,
809 uri_ref.to_glib_none().0,
810 flags.into_glib(),
811 &mut error,
812 );
813 if error.is_null() {
814 Ok(from_glib_full(ret))
815 } else {
816 Err(from_glib_full(error))
817 }
818 }
819 }
820
821 /// Parses @uri_ref (which can be an
822 /// [absolute or relative URI](#relative-and-absolute-uris)) according to @flags, and
823 /// returns the pieces. Any component that doesn't appear in @uri_ref will be
824 /// returned as [`None`] (but note that all URIs always have a path component,
825 /// though it may be the empty string).
826 ///
827 /// If @flags contains [`UriFlags::ENCODED`][crate::UriFlags::ENCODED], then `%`-encoded characters in
828 /// @uri_ref will remain encoded in the output strings. (If not,
829 /// then all such characters will be decoded.) Note that decoding will
830 /// only work if the URI components are ASCII or UTF-8, so you will
831 /// need to use [`UriFlags::ENCODED`][crate::UriFlags::ENCODED] if they are not.
832 ///
833 /// Note that the [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD] and
834 /// [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS] @flags are ignored by g_uri_split(),
835 /// since it always returns only the full userinfo; use
836 /// g_uri_split_with_user() if you want it split up.
837 /// ## `uri_ref`
838 /// a string containing a relative or absolute URI
839 /// ## `flags`
840 /// flags for parsing @uri_ref
841 ///
842 /// # Returns
843 ///
844 /// [`true`] if @uri_ref parsed successfully, [`false`]
845 /// on error.
846 ///
847 /// ## `scheme`
848 /// on return, contains
849 /// the scheme (converted to lowercase), or [`None`]
850 ///
851 /// ## `userinfo`
852 /// on return, contains
853 /// the userinfo, or [`None`]
854 ///
855 /// ## `host`
856 /// on return, contains the
857 /// host, or [`None`]
858 ///
859 /// ## `port`
860 /// on return, contains the
861 /// port, or `-1`
862 ///
863 /// ## `path`
864 /// on return, contains the
865 /// path
866 ///
867 /// ## `query`
868 /// on return, contains the
869 /// query, or [`None`]
870 ///
871 /// ## `fragment`
872 /// on return, contains
873 /// the fragment, or [`None`]
874 #[doc(alias = "g_uri_split")]
875 pub fn split(
876 uri_ref: &str,
877 flags: UriFlags,
878 ) -> Result<
879 (
880 Option<crate::GString>,
881 Option<crate::GString>,
882 Option<crate::GString>,
883 i32,
884 crate::GString,
885 Option<crate::GString>,
886 Option<crate::GString>,
887 ),
888 crate::Error,
889 > {
890 unsafe {
891 let mut scheme = std::ptr::null_mut();
892 let mut userinfo = std::ptr::null_mut();
893 let mut host = std::ptr::null_mut();
894 let mut port = std::mem::MaybeUninit::uninit();
895 let mut path = std::ptr::null_mut();
896 let mut query = std::ptr::null_mut();
897 let mut fragment = std::ptr::null_mut();
898 let mut error = std::ptr::null_mut();
899 let is_ok = ffi::g_uri_split(
900 uri_ref.to_glib_none().0,
901 flags.into_glib(),
902 &mut scheme,
903 &mut userinfo,
904 &mut host,
905 port.as_mut_ptr(),
906 &mut path,
907 &mut query,
908 &mut fragment,
909 &mut error,
910 );
911 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
912 if error.is_null() {
913 Ok((
914 from_glib_full(scheme),
915 from_glib_full(userinfo),
916 from_glib_full(host),
917 port.assume_init(),
918 from_glib_full(path),
919 from_glib_full(query),
920 from_glib_full(fragment),
921 ))
922 } else {
923 Err(from_glib_full(error))
924 }
925 }
926 }
927
928 /// Parses @uri_string (which must be an [absolute URI](#relative-and-absolute-uris))
929 /// according to @flags, and returns the pieces relevant to connecting to a host.
930 /// See the documentation for g_uri_split() for more details; this is
931 /// mostly a wrapper around that function with simpler arguments.
932 /// However, it will return an error if @uri_string is a relative URI,
933 /// or does not contain a hostname component.
934 /// ## `uri_string`
935 /// a string containing an absolute URI
936 /// ## `flags`
937 /// flags for parsing @uri_string
938 ///
939 /// # Returns
940 ///
941 /// [`true`] if @uri_string parsed successfully,
942 /// [`false`] on error.
943 ///
944 /// ## `scheme`
945 /// on return, contains
946 /// the scheme (converted to lowercase), or [`None`]
947 ///
948 /// ## `host`
949 /// on return, contains the
950 /// host, or [`None`]
951 ///
952 /// ## `port`
953 /// on return, contains the
954 /// port, or `-1`
955 #[doc(alias = "g_uri_split_network")]
956 pub fn split_network(
957 uri_string: &str,
958 flags: UriFlags,
959 ) -> Result<(Option<crate::GString>, Option<crate::GString>, i32), crate::Error> {
960 unsafe {
961 let mut scheme = std::ptr::null_mut();
962 let mut host = std::ptr::null_mut();
963 let mut port = std::mem::MaybeUninit::uninit();
964 let mut error = std::ptr::null_mut();
965 let is_ok = ffi::g_uri_split_network(
966 uri_string.to_glib_none().0,
967 flags.into_glib(),
968 &mut scheme,
969 &mut host,
970 port.as_mut_ptr(),
971 &mut error,
972 );
973 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
974 if error.is_null() {
975 Ok((
976 from_glib_full(scheme),
977 from_glib_full(host),
978 port.assume_init(),
979 ))
980 } else {
981 Err(from_glib_full(error))
982 }
983 }
984 }
985
986 /// Parses @uri_ref (which can be an
987 /// [absolute or relative URI](#relative-and-absolute-uris)) according to @flags, and
988 /// returns the pieces. Any component that doesn't appear in @uri_ref will be
989 /// returned as [`None`] (but note that all URIs always have a path component,
990 /// though it may be the empty string).
991 ///
992 /// See g_uri_split(), and the definition of #GUriFlags, for more
993 /// information on the effect of @flags. Note that @password will only
994 /// be parsed out if @flags contains [`UriFlags::HAS_PASSWORD`][crate::UriFlags::HAS_PASSWORD], and
995 /// @auth_params will only be parsed out if @flags contains
996 /// [`UriFlags::HAS_AUTH_PARAMS`][crate::UriFlags::HAS_AUTH_PARAMS].
997 /// ## `uri_ref`
998 /// a string containing a relative or absolute URI
999 /// ## `flags`
1000 /// flags for parsing @uri_ref
1001 ///
1002 /// # Returns
1003 ///
1004 /// [`true`] if @uri_ref parsed successfully, [`false`]
1005 /// on error.
1006 ///
1007 /// ## `scheme`
1008 /// on return, contains
1009 /// the scheme (converted to lowercase), or [`None`]
1010 ///
1011 /// ## `user`
1012 /// on return, contains
1013 /// the user, or [`None`]
1014 ///
1015 /// ## `password`
1016 /// on return, contains
1017 /// the password, or [`None`]
1018 ///
1019 /// ## `auth_params`
1020 /// on return, contains
1021 /// the auth_params, or [`None`]
1022 ///
1023 /// ## `host`
1024 /// on return, contains the
1025 /// host, or [`None`]
1026 ///
1027 /// ## `port`
1028 /// on return, contains the
1029 /// port, or `-1`
1030 ///
1031 /// ## `path`
1032 /// on return, contains the
1033 /// path
1034 ///
1035 /// ## `query`
1036 /// on return, contains the
1037 /// query, or [`None`]
1038 ///
1039 /// ## `fragment`
1040 /// on return, contains
1041 /// the fragment, or [`None`]
1042 #[doc(alias = "g_uri_split_with_user")]
1043 pub fn split_with_user(
1044 uri_ref: &str,
1045 flags: UriFlags,
1046 ) -> Result<
1047 (
1048 Option<crate::GString>,
1049 Option<crate::GString>,
1050 Option<crate::GString>,
1051 Option<crate::GString>,
1052 Option<crate::GString>,
1053 i32,
1054 crate::GString,
1055 Option<crate::GString>,
1056 Option<crate::GString>,
1057 ),
1058 crate::Error,
1059 > {
1060 unsafe {
1061 let mut scheme = std::ptr::null_mut();
1062 let mut user = std::ptr::null_mut();
1063 let mut password = std::ptr::null_mut();
1064 let mut auth_params = std::ptr::null_mut();
1065 let mut host = std::ptr::null_mut();
1066 let mut port = std::mem::MaybeUninit::uninit();
1067 let mut path = std::ptr::null_mut();
1068 let mut query = std::ptr::null_mut();
1069 let mut fragment = std::ptr::null_mut();
1070 let mut error = std::ptr::null_mut();
1071 let is_ok = ffi::g_uri_split_with_user(
1072 uri_ref.to_glib_none().0,
1073 flags.into_glib(),
1074 &mut scheme,
1075 &mut user,
1076 &mut password,
1077 &mut auth_params,
1078 &mut host,
1079 port.as_mut_ptr(),
1080 &mut path,
1081 &mut query,
1082 &mut fragment,
1083 &mut error,
1084 );
1085 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
1086 if error.is_null() {
1087 Ok((
1088 from_glib_full(scheme),
1089 from_glib_full(user),
1090 from_glib_full(password),
1091 from_glib_full(auth_params),
1092 from_glib_full(host),
1093 port.assume_init(),
1094 from_glib_full(path),
1095 from_glib_full(query),
1096 from_glib_full(fragment),
1097 ))
1098 } else {
1099 Err(from_glib_full(error))
1100 }
1101 }
1102 }
1103
1104 /// Unescapes a segment of an escaped string as binary data.
1105 ///
1106 /// Note that in contrast to g_uri_unescape_string(), this does allow
1107 /// nul bytes to appear in the output.
1108 ///
1109 /// If any of the characters in @illegal_characters appears as an escaped
1110 /// character in @escaped_string, then that is an error and [`None`] will be
1111 /// returned. This is useful if you want to avoid for instance having a slash
1112 /// being expanded in an escaped path element, which might confuse pathname
1113 /// handling.
1114 /// ## `escaped_string`
1115 /// A URI-escaped string
1116 /// ## `length`
1117 /// the length (in bytes) of @escaped_string to escape, or `-1` if it
1118 /// is nul-terminated.
1119 /// ## `illegal_characters`
1120 /// a string of illegal characters
1121 /// not to be allowed, or [`None`].
1122 ///
1123 /// # Returns
1124 ///
1125 /// an unescaped version of @escaped_string
1126 /// or [`None`] on error (if decoding failed, using [`UriError::Failed`][crate::UriError::Failed] error
1127 /// code). The returned #GBytes should be unreffed when no longer needed.
1128 #[doc(alias = "g_uri_unescape_bytes")]
1129 pub fn unescape_bytes(
1130 escaped_string: &str,
1131 illegal_characters: Option<&str>,
1132 ) -> Result<Bytes, crate::Error> {
1133 let length = escaped_string.len() as _;
1134 unsafe {
1135 let mut error = std::ptr::null_mut();
1136 let ret = ffi::g_uri_unescape_bytes(
1137 escaped_string.to_glib_none().0,
1138 length,
1139 illegal_characters.to_glib_none().0,
1140 &mut error,
1141 );
1142 if error.is_null() {
1143 Ok(from_glib_full(ret))
1144 } else {
1145 Err(from_glib_full(error))
1146 }
1147 }
1148 }
1149
1150 /// Unescapes a segment of an escaped string.
1151 ///
1152 /// If any of the characters in @illegal_characters or the NUL
1153 /// character appears as an escaped character in @escaped_string, then
1154 /// that is an error and [`None`] will be returned. This is useful if you
1155 /// want to avoid for instance having a slash being expanded in an
1156 /// escaped path element, which might confuse pathname handling.
1157 ///
1158 /// Note: `NUL` byte is not accepted in the output, in contrast to
1159 /// g_uri_unescape_bytes().
1160 /// ## `escaped_string`
1161 /// A string, may be [`None`]
1162 /// ## `escaped_string_end`
1163 /// Pointer to end of @escaped_string,
1164 /// may be [`None`]
1165 /// ## `illegal_characters`
1166 /// An optional string of illegal
1167 /// characters not to be allowed, may be [`None`]
1168 ///
1169 /// # Returns
1170 ///
1171 /// an unescaped version of @escaped_string,
1172 /// or [`None`] on error. The returned string should be freed when no longer
1173 /// needed. As a special case if [`None`] is given for @escaped_string, this
1174 /// function will return [`None`].
1175 #[doc(alias = "g_uri_unescape_segment")]
1176 pub fn unescape_segment(
1177 escaped_string: Option<&str>,
1178 escaped_string_end: Option<&str>,
1179 illegal_characters: Option<&str>,
1180 ) -> Option<crate::GString> {
1181 unsafe {
1182 from_glib_full(ffi::g_uri_unescape_segment(
1183 escaped_string.to_glib_none().0,
1184 escaped_string_end.to_glib_none().0,
1185 illegal_characters.to_glib_none().0,
1186 ))
1187 }
1188 }
1189
1190 /// Unescapes a whole escaped string.
1191 ///
1192 /// If any of the characters in @illegal_characters or the NUL
1193 /// character appears as an escaped character in @escaped_string, then
1194 /// that is an error and [`None`] will be returned. This is useful if you
1195 /// want to avoid for instance having a slash being expanded in an
1196 /// escaped path element, which might confuse pathname handling.
1197 /// ## `escaped_string`
1198 /// an escaped string to be unescaped.
1199 /// ## `illegal_characters`
1200 /// a string of illegal characters
1201 /// not to be allowed, or [`None`].
1202 ///
1203 /// # Returns
1204 ///
1205 /// an unescaped version of @escaped_string.
1206 /// The returned string should be freed when no longer needed.
1207 #[doc(alias = "g_uri_unescape_string")]
1208 pub fn unescape_string(
1209 escaped_string: &str,
1210 illegal_characters: Option<&str>,
1211 ) -> Option<crate::GString> {
1212 unsafe {
1213 from_glib_full(ffi::g_uri_unescape_string(
1214 escaped_string.to_glib_none().0,
1215 illegal_characters.to_glib_none().0,
1216 ))
1217 }
1218 }
1219}
1220
1221impl std::fmt::Display for Uri {
1222 #[inline]
1223 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1224 f.write_str(&self.to_str())
1225 }
1226}
1227
1228unsafe impl Send for Uri {}
1229unsafe impl Sync for Uri {}