1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-08-31 19:37:00 +02:00

Deploying to gh-pages from @ 8741cd32cc 🚀

This commit is contained in:
robjtede
2021-04-09 13:42:26 +00:00
parent c00153b487
commit 55e0870178
118 changed files with 1511 additions and 1511 deletions

View File

@@ -68,14 +68,14 @@
<span class="doccomment">/// Default as `AllOrSome::All`.</span>
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Default</span> <span class="kw">for</span> <span class="ident">AllOrSome</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">default</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span>
<span class="ident">AllOrSome::All</span>
}
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">AllOrSome</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="doccomment">/// Returns whether this is an `All` variant.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">is_all</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">bool</span> {
<span class="macro">matches</span><span class="macro">!</span>(<span class="self">self</span>, <span class="ident">AllOrSome</span>::<span class="ident">All</span>)
<span class="macro">matches!</span>(<span class="self">self</span>, <span class="ident">AllOrSome::All</span>)
}
<span class="doccomment">/// Returns whether this is a `Some` variant.</span>
@@ -87,16 +87,16 @@
<span class="doccomment">/// Provides a shared reference to `T` if variant is `Some`.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">as_ref</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">None</span>,
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">t</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Some</span>(<span class="ident">t</span>),
<span class="ident">AllOrSome::All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">None</span>,
<span class="ident">AllOrSome::Some</span>(<span class="kw-2">ref</span> <span class="ident">t</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Some</span>(<span class="ident">t</span>),
}
}
<span class="doccomment">/// Provides a mutable reference to `T` if variant is `Some`.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">as_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span><span class="op">&gt;</span> {
<span class="kw">match</span> <span class="kw-2">*</span><span class="self">self</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">None</span>,
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">t</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Some</span>(<span class="ident">t</span>),
<span class="ident">AllOrSome::All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">None</span>,
<span class="ident">AllOrSome::Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">t</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Some</span>(<span class="ident">t</span>),
}
}
}
@@ -104,11 +104,11 @@
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
<span class="attribute">#[<span class="ident">test</span>]</span>
<span class="kw">fn</span> <span class="ident">tests</span>() {
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">AllOrSome</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span>::<span class="ident">All</span>.<span class="ident">is_all</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">AllOrSome</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span>::<span class="ident">All</span>.<span class="ident">is_some</span>());
<span class="macro">assert!</span>(<span class="ident">AllOrSome</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span><span class="ident">::All</span>.<span class="ident">is_all</span>());
<span class="macro">assert!</span>(<span class="op">!</span><span class="ident">AllOrSome</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span><span class="ident">::All</span>.<span class="ident">is_some</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(()).<span class="ident">is_all</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(()).<span class="ident">is_some</span>());
<span class="macro">assert!</span>(<span class="op">!</span><span class="ident">AllOrSome::Some</span>(()).<span class="ident">is_all</span>());
<span class="macro">assert!</span>(<span class="ident">AllOrSome::Some</span>(()).<span class="ident">is_some</span>());
}
</pre></div>
</section><section id="search" class="content hidden"></section><section class="footer"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_cors" data-search-js="../../search-index.js"></div>

View File

