scufflecloud_ext/http_ext.rs
1use std::sync::Arc;
2
3use crate::OptionExt;
4
5pub trait RequestExt {
6 fn extensions(&self) -> &http::Extensions;
7
8 fn global<G: Send + Sync + 'static>(&self) -> Result<Arc<G>, tonic::Status> {
9 self.extensions()
10 .get::<Arc<G>>()
11 .map(Arc::clone)
12 .into_tonic_internal_err("missing global extension")
13 }
14
15 fn origin(&self) -> Option<url::Url>;
16}
17
18impl<T> RequestExt for tonic::Request<T> {
19 fn extensions(&self) -> &http::Extensions {
20 self.extensions()
21 }
22
23 fn origin(&self) -> Option<url::Url> {
24 self.metadata().get("origin")?.to_str().ok()?.parse().ok()
25 }
26}
27
28impl RequestExt for tonic::Extensions {
29 fn extensions(&self) -> &http::Extensions {
30 self
31 }
32
33 fn origin(&self) -> Option<url::Url> {
34 None
35 }
36}
37
38impl<T> RequestExt for http::Request<T> {
39 fn extensions(&self) -> &http::Extensions {
40 self.extensions()
41 }
42
43 fn origin(&self) -> Option<url::Url> {
44 self.headers().get("origin")?.to_str().ok()?.parse().ok()
45 }
46}