mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
44 lines
6.5 KiB
HTML
44 lines
6.5 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><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_cors" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (b5b13568f 2024-06-10)" data-channel="nightly" data-search-js="search-0fe7219eb170c82e.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.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 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" title="show sidebar"></button><a class="logo-container" href="../actix_cors/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_cors/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_cors/index.html">actix_cors</a><span class="version">0.7.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><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><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>Crate <a class="mod" href="#">actix_cors</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_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 class="doc-anchor" href="#crate-features">§</a>Crate Features</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 class="doc-anchor" href="#example">§</a>Example</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="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name" id="reexport.CorsMiddleware"><code>pub use crate::middleware::CorsMiddleware;</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.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="section-header">Enums<a href="#enums" class="anchor">§</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> |