blob: 7183c19855dd5ac7a11981800ca9a5e8666e4dd0 [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="HTTP/3 wire protocol and QPACK implementation."><title>quiche::h3 - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-b21aa549bf6d91ff.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="quiche" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (79734f1db 2024-05-02)" data-channel="nightly" data-search-js="search-bf21c90c8c1d92b1.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-e32f0c247825364d.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-09095024cf37855e.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../quiche/index.html">quiche</a><span class="version">0.21.0</span></h2></div><h2 class="location"><a href="#">Module h3</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Aliases</a></li></ul></section><h2><a href="../index.html">In crate quiche</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../quiche/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings">Settings</a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">quiche</a>::<wbr><a class="mod" href="#">h3</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../../src/quiche/h3/mod.rs.html#27-6496">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>HTTP/3 wire protocol and QPACK implementation.</p>
<p>This module provides a high level API for sending and receiving HTTP/3
requests and responses on top of the QUIC transport protocol.</p>
<h3 id="connection-setup"><a class="doc-anchor" href="#connection-setup">§</a>Connection setup</h3>
<p>HTTP/3 connections require a QUIC transport-layer connection, see
<a href="../index.html#connection-setup">Connection setup</a> for a full description of the setup process.</p>
<p>To use HTTP/3, the QUIC connection must be configured with a suitable
Application Layer Protocol Negotiation (ALPN) Protocol ID:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>config = quiche::Config::new(quiche::PROTOCOL_VERSION)<span class="question-mark">?</span>;
config.set_application_protos(quiche::h3::APPLICATION_PROTOCOL)<span class="question-mark">?</span>;</code></pre></div>
<p>The QUIC handshake is driven by <a href="../index.html#generating-outgoing-packets">sending</a> and <a href="../index.html#handling-incoming-packets">receiving</a> QUIC packets.</p>
<p>Once the handshake has completed, the first step in establishing an HTTP/3
connection is creating its configuration object:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>h3_config = quiche::h3::Config::new()<span class="question-mark">?</span>;</code></pre></div>
<p>HTTP/3 client and server connections are both created using the
<a href="struct.Connection.html#method.with_transport"><code>with_transport()</code></a> function, the role is inferred from the type of QUIC
connection:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>h3_conn = quiche::h3::Connection::with_transport(<span class="kw-2">&amp;mut </span>conn, <span class="kw-2">&amp;</span>h3_config)<span class="question-mark">?</span>;</code></pre></div>
<h3 id="sending-a-request"><a class="doc-anchor" href="#sending-a-request">§</a>Sending a request</h3>
<p>An HTTP/3 client can send a request by using the connection’s
<a href="struct.Connection.html#method.send_response"><code>send_request()</code></a> method to queue request headers; <a href="../index.html#generating-outgoing-packets">sending</a> QUIC packets
causes the requests to get sent to the peer:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>req = <span class="macro">vec!</span>[
quiche::h3::Header::new(<span class="string">b":method"</span>, <span class="string">b"GET"</span>),
quiche::h3::Header::new(<span class="string">b":scheme"</span>, <span class="string">b"https"</span>),
quiche::h3::Header::new(<span class="string">b":authority"</span>, <span class="string">b"quic.tech"</span>),
quiche::h3::Header::new(<span class="string">b":path"</span>, <span class="string">b"/"</span>),
quiche::h3::Header::new(<span class="string">b"user-agent"</span>, <span class="string">b"quiche"</span>),
];
h3_conn.send_request(<span class="kw-2">&amp;mut </span>conn, <span class="kw-2">&amp;</span>req, <span class="bool-val">true</span>)<span class="question-mark">?</span>;</code></pre></div>
<p>An HTTP/3 client can send a request with additional body data by using
the connection’s <a href="struct.Connection.html#method.send_body"><code>send_body()</code></a> method:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>req = <span class="macro">vec!</span>[
quiche::h3::Header::new(<span class="string">b":method"</span>, <span class="string">b"GET"</span>),
quiche::h3::Header::new(<span class="string">b":scheme"</span>, <span class="string">b"https"</span>),
quiche::h3::Header::new(<span class="string">b":authority"</span>, <span class="string">b"quic.tech"</span>),
quiche::h3::Header::new(<span class="string">b":path"</span>, <span class="string">b"/"</span>),
quiche::h3::Header::new(<span class="string">b"user-agent"</span>, <span class="string">b"quiche"</span>),
];
<span class="kw">let </span>stream_id = h3_conn.send_request(<span class="kw-2">&amp;mut </span>conn, <span class="kw-2">&amp;</span>req, <span class="bool-val">false</span>)<span class="question-mark">?</span>;
h3_conn.send_body(<span class="kw-2">&amp;mut </span>conn, stream_id, <span class="string">b"Hello World!"</span>, <span class="bool-val">true</span>)<span class="question-mark">?</span>;</code></pre></div>
<h3 id="handling-requests-and-responses"><a class="doc-anchor" href="#handling-requests-and-responses">§</a>Handling requests and responses</h3>
<p>After <a href="../index.html#handling-incoming-packets">receiving</a> QUIC packets, HTTP/3 data is processed using the
connection’s <a href="struct.Connection.html#method.poll"><code>poll()</code></a> method. On success, this returns an <a href="enum.Event.html"><code>Event</code></a> object
and an ID corresponding to the stream where the <code>Event</code> originated.</p>
<p>An HTTP/3 server uses <a href="struct.Connection.html#method.poll"><code>poll()</code></a> to read requests and responds to them using
<a href="struct.Connection.html#method.send_response"><code>send_response()</code></a> and <a href="struct.Connection.html#method.send_body"><code>send_body()</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>quiche::h3::NameValue;
<span class="kw">loop </span>{
<span class="kw">match </span>h3_conn.poll(<span class="kw-2">&amp;mut </span>conn) {
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Headers{list, has_body})) =&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>headers = list.into_iter();
<span class="comment">// Look for the request's method.
</span><span class="kw">let </span>method = headers.find(|h| h.name() == <span class="string">b":method"</span>).unwrap();
<span class="comment">// Look for the request's path.
</span><span class="kw">let </span>path = headers.find(|h| h.name() == <span class="string">b":path"</span>).unwrap();
<span class="kw">if </span>method.value() == <span class="string">b"GET" </span>&amp;&amp; path.value() == <span class="string">b"/" </span>{
<span class="kw">let </span>resp = <span class="macro">vec!</span>[
quiche::h3::Header::new(<span class="string">b":status"</span>, <span class="number">200</span>.to_string().as_bytes()),
quiche::h3::Header::new(<span class="string">b"server"</span>, <span class="string">b"quiche"</span>),
];
h3_conn.send_response(<span class="kw-2">&amp;mut </span>conn, stream_id, <span class="kw-2">&amp;</span>resp, <span class="bool-val">false</span>)<span class="question-mark">?</span>;
h3_conn.send_body(<span class="kw-2">&amp;mut </span>conn, stream_id, <span class="string">b"Hello World!"</span>, <span class="bool-val">true</span>)<span class="question-mark">?</span>;
}
},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Data)) =&gt; {
<span class="comment">// Request body data, handle it.
</span>},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Finished)) =&gt; {
<span class="comment">// Peer terminated stream, handle it.
</span>},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Reset(err))) =&gt; {
<span class="comment">// Peer reset the stream, handle it.
</span>},
<span class="prelude-val">Ok</span>((_flow_id, quiche::h3::Event::PriorityUpdate)) =&gt; (),
<span class="prelude-val">Ok</span>((goaway_id, quiche::h3::Event::GoAway)) =&gt; {
<span class="comment">// Peer signalled it is going away, handle it.
</span>},
<span class="prelude-val">Err</span>(quiche::h3::Error::Done) =&gt; {
<span class="comment">// Done reading.
</span><span class="kw">break</span>;
},
<span class="prelude-val">Err</span>(e) =&gt; {
<span class="comment">// An error occurred, handle it.
</span><span class="kw">break</span>;
},
}
}</code></pre></div>
<p>An HTTP/3 client uses <a href="struct.Connection.html#method.poll"><code>poll()</code></a> to read responses:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>quiche::h3::NameValue;
<span class="kw">loop </span>{
<span class="kw">match </span>h3_conn.poll(<span class="kw-2">&amp;mut </span>conn) {
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Headers{list, has_body})) =&gt; {
<span class="kw">let </span>status = list.iter().find(|h| h.name() == <span class="string">b":status"</span>).unwrap();
<span class="macro">println!</span>(<span class="string">"Received {} response on stream {}"</span>,
std::str::from_utf8(status.value()).unwrap(),
stream_id);
},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Data)) =&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>body = <span class="macro">vec!</span>[<span class="number">0</span>; <span class="number">4096</span>];
<span class="comment">// Consume all body data received on the stream.
</span><span class="kw">while let </span><span class="prelude-val">Ok</span>(read) =
h3_conn.recv_body(<span class="kw-2">&amp;mut </span>conn, stream_id, <span class="kw-2">&amp;mut </span>body)
{
<span class="macro">println!</span>(<span class="string">"Received {} bytes of payload on stream {}"</span>,
read, stream_id);
}
},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Finished)) =&gt; {
<span class="comment">// Peer terminated stream, handle it.
</span>},
<span class="prelude-val">Ok</span>((stream_id, quiche::h3::Event::Reset(err))) =&gt; {
<span class="comment">// Peer reset the stream, handle it.
</span>},
<span class="prelude-val">Ok</span>((_prioritized_element_id, quiche::h3::Event::PriorityUpdate)) =&gt; (),
<span class="prelude-val">Ok</span>((goaway_id, quiche::h3::Event::GoAway)) =&gt; {
<span class="comment">// Peer signalled it is going away, handle it.
</span>},
<span class="prelude-val">Err</span>(quiche::h3::Error::Done) =&gt; {
<span class="comment">// Done reading.
</span><span class="kw">break</span>;
},
<span class="prelude-val">Err</span>(e) =&gt; {
<span class="comment">// An error occurred, handle it.
</span><span class="kw">break</span>;
},
}
}</code></pre></div>
<h3 id="detecting-end-of-request-or-response"><a class="doc-anchor" href="#detecting-end-of-request-or-response">§</a>Detecting end of request or response</h3>
<p>A single HTTP/3 request or response may consist of several HEADERS and DATA
frames; it is finished when the QUIC stream is closed. Calling <a href="struct.Connection.html#method.poll"><code>poll()</code></a>
repeatedly will generate an <a href="enum.Event.html"><code>Event</code></a> for each of these. The application may
use these event to do additional HTTP semantic validation.</p>
<h3 id="http3-protocol-errors"><a class="doc-anchor" href="#http3-protocol-errors">§</a>HTTP/3 protocol errors</h3>
<p>Quiche is responsible for managing the HTTP/3 connection, ensuring it is in
a correct state and validating all messages received by a peer. This mainly
takes place in the <a href="struct.Connection.html#method.poll"><code>poll()</code></a> method. If an HTTP/3 error occurs, quiche will
close the connection and send an appropriate CONNECTION_CLOSE frame to the
peer. An <a href="enum.Error.html"><code>Error</code></a> is returned to the application so that it can perform any
required tidy up such as closing sockets.</p>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Config.html" title="struct quiche::h3::Config">Config</a></div><div class="desc docblock-short">An HTTP/3 configuration.</div></li><li><div class="item-name"><a class="struct" href="struct.Connection.html" title="struct quiche::h3::Connection">Connection</a></div><div class="desc docblock-short">An HTTP/3 connection.</div></li><li><div class="item-name"><a class="struct" href="struct.Header.html" title="struct quiche::h3::Header">Header</a></div><div class="desc docblock-short">An owned name-value pair representing a raw HTTP header.</div></li><li><div class="item-name"><a class="struct" href="struct.HeaderRef.html" title="struct quiche::h3::HeaderRef">HeaderRef</a></div><div class="desc docblock-short">A non-owned name-value pair representing a raw HTTP header.</div></li><li><div class="item-name"><a class="struct" href="struct.Priority.html" title="struct quiche::h3::Priority">Priority</a></div><div class="desc docblock-short">Extensible Priorities parameters.</div></li></ul><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.Error.html" title="enum quiche::h3::Error">Error</a></div><div class="desc docblock-short">An HTTP/3 error.</div></li><li><div class="item-name"><a class="enum" href="enum.Event.html" title="enum quiche::h3::Event">Event</a></div><div class="desc docblock-short">An HTTP/3 connection event.</div></li><li><div class="item-name"><a class="enum" href="enum.WireErrorCode.html" title="enum quiche::h3::WireErrorCode">WireErrorCode</a></div><div class="desc docblock-short">HTTP/3 error codes sent on the wire.</div></li></ul><h2 id="constants" class="section-header">Constants<a href="#constants" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="constant" href="constant.APPLICATION_PROTOCOL.html" title="constant quiche::h3::APPLICATION_PROTOCOL">APPLICATION_PROTOCOL</a></div><div class="desc docblock-short">List of ALPN tokens of supported HTTP/3 versions.</div></li></ul><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.NameValue.html" title="trait quiche::h3::NameValue">NameValue</a></div><div class="desc docblock-short">A trait for types with associated string name and value.</div></li></ul><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.grease_value.html" title="fn quiche::h3::grease_value">grease_value</a></div><div class="desc docblock-short">Generates an HTTP/3 GREASE variable length integer.</div></li></ul><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="type" href="type.Result.html" title="type quiche::h3::Result">Result</a></div><div class="desc docblock-short">A specialized <a href="https://doc.rust-lang.org/std/result/enum.Result.html"><code>Result</code></a> type for quiche HTTP/3 operations.</div></li></ul></section></div></main></body></html>