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
use PredicateResult;
use Body as HttpBody;
use ;
use crateBufferedBody;
/// Matching operations for HTTP body content.
///
/// # Caveats
///
/// Body predicates may consume bytes from the stream. The body is transitioned
/// to [`BufferedBody::Partial`] or [`BufferedBody::Complete`] after evaluation.
///
/// # Examples
///
/// ## Size Limit
///
/// Only cache responses smaller than 1MB:
///
/// ```
/// use hitbox_http::predicates::body::Operation;
///
/// let op = Operation::Limit { bytes: 1024 * 1024 };
/// ```
///
/// ## Plain Text Matching
///
/// Cache only if body contains a success marker:
///
/// ```
/// use bytes::Bytes;
/// use hitbox_http::predicates::body::{Operation, PlainOperation};
///
/// let op = Operation::Plain(PlainOperation::Contains(Bytes::from("\"success\":true")));
/// ```
///
/// Cache only if body starts with JSON array:
///
/// ```
/// use bytes::Bytes;
/// use hitbox_http::predicates::body::{Operation, PlainOperation};
///
/// let op = Operation::Plain(PlainOperation::Starts(Bytes::from("[")));
/// ```
///
/// Cache only if body matches a regex pattern:
///
/// ```
/// use hitbox_http::predicates::body::{Operation, PlainOperation};
///
/// let regex = regex::bytes::Regex::new(r#""status":\s*"(ok|success)""#).unwrap();
/// let op = Operation::Plain(PlainOperation::RegExp(regex));
/// ```
///
/// ## JQ (JSON) Matching
///
/// Cache only if response has non-empty items array:
///
/// ```
/// use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
///
/// let op = Operation::Jq {
/// filter: JqExpression::compile(".items | length > 0").unwrap(),
/// operation: JqOperation::Eq(serde_json::Value::Bool(true)),
/// };
/// ```
///
/// Cache only if user role exists:
///
/// ```
/// use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
///
/// let op = Operation::Jq {
/// filter: JqExpression::compile(".user.role").unwrap(),
/// operation: JqOperation::Exist,
/// };
/// ```
///
/// Cache only if status is one of allowed values:
///
/// ```
/// use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};
///
/// let op = Operation::Jq {
/// filter: JqExpression::compile(".status").unwrap(),
/// operation: JqOperation::In(vec![
/// serde_json::json!("published"),
/// serde_json::json!("approved"),
/// ]),
/// };
/// ```