1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00
actix-web/actix_web/guard/index.html
2024-05-27 01:16:31 +00:00

37 lines
12 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="Route guards."><title>actix_web::guard - 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-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="actix_web" data-themes="" data-resource-suffix="" data-rustdoc-version="1.80.0-nightly (bdbbb6c6a 2024-05-26)" data-channel="nightly" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-118b08c4c78b968e.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-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></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><a class="logo-container" href="../../actix_web/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../actix_web/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../../actix_web/index.html">actix_web</a><span class="version">4.6.0</span></h2></div><h2 class="location"><a href="#">Module guard</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></section><h2><a href="../index.html">In crate actix_web</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">actix_web</a>::<wbr><a class="mod" href="#">guard</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/actix_web/guard/mod.rs.html#1-515">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>Route guards.</p>
<p>Guards are used during routing to help select a matching service or handler using some aspect of
the request; though guards should not be used for path matching since it is a built-in function
of the Actix Web router.</p>
<p>Guards can be used on <a href="../struct.Scope.html#method.guard" title="method actix_web::Scope::guard"><code>Scope</code></a>s, <a href="../struct.Resource.html#method.guard" title="method actix_web::Resource::guard"><code>Resource</code></a>s, <a href="../struct.Route.html#method.guard" title="method actix_web::Route::guard"><code>Route</code></a>s, and other custom services.</p>
<p>Fundamentally, a guard is a predicate function that receives a reference to a request context
object and returns a boolean; true if the request <em>should</em> be handled by the guarded service
or handler. This interface is defined by the <a href="trait.Guard.html" title="trait actix_web::guard::Guard"><code>Guard</code></a> trait.</p>
<p>Commonly-used guards are provided in this module as well as a way of creating a guard from a
closure (<a href="fn.fn_guard.html" title="fn actix_web::guard::fn_guard"><code>fn_guard</code></a>). The <a href="struct.Not.html" title="struct actix_web::guard::Not"><code>Not</code></a>, <a href="fn.Any.html" title="fn actix_web::guard::Any"><code>Any</code></a>, and <a href="fn.All.html" title="fn actix_web::guard::All"><code>All</code></a> guards are noteworthy, as they can be
used to compose other guards in a more flexible and semantic way than calling <code>.guard(...)</code> on
services multiple times (which might have different combining behavior than you want).</p>
<p>There are shortcuts for routes with method guards in the <a href="../web/index.html" title="mod actix_web::web"><code>web</code></a> module:
<a href="../web/fn.get.html" title="fn actix_web::web::get"><code>web::get()</code></a>, <a href="../web/fn.post.html" title="fn actix_web::web::post"><code>web::post()</code></a>, etc. The routes created by
the following calls are equivalent:</p>
<ul>
<li><code>web::get()</code> (recommended form)</li>
<li><code>web::route().guard(guard::Get())</code></li>
</ul>
<p>Guards can not modify anything about the request. However, it is possible to store extra
attributes in the request-local data container obtained with <a href="struct.GuardContext.html#method.req_data_mut" title="method actix_web::guard::GuardContext::req_data_mut"><code>GuardContext::req_data_mut</code></a>.</p>
<p>Guards can prevent resource definitions from overlapping which, when only considering paths,
would result in inaccessible routes. See the <a href="fn.Host.html" title="fn actix_web::guard::Host"><code>Host</code></a> guard for an example of virtual hosting.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>In the following code, the <code>/guarded</code> resource has one defined route whose handler will only be
called if the request method is GET or POST and there is a <code>x-guarded</code> request header with value
equal to <code>secret</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_web::{web, http::Method, guard, HttpResponse};
web::resource(<span class="string">"/guarded"</span>).route(
web::route()
.guard(guard::Any(guard::Get()).or(guard::Post()))
.guard(guard::Header(<span class="string">"x-guarded"</span>, <span class="string">"secret"</span>))
.to(|| HttpResponse::Ok())
);</code></pre></div>
</div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name" id="reexport.HostGuard"><code>pub use self::host::HostGuard;</code></div></li></ul><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.Acceptable.html" title="struct actix_web::guard::Acceptable">Acceptable</a></div><div class="desc docblock-short">A guard that verifies that an <code>Accept</code> header is present and it contains a compatible MIME type.</div></li><li><div class="item-name"><a class="struct" href="struct.AllGuard.html" title="struct actix_web::guard::AllGuard">AllGuard</a></div><div class="desc docblock-short">A collection of guards that match if the conjunction of their <code>check</code> outcomes is true.</div></li><li><div class="item-name"><a class="struct" href="struct.AnyGuard.html" title="struct actix_web::guard::AnyGuard">AnyGuard</a></div><div class="desc docblock-short">A collection of guards that match if the disjunction of their <code>check</code> outcomes is true.</div></li><li><div class="item-name"><a class="struct" href="struct.GuardContext.html" title="struct actix_web::guard::GuardContext">GuardContext</a></div><div class="desc docblock-short">Provides access to request parts that are useful during routing.</div></li><li><div class="item-name"><a class="struct" href="struct.Not.html" title="struct actix_web::guard::Not">Not</a></div><div class="desc docblock-short">Wraps a guard and inverts the outcome of its <code>Guard</code> implementation.</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.Guard.html" title="trait actix_web::guard::Guard">Guard</a></div><div class="desc docblock-short">Interface for routing guards.</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.All.html" title="fn actix_web::guard::All">All</a></div><div class="desc docblock-short">Creates a guard that matches if all added guards match.</div></li><li><div class="item-name"><a class="fn" href="fn.Any.html" title="fn actix_web::guard::Any">Any</a></div><div class="desc docblock-short">Creates a guard that matches if any added guards match.</div></li><li><div class="item-name"><a class="fn" href="fn.Connect.html" title="fn actix_web::guard::Connect">Connect</a></div><div class="desc docblock-short">Creates a guard that matches the <code>CONNECT</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Delete.html" title="fn actix_web::guard::Delete">Delete</a></div><div class="desc docblock-short">Creates a guard that matches the <code>DELETE</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Get.html" title="fn actix_web::guard::Get">Get</a></div><div class="desc docblock-short">Creates a guard that matches the <code>GET</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Head.html" title="fn actix_web::guard::Head">Head</a></div><div class="desc docblock-short">Creates a guard that matches the <code>HEAD</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Header.html" title="fn actix_web::guard::Header">Header</a></div><div class="desc docblock-short">Creates a guard that matches if request contains given header name and value.</div></li><li><div class="item-name"><a class="fn" href="fn.Host.html" title="fn actix_web::guard::Host">Host</a></div><div class="desc docblock-short">Creates a guard that matches requests targeting a specific host.</div></li><li><div class="item-name"><a class="fn" href="fn.Method.html" title="fn actix_web::guard::Method">Method</a></div><div class="desc docblock-short">Creates a guard that matches a specified HTTP method.</div></li><li><div class="item-name"><a class="fn" href="fn.Options.html" title="fn actix_web::guard::Options">Options</a></div><div class="desc docblock-short">Creates a guard that matches the <code>OPTIONS</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Patch.html" title="fn actix_web::guard::Patch">Patch</a></div><div class="desc docblock-short">Creates a guard that matches the <code>PATCH</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Post.html" title="fn actix_web::guard::Post">Post</a></div><div class="desc docblock-short">Creates a guard that matches the <code>POST</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Put.html" title="fn actix_web::guard::Put">Put</a></div><div class="desc docblock-short">Creates a guard that matches the <code>PUT</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.Trace.html" title="fn actix_web::guard::Trace">Trace</a></div><div class="desc docblock-short">Creates a guard that matches the <code>TRACE</code> request method.</div></li><li><div class="item-name"><a class="fn" href="fn.fn_guard.html" title="fn actix_web::guard::fn_guard">fn_guard</a></div><div class="desc docblock-short">Creates a guard using the given function.</div></li></ul></section></div></main></body></html>