107 lines
14 KiB
HTML
107 lines
14 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 `server` mod in crate `h2`."><meta name="keywords" content="rust, rustlang, rust-lang, server"><title>h2::server - 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="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><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='../../h2/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module server</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'>h2</a></p><script>window.sidebarCurrent = {name: 'server', 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!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></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><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/h2/server.rs.html#1-1355' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>h2</a>::<wbr><a class="mod" href=''>server</a></span></h1><div class='docblock'><p>Server implementation of the HTTP/2.0 protocol.</p>
|
||
<h1 id="getting-started" class="section-header"><a href="#getting-started">Getting started</a></h1>
|
||
<p>Running an HTTP/2.0 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.0 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.0 handshake</a>. This returns a future that
|
||
completes once the handshake process is performed and HTTP/2.0 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>
|
||
<h1 id="inbound-streams" class="section-header"><a href="#inbound-streams">Inbound streams</a></h1>
|
||
<p>The <a href="struct.Connection.html"><code>Connection</code></a> instance is used to accept inbound HTTP/2.0 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.poll"><code>Connection::poll</code></a> will return <code>(request, response)</code>.
|
||
The <code>request</code> handle (of type <a href="../struct.RecvStream.html"><code>http::Request<RecvStream></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.SendStream.html"><code>SendStream</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>
|
||
<h1 id="managing-the-connection" class="section-header"><a href="#managing-the-connection">Managing the connection</a></h1>
|
||
<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.poll"><code>Connection::poll</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.poll"><code>Connection::poll</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.poll"><code>Connection::poll</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.poll"><code>Connection::poll</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>
|
||
<h1 id="shutting-down-the-server" class="section-header"><a href="#shutting-down-the-server">Shutting down the server</a></h1>
|
||
<p>Graceful shutdown of the server is <a href="https://github.com/hyperium/h2/issues/69">not yet
|
||
implemented</a>.</p>
|
||
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
|
||
<p>A basic HTTP/2.0 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.0 protocol without prior negotiation.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">futures</span>;
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">h2</span>;
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">http</span>;
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">tokio</span>;
|
||
|
||
<span class="kw">use</span> <span class="ident">futures</span>::{<span class="ident">Future</span>, <span class="ident">Stream</span>};
|
||
<span class="kw">use</span> <span class="ident">h2</span>::<span class="ident">server</span>;
|
||
<span class="kw">use</span> <span class="ident">http</span>::{<span class="ident">Response</span>, <span class="ident">StatusCode</span>};
|
||
<span class="kw">use</span> <span class="ident">tokio</span>::<span class="ident">net</span>::<span class="ident">TcpListener</span>;
|
||
|
||
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">main</span> () {
|
||
<span class="kw">let</span> <span class="ident">addr</span> <span class="op">=</span> <span class="string">"127.0.0.1:5928"</span>.<span class="ident">parse</span>().<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="ident">listener</span> <span class="op">=</span> <span class="ident">TcpListener</span>::<span class="ident">bind</span>(<span class="kw-2">&</span><span class="ident">addr</span>,).<span class="ident">unwrap</span>();
|
||
|
||
<span class="ident">tokio</span>::<span class="ident">run</span>({
|
||
<span class="comment">// Accept all incoming TCP connections.</span>
|
||
<span class="ident">listener</span>.<span class="ident">incoming</span>().<span class="ident">for_each</span>(<span class="kw">move</span> <span class="op">|</span><span class="ident">socket</span><span class="op">|</span> {
|
||
<span class="comment">// Spawn a new task to process each connection.</span>
|
||
<span class="ident">tokio</span>::<span class="ident">spawn</span>({
|
||
<span class="comment">// Start the HTTP/2.0 connection handshake</span>
|
||
<span class="ident">server</span>::<span class="ident">handshake</span>(<span class="ident">socket</span>)
|
||
.<span class="ident">and_then</span>(<span class="op">|</span><span class="ident">h2</span><span class="op">|</span> {
|
||
<span class="comment">// Accept all inbound HTTP/2.0 streams sent over the</span>
|
||
<span class="comment">// connection.</span>
|
||
<span class="ident">h2</span>.<span class="ident">for_each</span>(<span class="op">|</span>(<span class="ident">request</span>, <span class="kw-2">mut</span> <span class="ident">respond</span>)<span class="op">|</span> {
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Received request: {:?}"</span>, <span class="ident">request</span>);
|
||
|
||
<span class="comment">// Build a response with no body</span>
|
||
<span class="kw">let</span> <span class="ident">response</span> <span class="op">=</span> <span class="ident">Response</span>::<span class="ident">builder</span>()
|
||
.<span class="ident">status</span>(<span class="ident">StatusCode</span>::<span class="ident">OK</span>)
|
||
.<span class="ident">body</span>(())
|
||
.<span class="ident">unwrap</span>();
|
||
|
||
<span class="comment">// Send the response back to the client</span>
|
||
<span class="ident">respond</span>.<span class="ident">send_response</span>(<span class="ident">response</span>, <span class="bool-val">true</span>)
|
||
.<span class="ident">unwrap</span>();
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
})
|
||
})
|
||
.<span class="ident">map_err</span>(<span class="op">|</span><span class="ident">e</span><span class="op">|</span> <span class="macro">panic</span><span class="macro">!</span>(<span class="string">"unexpected error = {:?}"</span>, <span class="ident">e</span>))
|
||
});
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
})
|
||
.<span class="ident">map_err</span>(<span class="op">|</span><span class="ident">e</span><span class="op">|</span> <span class="macro">panic</span><span class="macro">!</span>(<span class="string">"failed to run HTTP/2.0 server: {:?}"</span>, <span class="ident">e</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='h2::server::Builder struct'>Builder</a></td><td class='docblock-short'><p>Builds server connections with custom configuration values.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Connection.html" title='h2::server::Connection struct'>Connection</a></td><td class='docblock-short'><p>Accepts inbound HTTP/2.0 streams on a connection.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.Handshake.html" title='h2::server::Handshake struct'>Handshake</a></td><td class='docblock-short'><p>In progress HTTP/2.0 connection handshake future.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.SendResponse.html" title='h2::server::SendResponse struct'>SendResponse</a></td><td class='docblock-short'><p>Send a response back to the client</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='h2::server::handshake fn'>handshake</a></td><td class='docblock-short'><p>Creates a new configured HTTP/2.0 server with default configuration
|
||
values backed by <code>io</code>.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "h2";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |