83 lines
17 KiB
HTML
83 lines
17 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="rust-url is an implementation of the URL Standard for the Rust programming language."><meta name="keywords" content="rust, rustlang, rust-lang, url"><title>url - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
|
||
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../url/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.png" alt="logo"></div>
|
||
</a><h2 class="location">Crate url</h2><div class="block version"><div class="narrow-helper"></div><p>Version 2.2.2</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all url's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#types">Type Definitions</a></li></ul></div><div id="sidebar-vars" data-name="url" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../url/index.html"><img class="rust-logo" src="../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">url</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/url/lib.rs.html#9-2889" title="goto source code">[src]</a></span></h1><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>rust-url is an implementation of the <a href="http://url.spec.whatwg.org/">URL Standard</a>
|
||
for the <a href="http://rust-lang.org/">Rust</a> programming language.</p>
|
||
<h2 id="url-parsing-and-data-structures" class="section-header"><a href="#url-parsing-and-data-structures">URL parsing and data structures</a></h2>
|
||
<p>First, URL parsing may fail for various reasons and therefore returns a <code>Result</code>.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">url</span>::{<span class="ident">Url</span>, <span class="ident">ParseError</span>};
|
||
|
||
<span class="macro">assert!</span>(<span class="ident">Url::parse</span>(<span class="string">"http://[:::1]"</span>) <span class="op">==</span> <span class="prelude-val">Err</span>(<span class="ident">ParseError::InvalidIpv6Address</span>))</code></pre></div>
|
||
<p>Let’s parse a valid URL and look at its components.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">url</span>::{<span class="ident">Url</span>, <span class="ident">Host</span>, <span class="ident">Position</span>};
|
||
<span class="kw">let</span> <span class="ident">issue_list_url</span> <span class="op">=</span> <span class="ident">Url::parse</span>(
|
||
<span class="string">"https://github.com/rust-lang/rust/issues?labels=E-easy&state=open"</span>
|
||
)<span class="question-mark">?</span>;
|
||
|
||
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">scheme</span>() <span class="op">==</span> <span class="string">"https"</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">username</span>() <span class="op">==</span> <span class="string">""</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">password</span>() <span class="op">==</span> <span class="prelude-val">None</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">host_str</span>() <span class="op">==</span> <span class="prelude-val">Some</span>(<span class="string">"github.com"</span>));
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">host</span>() <span class="op">==</span> <span class="prelude-val">Some</span>(<span class="ident">Host::Domain</span>(<span class="string">"github.com"</span>)));
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">port</span>() <span class="op">==</span> <span class="prelude-val">None</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">path</span>() <span class="op">==</span> <span class="string">"/rust-lang/rust/issues"</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">path_segments</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span>.<span class="ident">collect</span>::<span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="kw">_</span><span class="op">></span><span class="op">></span>()) <span class="op">==</span>
|
||
<span class="prelude-val">Some</span>(<span class="macro">vec!</span>[<span class="string">"rust-lang"</span>, <span class="string">"rust"</span>, <span class="string">"issues"</span>]));
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">query</span>() <span class="op">==</span> <span class="prelude-val">Some</span>(<span class="string">"labels=E-easy&state=open"</span>));
|
||
<span class="macro">assert!</span>(<span class="kw-2">&</span><span class="ident">issue_list_url</span>[<span class="ident">Position::BeforePath</span>..] <span class="op">==</span> <span class="string">"/rust-lang/rust/issues?labels=E-easy&state=open"</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">issue_list_url</span>.<span class="ident">fragment</span>() <span class="op">==</span> <span class="prelude-val">None</span>);
|
||
<span class="macro">assert!</span>(<span class="op">!</span><span class="ident">issue_list_url</span>.<span class="ident">cannot_be_a_base</span>());</code></pre></div>
|
||
<p>Some URLs are said to be <em>cannot-be-a-base</em>:
|
||
they don’t have a username, password, host, or port,
|
||
and their “path” is an arbitrary string rather than slash-separated segments:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">url::Url</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">data_url</span> <span class="op">=</span> <span class="ident">Url::parse</span>(<span class="string">"data:text/plain,Hello?World#"</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">cannot_be_a_base</span>());
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">scheme</span>() <span class="op">==</span> <span class="string">"data"</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">path</span>() <span class="op">==</span> <span class="string">"text/plain,Hello"</span>);
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">path_segments</span>().<span class="ident">is_none</span>());
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">query</span>() <span class="op">==</span> <span class="prelude-val">Some</span>(<span class="string">"World"</span>));
|
||
<span class="macro">assert!</span>(<span class="ident">data_url</span>.<span class="ident">fragment</span>() <span class="op">==</span> <span class="prelude-val">Some</span>(<span class="string">""</span>));</code></pre></div>
|
||
<h3 id="serde" class="section-header"><a href="#serde">Serde</a></h3>
|
||
<p>Enable the <code>serde</code> feature to include <code>Deserialize</code> and <code>Serialize</code> implementations for <code>url::Url</code>.</p>
|
||
<h2 id="base-url" class="section-header"><a href="#base-url">Base URL</a></h2>
|
||
<p>Many contexts allow URL <em>references</em> that can be relative to a <em>base URL</em>:</p>
|
||
<div class="example-wrap"><pre class="language-html"><code><link rel="stylesheet" href="../main.css"></code></pre></div>
|
||
<p>Since parsed URLs are absolute, giving a base is required for parsing relative URLs:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">url</span>::{<span class="ident">Url</span>, <span class="ident">ParseError</span>};
|
||
|
||
<span class="macro">assert!</span>(<span class="ident">Url::parse</span>(<span class="string">"../main.css"</span>) <span class="op">==</span> <span class="prelude-val">Err</span>(<span class="ident">ParseError::RelativeUrlWithoutBase</span>))</code></pre></div>
|
||
<p>Use the <code>join</code> method on an <code>Url</code> to use it as a base URL:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">url::Url</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">this_document</span> <span class="op">=</span> <span class="ident">Url::parse</span>(<span class="string">"http://servo.github.io/rust-url/url/index.html"</span>)<span class="question-mark">?</span>;
|
||
<span class="kw">let</span> <span class="ident">css_url</span> <span class="op">=</span> <span class="ident">this_document</span>.<span class="ident">join</span>(<span class="string">"../main.css"</span>)<span class="question-mark">?</span>;
|
||
<span class="macro">assert_eq!</span>(<span class="ident">css_url</span>.<span class="ident">as_str</span>(), <span class="string">"http://servo.github.io/rust-url/main.css"</span>);</code></pre></div>
|
||
<h2 id="feature-serde" class="section-header"><a href="#feature-serde">Feature: <code>serde</code></a></h2>
|
||
<p>If you enable the <code>serde</code> feature, <a href="struct.Url.html"><code>Url</code></a> will implement
|
||
<a href="https://docs.rs/serde/1/serde/trait.Serialize.html"><code>serde::Serialize</code></a> and
|
||
<a href="https://docs.rs/serde/1/serde/trait.Deserialize.html"><code>serde::Deserialize</code></a>.
|
||
See <a href="https://serde.rs">serde documentation</a> for more information.</p>
|
||
<div class="example-wrap"><pre class="language-toml"><code>url = { version = "2", features = ["serde"] }</code></pre></div></div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left import-item"><code>pub use <a class="mod" href="../form_urlencoded/index.html" title="mod form_urlencoded">form_urlencoded</a>;</code></div><div class="item-right docblock-short"></div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.OpaqueOrigin.html" title="url::OpaqueOrigin struct">OpaqueOrigin</a></div><div class="item-right docblock-short"><p>Opaque identifier for URLs that have file or other schemes</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ParseOptions.html" title="url::ParseOptions struct">ParseOptions</a></div><div class="item-right docblock-short"><p>Full configuration for the URL parser.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.PathSegmentsMut.html" title="url::PathSegmentsMut struct">PathSegmentsMut</a></div><div class="item-right docblock-short"><p>Exposes methods to manipulate the path of an URL that is not cannot-be-base.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Url.html" title="url::Url struct">Url</a></div><div class="item-right docblock-short"><p>A parsed URL record.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.UrlQuery.html" title="url::UrlQuery struct">UrlQuery</a></div><div class="item-right docblock-short"><p>Implementation detail of <code>Url::query_pairs_mut</code>. Typically not used directly.</p>
|
||
</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Host.html" title="url::Host enum">Host</a></div><div class="item-right docblock-short"><p>The host name of an URL.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Origin.html" title="url::Origin enum">Origin</a></div><div class="item-right docblock-short"><p>The origin of an URL</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.ParseError.html" title="url::ParseError enum">ParseError</a></div><div class="item-right docblock-short"><p>Errors that can occur during parsing.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Position.html" title="url::Position enum">Position</a></div><div class="item-right docblock-short"><p>Indicates a position within a URL based on its components.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.SyntaxViolation.html" title="url::SyntaxViolation enum">SyntaxViolation</a></div><div class="item-right docblock-short"><p>Non-fatal syntax violations that can occur during parsing.</p>
|
||
</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.EncodingOverride.html" title="url::EncodingOverride type">EncodingOverride</a></div><div class="item-right docblock-short"></div></div></div></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="url" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |