blob: 6e139d9f2926245bbb190f80efd36172ff880dcc [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 Server connection API."><meta name="keywords" content="rust, rustlang, rust-lang, conn"><title>hyper::server::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></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">server</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/server/conn.rs.html#1-1079">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 Server connection API.</p>
<p>The types in this module are to provide a lower-level API based around a
single connection. Accepting a connection and binding it with a service
are not handled at this level. This module provides the building blocks to
customize those things externally.</p>
<p>If you don’t have need to manage connections yourself, consider using the
higher-level <a href="../index.html">Server</a> API.</p>
<h3 id="example"><a href="#example">Example</a></h3>
<p>A simple example that uses the <code>Http</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>http::{Request, Response, StatusCode};
<span class="kw">use </span>hyper::{server::conn::Http, service::service_fn, Body};
<span class="kw">use </span>std::{net::SocketAddr, convert::Infallible};
<span class="kw">use </span>tokio::net::TcpListener;
<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 + Send + Sync&gt;&gt; {
<span class="kw">let </span>addr: SocketAddr = ([<span class="number">127</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>], <span class="number">8080</span>).into();
<span class="kw">let </span><span class="kw-2">mut </span>tcp_listener = TcpListener::bind(addr).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">loop </span>{
<span class="kw">let </span>(tcp_stream, <span class="kw">_</span>) = tcp_listener.accept().<span class="kw">await</span><span class="question-mark">?</span>;
tokio::task::spawn(<span class="kw">async move </span>{
<span class="kw">if let </span><span class="prelude-val">Err</span>(http_err) = Http::new()
.http1_only(<span class="bool-val">true</span>)
.http1_keep_alive(<span class="bool-val">true</span>)
.serve_connection(tcp_stream, service_fn(hello))
.<span class="kw">await </span>{
<span class="macro">eprintln!</span>(<span class="string">&quot;Error while serving HTTP connection: {}&quot;</span>, http_err);
}
});
}
}
<span class="kw">async fn </span>hello(_req: Request&lt;Body&gt;) -&gt; <span class="prelude-ty">Result</span>&lt;Response&lt;Body&gt;, Infallible&gt; {
<span class="prelude-val">Ok</span>(Response::new(Body::from(<span class="string">&quot;Hello World!&quot;</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.AddrIncoming.html" title="hyper::server::conn::AddrIncoming struct">AddrIncoming</a></div><div class="item-right docblock-short">A stream of connections from binding to an address.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.AddrStream.html" title="hyper::server::conn::AddrStream struct">AddrStream</a></div><div class="item-right docblock-short">A transport returned yieled by <code>AddrIncoming</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Connecting.html" title="hyper::server::conn::Connecting struct">Connecting</a></div><div class="item-right docblock-short">A future building a new <code>Service</code> to a <code>Connection</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Connection.html" title="hyper::server::conn::Connection struct">Connection</a></div><div class="item-right docblock-short">A future binding a connection with a Service.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Http.html" title="hyper::server::conn::Http struct">Http</a></div><div class="item-right docblock-short">A lower-level configuration of the HTTP protocol.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Parts.html" title="hyper::server::conn::Parts struct">Parts</a></div><div class="item-right docblock-short">Deconstructed parts of a <code>Connection</code>.</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>