@@ -626,18 +626,18 @@
<span id="625">625</span>
<span id="626">626</span>
</pre><div class="example-wrap"><pre class="rust ">
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">collections</span>::<span class="ident">HashSet</span>, <span class="ident">convert</span>::<span class="ident">TryInto</span>, <span class="ident">iter</span>::<span class="ident">FromIterator</span>, <span class="ident">rc</span>::<span class="ident">Rc</span>};
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">collections::HashSet</span>, <span class="ident">convert::TryInto</span>, <span class="ident">iter::FromIterator</span>, <span class="ident">rc::Rc</span>};
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::{<span class="ident">RequestHead</span>, <span class="ident">Service</span>, <span class="ident">ServiceRequest</span>, <span class="ident">ServiceResponse</span>, <span class="ident">Transform</span>},
<span class="ident">error</span>::{<span class="ident">Error</span>, <span class="prelude-ty">Result</span>},
<span class="ident">http</span>::{<span class="self">self</span>, <span class="ident">header</span>::<span class="ident">HeaderName</span>, <span class="ident">Error</span> <span class="kw">as</span> <span class="ident">HttpError</span>, <span class="ident">HeaderValue</span>, <span class="ident">Method</span>, <span class="ident">Uri</span>},
<span class="ident">http</span>::{<span class="self">self</span>, <span class="ident">header::HeaderName</span>, <span class="ident">Error</span> <span class="kw">as</span> <span class="ident">HttpError</span>, <span class="ident">HeaderValue</span>, <span class="ident">Method</span>, <span class="ident">Uri</span>},
<span class="ident">Either</span>,
};
<span class="kw">use</span> <span class="ident">futures_util</span>::<span class="ident">future</span>::{<span class="self">self</span>, <span class="ident">Ready</span>};
<span class="kw">use</span> <span class="ident">log</span>::<span class="ident">error</span>;
<span class="kw">use</span> <span class="ident">once_cell</span>::<span class="ident">sync</span>::<span class="ident">Lazy</span>;
<span class="kw">use</span> <span class="ident">tinyvec</span>::<span class="ident">tiny_vec</span>;
<span class="kw">use</span> <span class="ident">futures_util::future</span>::{<span class="self">self</span>, <span class="ident">Ready</span>};
<span class="kw">use</span> <span class="ident">log::error</span>;
<span class="kw">use</span> <span class="ident">once_cell::sync::Lazy</span>;
<span class="kw">use</span> <span class="ident">tinyvec::tiny_vec</span>;
<span class="kw">use</span> <span class="kw">crate</span>::{<span class="ident">AllOrSome</span>, <span class="ident">CorsError</span>, <span class="ident">CorsMiddleware</span>, <span class="ident">Inner</span>, <span class="ident">OriginFn</span>};
@@ -645,26 +645,26 @@
<span class="doccomment">/// Additionally, always causes first error (if any) to be reported during initialization.</span>
<span class="kw">fn</span> <span class="ident">cors</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span>(
<span class="ident">inner</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="kw-2">mut</span> <span class="ident">Rc</span><span class="op">&lt;</span><span class="ident">Inner</span><span class="op">&gt;</span>,
<span class="ident">err</span>: <span class="kw-2">&amp;</span><span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Either</span><span class="op">&lt;</span><span class="ident">http</span>::<span class="ident">Error</span>, <span class="ident">CorsError</span><span class="op">&gt;</span><span class="op">&gt;</span>,
<span class="ident">err</span>: <span class="kw-2">&amp;</span><span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Either</span><span class="op">&lt;</span><span class="ident">http::Error</span>, <span class="ident">CorsError</span><span class="op">&gt;</span><span class="op">&gt;</span>,
) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="kw-2">mut</span> <span class="ident">Inner</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">err</span>.<span class="ident">is_some</span>() {
<span class="kw">return</span> <span class="prelude-val">None</span>;
}
<span class="ident">Rc</span>::<span class="ident">get_mut</span>(<span class="ident">inner</span>)
<span class="ident">Rc::get_mut</span>(<span class="ident">inner</span>)
}
<span class="kw">static</span> <span class="ident">ALL_METHODS_SET</span>: <span class="ident">Lazy</span><span class="op">&lt;</span><span class="ident">HashSet</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Lazy</span>::<span class="ident">new</span>(<span class="op">|</span><span class="op">|</span> {
<span class="ident">HashSet</span>::<span class="ident">from_iter</span>(<span class="macro">vec</span><span class="macro">!</span>[
<span class="ident">Method</span>::<span class="ident">GET</span>,
<span class="ident">Method</span>::<span class="ident">POST</span>,
<span class="ident">Method</span>::<span class="ident">PUT</span>,
<span class="ident">Method</span>::<span class="ident">DELETE</span>,
<span class="ident">Method</span>::<span class="ident">HEAD</span>,
<span class="ident">Method</span>::<span class="ident">OPTIONS</span>,
<span class="ident">Method</span>::<span class="ident">CONNECT</span>,
<span class="ident">Method</span>::<span class="ident">PATCH</span>,
<span class="ident">Method</span>::<span class="ident">TRACE</span>,
<span class="kw">static</span> <span class="ident">ALL_METHODS_SET</span>: <span class="ident">Lazy</span><span class="op">&lt;</span><span class="ident">HashSet</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Lazy::new</span>(<span class="op">|</span><span class="op">|</span> {
<span class="ident">HashSet::from_iter</span>(<span class="macro">vec!</span>[
<span class="ident">Method::GET</span>,
<span class="ident">Method::POST</span>,
<span class="ident">Method::PUT</span>,
<span class="ident">Method::DELETE</span>,
<span class="ident">Method::HEAD</span>,
<span class="ident">Method::OPTIONS</span>,
<span class="ident">Method::CONNECT</span>,
<span class="ident">Method::PATCH</span>,
<span class="ident">Method::TRACE</span>,
])
});
@@ -699,7 +699,7 @@
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>)]</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Cors</span> {
<span class="ident">inner</span>: <span class="ident">Rc</span><span class="op">&lt;</span><span class="ident">Inner</span><span class="op">&gt;</span>,
<span class="ident">error</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Either</span><span class="op">&lt;</span><span class="ident">http</span>::<span class="ident">Error</span>, <span class="ident">CorsError</span><span class="op">&gt;</span><span class="op">&gt;</span>,
<span class="ident">error</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Either</span><span class="op">&lt;</span><span class="ident">http::Error</span>, <span class="ident">CorsError</span><span class="op">&gt;</span><span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Cors</span> {
@@ -709,16 +709,16 @@
<span class="doccomment">/// Max age 1 hour. Does not send wildcard.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">permissive</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Inner</span> {
<span class="ident">allowed_origins</span>: <span class="ident">AllOrSome</span>::<span class="ident">All</span>,
<span class="ident">allowed_origins_fns</span>: <span class="macro">tiny_vec</span><span class="macro">!</span>[],
<span class="ident">allowed_origins</span>: <span class="ident">AllOrSome::All</span>,
<span class="ident">allowed_origins_fns</span>: <span class="macro">tiny_vec!</span>[],
<span class="ident">allowed_methods</span>: <span class="ident">ALL_METHODS_SET</span>.<span class="ident">clone</span>(),
<span class="ident">allowed_methods_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">allowed_headers</span>: <span class="ident">AllOrSome</span>::<span class="ident">All</span>,
<span class="ident">allowed_headers</span>: <span class="ident">AllOrSome::All</span>,
<span class="ident">allowed_headers_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">expose_headers</span>: <span class="ident">AllOrSome</span>::<span class="ident">All</span>,
<span class="ident">expose_headers</span>: <span class="ident">AllOrSome::All</span>,
<span class="ident">expose_headers_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">max_age</span>: <span class="prelude-val">Some</span>(<span class="number">3600</span>),
<span class="ident">preflight</span>: <span class="bool-val">true</span>,
@@ -728,7 +728,7 @@
};
<span class="ident">Cors</span> {
<span class="ident">inner</span>: <span class="ident">Rc</span>::<span class="ident">new</span>(<span class="ident">inner</span>),
<span class="ident">inner</span>: <span class="ident">Rc::new</span>(<span class="ident">inner</span>),
<span class="ident">error</span>: <span class="prelude-val">None</span>,
}
}
@@ -738,7 +738,7 @@
<span class="doccomment">/// See [`Cors::allowed_origin`] for more info on allowed origins.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">allow_any_origin</span>(<span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="ident">cors</span>.<span class="ident">allowed_origins</span> <span class="op">=</span> <span class="ident">AllOrSome</span>::<span class="ident">All</span>;
<span class="ident">cors</span>.<span class="ident">allowed_origins</span> <span class="op">=</span> <span class="ident">AllOrSome::All</span>;
}
<span class="self">self</span>
@@ -770,16 +770,16 @@
<span class="doccomment">/// [Fetch Standard]: https://fetch.spec.whatwg.org/#origin-header</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">allowed_origin</span>(<span class="kw-2">mut</span> <span class="self">self</span>, <span class="ident">origin</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="kw">match</span> <span class="ident">TryInto</span>::<span class="op">&lt;</span><span class="ident">Uri</span><span class="op">&gt;</span>::<span class="ident">try_into</span>(<span class="ident">origin</span>) {
<span class="kw">match</span> <span class="ident">TryInto</span>::<span class="op">&lt;</span><span class="ident">Uri</span><span class="op">&gt;</span><span class="ident">::try_into</span>(<span class="ident">origin</span>) {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) <span class="kw">if</span> <span class="ident">origin</span> <span class="op">=</span><span class="op">=</span> <span class="string">&quot;*&quot;</span> <span class="op">=</span><span class="op">&gt;</span> {
<span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;Wildcard in `allowed_origin` is not allowed. Use `send_wildcard`.&quot;</span>);
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Right</span>(<span class="ident">CorsError</span>::<span class="ident">WildcardOrigin</span>));
<span class="macro">error!</span>(<span class="string">&quot;Wildcard in `allowed_origin` is not allowed. Use `send_wildcard`.&quot;</span>);
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Right</span>(<span class="ident">CorsError::WildcardOrigin</span>));
}
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">cors</span>.<span class="ident">allowed_origins</span>.<span class="ident">is_all</span>() {
<span class="ident">cors</span>.<span class="ident">allowed_origins</span> <span class="op">=</span>
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>));
<span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">origins</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">allowed_origins</span>.<span class="ident">as_mut</span>() {
@@ -790,7 +790,7 @@
}
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
}
}
}
@@ -812,7 +812,7 @@
{
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="ident">cors</span>.<span class="ident">allowed_origins_fns</span>.<span class="ident">push</span>(<span class="ident">OriginFn</span> {
<span class="ident">boxed_fn</span>: <span class="ident">Rc</span>::<span class="ident">new</span>(<span class="ident">f</span>),
<span class="ident">boxed_fn</span>: <span class="ident">Rc::new</span>(<span class="ident">f</span>),
});
}
@@ -842,7 +842,7 @@
<span class="kw">where</span>
<span class="ident">U</span>: <span class="ident">IntoIterator</span><span class="op">&lt;</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">M</span><span class="op">&gt;</span>,
<span class="ident">M</span>: <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">M</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span><span class="op">&gt;</span>::<span class="ident">Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">M</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span><span class="op">&gt;</span><span class="ident">::Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
{
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="kw">for</span> <span class="ident">m</span> <span class="kw">in</span> <span class="ident">methods</span> {
@@ -852,7 +852,7 @@
}
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="kw">break</span>;
}
}
@@ -867,7 +867,7 @@
<span class="doccomment">/// See [`Cors::allowed_headers`] for more info on allowed request headers.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">allow_any_header</span>(<span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="ident">cors</span>.<span class="ident">allowed_headers</span> <span class="op">=</span> <span class="ident">AllOrSome</span>::<span class="ident">All</span>;
<span class="ident">cors</span>.<span class="ident">allowed_headers</span> <span class="op">=</span> <span class="ident">AllOrSome::All</span>;
}
<span class="self">self</span>
@@ -879,22 +879,22 @@
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">allowed_header</span><span class="op">&lt;</span><span class="ident">H</span><span class="op">&gt;</span>(<span class="kw-2">mut</span> <span class="self">self</span>, <span class="ident">header</span>: <span class="ident">H</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span>
<span class="kw">where</span>
<span class="ident">H</span>: <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span>::<span class="ident">Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span><span class="ident">::Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
{
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="kw">match</span> <span class="ident">header</span>.<span class="ident">try_into</span>() {
<span class="prelude-val">Ok</span>(<span class="ident">method</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span>.<span class="ident">is_all</span>() {
<span class="ident">cors</span>.<span class="ident">allowed_headers</span> <span class="op">=</span>
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>));
<span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome::Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span> {
<span class="ident">headers</span>.<span class="ident">insert</span>(<span class="ident">method</span>);
}
}
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>.<span class="ident">into</span>())),
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Left</span>(<span class="ident">err</span>.<span class="ident">into</span>())),
}
}
@@ -915,7 +915,7 @@
<span class="kw">where</span>
<span class="ident">U</span>: <span class="ident">IntoIterator</span><span class="op">&lt;</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">H</span><span class="op">&gt;</span>,
<span class="ident">H</span>: <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span>::<span class="ident">Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span><span class="ident">::Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
{
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="kw">for</span> <span class="ident">h</span> <span class="kw">in</span> <span class="ident">headers</span> {
@@ -923,15 +923,15 @@
<span class="prelude-val">Ok</span>(<span class="ident">method</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span>.<span class="ident">is_all</span>() {
<span class="ident">cors</span>.<span class="ident">allowed_headers</span> <span class="op">=</span>
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>));
<span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome::Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">allowed_headers</span> {
<span class="ident">headers</span>.<span class="ident">insert</span>(<span class="ident">method</span>);
}
}
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="kw">break</span>;
}
}
@@ -946,7 +946,7 @@
<span class="doccomment">/// See [`Cors::expose_headers`] for more info on exposed response headers.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">expose_any_header</span>(<span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="ident">cors</span>.<span class="ident">expose_headers</span> <span class="op">=</span> <span class="ident">AllOrSome</span>::<span class="ident">All</span>;
<span class="ident">cors</span>.<span class="ident">expose_headers</span> <span class="op">=</span> <span class="ident">AllOrSome::All</span>;
}
<span class="self">self</span>
@@ -963,7 +963,7 @@
<span class="kw">where</span>
<span class="ident">U</span>: <span class="ident">IntoIterator</span><span class="op">&lt;</span><span class="ident">Item</span> <span class="op">=</span> <span class="ident">H</span><span class="op">&gt;</span>,
<span class="ident">H</span>: <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span>::<span class="ident">Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
<span class="op">&lt;</span><span class="ident">H</span> <span class="kw">as</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span><span class="op">&gt;</span><span class="ident">::Error</span>: <span class="ident">Into</span><span class="op">&lt;</span><span class="ident">HttpError</span><span class="op">&gt;</span>,
{
<span class="kw">for</span> <span class="ident">h</span> <span class="kw">in</span> <span class="ident">headers</span> {
<span class="kw">match</span> <span class="ident">h</span>.<span class="ident">try_into</span>() {
@@ -971,15 +971,15 @@
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">cors</span>) <span class="op">=</span> <span class="ident">cors</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">inner</span>, <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">error</span>) {
<span class="kw">if</span> <span class="ident">cors</span>.<span class="ident">expose_headers</span>.<span class="ident">is_all</span>() {
<span class="ident">cors</span>.<span class="ident">expose_headers</span> <span class="op">=</span>
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>));
<span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">expose_headers</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="ident">AllOrSome::Some</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">expose_headers</span> {
<span class="ident">headers</span>.<span class="ident">insert</span>(<span class="ident">header</span>);
}
}
}
<span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="self">self</span>.<span class="ident">error</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">Either::Left</span>(<span class="ident">err</span>.<span class="ident">into</span>()));
<span class="kw">break</span>;
}
}
@@ -1085,16 +1085,16 @@
<span class="doccomment">/// not supported. No max age (will use browser&#39;s default).</span>
<span class="kw">fn</span> <span class="ident">default</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">Cors</span> {
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Inner</span> {
<span class="ident">allowed_origins</span>: <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>)),
<span class="ident">allowed_origins_fns</span>: <span class="macro">tiny_vec</span><span class="macro">!</span>[],
<span class="ident">allowed_origins</span>: <span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>)),
<span class="ident">allowed_origins_fns</span>: <span class="macro">tiny_vec!</span>[],
<span class="ident">allowed_methods</span>: <span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>),
<span class="ident">allowed_methods</span>: <span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>),
<span class="ident">allowed_methods_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">allowed_headers</span>: <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>)),
<span class="ident">allowed_headers</span>: <span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>)),
<span class="ident">allowed_headers_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">expose_headers</span>: <span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>)),
<span class="ident">expose_headers</span>: <span class="ident">AllOrSome::Some</span>(<span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>)),
<span class="ident">expose_headers_baked</span>: <span class="prelude-val">None</span>,
<span class="ident">max_age</span>: <span class="prelude-val">None</span>,
@@ -1105,7 +1105,7 @@
};
<span class="ident">Cors</span> {
<span class="ident">inner</span>: <span class="ident">Rc</span>::<span class="ident">new</span>(<span class="ident">inner</span>),
<span class="ident">inner</span>: <span class="ident">Rc::new</span>(<span class="ident">inner</span>),
<span class="ident">error</span>: <span class="prelude-val">None</span>,
}
}
@@ -1114,41 +1114,41 @@
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">S</span>, <span class="ident">B</span><span class="op">&gt;</span> <span class="ident">Transform</span><span class="op">&lt;</span><span class="ident">S</span>, <span class="ident">ServiceRequest</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">Cors</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Service</span><span class="op">&lt;</span><span class="ident">ServiceRequest</span>, <span class="ident">Response</span> <span class="op">=</span> <span class="ident">ServiceResponse</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>, <span class="ident">Error</span> <span class="op">=</span> <span class="ident">Error</span><span class="op">&gt;</span>,
<span class="ident">S</span>::<span class="ident">Future</span>: <span class="lifetime">&#39;static</span>,
<span class="ident">S::Future</span>: <span class="lifetime">&#39;static</span>,
<span class="ident">B</span>: <span class="lifetime">&#39;static</span>,
{
<span class="kw">type</span> <span class="ident">Response</span> <span class="op">=</span> <span class="ident">ServiceResponse</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>;
<span class="kw">type</span> <span class="ident">Error</span> <span class="op">=</span> <span class="ident">Error</span>;
<span class="kw">type</span> <span class="ident">InitError</span> <span class="op">=</span> ();
<span class="kw">type</span> <span class="ident">Transform</span> <span class="op">=</span> <span class="ident">CorsMiddleware</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>;
<span class="kw">type</span> <span class="ident">Future</span> <span class="op">=</span> <span class="ident">Ready</span><span class="op">&lt;</span><span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="self">Self</span>::<span class="ident">Transform</span>, <span class="self">Self</span>::<span class="ident">InitError</span><span class="op">&gt;</span><span class="op">&gt;</span>;
<span class="kw">type</span> <span class="ident">Future</span> <span class="op">=</span> <span class="ident">Ready</span><span class="op">&lt;</span><span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="self">Self</span><span class="ident">::Transform</span>, <span class="self">Self</span><span class="ident">::InitError</span><span class="op">&gt;</span><span class="op">&gt;</span>;
<span class="kw">fn</span> <span class="ident">new_transform</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">service</span>: <span class="ident">S</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span>::<span class="ident">Future</span> {
<span class="kw">fn</span> <span class="ident">new_transform</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">service</span>: <span class="ident">S</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span><span class="ident">::Future</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">err</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">error</span> {
<span class="kw">match</span> <span class="ident">err</span> {
<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">err</span>),
<span class="ident">Either</span>::<span class="ident">Right</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">err</span>),
<span class="ident">Either::Left</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="macro">error!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">err</span>),
<span class="ident">Either::Right</span>(<span class="ident">err</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="macro">error!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">err</span>),
}
<span class="kw">return</span> <span class="ident">future</span>::<span class="ident">err</span>(());
<span class="kw">return</span> <span class="ident">future::err</span>(());
}
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc::clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">if</span> <span class="ident">inner</span>.<span class="ident">supports_credentials</span>
<span class="op">&amp;&amp;</span> <span class="ident">inner</span>.<span class="ident">send_wildcard</span>
<span class="op">&amp;&amp;</span> <span class="ident">inner</span>.<span class="ident">allowed_origins</span>.<span class="ident">is_all</span>()
{
<span class="macro">error</span><span class="macro">!</span>(<span class="string">&quot;Illegal combination of CORS options: credentials can not be supported when all \
<span class="macro">error!</span>(<span class="string">&quot;Illegal combination of CORS options: credentials can not be supported when all \
origins are allowed and `send_wildcard` is enabled.&quot;</span>);
<span class="kw">return</span> <span class="ident">future</span>::<span class="ident">err</span>(());
<span class="kw">return</span> <span class="ident">future::err</span>(());
}
<span class="comment">// bake allowed headers value if Some and not empty</span>
<span class="kw">match</span> <span class="ident">inner</span>.<span class="ident">allowed_headers</span>.<span class="ident">as_ref</span>() {
<span class="prelude-val">Some</span>(<span class="ident">header_set</span>) <span class="kw">if</span> <span class="op">!</span><span class="ident">header_set</span>.<span class="ident">is_empty</span>() <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">allowed_headers_str</span> <span class="op">=</span> <span class="ident">intersperse_header_values</span>(<span class="ident">header_set</span>);
<span class="ident">Rc</span>::<span class="ident">make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">allowed_headers_baked</span> <span class="op">=</span>
<span class="ident">Rc::make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">allowed_headers_baked</span> <span class="op">=</span>
<span class="prelude-val">Some</span>(<span class="ident">allowed_headers_str</span>);
}
<span class="kw">_</span> <span class="op">=</span><span class="op">&gt;</span> {}
@@ -1157,19 +1157,19 @@
<span class="comment">// bake allowed methods value if not empty</span>
<span class="kw">if</span> <span class="op">!</span><span class="ident">inner</span>.<span class="ident">allowed_methods</span>.<span class="ident">is_empty</span>() {
<span class="kw">let</span> <span class="ident">allowed_methods_str</span> <span class="op">=</span> <span class="ident">intersperse_header_values</span>(<span class="kw-2">&amp;</span><span class="ident">inner</span>.<span class="ident">allowed_methods</span>);
<span class="ident">Rc</span>::<span class="ident">make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">allowed_methods_baked</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">allowed_methods_str</span>);
<span class="ident">Rc::make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">allowed_methods_baked</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">allowed_methods_str</span>);
}
<span class="comment">// bake exposed headers value if Some and not empty</span>
<span class="kw">match</span> <span class="ident">inner</span>.<span class="ident">expose_headers</span>.<span class="ident">as_ref</span>() {
<span class="prelude-val">Some</span>(<span class="ident">header_set</span>) <span class="kw">if</span> <span class="op">!</span><span class="ident">header_set</span>.<span class="ident">is_empty</span>() <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">expose_headers_str</span> <span class="op">=</span> <span class="ident">intersperse_header_values</span>(<span class="ident">header_set</span>);
<span class="ident">Rc</span>::<span class="ident">make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">expose_headers_baked</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">expose_headers_str</span>);
<span class="ident">Rc::make_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">inner</span>).<span class="ident">expose_headers_baked</span> <span class="op">=</span> <span class="prelude-val">Some</span>(<span class="ident">expose_headers_str</span>);
}
<span class="kw">_</span> <span class="op">=</span><span class="op">&gt;</span> {}
}
<span class="ident">future</span>::<span class="ident">ok</span>(<span class="ident">CorsMiddleware</span> { <span class="ident">service</span>, <span class="ident">inner</span> })
<span class="ident">future::ok</span>(<span class="ident">CorsMiddleware</span> { <span class="ident">service</span>, <span class="ident">inner</span> })
}
}
@@ -1180,7 +1180,7 @@
{
<span class="ident">val_set</span>
.<span class="ident">iter</span>()
.<span class="ident">fold</span>(<span class="ident">String</span>::<span class="ident">with_capacity</span>(<span class="number">32</span>), <span class="op">|</span><span class="kw-2">mut</span> <span class="ident">acc</span>, <span class="ident">val</span><span class="op">|</span> {
.<span class="ident">fold</span>(<span class="ident">String::with_capacity</span>(<span class="number">32</span>), <span class="op">|</span><span class="kw-2">mut</span> <span class="ident">acc</span>, <span class="ident">val</span><span class="op">|</span> {
<span class="ident">acc</span>.<span class="ident">push_str</span>(<span class="string">&quot;, &quot;</span>);
<span class="ident">acc</span>.<span class="ident">push_str</span>(<span class="ident">val</span>.<span class="ident">as_ref</span>());
<span class="ident">acc</span>
@@ -1194,10 +1194,10 @@
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
<span class="kw">mod</span> <span class="ident">test</span> {
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">convert</span>::{<span class="ident">Infallible</span>, <span class="ident">TryInto</span>};
<span class="kw">use</span> <span class="ident">std::convert</span>::{<span class="ident">Infallible</span>, <span class="ident">TryInto</span>};
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::<span class="ident">Transform</span>,
<span class="ident">dev::Transform</span>,
<span class="ident">http</span>::{<span class="ident">HeaderName</span>, <span class="ident">StatusCode</span>},
<span class="ident">test</span>::{<span class="self">self</span>, <span class="ident">TestRequest</span>},
};
@@ -1209,47 +1209,47 @@
<span class="comment">// using the permissive defaults (all origins allowed) and adding send_wildcard</span>
<span class="comment">// and supports_credentials should error on construction</span>
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">Cors</span>::<span class="ident">permissive</span>()
<span class="macro">assert!</span>(<span class="ident">Cors::permissive</span>()
.<span class="ident">supports_credentials</span>()
.<span class="ident">send_wildcard</span>()
.<span class="ident">new_transform</span>(<span class="ident">test</span>::<span class="ident">ok_service</span>())
.<span class="ident">new_transform</span>(<span class="ident">test::ok_service</span>())
.<span class="ident">into_inner</span>()
.<span class="ident">is_err</span>());
}
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">restrictive_defaults</span>() {
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>()
.<span class="ident">new_transform</span>(<span class="ident">test</span>::<span class="ident">ok_service</span>())
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>()
.<span class="ident">new_transform</span>(<span class="ident">test::ok_service</span>())
.<span class="kw">await</span>
.<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">default</span>()
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::default</span>()
.<span class="ident">insert_header</span>((<span class="string">&quot;Origin&quot;</span>, <span class="string">&quot;https://www.example.com&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode</span>::<span class="ident">BAD_REQUEST</span>);
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test::call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode::BAD_REQUEST</span>);
}
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">allowed_header_try_from</span>() {
<span class="kw">let</span> <span class="ident">_cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>().<span class="ident">allowed_header</span>(<span class="string">&quot;Content-Type&quot;</span>);
<span class="kw">let</span> <span class="ident">_cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>().<span class="ident">allowed_header</span>(<span class="string">&quot;Content-Type&quot;</span>);
}
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">allowed_header_try_into</span>() {
<span class="kw">struct</span> <span class="ident">ContentType</span>;
<span class="kw">impl</span> <span class="ident">TryInto</span><span class="op">&lt;</span><span class="ident">HeaderName</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">ContentType</span> {
<span class="kw">type</span> <span class="ident">Error</span> <span class="op">=</span> <span class="ident">Infallible</span>;
<span class="kw">fn</span> <span class="ident">try_into</span>(<span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">HeaderName</span>, <span class="self">Self</span>::<span class="ident">Error</span><span class="op">&gt;</span> {
<span class="prelude-val">Ok</span>(<span class="ident">HeaderName</span>::<span class="ident">from_static</span>(<span class="string">&quot;content-type&quot;</span>))
<span class="kw">fn</span> <span class="ident">try_into</span>(<span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">HeaderName</span>, <span class="self">Self</span><span class="ident">::Error</span><span class="op">&gt;</span> {
<span class="prelude-val">Ok</span>(<span class="ident">HeaderName::from_static</span>(<span class="string">&quot;content-type&quot;</span>))
}
}
<span class="kw">let</span> <span class="ident">_cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>().<span class="ident">allowed_header</span>(<span class="ident">ContentType</span>);
<span class="kw">let</span> <span class="ident">_cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>().<span class="ident">allowed_header</span>(<span class="ident">ContentType</span>);
}
}
</pre></div>

View File

@@ -56,7 +56,7 @@
<span id="55">55</span>
<span id="56">56</span>
</pre><div class="example-wrap"><pre class="rust ">
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">http</span>::<span class="ident">StatusCode</span>, <span class="ident">HttpResponse</span>, <span class="ident">ResponseError</span>};
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">http::StatusCode</span>, <span class="ident">HttpResponse</span>, <span class="ident">ResponseError</span>};
<span class="kw">use</span> <span class="ident">derive_more</span>::{<span class="ident">Display</span>, <span class="ident">Error</span>};
@@ -105,11 +105,11 @@
<span class="kw">impl</span> <span class="ident">ResponseError</span> <span class="kw">for</span> <span class="ident">CorsError</span> {
<span class="kw">fn</span> <span class="ident">status_code</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">StatusCode</span> {
<span class="ident">StatusCode</span>::<span class="ident">BAD_REQUEST</span>
<span class="ident">StatusCode::BAD_REQUEST</span>
}
<span class="kw">fn</span> <span class="ident">error_response</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">HttpResponse</span> {
<span class="ident">HttpResponse</span>::<span class="ident">with_body</span>(<span class="ident">StatusCode</span>::<span class="ident">BAD_REQUEST</span>, <span class="self">self</span>.<span class="ident">to_string</span>().<span class="ident">into</span>())
<span class="ident">HttpResponse::with_body</span>(<span class="ident">StatusCode::BAD_REQUEST</span>, <span class="self">self</span>.<span class="ident">to_string</span>().<span class="ident">into</span>())
}
}
</pre></div>

