mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
44 lines
7.3 KiB
HTML
44 lines
7.3 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="Cross-Origin Resource Sharing (CORS) controls for Actix Web."><title>actix_cors - 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/SourceSerif4-Bold-a2c9cd1067f8b328.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-fa3bb1812debf86c.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_cors" data-themes="" data-resource-suffix="" data-rustdoc-version="1.75.0-nightly (2e5a9dd6c 2023-10-02)" data-channel="nightly" data-search-js="search-8be46b629f5f14a8.js" data-settings-js="settings-74424d7eec62a23e.js" ><script src="../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-c5bd66d33317d69f.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-5d8b3c7633ad77ba.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></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><a class="logo-container" href="../actix_cors/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a></nav><nav class="sidebar"><a class="logo-container" href="../actix_cors/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2 class="location"><a href="#">Crate actix_cors</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.6.4</li><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="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><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" title="help" tabindex="-1"><a href="../help.html">?</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="#">actix_cors</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/actix_cors/lib.rs.html#1-68">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>Cross-Origin Resource Sharing (CORS) controls for Actix Web.</p>
|
||
<p>This middleware can be applied to both applications and resources. Once built, a <a href="struct.Cors.html" title="struct actix_cors::Cors"><code>Cors</code></a>
|
||
builder can be used as an argument for Actix Web’s <code>App::wrap()</code>, <code>Scope::wrap()</code>, or
|
||
<code>Resource::wrap()</code> methods.</p>
|
||
<p>This CORS middleware automatically handles <code>OPTIONS</code> preflight requests.</p>
|
||
<h2 id="crate-features"><a href="#crate-features">Crate Features</a></h2>
|
||
<ul>
|
||
<li><code>draft-private-network-access</code>: ⚠️ Unstable. Adds opt-in support for the <a href="https://wicg.github.io/private-network-access">Private Network
|
||
Access</a> spec extensions. This feature is unstable since it will follow breaking changes in the
|
||
draft spec until it is finalized.</li>
|
||
</ul>
|
||
<h2 id="example"><a href="#example">Example</a></h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>actix_cors::Cors;
|
||
<span class="kw">use </span>actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
|
||
|
||
<span class="attr">#[get(<span class="string">"/index.html"</span>)]
|
||
</span><span class="kw">async fn </span>index(req: HttpRequest) -> <span class="kw-2">&</span><span class="lifetime">'static </span>str {
|
||
<span class="string">"<p>Hello World!</p>"
|
||
</span>}
|
||
|
||
<span class="attr">#[actix_web::main]
|
||
</span><span class="kw">async fn </span>main() -> std::io::Result<()> {
|
||
HttpServer::new(|| {
|
||
<span class="kw">let </span>cors = Cors::default()
|
||
.allowed_origin(<span class="string">"https://www.rust-lang.org"</span>)
|
||
.allowed_origin_fn(|origin, _req_head| {
|
||
origin.as_bytes().ends_with(<span class="string">b".rust-lang.org"</span>)
|
||
})
|
||
.allowed_methods(<span class="macro">vec!</span>[<span class="string">"GET"</span>, <span class="string">"POST"</span>])
|
||
.allowed_headers(<span class="macro">vec!</span>[http::header::AUTHORIZATION, http::header::ACCEPT])
|
||
.allowed_header(http::header::CONTENT_TYPE)
|
||
.max_age(<span class="number">3600</span>);
|
||
|
||
App::new()
|
||
.wrap(cors)
|
||
.service(index)
|
||
})
|
||
.bind((<span class="string">"127.0.0.1"</span>, <span class="number">8080</span>))<span class="question-mark">?
|
||
</span>.run()
|
||
.<span class="kw">await</span>;
|
||
|
||
<span class="prelude-val">Ok</span>(())
|
||
}</code></pre></div>
|
||
</div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><ul class="item-table"><li><div class="item-name" id="reexport.CorsMiddleware"><code>pub use middleware::CorsMiddleware;</code></div></li></ul><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Cors.html" title="struct actix_cors::Cors">Cors</a></div><div class="desc docblock-short">Builder for CORS middleware.</div></li></ul><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.CorsError.html" title="enum actix_cors::CorsError">CorsError</a></div><div class="desc docblock-short">Errors that can occur when processing CORS guarded requests.</div></li></ul></section></div></main></body></html> |