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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::fmt::{Display, Formatter};
use std::str::from_utf8;
use base64::encode;
use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tracing::warn;
use crate::content_types::{ContentType, ContentTypeHint};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(untagged)]
pub enum OptionalBody {
Missing,
Empty,
Null,
Present(Bytes, Option<ContentType>, Option<ContentTypeHint>)
}
impl OptionalBody {
pub fn is_present(&self) -> bool {
matches!(*self, OptionalBody::Present(_, _, _))
}
pub fn value(&self) -> Option<Bytes> {
match self {
OptionalBody::Present(s, _, _) => Some(s.clone()),
_ => None
}
}
pub fn value_as_string(&self) -> Option<String> {
match self {
OptionalBody::Present(s, ct, hint) => {
if Self::is_text(ct, hint) {
from_utf8(s)
.map(|s| s.to_string())
.ok()
} else {
None
}
},
_ => None
}
}
fn is_text(ct: &Option<ContentType>, hint: &Option<ContentTypeHint>) -> bool {
if let Some(hint) = hint {
*hint == ContentTypeHint::TEXT
} else if let Some(ct) = ct {
ct.is_text()
} else {
false
}
}
#[deprecated(since = "0.4.2", note = "This does not deal with binary bodies, use value_as_str or display_string instead")]
pub fn str_value(&self) -> &str {
match self {
OptionalBody::Present(s, _, _) => from_utf8(s).unwrap_or(""),
_ => ""
}
}
pub fn display_string(&self) -> String {
match self {
OptionalBody::Present(s, ct, hint) => {
if Self::is_text(ct, hint) {
from_utf8(s)
.map(|s| s.to_string())
.unwrap_or_else(|_| self.display_bytes(32))
} else {
self.display_bytes(32)
}
},
_ => String::default()
}
}
pub fn has_content_type(&self) -> bool {
match self {
OptionalBody::Present(_, content_type, _) => content_type.is_some(),
_ => false
}
}
pub fn content_type(&self) -> Option<ContentType> {
match self {
OptionalBody::Present(_, content_type, _) =>
content_type.clone(),
_ => None
}
}
pub fn to_v4_json(&self) -> Value {
match self {
OptionalBody::Present(bytes, content_type, ct_override) => {
let content_type = content_type.as_ref().cloned().unwrap_or_default();
let content_type_override = ct_override.unwrap_or_default();
let (contents, encoded) = if content_type.is_json() {
match serde_json::from_slice(bytes) {
Ok(json_body) => (json_body, Value::Bool(false)),
Err(err) => {
warn!("Failed to parse json body: {}", err);
(Value::String(encode(bytes)), Value::String("base64".to_string()))
}
}
} else if content_type_override == ContentTypeHint::BINARY || content_type.is_binary() {
(Value::String(encode(bytes)), Value::String("base64".to_string()))
} else {
match from_utf8(bytes) {
Ok(s) => (Value::String(s.to_string()), Value::Bool(false)),
Err(_) => (Value::String(encode(bytes)), Value::String("base64".to_string()))
}
};
if let Some(ct_override) = ct_override {
json!({
"content": contents,
"contentType": content_type.to_string(),
"contentTypeHint": ct_override.to_string(),
"encoded": encoded
})
} else {
json!({
"content": contents,
"contentType": content_type.to_string(),
"encoded": encoded
})
}
},
OptionalBody::Empty => json!({"content": ""}),
_ => Value::Null
}
}
pub fn set_content_type(&mut self, content_type: &ContentType) {
if let OptionalBody::Present(_, ct, _) = self {
*ct = Some(content_type.clone());
}
}
pub(crate) fn display_bytes(&self, max_bytes: usize) -> String {
if let OptionalBody::Present(bytes, _, _) = self {
if bytes.len() <= max_bytes {
let b_str: String = bytes.iter().map(|b| format!("{:0X}", b)).collect();
format!("{} ({} bytes)", b_str, bytes.len())
} else {
let b_str: String = bytes
.slice(0..max_bytes)
.iter()
.map(|b| format!("{:0X}", b))
.collect();
format!("{}... ({} bytes)", b_str, bytes.len())
}
} else {
String::default()
}
}
}
impl From<String> for OptionalBody {
fn from(s: String) -> Self {
if s.is_empty() {
OptionalBody::Empty
} else {
OptionalBody::Present(Bytes::from(s), None, None)
}
}
}
impl <'a> From<&'a str> for OptionalBody {
fn from(s: &'a str) -> Self {
if s.is_empty() {
OptionalBody::Empty
} else {
let mut buf = BytesMut::with_capacity(0);
buf.extend_from_slice(s.as_bytes());
OptionalBody::Present(buf.freeze(), None, None)
}
}
}
impl From<Value> for OptionalBody {
fn from(json: Value) -> Self {
OptionalBody::from(&json)
}
}
impl From<&Value> for OptionalBody {
fn from(json: &Value) -> Self {
OptionalBody::Present(Bytes::from(json.to_string()),
Some(ContentType::from("application/json;charset=UTF-8")),
None)
}
}
impl Display for OptionalBody {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match *self {
OptionalBody::Missing => write!(f, "Missing"),
OptionalBody::Empty => write!(f, "Empty"),
OptionalBody::Null => write!(f, "Null"),
OptionalBody::Present(ref s, ref content_type, _) => {
if let Some(content_type) = content_type {
write!(f, "Present({} bytes, {})", s.len(), content_type)
} else {
write!(f, "Present({} bytes)", s.len())
}
}
}
}
}
impl Default for OptionalBody {
fn default() -> Self {
OptionalBody::Missing
}
}
#[cfg(test)]
mod tests {
use expectest::prelude::*;
use crate::content_types::{ContentTypeHint, JSON, TEXT};
use super::OptionalBody;
#[test]
fn display_tests() {
expect!(format!("{}", OptionalBody::Missing)).to(be_equal_to("Missing"));
expect!(format!("{}", OptionalBody::Empty)).to(be_equal_to("Empty"));
expect!(format!("{}", OptionalBody::Null)).to(be_equal_to("Null"));
expect!(format!("{}", OptionalBody::Present("hello".into(), None, None))).to(be_equal_to("Present(5 bytes)"));
expect!(format!("{}", OptionalBody::Present("\"hello\"".into(), Some(JSON.clone()), None))).to(be_equal_to("Present(7 bytes, application/json)"));
}
#[test]
fn display_bytes_test() {
expect!(OptionalBody::Missing.display_bytes(8)).to(be_equal_to(""));
expect!(OptionalBody::Empty.display_bytes(8)).to(be_equal_to(""));
expect!(OptionalBody::Null.display_bytes(8)).to(be_equal_to(""));
expect!(OptionalBody::Present("hello".into(), None, None).display_bytes(8)).to(be_equal_to("68656C6C6F (5 bytes)"));
expect!(OptionalBody::Present("12345678".into(), None, None).display_bytes(8)).to(be_equal_to("3132333435363738 (8 bytes)"));
expect!(OptionalBody::Present("123456789012345".into(), None, None).display_bytes(8)).to(be_equal_to("3132333435363738... (15 bytes)"));
}
#[test]
fn display_string_test() {
expect!(OptionalBody::Missing.display_string()).to(be_equal_to(""));
expect!(OptionalBody::Empty.display_string()).to(be_equal_to(""));
expect!(OptionalBody::Null.display_string()).to(be_equal_to(""));
expect!(OptionalBody::Present("hello".into(), None, None).display_string()).to(be_equal_to("68656C6C6F (5 bytes)"));
expect!(OptionalBody::Present("12345678".into(), None, None).display_string()).to(be_equal_to("3132333435363738 (8 bytes)"));
expect!(OptionalBody::Present("123456789012345".into(), None, None).display_string()).to(be_equal_to("313233343536373839303132333435 (15 bytes)"));
expect!(OptionalBody::Present("hello".into(), Some(TEXT.clone()), None).display_string()).to(be_equal_to("hello"));
expect!(OptionalBody::Present("hello".into(), None, Some(ContentTypeHint::TEXT)).display_string()).to(be_equal_to("hello"));
}
#[test]
fn value_as_string_test() {
expect!(OptionalBody::Missing.value_as_string()).to(be_none());
expect!(OptionalBody::Empty.value_as_string()).to(be_none());
expect!(OptionalBody::Null.value_as_string()).to(be_none());
expect!(OptionalBody::Present("hello".into(), None, None).value_as_string()).to(be_none());
expect!(OptionalBody::Present("hello".into(), Some(TEXT.clone()), None).value_as_string()).to(be_some().value("hello"));
expect!(OptionalBody::Present("hello".into(), None, Some(ContentTypeHint::TEXT)).value_as_string()).to(be_some().value("hello"));
expect!(OptionalBody::Present("hello".into(), None, Some(ContentTypeHint::BINARY)).value_as_string()).to(be_none());
}
}