View File

@@ -338,18 +338,18 @@
<span id="337">337</span>
<span id="338">338</span>
</pre><div class="example-wrap"><pre class="rust ">
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">collections</span>::<span class="ident">HashSet</span>, <span class="ident">convert</span>::<span class="ident">TryFrom</span>, <span class="ident">convert</span>::<span class="ident">TryInto</span>, <span class="ident">fmt</span>, <span class="ident">rc</span>::<span class="ident">Rc</span>};
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">collections::HashSet</span>, <span class="ident">convert::TryFrom</span>, <span class="ident">convert::TryInto</span>, <span class="ident">fmt</span>, <span class="ident">rc::Rc</span>};
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::<span class="ident">RequestHead</span>,
<span class="ident">error</span>::<span class="prelude-ty">Result</span>,
<span class="ident">dev::RequestHead</span>,
<span class="ident">error::Result</span>,
<span class="ident">http</span>::{
<span class="ident">header</span>::{<span class="self">self</span>, <span class="ident">HeaderName</span>, <span class="ident">HeaderValue</span>},
<span class="ident">Method</span>,
},
};
<span class="kw">use</span> <span class="ident">once_cell</span>::<span class="ident">sync</span>::<span class="ident">Lazy</span>;
<span class="kw">use</span> <span class="ident">tinyvec</span>::<span class="ident">TinyVec</span>;
<span class="kw">use</span> <span class="ident">once_cell::sync::Lazy</span>;
<span class="kw">use</span> <span class="ident">tinyvec::TinyVec</span>;
<span class="kw">use</span> <span class="kw">crate</span>::{<span class="ident">AllOrSome</span>, <span class="ident">CorsError</span>};
@@ -361,13 +361,13 @@
<span class="kw">impl</span> <span class="ident">Default</span> <span class="kw">for</span> <span class="ident">OriginFn</span> {
<span class="doccomment">/// Dummy default for use in tiny_vec. Do not use.</span>
<span class="kw">fn</span> <span class="ident">default</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span> {
<span class="kw">let</span> <span class="ident">boxed_fn</span>: <span class="ident">Rc</span><span class="op">&lt;</span><span class="kw">dyn</span> <span class="ident">Fn</span>(<span class="kw-2">&amp;</span><span class="kw">_</span>, <span class="kw-2">&amp;</span><span class="kw">_</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="kw">_</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">new</span>(<span class="op">|</span><span class="ident">_origin</span>, <span class="ident">_req_head</span><span class="op">|</span> <span class="bool-val">false</span>);
<span class="kw">let</span> <span class="ident">boxed_fn</span>: <span class="ident">Rc</span><span class="op">&lt;</span><span class="kw">dyn</span> <span class="ident">Fn</span>(<span class="kw-2">&amp;</span><span class="kw">_</span>, <span class="kw-2">&amp;</span><span class="kw">_</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="kw">_</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Rc::new</span>(<span class="op">|</span><span class="ident">_origin</span>, <span class="ident">_req_head</span><span class="op">|</span> <span class="bool-val">false</span>);
<span class="self">Self</span> { <span class="ident">boxed_fn</span> }
}
}
<span class="kw">impl</span> <span class="ident">fmt</span>::<span class="ident">Debug</span> <span class="kw">for</span> <span class="ident">OriginFn</span> {
<span class="kw">fn</span> <span class="ident">fmt</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">f</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">fmt</span>::<span class="ident">Formatter</span><span class="op">&lt;</span><span class="lifetime">&#39;_</span><span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">fmt</span>::<span class="prelude-ty">Result</span> {
<span class="kw">impl</span> <span class="ident">fmt::Debug</span> <span class="kw">for</span> <span class="ident">OriginFn</span> {
<span class="kw">fn</span> <span class="ident">fmt</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">f</span>: <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">fmt::Formatter</span><span class="op">&lt;</span><span class="lifetime">&#39;_</span><span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">fmt::Result</span> {
<span class="ident">f</span>.<span class="ident">write_str</span>(<span class="string">&quot;origin_fn&quot;</span>)
}
}
@@ -376,7 +376,7 @@
<span class="kw">fn</span> <span class="ident">header_value_try_into_method</span>(<span class="ident">hdr</span>: <span class="kw-2">&amp;</span><span class="ident">HeaderValue</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Method</span><span class="op">&gt;</span> {
<span class="ident">hdr</span>.<span class="ident">to_str</span>()
.<span class="ident">ok</span>()
.<span class="ident">and_then</span>(<span class="op">|</span><span class="ident">meth</span><span class="op">|</span> <span class="ident">Method</span>::<span class="ident">try_from</span>(<span class="ident">meth</span>).<span class="ident">ok</span>())
.<span class="ident">and_then</span>(<span class="op">|</span><span class="ident">meth</span><span class="op">|</span> <span class="ident">Method::try_from</span>(<span class="ident">meth</span>).<span class="ident">ok</span>())
}
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">Clone</span>)]</span>
@@ -401,21 +401,21 @@
<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="ident">vary_header</span>: <span class="ident">bool</span>,
}
<span class="kw">static</span> <span class="ident">EMPTY_ORIGIN_SET</span>: <span class="ident">Lazy</span><span class="op">&lt;</span><span class="ident">HashSet</span><span class="op">&lt;</span><span class="ident">HeaderValue</span><span class="op">&gt;</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Lazy</span>::<span class="ident">new</span>(<span class="ident">HashSet</span>::<span class="ident">new</span>);
<span class="kw">static</span> <span class="ident">EMPTY_ORIGIN_SET</span>: <span class="ident">Lazy</span><span class="op">&lt;</span><span class="ident">HashSet</span><span class="op">&lt;</span><span class="ident">HeaderValue</span><span class="op">&gt;</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Lazy::new</span>(<span class="ident">HashSet::new</span>);
<span class="kw">impl</span> <span class="ident">Inner</span> {
<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn</span> <span class="ident">validate_origin</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">req</span>: <span class="kw-2">&amp;</span><span class="ident">RequestHead</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>(), <span class="ident">CorsError</span><span class="op">&gt;</span> {
<span class="comment">// return early if all origins are allowed or get ref to allowed origins set</span>
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy</span>::<span class="ident">mutable_key_type</span>)]</span>
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy::mutable_key_type</span>)]</span>
<span class="kw">let</span> <span class="ident">allowed_origins</span> <span class="op">=</span> <span class="kw">match</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">allowed_origins</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span> <span class="kw">if</span> <span class="self">self</span>.<span class="ident">allowed_origins_fns</span>.<span class="ident">is_empty</span>() <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Ok</span>(()),
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">allowed_origins</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">allowed_origins</span>,
<span class="ident">AllOrSome::All</span> <span class="kw">if</span> <span class="self">self</span>.<span class="ident">allowed_origins_fns</span>.<span class="ident">is_empty</span>() <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Ok</span>(()),
<span class="ident">AllOrSome::Some</span>(<span class="ident">allowed_origins</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">allowed_origins</span>,
<span class="comment">// only function origin validators are defined</span>
<span class="kw">_</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw-2">&amp;</span><span class="ident">EMPTY_ORIGIN_SET</span>,
};
<span class="comment">// get origin header and try to parse as string</span>
<span class="kw">match</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ORIGIN</span>) {
<span class="kw">match</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header::ORIGIN</span>) {
<span class="comment">// origin header exists and is a string</span>
<span class="prelude-val">Some</span>(<span class="ident">origin</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="ident">allowed_origins</span>.<span class="ident">contains</span>(<span class="ident">origin</span>)
@@ -423,14 +423,14 @@
{
<span class="prelude-val">Ok</span>(())
} <span class="kw">else</span> {
<span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">OriginNotAllowed</span>)
<span class="prelude-val">Err</span>(<span class="ident">CorsError::OriginNotAllowed</span>)
}
}
<span class="comment">// origin header is missing</span>
<span class="comment">// note: with our implementation, the origin header is required for OPTIONS request or</span>
<span class="comment">// else this would be unreachable</span>
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">MissingOrigin</span>),
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::MissingOrigin</span>),
}
}
@@ -446,19 +446,19 @@
<span class="kw-2">&amp;</span><span class="self">self</span>,
<span class="ident">req</span>: <span class="kw-2">&amp;</span><span class="ident">RequestHead</span>,
) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">HeaderValue</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">origin</span> <span class="op">=</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ORIGIN</span>);
<span class="kw">let</span> <span class="ident">origin</span> <span class="op">=</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header::ORIGIN</span>);
<span class="kw">match</span> <span class="self">self</span>.<span class="ident">allowed_origins</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span> <span class="op">=</span><span class="op">&gt;</span> {
<span class="ident">AllOrSome::All</span> <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="self">self</span>.<span class="ident">send_wildcard</span> {
<span class="prelude-val">Some</span>(<span class="ident">HeaderValue</span>::<span class="ident">from_static</span>(<span class="string">&quot;*&quot;</span>))
<span class="prelude-val">Some</span>(<span class="ident">HeaderValue::from_static</span>(<span class="string">&quot;*&quot;</span>))
} <span class="kw">else</span> {
<span class="comment">// see note below about why `.cloned()` is correct</span>
<span class="ident">origin</span>.<span class="ident">cloned</span>()
}
}
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="ident">AllOrSome::Some</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="comment">// since origin (if it exists) is known to be allowed if this method is called</span>
<span class="comment">// then cloning the option is all that is required to be used as an echoed back</span>
<span class="comment">// header value (or omitted if None)</span>
@@ -476,7 +476,7 @@
<span class="comment">// extract access control header and try to parse as method</span>
<span class="kw">let</span> <span class="ident">request_method</span> <span class="op">=</span> <span class="ident">req</span>
.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_METHOD</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_REQUEST_METHOD</span>)
.<span class="ident">map</span>(<span class="ident">header_value_try_into_method</span>);
<span class="kw">match</span> <span class="ident">request_method</span> {
@@ -484,13 +484,13 @@
<span class="prelude-val">Some</span>(<span class="prelude-val">Some</span>(<span class="ident">method</span>)) <span class="kw">if</span> <span class="self">self</span>.<span class="ident">allowed_methods</span>.<span class="ident">contains</span>(<span class="kw-2">&amp;</span><span class="ident">method</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Ok</span>(()),
<span class="comment">// method valid but not allowed</span>
<span class="prelude-val">Some</span>(<span class="prelude-val">Some</span>(<span class="kw">_</span>)) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">MethodNotAllowed</span>),
<span class="prelude-val">Some</span>(<span class="prelude-val">Some</span>(<span class="kw">_</span>)) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::MethodNotAllowed</span>),
<span class="comment">// method invalid</span>
<span class="prelude-val">Some</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">BadRequestMethod</span>),
<span class="prelude-val">Some</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::BadRequestMethod</span>),
<span class="comment">// method missing</span>
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">MissingRequestMethod</span>),
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::MissingRequestMethod</span>),
}
}
@@ -499,17 +499,17 @@
<span class="ident">req</span>: <span class="kw-2">&amp;</span><span class="ident">RequestHead</span>,
) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>(), <span class="ident">CorsError</span><span class="op">&gt;</span> {
<span class="comment">// return early if all headers are allowed or get ref to allowed origins set</span>
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy</span>::<span class="ident">mutable_key_type</span>)]</span>
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy::mutable_key_type</span>)]</span>
<span class="kw">let</span> <span class="ident">allowed_headers</span> <span class="op">=</span> <span class="kw">match</span> <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">allowed_headers</span> {
<span class="ident">AllOrSome</span>::<span class="ident">All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Ok</span>(()),
<span class="ident">AllOrSome</span>::<span class="prelude-val">Some</span>(<span class="ident">allowed_headers</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">allowed_headers</span>,
<span class="ident">AllOrSome::All</span> <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Ok</span>(()),
<span class="ident">AllOrSome::Some</span>(<span class="ident">allowed_headers</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">allowed_headers</span>,
};
<span class="comment">// extract access control header as string</span>
<span class="comment">// header format should be comma separated header names</span>
<span class="kw">let</span> <span class="ident">request_headers</span> <span class="op">=</span> <span class="ident">req</span>
.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>)
.<span class="ident">map</span>(<span class="op">|</span><span class="ident">hdr</span><span class="op">|</span> <span class="ident">hdr</span>.<span class="ident">to_str</span>());
<span class="kw">match</span> <span class="ident">request_headers</span> {
@@ -517,32 +517,32 @@
<span class="prelude-val">Some</span>(<span class="prelude-val">Ok</span>(<span class="ident">headers</span>)) <span class="op">=</span><span class="op">&gt;</span> {
<span class="comment">// the set is ephemeral we take care not to mutate the</span>
<span class="comment">// inserted keys so this lint exception is acceptable</span>
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy</span>::<span class="ident">mutable_key_type</span>)]</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">request_headers</span> <span class="op">=</span> <span class="ident">HashSet</span>::<span class="ident">with_capacity</span>(<span class="number">8</span>);
<span class="attribute">#[<span class="ident">allow</span>(<span class="ident">clippy::mutable_key_type</span>)]</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">request_headers</span> <span class="op">=</span> <span class="ident">HashSet::with_capacity</span>(<span class="number">8</span>);
<span class="comment">// try to convert each header name in the comma-separated list</span>
<span class="kw">for</span> <span class="ident">hdr</span> <span class="kw">in</span> <span class="ident">headers</span>.<span class="ident">split</span>(<span class="string">&#39;,&#39;</span>) {
<span class="kw">match</span> <span class="ident">hdr</span>.<span class="ident">trim</span>().<span class="ident">try_into</span>() {
<span class="prelude-val">Ok</span>(<span class="ident">hdr</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="ident">request_headers</span>.<span class="ident">insert</span>(<span class="ident">hdr</span>),
<span class="prelude-val">Err</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">BadRequestHeaders</span>),
<span class="prelude-val">Err</span>(<span class="kw">_</span>) <span class="op">=</span><span class="op">&gt;</span> <span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::BadRequestHeaders</span>),
};
}
<span class="comment">// header list must contain 1 or more header name</span>
<span class="kw">if</span> <span class="ident">request_headers</span>.<span class="ident">is_empty</span>() {
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">BadRequestHeaders</span>);
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::BadRequestHeaders</span>);
}
<span class="comment">// request header list must be a subset of allowed headers</span>
<span class="kw">if</span> <span class="op">!</span><span class="ident">request_headers</span>.<span class="ident">is_subset</span>(<span class="ident">allowed_headers</span>) {
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">HeadersNotAllowed</span>);
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::HeadersNotAllowed</span>);
}
<span class="prelude-val">Ok</span>(())
}
<span class="comment">// header list is not a string</span>
<span class="prelude-val">Some</span>(<span class="prelude-val">Err</span>(<span class="kw">_</span>)) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError</span>::<span class="ident">BadRequestHeaders</span>),
<span class="prelude-val">Some</span>(<span class="prelude-val">Err</span>(<span class="kw">_</span>)) <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Err</span>(<span class="ident">CorsError::BadRequestHeaders</span>),
<span class="comment">// header list missing</span>
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="prelude-val">Ok</span>(()),
@@ -552,128 +552,128 @@
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
<span class="kw">mod</span> <span class="ident">test</span> {
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">rc</span>::<span class="ident">Rc</span>;
<span class="kw">use</span> <span class="ident">std::rc::Rc</span>;
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::<span class="ident">Transform</span>,
<span class="ident">dev::Transform</span>,
<span class="ident">http</span>::{<span class="ident">header</span>, <span class="ident">HeaderValue</span>, <span class="ident">Method</span>, <span class="ident">StatusCode</span>},
<span class="ident">test</span>::{<span class="self">self</span>, <span class="ident">TestRequest</span>},
};
<span class="kw">use</span> <span class="kw">crate</span>::<span class="ident">Cors</span>;
<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::Cors</span>;
<span class="kw">fn</span> <span class="ident">val_as_str</span>(<span class="ident">val</span>: <span class="kw-2">&amp;</span><span class="ident">HeaderValue</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="kw-2">&amp;</span><span class="ident">str</span> {
<span class="ident">val</span>.<span class="ident">to_str</span>().<span class="ident">unwrap</span>()
}
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">test_validate_not_allowed_origin</span>() {
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>()
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>()
.<span class="ident">allowed_origin</span>(<span class="string">&quot;https://www.example.com&quot;</span>)
.<span class="ident">new_transform</span>(<span class="ident">test</span>::<span class="ident">ok_service</span>())
.<span class="ident">new_transform</span>(<span class="ident">test::ok_service</span>())
.<span class="kw">await</span>
.<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ORIGIN</span>, <span class="string">&quot;https://www.unknown.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>, <span class="string">&quot;DNT&quot;</span>))
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header::ORIGIN</span>, <span class="string">&quot;https://www.unknown.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>, <span class="string">&quot;DNT&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_origin</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_origin</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
}
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">test_preflight</span>() {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>()
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>()
.<span class="ident">allow_any_origin</span>()
.<span class="ident">send_wildcard</span>()
.<span class="ident">max_age</span>(<span class="number">3600</span>)
.<span class="ident">allowed_methods</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Method</span>::<span class="ident">GET</span>, <span class="ident">Method</span>::<span class="ident">OPTIONS</span>, <span class="ident">Method</span>::<span class="ident">POST</span>])
.<span class="ident">allowed_headers</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="ident">header</span>::<span class="ident">AUTHORIZATION</span>, <span class="ident">header</span>::<span class="ident">ACCEPT</span>])
.<span class="ident">allowed_header</span>(<span class="ident">header</span>::<span class="ident">CONTENT_TYPE</span>)
.<span class="ident">new_transform</span>(<span class="ident">test</span>::<span class="ident">ok_service</span>())
.<span class="ident">allowed_methods</span>(<span class="macro">vec!</span>[<span class="ident">Method::GET</span>, <span class="ident">Method::OPTIONS</span>, <span class="ident">Method::POST</span>])
.<span class="ident">allowed_headers</span>(<span class="macro">vec!</span>[<span class="ident">header::AUTHORIZATION</span>, <span class="ident">header::ACCEPT</span>])
.<span class="ident">allowed_header</span>(<span class="ident">header::CONTENT_TYPE</span>)
.<span class="ident">new_transform</span>(<span class="ident">test::ok_service</span>())
.<span class="kw">await</span>
.<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">default</span>()
.<span class="ident">method</span>(<span class="ident">Method</span>::<span class="ident">OPTIONS</span>)
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::default</span>()
.<span class="ident">method</span>(<span class="ident">Method::OPTIONS</span>)
.<span class="ident">insert_header</span>((<span class="string">&quot;Origin&quot;</span>, <span class="string">&quot;https://www.example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>, <span class="string">&quot;X-Not-Allowed&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>, <span class="string">&quot;X-Not-Allowed&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode</span>::<span class="ident">BAD_REQUEST</span>);
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test::call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode::BAD_REQUEST</span>);
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">default</span>()
.<span class="ident">method</span>(<span class="ident">Method</span>::<span class="ident">OPTIONS</span>)
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::default</span>()
.<span class="ident">method</span>(<span class="ident">Method::OPTIONS</span>)
.<span class="ident">insert_header</span>((<span class="string">&quot;Origin&quot;</span>, <span class="string">&quot;https://www.example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;put&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;put&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_ok</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_method</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_err</span>());
<span class="macro">assert!</span>(<span class="ident">cors</span>.<span class="ident">inner</span>.<span class="ident">validate_allowed_headers</span>(<span class="ident">req</span>.<span class="ident">head</span>()).<span class="ident">is_ok</span>());
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">default</span>()
.<span class="ident">method</span>(<span class="ident">Method</span>::<span class="ident">OPTIONS</span>)
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::default</span>()
.<span class="ident">method</span>(<span class="ident">Method::OPTIONS</span>)
.<span class="ident">insert_header</span>((<span class="string">&quot;Origin&quot;</span>, <span class="string">&quot;https://www.example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;POST&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;POST&quot;</span>))
.<span class="ident">insert_header</span>((
<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>,
<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>,
<span class="string">&quot;AUTHORIZATION,ACCEPT&quot;</span>,
))
.<span class="ident">to_srv_request</span>();
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test::call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq!</span>(
<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;*&quot;</span>[..]),
<span class="ident">resp</span>.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue</span>::<span class="ident">as_bytes</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue::as_bytes</span>)
);
<span class="macro">assert_eq</span><span class="macro">!</span>(
<span class="macro">assert_eq!</span>(
<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;3600&quot;</span>[..]),
<span class="ident">resp</span>.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_MAX_AGE</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue</span>::<span class="ident">as_bytes</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_MAX_AGE</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue::as_bytes</span>)
);
<span class="kw">let</span> <span class="ident">hdr</span> <span class="op">=</span> <span class="ident">resp</span>
.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_HEADERS</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_HEADERS</span>)
.<span class="ident">map</span>(<span class="ident">val_as_str</span>)
.<span class="ident">unwrap</span>();
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;authorization&quot;</span>));
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;accept&quot;</span>));
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;content-type&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;authorization&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;accept&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">hdr</span>.<span class="ident">contains</span>(<span class="string">&quot;content-type&quot;</span>));
<span class="kw">let</span> <span class="ident">methods</span> <span class="op">=</span> <span class="ident">resp</span>
.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_METHODS</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_METHODS</span>)
.<span class="ident">unwrap</span>()
.<span class="ident">to_str</span>()
.<span class="ident">unwrap</span>();
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;POST&quot;</span>));
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;GET&quot;</span>));
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;OPTIONS&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;POST&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;GET&quot;</span>));
<span class="macro">assert!</span>(<span class="ident">methods</span>.<span class="ident">contains</span>(<span class="string">&quot;OPTIONS&quot;</span>));
<span class="ident">Rc</span>::<span class="ident">get_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">cors</span>.<span class="ident">inner</span>).<span class="ident">unwrap</span>().<span class="ident">preflight</span> <span class="op">=</span> <span class="bool-val">false</span>;
<span class="ident">Rc::get_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">cors</span>.<span class="ident">inner</span>).<span class="ident">unwrap</span>().<span class="ident">preflight</span> <span class="op">=</span> <span class="bool-val">false</span>;
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">default</span>()
.<span class="ident">method</span>(<span class="ident">Method</span>::<span class="ident">OPTIONS</span>)
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::default</span>()
.<span class="ident">method</span>(<span class="ident">Method::OPTIONS</span>)
.<span class="ident">insert_header</span>((<span class="string">&quot;Origin&quot;</span>, <span class="string">&quot;https://www.example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;POST&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">&quot;POST&quot;</span>))
.<span class="ident">insert_header</span>((
<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>,
<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>,
<span class="string">&quot;AUTHORIZATION,ACCEPT&quot;</span>,
))
.<span class="ident">to_srv_request</span>();
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode</span>::<span class="ident">OK</span>);
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">test::call_service</span>(<span class="kw-2">&amp;</span><span class="ident">cors</span>, <span class="ident">req</span>).<span class="kw">await</span>;
<span class="macro">assert_eq!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode::OK</span>);
}
}
</pre></div>

