113 lines
17 KiB
HTML
113 lines
17 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="A general purpose library of common HTTP types"><title>http - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="http" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate http</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../http/index.html">http</a><span class="version">1.4.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#requests-and-responses" title="Requests and Responses">Requests and Responses</a></li><li><a href="#http-headers" title="HTTP Headers">HTTP Headers</a></li><li><a href="#uris" title="URIs">URIs</a></li></ul><h3><a href="#reexports">Crate Items</a></h3><ul class="block"><li><a href="#reexports" title="Re-exports">Re-exports</a></li><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#types" title="Type Aliases">Type Aliases</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>http</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/http/lib.rs.html#1-210">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A general purpose library of common HTTP types</p>
|
||
<p>This crate is a general purpose library for common types found when working
|
||
with the HTTP protocol. You’ll find <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> and <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a> types for
|
||
working as either a client or a server as well as all of their components.
|
||
Notably you’ll find <code>Uri</code> for what a <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> is requesting, a <a href="method/struct.Method.html" title="struct http::method::Method"><code>Method</code></a>
|
||
for how it’s being requested, a <a href="status/struct.StatusCode.html" title="struct http::status::StatusCode"><code>StatusCode</code></a> for what sort of response came
|
||
back, a <a href="version/struct.Version.html" title="struct http::version::Version"><code>Version</code></a> for how this was communicated, and
|
||
<a href="header/struct.HeaderName.html" title="struct http::header::HeaderName"><code>HeaderName</code></a>/<a href="header/struct.HeaderValue.html" title="struct http::header::HeaderValue"><code>HeaderValue</code></a> definitions to get grouped in a <a href="header/struct.HeaderMap.html" title="struct http::header::HeaderMap"><code>HeaderMap</code></a> to
|
||
work with request/response headers.</p>
|
||
<p>You will notably <em>not</em> find an implementation of sending requests or
|
||
spinning up a server in this crate. It’s intended that this crate is the
|
||
“standard library” for HTTP clients and servers without dictating any
|
||
particular implementation.</p>
|
||
<h3 id="requests-and-responses"><a class="doc-anchor" href="#requests-and-responses">§</a>Requests and Responses</h3>
|
||
<p>Perhaps the main two types in this crate are the <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> and <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a>
|
||
types. A <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> could either be constructed to get sent off as a client
|
||
or it can also be received to generate a <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a> for a server. Similarly
|
||
as a client a <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a> is what you get after sending a <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a>, whereas
|
||
on a server you’ll be manufacturing a <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a> to send back to the client.</p>
|
||
<p>Each type has a number of accessors for the component fields. For as a
|
||
server you might want to inspect a requests URI to dispatch it:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::{Request, Response};
|
||
|
||
<span class="kw">fn </span>response(req: Request<()>) -> http::Result<Response<()>> {
|
||
<span class="kw">match </span>req.uri().path() {
|
||
<span class="string">"/" </span>=> index(req),
|
||
<span class="string">"/foo" </span>=> foo(req),
|
||
<span class="string">"/bar" </span>=> bar(req),
|
||
<span class="kw">_ </span>=> not_found(req),
|
||
}
|
||
}</code></pre></div>
|
||
<p>On a <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> you’ll also find accessors like <a href="request/struct.Request.html#method.method" title="method http::request::Request::method"><code>method</code></a> to return a
|
||
<a href="method/struct.Method.html" title="struct http::method::Method"><code>Method</code></a> and <a href="request/struct.Request.html#method.method" title="method http::request::Request::method"><code>headers</code></a> to inspect the various headers. A <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a>
|
||
has similar methods for headers, the status code, etc.</p>
|
||
<p>In addition to getters, request/response types also have mutable accessors
|
||
to edit the request/response:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::{HeaderValue, Response, StatusCode};
|
||
<span class="kw">use </span>http::header::CONTENT_TYPE;
|
||
|
||
<span class="kw">fn </span>add_server_headers<T>(response: <span class="kw-2">&mut </span>Response<T>) {
|
||
response.headers_mut()
|
||
.insert(CONTENT_TYPE, HeaderValue::from_static(<span class="string">"text/html"</span>));
|
||
<span class="kw-2">*</span>response.status_mut() = StatusCode::OK;
|
||
}</code></pre></div>
|
||
<p>And finally, one of the most important aspects of requests/responses, the
|
||
body! The <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> and <a href="response/struct.Response.html" title="struct http::response::Response"><code>Response</code></a> types in this crate are <em>generic</em> in
|
||
what their body is. This allows downstream libraries to use different
|
||
representations such as <code>Request<Vec<u8>></code>, <code>Response<impl Read></code>,
|
||
<code>Request<impl Stream<Item = Vec<u8>, Error = _>></code>, or even
|
||
<code>Response<MyCustomType></code> where the custom type was deserialized from JSON.</p>
|
||
<p>The body representation is intentionally flexible to give downstream
|
||
libraries maximal flexibility in implementing the body as appropriate.</p>
|
||
<h3 id="http-headers"><a class="doc-anchor" href="#http-headers">§</a>HTTP Headers</h3>
|
||
<p>Another major piece of functionality in this library is HTTP header
|
||
interpretation and generation. The <code>HeaderName</code> type serves as a way to
|
||
define header <em>names</em>, or what’s to the left of the colon. A <code>HeaderValue</code>
|
||
conversely is the header <em>value</em>, or what’s to the right of a colon.</p>
|
||
<p>For example, if you have an HTTP request that looks like:</p>
|
||
<div class="example-wrap"><pre class="language-http"><code>GET /foo HTTP/1.1
|
||
Accept: text/html</code></pre></div>
|
||
<p>Then <code>"Accept"</code> is a <a href="header/struct.HeaderName.html" title="struct http::header::HeaderName"><code>HeaderName</code></a> while <code>"text/html"</code> is a <a href="header/struct.HeaderValue.html" title="struct http::header::HeaderValue"><code>HeaderValue</code></a>.
|
||
Each of these is a dedicated type to allow for a number of interesting
|
||
optimizations and to also encode the static guarantees of each type. For
|
||
example a <a href="header/struct.HeaderName.html" title="struct http::header::HeaderName"><code>HeaderName</code></a> is always a valid <code>&str</code>, but a <a href="header/struct.HeaderValue.html" title="struct http::header::HeaderValue"><code>HeaderValue</code></a> may
|
||
not be valid UTF-8.</p>
|
||
<p>The most common header names are already defined for you as constant values
|
||
in the <a href="header/index.html" title="mod http::header"><code>header</code></a> module of this crate. For example:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::header::{<span class="self">self</span>, HeaderName};
|
||
|
||
<span class="kw">let </span>name: HeaderName = header::ACCEPT;
|
||
<span class="macro">assert_eq!</span>(name.as_str(), <span class="string">"accept"</span>);</code></pre></div>
|
||
<p>You can, however, also parse header names from strings:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::header::{<span class="self">self</span>, HeaderName};
|
||
|
||
<span class="kw">let </span>name = <span class="string">"Accept"</span>.parse::<HeaderName>().unwrap();
|
||
<span class="macro">assert_eq!</span>(name, header::ACCEPT);</code></pre></div>
|
||
<p>Header values can be created from string literals through the <a href="header/struct.HeaderValue.html#method.from_static" title="associated function http::header::HeaderValue::from_static"><code>from_static</code></a>
|
||
function:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::HeaderValue;
|
||
|
||
<span class="kw">let </span>value = HeaderValue::from_static(<span class="string">"text/html"</span>);
|
||
<span class="macro">assert_eq!</span>(value.as_bytes(), <span class="string">b"text/html"</span>);</code></pre></div>
|
||
<p>And header values can also be parsed like names:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::HeaderValue;
|
||
|
||
<span class="kw">let </span>value = <span class="string">"text/html"</span>;
|
||
<span class="kw">let </span>value = value.parse::<HeaderValue>().unwrap();</code></pre></div>
|
||
<p>Most HTTP requests and responses tend to come with more than one header, so
|
||
it’s not too useful to just work with names and values only! This crate also
|
||
provides a <a href="header/struct.HeaderMap.html" title="struct http::header::HeaderMap"><code>HeaderMap</code></a> type which is a specialized hash map for keys as
|
||
<a href="header/struct.HeaderName.html" title="struct http::header::HeaderName"><code>HeaderName</code></a> and generic values. This type, like header names, is optimized
|
||
for common usage but should continue to scale with your needs over time.</p>
|
||
<h2 id="uris"><a class="doc-anchor" href="#uris">§</a>URIs</h2>
|
||
<p>Each HTTP <a href="request/struct.Request.html" title="struct http::request::Request"><code>Request</code></a> has an associated URI with it. This may just be a path
|
||
like <code>/index.html</code> but it could also be an absolute URL such as
|
||
<code>https://www.rust-lang.org/index.html</code>. A <a href="uri/struct.Uri.html" title="struct http::uri::Uri"><code>URI</code></a> has a number of accessors to
|
||
interpret it:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>http::Uri;
|
||
<span class="kw">use </span>http::uri::Scheme;
|
||
|
||
<span class="kw">let </span>uri = <span class="string">"https://www.rust-lang.org/index.html"</span>.parse::<Uri>().unwrap();
|
||
|
||
<span class="macro">assert_eq!</span>(uri.scheme(), <span class="prelude-val">Some</span>(<span class="kw-2">&</span>Scheme::HTTPS));
|
||
<span class="macro">assert_eq!</span>(uri.host(), <span class="prelude-val">Some</span>(<span class="string">"www.rust-lang.org"</span>));
|
||
<span class="macro">assert_eq!</span>(uri.path(), <span class="string">"/index.html"</span>);
|
||
<span class="macro">assert_eq!</span>(uri.query(), <span class="prelude-val">None</span>);</code></pre></div></div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><dl class="item-table reexports"><dt id="reexport.HeaderMap"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderMap.html" title="struct http::header::HeaderMap">HeaderMap</a>;</code></dt><dt id="reexport.HeaderName"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderName.html" title="struct http::header::HeaderName">HeaderName</a>;</code></dt><dt id="reexport.HeaderValue"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderValue.html" title="struct http::header::HeaderValue">HeaderValue</a>;</code></dt><dt id="reexport.Method"><code>pub use crate::method::<a class="struct" href="method/struct.Method.html" title="struct http::method::Method">Method</a>;</code></dt><dt id="reexport.Request"><code>pub use crate::request::<a class="struct" href="request/struct.Request.html" title="struct http::request::Request">Request</a>;</code></dt><dt id="reexport.Response"><code>pub use crate::response::<a class="struct" href="response/struct.Response.html" title="struct http::response::Response">Response</a>;</code></dt><dt id="reexport.StatusCode"><code>pub use crate::status::<a class="struct" href="status/struct.StatusCode.html" title="struct http::status::StatusCode">StatusCode</a>;</code></dt><dt id="reexport.Uri"><code>pub use crate::uri::<a class="struct" href="uri/struct.Uri.html" title="struct http::uri::Uri">Uri</a>;</code></dt><dt id="reexport.Version"><code>pub use crate::version::<a class="struct" href="version/struct.Version.html" title="struct http::version::Version">Version</a>;</code></dt></dl><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="header/index.html" title="mod http::header">header</a></dt><dd>HTTP header types</dd><dt><a class="mod" href="method/index.html" title="mod http::method">method</a></dt><dd>The HTTP request method</dd><dt><a class="mod" href="request/index.html" title="mod http::request">request</a></dt><dd>HTTP request types.</dd><dt><a class="mod" href="response/index.html" title="mod http::response">response</a></dt><dd>HTTP response types.</dd><dt><a class="mod" href="status/index.html" title="mod http::status">status</a></dt><dd>HTTP status codes</dd><dt><a class="mod" href="uri/index.html" title="mod http::uri">uri</a></dt><dd>URI component of request and response lines</dd><dt><a class="mod" href="version/index.html" title="mod http::version">version</a></dt><dd>HTTP version</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Error.html" title="struct http::Error">Error</a></dt><dd>A generic “error” for HTTP connections</dd><dt><a class="struct" href="struct.Extensions.html" title="struct http::Extensions">Extensions</a></dt><dd>A type map of protocol extensions.</dd></dl><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><dl class="item-table"><dt><a class="type" href="type.Result.html" title="type http::Result">Result</a></dt><dd>A <code>Result</code> typedef to use with the <code>http::Error</code> type</dd></dl></section></div></main></body></html> |