mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-01-25 12:44:07 +01:00
120 lines
16 KiB
HTML
120 lines
16 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><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-ac92e1bbe349e143.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.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><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 crate"><!--[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">☰</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../http/index.html">http</a><span class="version">0.2.9</span></h2></div><div class="sidebar-elems"><ul class="block">
|
||
<li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#types">Type Aliases</a></li></ul></section></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="../http/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="Click or press ‘S’ 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"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">http</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/http/lib.rs.html#1-211">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></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 <code>Request</code> and <code>Response</code> 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 <code>Request</code> is requesting, a <code>Method</code>
|
||
for how it’s being requested, a <code>StatusCode</code> for what sort of response came
|
||
back, a <code>Version</code> for how this was communicated, and
|
||
<code>HeaderName</code>/<code>HeaderValue</code> definitions to get grouped in a <code>HeaderMap</code> 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. Note that this crate is still early on in its
|
||
lifecycle so the support libraries that integrate with the <code>http</code> crate are
|
||
a work in progress! Stay tuned and we’ll be sure to highlight crates here
|
||
in the future.</p>
|
||
<h3 id="requests-and-responses"><a href="#requests-and-responses">Requests and Responses</a></h3>
|
||
<p>Perhaps the main two types in this crate are the <code>Request</code> and <code>Response</code>
|
||
types. A <code>Request</code> could either be constructed to get sent off as a client
|
||
or it can also be received to generate a <code>Response</code> for a server. Similarly
|
||
as a client a <code>Response</code> is what you get after sending a <code>Request</code>, whereas
|
||
on a server you’ll be manufacturing a <code>Response</code> 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 <code>Request</code> you’ll also find accessors like <code>method</code> to return a
|
||
<code>Method</code> and <code>headers</code> to inspect the various headers. A <code>Response</code>
|
||
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 <code>Request</code> and <code>Response</code> 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 href="#http-headers">HTTP Headers</a></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 <code>HeaderName</code> while <code>"text/html"</code> is a <code>HeaderValue</code>.
|
||
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 <code>HeaderName</code> is always a valid <code>&str</code>, but a <code>HeaderValue</code> may
|
||
not be valid UTF-8.</p>
|
||
<p>The most common header names are already defined for you as constant values
|
||
in the <code>header</code> 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 <code>from_static</code>
|
||
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 <code>HeaderMap</code> type which is a specialized hash map for keys as
|
||
<code>HeaderName</code> 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 href="#uris">URIs</a></h2>
|
||
<p>Each HTTP <code>Request</code> 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 <code>URI</code> 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"><a href="#reexports">Re-exports</a></h2><ul class="item-table"><li><div class="item-name" id="reexport.HeaderMap"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderMap.html" title="struct http::header::HeaderMap">HeaderMap</a>;</code></div></li><li><div class="item-name" id="reexport.HeaderName"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderName.html" title="struct http::header::HeaderName">HeaderName</a>;</code></div></li><li><div class="item-name" id="reexport.HeaderValue"><code>pub use crate::header::<a class="struct" href="header/struct.HeaderValue.html" title="struct http::header::HeaderValue">HeaderValue</a>;</code></div></li><li><div class="item-name" id="reexport.Method"><code>pub use crate::method::<a class="struct" href="method/struct.Method.html" title="struct http::method::Method">Method</a>;</code></div></li><li><div class="item-name" id="reexport.Request"><code>pub use crate::request::<a class="struct" href="request/struct.Request.html" title="struct http::request::Request">Request</a>;</code></div></li><li><div class="item-name" id="reexport.Response"><code>pub use crate::response::<a class="struct" href="response/struct.Response.html" title="struct http::response::Response">Response</a>;</code></div></li><li><div class="item-name" id="reexport.StatusCode"><code>pub use crate::status::<a class="struct" href="status/struct.StatusCode.html" title="struct http::status::StatusCode">StatusCode</a>;</code></div></li><li><div class="item-name" id="reexport.Uri"><code>pub use crate::uri::<a class="struct" href="uri/struct.Uri.html" title="struct http::uri::Uri">Uri</a>;</code></div></li><li><div class="item-name" id="reexport.Version"><code>pub use crate::version::<a class="struct" href="version/struct.Version.html" title="struct http::version::Version">Version</a>;</code></div></li></ul><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="header/index.html" title="mod http::header">header</a></div><div class="desc docblock-short">HTTP header types</div></li><li><div class="item-name"><a class="mod" href="method/index.html" title="mod http::method">method</a></div><div class="desc docblock-short">The HTTP request method</div></li><li><div class="item-name"><a class="mod" href="request/index.html" title="mod http::request">request</a></div><div class="desc docblock-short">HTTP request types.</div></li><li><div class="item-name"><a class="mod" href="response/index.html" title="mod http::response">response</a></div><div class="desc docblock-short">HTTP response types.</div></li><li><div class="item-name"><a class="mod" href="status/index.html" title="mod http::status">status</a></div><div class="desc docblock-short">HTTP status codes</div></li><li><div class="item-name"><a class="mod" href="uri/index.html" title="mod http::uri">uri</a></div><div class="desc docblock-short">URI component of request and response lines</div></li><li><div class="item-name"><a class="mod" href="version/index.html" title="mod http::version">version</a></div><div class="desc docblock-short">HTTP version</div></li></ul><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Error.html" title="struct http::Error">Error</a></div><div class="desc docblock-short">A generic “error” for HTTP connections</div></li><li><div class="item-name"><a class="struct" href="struct.Extensions.html" title="struct http::Extensions">Extensions</a></div><div class="desc docblock-short">A type map of protocol extensions.</div></li></ul><h2 id="types" class="section-header"><a href="#types">Type Aliases</a></h2><ul class="item-table"><li><div class="item-name"><a class="type" href="type.Result.html" title="type http::Result">Result</a></div><div class="desc docblock-short">A <code>Result</code> typedef to use with the <code>http::Error</code> type</div></li></ul></section></div></main></body></html> |