1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::sync::Arc;
use actix_web::{web, HttpMessage, HttpRequest};
use crate::error::XMLPayloadError;
#[derive(Clone)]
pub struct XmlConfig {
pub(crate) limit: usize,
content_type: Option<Arc<dyn Fn(mime::Mime) -> bool + Send + Sync>>,
}
const DEFAULT_CONFIG: XmlConfig = XmlConfig {
limit: 262_144,
content_type: None,
};
impl Default for XmlConfig {
fn default() -> Self {
DEFAULT_CONFIG.clone()
}
}
impl XmlConfig {
pub fn new() -> Self {
Default::default()
}
pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit;
self
}
pub fn content_type<F>(mut self, predicate: F) -> Self
where
F: Fn(mime::Mime) -> bool + Send + Sync + 'static,
{
self.content_type = Some(Arc::new(predicate));
self
}
pub(crate) fn check_content_type(&self, req: &HttpRequest) -> Result<(), XMLPayloadError> {
if let Ok(Some(mime)) = req.mime_type() {
if mime == "text/xml"
|| mime == "application/xml"
|| self
.content_type
.as_ref()
.map_or(false, |predicate| predicate(mime))
{
Ok(())
} else {
Err(XMLPayloadError::ContentType)
}
} else {
Err(XMLPayloadError::ContentType)
}
}
pub(crate) fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
}