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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Builder for immutable HTTP redaction policy snapshots.
use super::HttpFieldContext;
use super::TextBodyPolicy;
use super::UrlPathPolicy;
use super::context_rules_builder::ContextRulesBuilder;
use super::internal::HttpPolicyParts;
use crate::PolicyError;
use crate::PolicyLocation;
use crate::RedactionFloor;
use crate::RedactionRules;
use crate::Sensitivity;
/// Mutable construction state for an [`super::HttpPolicy`].
#[derive(Debug, Clone)]
pub struct HttpPolicyBuilder {
/// Mutable header classification state.
header: ContextRulesBuilder,
/// Mutable query and form classification state.
query: ContextRulesBuilder,
/// Mutable structured-body classification state.
body: ContextRulesBuilder,
/// Selected URL path visibility rule.
url_path_policy: UrlPathPolicy,
/// Selected opaque text-body visibility rule.
text_body_policy: TextBodyPolicy,
}
impl HttpPolicyBuilder {
/// Creates empty context overrides with standard HTTP rendering choices.
///
/// The enclosing redaction policy supplies the base safety floor.
///
/// # Returns
///
/// Empty context overrides with the standard URL-path and text-body
/// policies.
#[must_use]
#[inline]
pub fn new() -> Self {
Self {
header: ContextRulesBuilder::empty(PolicyLocation::HttpHeader),
query: ContextRulesBuilder::empty(PolicyLocation::HttpQuery),
body: ContextRulesBuilder::empty(PolicyLocation::HttpBody),
url_path_policy: UrlPathPolicy::default(),
text_body_policy: TextBodyPolicy::default(),
}
}
/// Copies the immutable HTTP context snapshot.
///
/// # Parameters
///
/// - `policy`: Immutable HTTP snapshot to copy.
///
/// # Returns
///
/// An independent mutable builder preserving all HTTP policy choices.
#[must_use]
#[inline]
pub(crate) fn from_policy(policy: &super::HttpPolicy) -> Self {
Self {
header: ContextRulesBuilder::from_rules(policy.header_rules(), PolicyLocation::HttpHeader),
query: ContextRulesBuilder::from_rules(policy.query_rules(), PolicyLocation::HttpQuery),
body: ContextRulesBuilder::from_rules(policy.body_rules(), PolicyLocation::HttpBody),
url_path_policy: policy.url_path_policy(),
text_body_policy: policy.text_body_policy(),
}
}
/// Replaces the rules for one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context whose rules are replaced.
/// * `rules` - Application rules to copy into the builder.
#[inline(always)]
pub(crate) fn rules_mut(&mut self, context: HttpFieldContext, rules: RedactionRules) {
*self.context_mut(context) = ContextRulesBuilder::from_rules(&rules, context.location());
}
/// Replaces the minimum sensitivity floor for one HTTP context.
///
/// # Parameters
///
/// * `context` - HTTP field context whose floor is changed.
/// * `floor` - Minimum sensitivity required for that context.
#[inline(always)]
pub(crate) fn floor_mut(&mut self, context: HttpFieldContext, floor: RedactionFloor) {
self.context_mut(context).with_floor(floor);
}
/// Disables the minimum floor for one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context whose floor is disabled.
#[inline(always)]
pub(crate) fn disable_floor_mut(&mut self, context: HttpFieldContext) {
self.context_mut(context).disable_floor();
}
/// Applies one minimum sensitivity floor to every HTTP context.
///
/// # Parameters
///
/// * `floor` - Minimum sensitivity copied into each context.
#[inline]
pub(crate) fn floor_all_mut(&mut self, floor: RedactionFloor) {
self.header.with_floor(floor.clone());
self.query.with_floor(floor.clone());
self.body.with_floor(floor);
}
/// Disables the minimum floor for every HTTP context.
#[inline]
pub(crate) fn disable_all_floors_mut(&mut self) {
self.header.disable_floor();
self.query.disable_floor();
self.body.disable_floor();
}
/// Raises one HTTP context field to at least `level`.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field name to canonicalize and update.
/// * `level` - Minimum sensitivity to apply.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn raise_mut(
&mut self,
context: HttpFieldContext,
name: &str,
level: Sensitivity,
) -> Result<(), PolicyError> {
self.context_mut(context).rules.raise(name, level)
}
/// Sets one HTTP context field to exactly `level`.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field name to canonicalize and update.
/// * `level` - Sensitivity to apply.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn override_level_mut(
&mut self,
context: HttpFieldContext,
name: &str,
level: Sensitivity,
) -> Result<(), PolicyError> {
self.context_mut(context).rules.override_level(name, level)
}
/// Adds an exact allow rule to one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field name to allow after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn allow_exact_mut(&mut self, context: HttpFieldContext, name: &str) -> Result<(), PolicyError> {
self.context_mut(context).rules.allow_canonical_exact(name)
}
/// Adds a token-suffix allow rule to one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field-name suffix to allow after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn allow_suffix_mut(&mut self, context: HttpFieldContext, name: &str) -> Result<(), PolicyError> {
self.context_mut(context).rules.allow_suffix(name)
}
/// Removes an exact allow rule from one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field name to remove after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn remove_allow_exact_mut(&mut self, context: HttpFieldContext, name: &str) -> Result<(), PolicyError> {
self.context_mut(context).rules.remove_allow_canonical_exact(name)
}
/// Removes a token-suffix allow rule from one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context to update.
/// * `name` - Field-name suffix to remove after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `name` has no canonical
/// field name.
///
/// # Returns
///
/// Success after updating the requested rule; an invalid name leaves that
/// update unapplied.
#[inline(always)]
pub(crate) fn remove_allow_suffix_mut(&mut self, context: HttpFieldContext, name: &str) -> Result<(), PolicyError> {
self.context_mut(context).rules.remove_allow_suffix(name)
}
/// Removes all allow rules from one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context to clear.
#[inline(always)]
pub(crate) fn clear_allow_rules_mut(&mut self, context: HttpFieldContext) {
self.context_mut(context).rules.clear_allow_rules();
}
/// Sets the URL path handling policy.
///
/// # Parameters
///
/// * `policy` - Policy controlling URL path redaction.
#[inline(always)]
pub(crate) fn url_path_mut(&mut self, policy: UrlPathPolicy) {
self.url_path_policy = policy;
}
/// Sets the text-body handling policy.
///
/// # Parameters
///
/// * `policy` - Policy controlling text-body redaction.
#[inline(always)]
pub(crate) fn text_body_mut(&mut self, policy: TextBodyPolicy) {
self.text_body_policy = policy;
}
/// Builds the complete HTTP policy from the validated context state.
///
/// # Returns
///
/// The immutable HTTP policy assembled from each context and rendering
/// option.
///
/// # Errors
///
/// Currently always succeeds because rule setters validate their inputs.
#[inline]
pub(crate) fn build(self) -> Result<super::HttpPolicy, PolicyError> {
Ok(super::HttpPolicy::from_parts(HttpPolicyParts {
header_rules: self.header.build()?,
query_rules: self.query.build()?,
body_rules: self.body.build()?,
url_path_policy: self.url_path_policy,
text_body_policy: self.text_body_policy,
}))
}
/// Returns mutable rules for one HTTP field context.
///
/// # Parameters
///
/// * `context` - HTTP field context whose rules are selected.
///
/// # Returns
///
/// The mutable rules builder for `context`.
#[must_use]
#[inline(always)]
fn context_mut(&mut self, context: HttpFieldContext) -> &mut ContextRulesBuilder {
match context {
HttpFieldContext::Header => &mut self.header,
HttpFieldContext::Query => &mut self.query,
HttpFieldContext::Body => &mut self.body,
}
}
}
impl Default for HttpPolicyBuilder {
/// Creates a builder with the standard HTTP handling defaults.
///
/// # Returns
///
/// A builder with empty context overrides and standard HTTP rendering
/// choices.
#[inline]
fn default() -> Self {
Self::new()
}
}