blob: 11beb65153cbd871b8ca5b8eb6e4618ee2fe2c98 [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="Server implementation of the HTTP/2 protocol."><meta name="keywords" content="rust, rustlang, rust-lang, server"><title>h2::server - 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="../../h2/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="../../h2/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module server</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">h2</a>::<wbr><a class="mod" href="#">server</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/h2/server.rs.html#1-1613">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>Server implementation of the HTTP/2 protocol.</p>
<h2 id="getting-started"><a href="#getting-started">Getting started</a></h2>
<p>Running an HTTP/2 server requires the caller to manage accepting the
connections as well as getting the connections to a state that is ready to
begin the HTTP/2 handshake. See <a href="../index.html#handshake">here</a> for more
details.</p>
<p>This could be as basic as using Tokio’s <a href="https://docs.rs/tokio-core/0.1/tokio_core/net/struct.TcpListener.html"><code>TcpListener</code></a> to accept
connections, but usually it means using either ALPN or HTTP/1.1 protocol
upgrades.</p>
<p>Once a connection is obtained, it is passed to <a href="fn.handshake.html"><code>handshake</code></a>,
which will begin the <a href="http://httpwg.org/specs/rfc7540.html#ConnectionHeader">HTTP/2 handshake</a>. This returns a future that
completes once the handshake process is performed and HTTP/2 streams may
be received.</p>
<p><a href="fn.handshake.html"><code>handshake</code></a> uses default configuration values. There are a number of
settings that can be changed by using <a href="struct.Builder.html"><code>Builder</code></a> instead.</p>
<h2 id="inbound-streams"><a href="#inbound-streams">Inbound streams</a></h2>
<p>The <a href="struct.Connection.html"><code>Connection</code></a> instance is used to accept inbound HTTP/2 streams. It
does this by implementing <a href="https://docs.rs/futures/0.1/futures/stream/trait.Stream.html"><code>futures::Stream</code></a>. When a new stream is
received, a call to <a href="struct.Connection.html#method.accept" title="Connection::accept"><code>Connection::accept</code></a> will return <code>(request, response)</code>.
The <code>request</code> handle (of type <a href="../struct.RecvStream.html"><code>http::Request&lt;RecvStream&gt;</code></a>) contains the
HTTP request head as well as provides a way to receive the inbound data
stream and the trailers. The <code>response</code> handle (of type <a href="struct.SendResponse.html" title="SendResponse"><code>SendResponse</code></a>)
allows responding to the request, stream the response payload, send
trailers, and send push promises.</p>
<p>The send (<a href="../struct.SendStream.html"><code>SendStream</code></a>) and receive (<a href="../struct.RecvStream.html"><code>RecvStream</code></a>) halves of the stream
can be operated independently.</p>
<h2 id="managing-the-connection"><a href="#managing-the-connection">Managing the connection</a></h2>
<p>The <a href="struct.Connection.html"><code>Connection</code></a> instance is used to manage connection state. The caller
is required to call either <a href="struct.Connection.html#method.accept" title="Connection::accept"><code>Connection::accept</code></a> or
<a href="struct.Connection.html#method.poll_close"><code>Connection::poll_close</code></a> in order to advance the connection state. Simply
operating on <a href="../struct.SendStream.html"><code>SendStream</code></a> or <a href="../struct.RecvStream.html"><code>RecvStream</code></a> will have no effect unless the
connection state is advanced.</p>
<p>It is not required to call <strong>both</strong> <a href="struct.Connection.html#method.accept" title="Connection::accept"><code>Connection::accept</code></a> and
<a href="struct.Connection.html#method.poll_close"><code>Connection::poll_close</code></a>. If the caller is ready to accept a new stream,
then only <a href="struct.Connection.html#method.accept" title="Connection::accept"><code>Connection::accept</code></a> should be called. When the caller <strong>does
not</strong> want to accept a new stream, <a href="struct.Connection.html#method.poll_close"><code>Connection::poll_close</code></a> should be
called.</p>
<p>The <a href="struct.Connection.html"><code>Connection</code></a> instance should only be dropped once
<a href="struct.Connection.html#method.poll_close"><code>Connection::poll_close</code></a> returns <code>Ready</code>. Once <a href="struct.Connection.html#method.accept" title="Connection::accept"><code>Connection::accept</code></a>
returns <code>Ready(None)</code>, there will no longer be any more inbound streams. At
this point, only <a href="struct.Connection.html#method.poll_close"><code>Connection::poll_close</code></a> should be called.</p>
<h2 id="shutting-down-the-server"><a href="#shutting-down-the-server">Shutting down the server</a></h2>
<p>Graceful shutdown of the server is <a href="https://github.com/hyperium/h2/issues/69">not yet
implemented</a>.</p>
<h2 id="example"><a href="#example">Example</a></h2>
<p>A basic HTTP/2 server example that runs over TCP and assumes <a href="http://httpwg.org/specs/rfc7540.html#known-http">prior
knowledge</a>, i.e. both the client and the server assume that the TCP socket
will use the HTTP/2 protocol without prior negotiation.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>h2::server;
<span class="kw">use </span>http::{Response, StatusCode};
<span class="kw">use </span>tokio::net::TcpListener;
<span class="attribute">#[tokio::main]
</span><span class="kw">pub async fn </span>main() {
<span class="kw">let </span><span class="kw-2">mut </span>listener = TcpListener::bind(<span class="string">&quot;127.0.0.1:5928&quot;</span>).<span class="kw">await</span>.unwrap();
<span class="comment">// Accept all incoming TCP connections.
</span><span class="kw">loop </span>{
<span class="kw">if let </span><span class="prelude-val">Ok</span>((socket, _peer_addr)) = listener.accept().<span class="kw">await </span>{
<span class="comment">// Spawn a new task to process each connection.
</span>tokio::spawn(<span class="kw">async </span>{
<span class="comment">// Start the HTTP/2 connection handshake
</span><span class="kw">let </span><span class="kw-2">mut </span>h2 = server::handshake(socket).<span class="kw">await</span>.unwrap();
<span class="comment">// Accept all inbound HTTP/2 streams sent over the
// connection.
</span><span class="kw">while let </span><span class="prelude-val">Some</span>(request) = h2.accept().<span class="kw">await </span>{
<span class="kw">let </span>(request, <span class="kw-2">mut </span>respond) = request.unwrap();
<span class="macro">println!</span>(<span class="string">&quot;Received request: {:?}&quot;</span>, request);
<span class="comment">// Build a response with no body
</span><span class="kw">let </span>response = Response::builder()
.status(StatusCode::OK)
.body(())
.unwrap();
<span class="comment">// Send the response back to the client
</span>respond.send_response(response, <span class="bool-val">true</span>)
.unwrap();
}
});
}
}
}</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="h2::server::Builder struct">Builder</a></div><div class="item-right docblock-short">Builds server connections with custom configuration values.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Connection.html" title="h2::server::Connection struct">Connection</a></div><div class="item-right docblock-short">Accepts inbound HTTP/2 streams on a connection.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Handshake.html" title="h2::server::Handshake struct">Handshake</a></div><div class="item-right docblock-short">In progress HTTP/2 connection handshake future.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SendPushedResponse.html" title="h2::server::SendPushedResponse struct">SendPushedResponse</a></div><div class="item-right docblock-short">Send a response to a promised request</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SendResponse.html" title="h2::server::SendResponse struct">SendResponse</a></div><div class="item-right docblock-short">Send a response back to the client</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="h2::server::handshake fn">handshake</a></div><div class="item-right docblock-short">Creates a new configured HTTP/2 server with default configuration
values backed by <code>io</code>.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="h2" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>