mirror of
https://github.com/actix/actix-extras.git
synced 2025-08-31 19:37:00 +02:00
Deploying to gh-pages from @ 695800c9bd
🚀
This commit is contained in:
@@ -55,10 +55,16 @@
|
||||
<span id="48">48</span>
|
||||
<span id="49">49</span>
|
||||
<span id="50">50</span>
|
||||
<span id="51">51</span>
|
||||
<span id="52">52</span>
|
||||
<span id="53">53</span>
|
||||
<span id="54">54</span>
|
||||
</pre><pre class="rust"><code><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>};
|
||||
|
||||
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::inner::add_vary_header</span>;
|
||||
|
||||
<span class="doccomment">/// Errors that can occur when processing CORS guarded requests.</span>
|
||||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">Clone</span>, <span class="ident">Display</span>, <span class="ident">Error</span>)]</span>
|
||||
<span class="attribute">#[<span class="ident">non_exhaustive</span>]</span>
|
||||
@@ -102,7 +108,9 @@
|
||||
}
|
||||
|
||||
<span class="kw">fn</span> <span class="ident">error_response</span>(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="ident">HttpResponse</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">map_into_boxed_body</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::with_body</span>(<span class="self">self</span>.<span class="ident">status_code</span>(), <span class="self">self</span>.<span class="ident">to_string</span>());
|
||||
<span class="ident">add_vary_header</span>(<span class="ident">res</span>.<span class="ident">headers_mut</span>());
|
||||
<span class="ident">res</span>.<span class="ident">map_into_boxed_body</span>()
|
||||
}
|
||||
}
|
||||
</code></pre></div>
|
||||
|
@@ -335,13 +335,65 @@
|
||||
<span id="328">328</span>
|
||||
<span id="329">329</span>
|
||||
<span id="330">330</span>
|
||||
<span id="331">331</span>
|
||||
<span id="332">332</span>
|
||||
<span id="333">333</span>
|
||||
<span id="334">334</span>
|
||||
<span id="335">335</span>
|
||||
<span id="336">336</span>
|
||||
<span id="337">337</span>
|
||||
<span id="338">338</span>
|
||||
<span id="339">339</span>
|
||||
<span id="340">340</span>
|
||||
<span id="341">341</span>
|
||||
<span id="342">342</span>
|
||||
<span id="343">343</span>
|
||||
<span id="344">344</span>
|
||||
<span id="345">345</span>
|
||||
<span id="346">346</span>
|
||||
<span id="347">347</span>
|
||||
<span id="348">348</span>
|
||||
<span id="349">349</span>
|
||||
<span id="350">350</span>
|
||||
<span id="351">351</span>
|
||||
<span id="352">352</span>
|
||||
<span id="353">353</span>
|
||||
<span id="354">354</span>
|
||||
<span id="355">355</span>
|
||||
<span id="356">356</span>
|
||||
<span id="357">357</span>
|
||||
<span id="358">358</span>
|
||||
<span id="359">359</span>
|
||||
<span id="360">360</span>
|
||||
<span id="361">361</span>
|
||||
<span id="362">362</span>
|
||||
<span id="363">363</span>
|
||||
<span id="364">364</span>
|
||||
<span id="365">365</span>
|
||||
<span id="366">366</span>
|
||||
<span id="367">367</span>
|
||||
<span id="368">368</span>
|
||||
<span id="369">369</span>
|
||||
<span id="370">370</span>
|
||||
<span id="371">371</span>
|
||||
<span id="372">372</span>
|
||||
<span id="373">373</span>
|
||||
<span id="374">374</span>
|
||||
<span id="375">375</span>
|
||||
<span id="376">376</span>
|
||||
<span id="377">377</span>
|
||||
<span id="378">378</span>
|
||||
<span id="379">379</span>
|
||||
<span id="380">380</span>
|
||||
<span id="381">381</span>
|
||||
<span id="382">382</span>
|
||||
</pre><pre class="rust"><code><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::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">header</span>::{<span class="self">self</span>, <span class="ident">HeaderMap</span>, <span class="ident">HeaderName</span>, <span class="ident">HeaderValue</span>},
|
||||
<span class="ident">Method</span>,
|
||||
},
|
||||
};
|
||||
@@ -536,6 +588,25 @@
|
||||
}
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Add CORS related request headers to response's Vary header.</span>
|
||||
<span class="doccomment">///</span>
|
||||
<span class="doccomment">/// See <https://fetch.spec.whatwg.org/#cors-protocol-and-http-caches>.</span>
|
||||
<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn</span> <span class="ident">add_vary_header</span>(<span class="ident">headers</span>: <span class="kw-2">&mut</span> <span class="ident">HeaderMap</span>) {
|
||||
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">headers</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="kw">let</span> <span class="kw-2">mut</span> <span class="ident">val</span>: <span class="ident">Vec</span><span class="op"><</span><span class="ident">u8</span><span class="op">></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">71</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", Origin, Access-Control-Request-Method, Access-Control-Request-Headers"</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="ident">HeaderValue::from_static</span>(
|
||||
<span class="string">"Origin, Access-Control-Request-Method, Access-Control-Request-Headers"</span>,
|
||||
),
|
||||
};
|
||||
|
||||
<span class="ident">headers</span>.<span class="ident">insert</span>(<span class="ident">header::VARY</span>, <span class="ident">value</span>);
|
||||
}
|
||||
|
||||
<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::rc::Rc</span>;
|
||||
@@ -664,6 +735,39 @@
|
||||
<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">&</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>);
|
||||
}
|
||||
|
||||
<span class="attribute">#[<span class="ident">actix_web::test</span>]</span>
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">allow_fn_origin_equals_head_origin</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_fn</span>(<span class="op">|</span><span class="ident">origin</span>, <span class="ident">head</span><span class="op">|</span> {
|
||||
<span class="kw">let</span> <span class="ident">head_origin</span> <span class="op">=</span> <span class="ident">head</span>
|
||||
.<span class="ident">headers</span>()
|
||||
.<span class="ident">get</span>(<span class="ident">header::ORIGIN</span>)
|
||||
.<span class="ident">expect</span>(<span class="string">"unwrapping origin header should never fail in allowed_origin_fn"</span>);
|
||||
<span class="macro">assert!</span>(<span class="ident">origin</span> <span class="op">==</span> <span class="ident">head_origin</span>);
|
||||
<span class="bool-val">true</span>
|
||||
})
|
||||
.<span class="ident">allow_any_method</span>()
|
||||
.<span class="ident">allow_any_header</span>()
|
||||
.<span class="ident">new_transform</span>(<span class="ident">test::simple_service</span>(<span class="ident">StatusCode::NO_CONTENT</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::default</span>()
|
||||
.<span class="ident">method</span>(<span class="ident">Method::OPTIONS</span>)
|
||||
.<span class="ident">insert_header</span>((<span class="string">"Origin"</span>, <span class="string">"https://www.example.com"</span>))
|
||||
.<span class="ident">insert_header</span>((<span class="ident">header::ACCESS_CONTROL_REQUEST_METHOD</span>, <span class="string">"POST"</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::call_service</span>(<span class="kw-2">&</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>);
|
||||
|
||||
<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::GET</span>)
|
||||
.<span class="ident">insert_header</span>((<span class="string">"Origin"</span>, <span class="string">"https://www.example.com"</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::call_service</span>(<span class="kw-2">&</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::NO_CONTENT</span>);
|
||||
}
|
||||
}
|
||||
</code></pre></div>
|
||||
</section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_cors" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.60.0-nightly (f624427f8 2022-02-06)" ></div>
|
||||
|
@@ -231,24 +231,7 @@
|
||||
<span id="224">224</span>
|
||||
<span id="225">225</span>
|
||||
<span id="226">226</span>
|
||||
<span id="227">227</span>
|
||||
<span id="228">228</span>
|
||||
<span id="229">229</span>
|
||||
<span id="230">230</span>
|
||||
<span id="231">231</span>
|
||||
<span id="232">232</span>
|
||||
<span id="233">233</span>
|
||||
<span id="234">234</span>
|
||||
<span id="235">235</span>
|
||||
<span id="236">236</span>
|
||||
<span id="237">237</span>
|
||||
<span id="238">238</span>
|
||||
<span id="239">239</span>
|
||||
<span id="240">240</span>
|
||||
<span id="241">241</span>
|
||||
<span id="242">242</span>
|
||||
<span id="243">243</span>
|
||||
</pre><pre class="rust"><code><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">rc::Rc</span>};
|
||||
</pre><pre class="rust"><code><span class="kw">use</span> <span class="ident">std</span>::{<span class="ident">collections::HashSet</span>, <span class="ident">rc::Rc</span>};
|
||||
|
||||
<span class="kw">use</span> <span class="ident">actix_utils::future::ok</span>;
|
||||
<span class="kw">use</span> <span class="ident">actix_web</span>::{
|
||||
@@ -263,7 +246,7 @@
|
||||
<span class="kw">use</span> <span class="ident">futures_util::future</span>::{<span class="ident">FutureExt</span> <span class="kw">as</span> <span class="kw">_</span>, <span class="ident">LocalBoxFuture</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">builder::intersperse_header_values</span>, <span class="ident">AllOrSome</span>, <span class="ident">Inner</span>};
|
||||
<span class="kw">use</span> <span class="kw">crate</span>::{<span class="ident">builder::intersperse_header_values</span>, <span class="ident">inner::add_vary_header</span>, <span class="ident">AllOrSome</span>, <span class="ident">Inner</span>};
|
||||
|
||||
<span class="doccomment">/// Service wrapper for Cross-Origin Resource Sharing support.</span>
|
||||
<span class="doccomment">///</span>
|
||||
@@ -317,7 +300,9 @@
|
||||
<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>();
|
||||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">res</span>.<span class="ident">finish</span>();
|
||||
<span class="ident">add_vary_header</span>(<span class="ident">res</span>.<span class="ident">headers_mut</span>());
|
||||
|
||||
<span class="ident">req</span>.<span class="ident">into_response</span>(<span class="ident">res</span>)
|
||||
}
|
||||
|
||||
@@ -366,21 +351,7 @@
|
||||
}
|
||||
|
||||
<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::VARY</span>) {
|
||||
<span class="prelude-val">Some</span>(<span class="ident">hdr</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"><</span><span class="ident">u8</span><span class="op">></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">71</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", Origin, Access-Control-Request-Method, Access-Control-Request-Headers"</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="ident">HeaderValue::from_static</span>(
|
||||
<span class="string">"Origin, Access-Control-Request-Method, Access-Control-Request-Headers"</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">add_vary_header</span>(<span class="ident">res</span>.<span class="ident">headers_mut</span>());
|
||||
}
|
||||
|
||||
<span class="ident">res</span>
|
||||
@@ -422,12 +393,7 @@
|
||||
<span class="kw">async</span> <span class="kw">move</span> {
|
||||
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">fut</span>.<span class="kw">await</span>;
|
||||
|
||||
<span class="kw">if</span> <span class="ident">origin</span>.<span class="ident">is_some</span>() {
|
||||
<span class="prelude-val">Ok</span>(<span class="ident"><span class="self">Self</span>::augment_response</span>(<span class="kw-2">&</span><span class="ident">inner</span>, <span class="ident">res</span><span class="question-mark">?</span>))
|
||||
} <span class="kw">else</span> {
|
||||
<span class="ident">res</span>.<span class="ident">map_err</span>(<span class="ident">Into::into</span>)
|
||||
}
|
||||
.<span class="ident">map</span>(<span class="op">|</span><span class="ident">res</span><span class="op">|</span> <span class="ident">res</span>.<span class="ident">map_into_left_body</span>())
|
||||
<span class="prelude-val">Ok</span>(<span class="ident"><span class="self">Self</span>::augment_response</span>(<span class="kw-2">&</span><span class="ident">inner</span>, <span class="ident">res</span><span class="question-mark">?</span>).<span class="ident">map_into_left_body</span>())
|
||||
}
|
||||
.<span class="ident">boxed_local</span>()
|
||||
}
|
||||
|
Reference in New Issue
Block a user