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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use super::*;
use std::fmt;
use actix_web::{FromRequest, HttpRequest};
use json::JsonValue;
use actix_web::http::header::HeaderValue;
pub type LocalError = super::error::Error;
type DUAList = Vec<DUA>;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DUAs{
list: DUAList,
}
impl fmt::Display for DUAs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}
impl DUAs {
pub fn new() -> DUAs {
DUAs {
list: Vec::new(),
}
}
fn value_to_vec(docs: &JsonValue) -> Vec<DUA> {
let mut v = Vec::new();
for d in 0..docs.len() {
v.push(DUA::from_serialized(&docs[d].to_string()));
}
v
}
pub fn duas_from_header_value(header_value: &HeaderValue) -> DUAs{
match header_value.to_str() {
Ok(list) => {
let docs = match json::parse(list) {
Ok(valid) => valid,
Err(_e) => {
warn!("{}", LocalError::BadDUAFormat);
return DUAs::new()
},
};
match docs.is_array() {
true => {
DUAs{
list: DUAs::value_to_vec(&docs),
}
},
false => {
warn!("{}", LocalError::BadDUAFormat);
return DUAs::new()
},
}
},
Err(_e) => {
warn!("{}", LocalError::BadDUAFormat);
return DUAs::new()
},
}
}
pub fn from_request(req: &HttpRequest) -> DUAs{
match req.headers().get(DUA_HEADER) {
Some(u) => {
return DUAs::duas_from_header_value(u)
},
None => {
warn!("{}", LocalError::MissingDUA);
return DUAs::new()
},
};
}
#[allow(dead_code)]
pub fn vec(&self) -> Vec<DUA> {
self.list.clone()
}
}
impl FromRequest for DUAs {
type Config = ();
type Future = Result<Self, Self::Error>;
type Error = LocalError;
fn from_request(req: &HttpRequest, _payload: &mut actix_web::dev::Payload) -> Self::Future {
Ok(DUAs::from_request(req))
}
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, web, http, App, HttpRequest, HttpResponse};
use actix_web::dev::Service;
use actix_web::http::{StatusCode};
fn index_extract_dua(duas: DUAs, _req: HttpRequest) -> HttpResponse {
if duas.vec().len() > 0 {
return HttpResponse::Ok()
.header(http::header::CONTENT_TYPE, "application/json")
.body(format!("{}", duas))
} else {
return HttpResponse::BadRequest()
.header(http::header::CONTENT_TYPE, "application/json")
.body(format!("{}", LocalError::BadDUA))
}
}
#[test]
fn test_http_header_name() {
assert_eq!(DUA_HEADER, "Data-Usage-Agreement");
}
#[test]
fn test_dua_extractor_good() {
let mut app = test::init_service(App::new().route("/", web::get().to(index_extract_dua)));
let req = test::TestRequest::get().uri("/")
.header("content-type", "application/json")
.header(DUA_HEADER, r#"[{"agreement_name":"billing","location":"www.dua.org/billing.pdf","agreed_dtm": 1553988607}]"#)
.to_request();
let resp = test::block_on(app.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_dua_extractor_missing() {
let mut app = test::init_service(App::new().route("/", web::get().to(index_extract_dua)));
let req = test::TestRequest::get().uri("/")
.header("content-type", "application/json")
.to_request();
let resp = test::call_service(&mut app, req);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
let bdy = test::read_body(resp);
assert_eq!(&bdy[..], actix_web::web::Bytes::from_static(b"Malformed or missing one or more Data Usage Agreements"));
}
}