View File

@@ -115,11 +115,11 @@
<span class="kw">mod</span> <span class="ident">inner</span>;
<span class="kw">mod</span> <span class="ident">middleware</span>;
<span class="kw">use</span> <span class="ident">all_or_some</span>::<span class="ident">AllOrSome</span>;
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">builder</span>::<span class="ident">Cors</span>;
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">error</span>::<span class="ident">CorsError</span>;
<span class="kw">use</span> <span class="ident">all_or_some::AllOrSome</span>;
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">builder::Cors</span>;
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">error::CorsError</span>;
<span class="kw">use</span> <span class="ident">inner</span>::{<span class="ident">Inner</span>, <span class="ident">OriginFn</span>};
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">middleware</span>::<span class="ident">CorsMiddleware</span>;
<span class="kw">pub</span> <span class="kw">use</span> <span class="ident">middleware::CorsMiddleware</span>;
</pre></div>
</section><section id="search" class="content hidden"></section><section class="footer"></section><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_cors" data-search-js="../../search-index.js"></div>
<script src="../../main.js"></script><script src="../../source-script.js"></script><script src="../../source-files.js"></script></body></html>

View File

@@ -217,7 +217,7 @@
<span id="216">216</span>
<span id="217">217</span>
</pre><div class="example-wrap"><pre class="rust ">
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">convert</span>::<span class="ident">TryInto</span>, <span class="ident">rc</span>::<span class="ident">Rc</span>};
<span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">convert::TryInto</span>, <span class="ident">rc::Rc</span>};
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::{<span class="ident">Service</span>, <span class="ident">ServiceRequest</span>, <span class="ident">ServiceResponse</span>},
@@ -228,10 +228,10 @@
},
<span class="ident">HttpResponse</span>,
};
<span class="kw">use</span> <span class="ident">futures_util</span>::<span class="ident">future</span>::{<span class="ident">ok</span>, <span class="ident">Either</span>, <span class="ident">FutureExt</span> <span class="kw">as</span> <span class="kw">_</span>, <span class="ident">LocalBoxFuture</span>, <span class="ident">Ready</span>};
<span class="kw">use</span> <span class="ident">log</span>::<span class="ident">debug</span>;
<span class="kw">use</span> <span class="ident">futures_util::future</span>::{<span class="ident">ok</span>, <span class="ident">Either</span>, <span class="ident">FutureExt</span> <span class="kw">as</span> <span class="kw">_</span>, <span class="ident">LocalBoxFuture</span>, <span class="ident">Ready</span>};
<span class="kw">use</span> <span class="ident">log::debug</span>;
<span class="kw">use</span> <span class="kw">crate</span>::<span class="ident">Inner</span>;
<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::Inner</span>;
<span class="doccomment">/// Service wrapper for Cross-Origin Resource Sharing support.</span>
<span class="doccomment">///</span>
@@ -254,37 +254,37 @@
<span class="kw">return</span> <span class="ident">req</span>.<span class="ident">error_response</span>(<span class="ident">err</span>);
}
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">HttpResponse</span>::<span class="prelude-val">Ok</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">HttpResponse::Ok</span>();
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">origin</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">access_control_allow_origin</span>(<span class="ident">req</span>.<span class="ident">head</span>()) {
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_ORIGIN</span>, <span class="ident">origin</span>));
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_ALLOW_ORIGIN</span>, <span class="ident">origin</span>));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">allowed_methods</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">allowed_methods_baked</span> {
<span class="ident">res</span>.<span class="ident">insert_header</span>((
<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_METHODS</span>,
<span class="ident">header::ACCESS_CONTROL_ALLOW_METHODS</span>,
<span class="ident">allowed_methods</span>.<span class="ident">clone</span>(),
));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">headers</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">allowed_headers_baked</span> {
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_HEADERS</span>, <span class="ident">headers</span>.<span class="ident">clone</span>()));
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_ALLOW_HEADERS</span>, <span class="ident">headers</span>.<span class="ident">clone</span>()));
} <span class="kw">else</span> <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">headers</span>) <span class="op">=</span>
<span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_REQUEST_HEADERS</span>)
<span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_REQUEST_HEADERS</span>)
{
<span class="comment">// all headers allowed, return</span>
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_HEADERS</span>, <span class="ident">headers</span>.<span class="ident">clone</span>()));
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_ALLOW_HEADERS</span>, <span class="ident">headers</span>.<span class="ident">clone</span>()));
}
<span class="kw">if</span> <span class="ident">inner</span>.<span class="ident">supports_credentials</span> {
<span class="ident">res</span>.<span class="ident">insert_header</span>((
<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_CREDENTIALS</span>,
<span class="ident">HeaderValue</span>::<span class="ident">from_static</span>(<span class="string">&quot;true&quot;</span>),
<span class="ident">header::ACCESS_CONTROL_ALLOW_CREDENTIALS</span>,
<span class="ident">HeaderValue::from_static</span>(<span class="string">&quot;true&quot;</span>),
));
}
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">max_age</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">max_age</span> {
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_MAX_AGE</span>, <span class="ident">max_age</span>.<span class="ident">to_string</span>()));
<span class="ident">res</span>.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_MAX_AGE</span>, <span class="ident">max_age</span>.<span class="ident">to_string</span>()));
}
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">res</span>.<span class="ident">finish</span>();
@@ -298,33 +298,33 @@
) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">ServiceResponse</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">origin</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">access_control_allow_origin</span>(<span class="ident">res</span>.<span class="ident">request</span>().<span class="ident">head</span>()) {
<span class="ident">res</span>.<span class="ident">headers_mut</span>()
.<span class="ident">insert</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_ORIGIN</span>, <span class="ident">origin</span>);
.<span class="ident">insert</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_ORIGIN</span>, <span class="ident">origin</span>);
};
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="kw-2">ref</span> <span class="ident">expose</span>) <span class="op">=</span> <span class="ident">inner</span>.<span class="ident">expose_headers_baked</span> {
<span class="ident">res</span>.<span class="ident">headers_mut</span>()
.<span class="ident">insert</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_EXPOSE_HEADERS</span>, <span class="ident">expose</span>.<span class="ident">clone</span>());
.<span class="ident">insert</span>(<span class="ident">header::ACCESS_CONTROL_EXPOSE_HEADERS</span>, <span class="ident">expose</span>.<span class="ident">clone</span>());
}
<span class="kw">if</span> <span class="ident">inner</span>.<span class="ident">supports_credentials</span> {
<span class="ident">res</span>.<span class="ident">headers_mut</span>().<span class="ident">insert</span>(
<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_CREDENTIALS</span>,
<span class="ident">HeaderValue</span>::<span class="ident">from_static</span>(<span class="string">&quot;true&quot;</span>),
<span class="ident">header::ACCESS_CONTROL_ALLOW_CREDENTIALS</span>,
<span class="ident">HeaderValue::from_static</span>(<span class="string">&quot;true&quot;</span>),
);
}
<span class="kw">if</span> <span class="ident">inner</span>.<span class="ident">vary_header</span> {
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">res</span>.<span class="ident">headers_mut</span>().<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">VARY</span>) {
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">res</span>.<span class="ident">headers_mut</span>().<span class="ident">get</span>(<span class="ident">header::VARY</span>) {
<span class="prelude-val">Some</span>(<span class="ident">hdr</span>) <span class="op">=</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">val</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">with_capacity</span>(<span class="ident">hdr</span>.<span class="ident">len</span>() <span class="op">+</span> <span class="number">8</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">val</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Vec::with_capacity</span>(<span class="ident">hdr</span>.<span class="ident">len</span>() <span class="op">+</span> <span class="number">8</span>);
<span class="ident">val</span>.<span class="ident">extend</span>(<span class="ident">hdr</span>.<span class="ident">as_bytes</span>());
<span class="ident">val</span>.<span class="ident">extend</span>(<span class="string">b&quot;, Origin&quot;</span>);
<span class="ident">val</span>.<span class="ident">try_into</span>().<span class="ident">unwrap</span>()
}
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="ident">HeaderValue</span>::<span class="ident">from_static</span>(<span class="string">&quot;Origin&quot;</span>),
<span class="prelude-val">None</span> <span class="op">=</span><span class="op">&gt;</span> <span class="ident">HeaderValue::from_static</span>(<span class="string">&quot;Origin&quot;</span>),
};
<span class="ident">res</span>.<span class="ident">headers_mut</span>().<span class="ident">insert</span>(<span class="ident">header</span>::<span class="ident">VARY</span>, <span class="ident">value</span>);
<span class="ident">res</span>.<span class="ident">headers_mut</span>().<span class="ident">insert</span>(<span class="ident">header::VARY</span>, <span class="ident">value</span>);
}
<span class="ident">res</span>
@@ -339,32 +339,32 @@
<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">S</span>, <span class="ident">B</span><span class="op">&gt;</span> <span class="ident">Service</span><span class="op">&lt;</span><span class="ident">ServiceRequest</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">CorsMiddleware</span><span class="op">&lt;</span><span class="ident">S</span><span class="op">&gt;</span>
<span class="kw">where</span>
<span class="ident">S</span>: <span class="ident">Service</span><span class="op">&lt;</span><span class="ident">ServiceRequest</span>, <span class="ident">Response</span> <span class="op">=</span> <span class="ident">ServiceResponse</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>, <span class="ident">Error</span> <span class="op">=</span> <span class="ident">Error</span><span class="op">&gt;</span>,
<span class="ident">S</span>::<span class="ident">Future</span>: <span class="lifetime">&#39;static</span>,
<span class="ident">S::Future</span>: <span class="lifetime">&#39;static</span>,
<span class="ident">B</span>: <span class="lifetime">&#39;static</span>,
{
<span class="kw">type</span> <span class="ident">Response</span> <span class="op">=</span> <span class="ident">ServiceResponse</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>;
<span class="kw">type</span> <span class="ident">Error</span> <span class="op">=</span> <span class="ident">Error</span>;
<span class="kw">type</span> <span class="ident">Future</span> <span class="op">=</span> <span class="ident">CorsMiddlewareServiceFuture</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>;
<span class="ident">actix_service</span>::<span class="macro">forward_ready</span><span class="macro">!</span>(<span class="ident">service</span>);
<span class="macro">actix_service::forward_ready!</span>(<span class="ident">service</span>);
<span class="kw">fn</span> <span class="ident">call</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">req</span>: <span class="ident">ServiceRequest</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span>::<span class="ident">Future</span> {
<span class="kw">if</span> <span class="self">self</span>.<span class="ident">inner</span>.<span class="ident">preflight</span> <span class="op">&amp;&amp;</span> <span class="ident">req</span>.<span class="ident">method</span>() <span class="op">=</span><span class="op">=</span> <span class="ident">Method</span>::<span class="ident">OPTIONS</span> {
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="self">Self</span>::<span class="ident">handle_preflight</span>(<span class="kw-2">&amp;</span><span class="ident">inner</span>, <span class="ident">req</span>);
<span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">ok</span>(<span class="ident">res</span>))
<span class="kw">fn</span> <span class="ident">call</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">req</span>: <span class="ident">ServiceRequest</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="self">Self</span><span class="ident">::Future</span> {
<span class="kw">if</span> <span class="self">self</span>.<span class="ident">inner</span>.<span class="ident">preflight</span> <span class="op">&amp;&amp;</span> <span class="ident">req</span>.<span class="ident">method</span>() <span class="op">=</span><span class="op">=</span> <span class="ident">Method::OPTIONS</span> {
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc::clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="self">Self</span><span class="ident">::handle_preflight</span>(<span class="kw-2">&amp;</span><span class="ident">inner</span>, <span class="ident">req</span>);
<span class="ident">Either::Left</span>(<span class="ident">ok</span>(<span class="ident">res</span>))
} <span class="kw">else</span> {
<span class="kw">let</span> <span class="ident">origin</span> <span class="op">=</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ORIGIN</span>).<span class="ident">cloned</span>();
<span class="kw">let</span> <span class="ident">origin</span> <span class="op">=</span> <span class="ident">req</span>.<span class="ident">headers</span>().<span class="ident">get</span>(<span class="ident">header::ORIGIN</span>).<span class="ident">cloned</span>();
<span class="kw">if</span> <span class="ident">origin</span>.<span class="ident">is_some</span>() {
<span class="comment">// Only check requests with a origin header.</span>
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">err</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">inner</span>.<span class="ident">validate_origin</span>(<span class="ident">req</span>.<span class="ident">head</span>()) {
<span class="macro">debug</span><span class="macro">!</span>(<span class="string">&quot;origin validation failed; inner service is not called&quot;</span>);
<span class="kw">return</span> <span class="ident">Either</span>::<span class="ident">Left</span>(<span class="ident">ok</span>(<span class="ident">req</span>.<span class="ident">error_response</span>(<span class="ident">err</span>)));
<span class="macro">debug!</span>(<span class="string">&quot;origin validation failed; inner service is not called&quot;</span>);
<span class="kw">return</span> <span class="ident">Either::Left</span>(<span class="ident">ok</span>(<span class="ident">req</span>.<span class="ident">error_response</span>(<span class="ident">err</span>)));
}
}
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">let</span> <span class="ident">inner</span> <span class="op">=</span> <span class="ident">Rc::clone</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">inner</span>);
<span class="kw">let</span> <span class="ident">fut</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">service</span>.<span class="ident">call</span>(<span class="ident">req</span>);
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="kw">async</span> <span class="kw">move</span> {
@@ -372,14 +372,14 @@
<span class="kw">if</span> <span class="ident">origin</span>.<span class="ident">is_some</span>() {
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">res</span><span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(<span class="self">Self</span>::<span class="ident">augment_response</span>(<span class="kw-2">&amp;</span><span class="ident">inner</span>, <span class="ident">res</span>))
<span class="prelude-val">Ok</span>(<span class="self">Self</span><span class="ident">::augment_response</span>(<span class="kw-2">&amp;</span><span class="ident">inner</span>, <span class="ident">res</span>))
} <span class="kw">else</span> {
<span class="ident">res</span>
}
}
.<span class="ident">boxed_local</span>();
<span class="ident">Either</span>::<span class="ident">Right</span>(<span class="ident">res</span>)
<span class="ident">Either::Right</span>(<span class="ident">res</span>)
}
}
}
@@ -387,50 +387,50 @@
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
<span class="kw">mod</span> <span class="ident">tests</span> {
<span class="kw">use</span> <span class="ident">actix_web</span>::{
<span class="ident">dev</span>::<span class="ident">Transform</span>,
<span class="ident">dev::Transform</span>,
<span class="ident">test</span>::{<span class="self">self</span>, <span class="ident">TestRequest</span>},
};
<span class="kw">use</span> <span class="kw">super</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="kw">crate</span>::<span class="ident">Cors</span>;
<span class="kw">use</span> <span class="kw">crate</span><span class="ident">::Cors</span>;
<span class="attribute">#[<span class="ident">actix_rt</span>::<span class="ident">test</span>]</span>
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">test_options_no_origin</span>() {
<span class="comment">// Tests case where allowed_origins is All but there are validate functions to run incase.</span>
<span class="comment">// In this case, origins are only allowed when the DNT header is sent.</span>
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors</span>::<span class="ident">default</span>()
<span class="kw">let</span> <span class="ident">cors</span> <span class="op">=</span> <span class="ident">Cors::default</span>()
.<span class="ident">allow_any_origin</span>()
.<span class="ident">allowed_origin_fn</span>(<span class="op">|</span><span class="ident">origin</span>, <span class="ident">req_head</span><span class="op">|</span> {
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="ident">origin</span>, <span class="ident">req_head</span>.<span class="ident">headers</span>.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ORIGIN</span>).<span class="ident">unwrap</span>());
<span class="macro">assert_eq!</span>(<span class="kw-2">&amp;</span><span class="ident">origin</span>, <span class="ident">req_head</span>.<span class="ident">headers</span>.<span class="ident">get</span>(<span class="ident">header::ORIGIN</span>).<span class="ident">unwrap</span>());
<span class="ident">req_head</span>.<span class="ident">headers</span>().<span class="ident">contains_key</span>(<span class="ident">header</span>::<span class="ident">DNT</span>)
<span class="ident">req_head</span>.<span class="ident">headers</span>().<span class="ident">contains_key</span>(<span class="ident">header::DNT</span>)
})
.<span class="ident">new_transform</span>(<span class="ident">test</span>::<span class="ident">ok_service</span>())
.<span class="ident">new_transform</span>(<span class="ident">test::ok_service</span>())
.<span class="kw">await</span>
.<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ORIGIN</span>, <span class="string">&quot;http://example.com&quot;</span>))
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header::ORIGIN</span>, <span class="string">&quot;http://example.com&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">call</span>(<span class="ident">req</span>).<span class="kw">await</span>.<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(
<span class="macro">assert_eq!</span>(
<span class="prelude-val">None</span>,
<span class="ident">res</span>.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue</span>::<span class="ident">as_bytes</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue::as_bytes</span>)
);
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest</span>::<span class="ident">get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">ORIGIN</span>, <span class="string">&quot;http://example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header</span>::<span class="ident">DNT</span>, <span class="string">&quot;1&quot;</span>))
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">TestRequest::get</span>()
.<span class="ident">insert_header</span>((<span class="ident">header::ORIGIN</span>, <span class="string">&quot;http://example.com&quot;</span>))
.<span class="ident">insert_header</span>((<span class="ident">header::DNT</span>, <span class="string">&quot;1&quot;</span>))
.<span class="ident">to_srv_request</span>();
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">cors</span>.<span class="ident">call</span>(<span class="ident">req</span>).<span class="kw">await</span>.<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(
<span class="macro">assert_eq!</span>(
<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;http://example.com&quot;</span>[..]),
<span class="ident">res</span>.<span class="ident">headers</span>()
.<span class="ident">get</span>(<span class="ident">header</span>::<span class="ident">ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue</span>::<span class="ident">as_bytes</span>)
.<span class="ident">get</span>(<span class="ident">header::ACCESS_CONTROL_ALLOW_ORIGIN</span>)
.<span class="ident">map</span>(<span class="ident">HeaderValue::as_bytes</span>)
);
}
}