53 lines
9.5 KiB
HTML
53 lines
9.5 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="API documentation for the Rust `conn` mod in crate `hyper`."><meta name="keywords" content="rust, rustlang, rust-lang, conn"><title>hyper::client::conn - Rust</title><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="../../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../../ayu.css" disabled ><script id="default-settings"></script><script src="../../../storage.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
|
||
<link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png">
|
||
<link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../../hyper/index.html'><div class='logo-container rust-logo'><img src='../../../rust-logo.png' alt='logo'></div></a><p class="location">Module conn</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class="location"><a href="../../index.html">hyper</a>::<wbr><a href="../index.html">client</a></p><script>window.sidebarCurrent = {name: "conn", ty: "mod", relpath: "../"};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../../../theme.js"></script><nav class="sub"><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" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" class="help-button">?</button>
|
||
<a id="settings-menu" href="../../../settings.html"><img src="../../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><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/hyper/client/conn.rs.html#1-808" title="goto source code">[src]</a></span><span class="in-band">Module <a href="../../index.html">hyper</a>::<wbr><a href="../index.html">client</a>::<wbr><a class="mod" href="">conn</a></span></h1><div class="docblock"><p>Lower-level client connection API.</p>
|
||
<p>The types in this module are to provide a lower-level API based around a
|
||
single connection. Connecting to a host, pooling connections, and the like
|
||
are not handled at this level. This module provides the building blocks to
|
||
customize those things externally.</p>
|
||
<p>If don't have need to manage connections yourself, consider using the
|
||
higher-level <a href="../../../hyper/client/index.html">Client</a> API.</p>
|
||
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
|
||
<p>A simple example that uses the <code>SendRequest</code> struct to talk HTTP over a Tokio TCP stream</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">http</span>::{<span class="ident">Request</span>, <span class="ident">StatusCode</span>};
|
||
<span class="kw">use</span> <span class="ident">hyper</span>::{<span class="ident">client</span>::<span class="ident">conn</span>::<span class="ident">Builder</span>, <span class="ident">Body</span>};
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">net</span>::<span class="ident">TcpStream</span>;
|
||
|
||
<span class="attribute">#[<span class="ident">tokio</span>::<span class="ident">main</span>]</span>
|
||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-</span><span class="op">></span> <span class="prelude-ty">Result</span><span class="op"><</span>(), <span class="ident">Box</span><span class="op"><</span><span class="kw">dyn</span> <span class="ident">std</span>::<span class="ident">error</span>::<span class="ident">Error</span><span class="op">></span><span class="op">></span> {
|
||
<span class="kw">let</span> <span class="ident">target_stream</span> <span class="op">=</span> <span class="ident">TcpStream</span>::<span class="ident">connect</span>(<span class="string">"example.com:80"</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let</span> (<span class="kw-2">mut</span> <span class="ident">request_sender</span>, <span class="ident">connection</span>) <span class="op">=</span> <span class="ident">Builder</span>::<span class="ident">new</span>()
|
||
.<span class="ident">handshake</span>::<span class="op"><</span><span class="ident">TcpStream</span>, <span class="ident">Body</span><span class="op">></span>(<span class="ident">target_stream</span>)
|
||
.<span class="kw">await</span><span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// spawn a task to poll the connection and drive the HTTP state</span>
|
||
<span class="ident">tokio</span>::<span class="ident">spawn</span>(<span class="kw">async</span> <span class="kw">move</span> {
|
||
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span> <span class="ident">connection</span>.<span class="kw">await</span> {
|
||
<span class="macro">eprintln</span><span class="macro">!</span>(<span class="string">"Error in connection: {}"</span>, <span class="ident">e</span>);
|
||
}
|
||
});
|
||
|
||
<span class="kw">let</span> <span class="ident">request</span> <span class="op">=</span> <span class="ident">Request</span>::<span class="ident">builder</span>()
|
||
<span class="comment">// We need to manually add the host header because SendRequest does not</span>
|
||
.<span class="ident">header</span>(<span class="string">"Host"</span>, <span class="string">"example.com"</span>)
|
||
.<span class="ident">method</span>(<span class="string">"GET"</span>)
|
||
.<span class="ident">body</span>(<span class="ident">Body</span>::<span class="ident">from</span>(<span class="string">""</span>))<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">response</span> <span class="op">=</span> <span class="ident">request_sender</span>.<span class="ident">send_request</span>(<span class="ident">request</span>).<span class="kw">await</span><span class="question-mark">?</span>;
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">response</span>.<span class="ident">status</span>() <span class="op">=</span><span class="op">=</span> <span class="ident">StatusCode</span>::<span class="ident">OK</span>);
|
||
<span class="prelude-val">Ok</span>(())
|
||
}
|
||
</pre></div>
|
||
</div><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
|
||
<table><tr class="module-item"><td><a class="struct" href="struct.Builder.html" title="hyper::client::conn::Builder struct">Builder</a></td><td class="docblock-short"><p>A builder to configure an HTTP connection.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Connection.html" title="hyper::client::conn::Connection struct">Connection</a></td><td class="docblock-short"><p>A future that processes all HTTP state for the IO object.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Parts.html" title="hyper::client::conn::Parts struct">Parts</a></td><td class="docblock-short"><p>Deconstructed parts of a <code>Connection</code>.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.ResponseFuture.html" title="hyper::client::conn::ResponseFuture struct">ResponseFuture</a></td><td class="docblock-short"><p>A future returned by <code>SendRequest::send_request</code>.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SendRequest.html" title="hyper::client::conn::SendRequest struct">SendRequest</a></td><td class="docblock-short"><p>The sender side of an established connection.</p>
|
||
</td></tr></table><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2>
|
||
<table><tr class="module-item"><td><a class="fn" href="fn.handshake.html" title="hyper::client::conn::handshake fn">handshake</a></td><td class="docblock-short"><p>Returns a handshake future over some IO.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../../";window.currentCrate = "hyper";</script><script src="../../../main.js"></script><script defer src="../../../search-index.js"></script></body></html> |