blob: 5bb690e526115e55f9b7a7453287b4affcd4f0ce [file] [log] [blame]
<!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="Lower-level client connection API."><meta name="keywords" content="rust, rustlang, rust-lang, conn"><title>hyper::client::conn - 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" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.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"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../../hyper/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../hyper/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module conn</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../../index.html">hyper</a>::<wbr><a href="../index.html">client</a>::<wbr><a class="mod" href="#">conn</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></h1><span class="out-of-band"><a class="srclink" href="../../../src/hyper/client/conn.rs.html#1-1156">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><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="../index.html">Client</a> API.</p>
<h3 id="example"><a href="#example">Example</a></h3>
<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"><code><span class="kw">use </span>tower::ServiceExt;
<span class="kw">use </span>http::{Request, StatusCode};
<span class="kw">use </span>hyper::{client::conn, Body};
<span class="kw">use </span>tokio::net::TcpStream;
<span class="attribute">#[tokio::main]
</span><span class="kw">async fn </span>main() -&gt; <span class="prelude-ty">Result</span>&lt;(), Box&lt;<span class="kw">dyn </span>std::error::Error&gt;&gt; {
<span class="kw">let </span>target_stream = TcpStream::connect(<span class="string">&quot;example.com:80&quot;</span>).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>(<span class="kw-2">mut </span>request_sender, connection) = conn::handshake(target_stream).<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>tokio::spawn(<span class="kw">async move </span>{
<span class="kw">if let </span><span class="prelude-val">Err</span>(e) = connection.<span class="kw">await </span>{
<span class="macro">eprintln!</span>(<span class="string">&quot;Error in connection: {}&quot;</span>, e);
}
});
<span class="kw">let </span>request = Request::builder()
<span class="comment">// We need to manually add the host header because SendRequest does not
</span>.header(<span class="string">&quot;Host&quot;</span>, <span class="string">&quot;example.com&quot;</span>)
.method(<span class="string">&quot;GET&quot;</span>)
.body(Body::from(<span class="string">&quot;&quot;</span>))<span class="question-mark">?</span>;
<span class="kw">let </span>response = request_sender.send_request(request).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">assert!</span>(response.status() == StatusCode::OK);
<span class="comment">// To send via the same connection again, it may not work as it may not be ready,
// so we have to wait until the request_sender becomes ready.
</span>request_sender.ready().<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>request = Request::builder()
.header(<span class="string">&quot;Host&quot;</span>, <span class="string">&quot;example.com&quot;</span>)
.method(<span class="string">&quot;GET&quot;</span>)
.body(Body::from(<span class="string">&quot;&quot;</span>))<span class="question-mark">?</span>;
<span class="kw">let </span>response = request_sender.send_request(request).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">assert!</span>(response.status() == StatusCode::OK);
<span class="prelude-val">Ok</span>(())
}
</code></pre></div>
</div></details><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.Builder.html" title="hyper::client::conn::Builder struct">Builder</a></div><div class="item-right docblock-short">A builder to configure an HTTP connection.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Connection.html" title="hyper::client::conn::Connection struct">Connection</a></div><div class="item-right docblock-short">A future that processes all HTTP state for the IO object.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Parts.html" title="hyper::client::conn::Parts struct">Parts</a></div><div class="item-right docblock-short">Deconstructed parts of a <code>Connection</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ResponseFuture.html" title="hyper::client::conn::ResponseFuture struct">ResponseFuture</a></div><div class="item-right docblock-short">A future returned by <code>SendRequest::send_request</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SendRequest.html" title="hyper::client::conn::SendRequest struct">SendRequest</a></div><div class="item-right docblock-short">The sender side of an established connection.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.handshake.html" title="hyper::client::conn::handshake fn">handshake</a></div><div class="item-right docblock-short">Returns a handshake future over some IO.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="hyper" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>