qubit_redact/facade/redactor/http.rs
1// =============================================================================
2// Copyright (c) 2025 - 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! HTTP URL, header, and body redaction operations.
9
10use http::HeaderMap;
11use http::HeaderValue;
12
13use super::Redactor;
14use crate::RedactionInspection;
15use crate::RedactionInspectionError;
16use crate::RedactionTextOutput;
17use crate::formats::http::BodyCapture;
18use crate::formats::http::inspection;
19
20impl Redactor {
21 /// Redacts an HTTP URL through one completed text transaction.
22 ///
23 /// # Parameters
24 ///
25 /// - `value`: Raw HTTP URL, including its authority, path, query, and
26 /// fragment.
27 ///
28 /// # Returns
29 ///
30 /// Final redacted text and its execution summary, including any truncation
31 /// or admission failure recorded while processing the value.
32 #[must_use]
33 #[inline]
34 pub fn redact_http_url(&self, value: &str) -> RedactionTextOutput {
35 let mut session = self.text_runtime();
36 session.http(|http| {
37 let _ = http.url(value);
38 });
39 session.finish()
40 }
41
42 /// Inspects one HTTP URL without rendering it.
43 ///
44 /// # Errors
45 ///
46 /// Returns [`RedactionInspectionError`] when the URL is invalid or a
47 /// shared resource limit prevents complete inspection.
48 ///
49 /// # Parameters
50 ///
51 /// - `value`: Raw HTTP URL, including its authority, path, query, and
52 /// fragment.
53 ///
54 /// # Returns
55 ///
56 /// A conclusive sensitivity inspection, or a value-free error containing
57 /// resource usage and reasons why complete classification was unavailable.
58 #[inline]
59 pub fn inspect_http_url(&self, value: &str) -> Result<RedactionInspection, RedactionInspectionError> {
60 let mut session = self.inspection_runtime();
61 inspection::inspect_url(&mut session, value);
62 session.finish()
63 }
64
65 /// Redacts an HTTP header collection through one completed transaction.
66 ///
67 /// # Parameters
68 ///
69 /// - `headers`: Borrowed header collection, including repeated values.
70 ///
71 /// # Returns
72 ///
73 /// Final redacted text and its execution summary, including any truncation
74 /// or admission failure recorded while processing the value.
75 #[must_use]
76 #[inline]
77 pub fn redact_http_headers(&self, headers: &HeaderMap) -> RedactionTextOutput {
78 let mut session = self.text_runtime();
79 session.http(|http| {
80 let _ = http.headers(headers);
81 });
82 session.finish()
83 }
84
85 /// Inspects HTTP headers without rendering their values.
86 ///
87 /// # Errors
88 ///
89 /// Returns [`RedactionInspectionError`] when a header cannot be decoded
90 /// safely or a shared resource limit prevents complete inspection.
91 ///
92 /// # Parameters
93 ///
94 /// - `headers`: Borrowed header collection, including repeated values.
95 ///
96 /// # Returns
97 ///
98 /// A conclusive sensitivity inspection, or a value-free error containing
99 /// resource usage and reasons why complete classification was unavailable.
100 #[inline]
101 pub fn inspect_http_headers(&self, headers: &HeaderMap) -> Result<RedactionInspection, RedactionInspectionError> {
102 let mut session = self.inspection_runtime();
103 inspection::inspect_headers(&mut session, headers);
104 session.finish()
105 }
106
107 /// Redacts one captured HTTP body through one completed session
108 /// transaction.
109 ///
110 /// # Parameters
111 ///
112 /// - `capture`: Captured bytes and completeness metadata used before body
113 /// parsing.
114 /// - `content_type`: Optional media-type metadata; None selects the
115 /// missing-type handling policy.
116 ///
117 /// # Returns
118 ///
119 /// Final redacted text and its execution summary, including any truncation
120 /// or admission failure recorded while processing the value.
121 #[must_use]
122 #[inline]
123 pub fn redact_http_body(
124 &self,
125 capture: BodyCapture<'_>,
126 content_type: Option<&HeaderValue>,
127 ) -> RedactionTextOutput {
128 let mut session = self.text_runtime();
129 session.http(|http| {
130 let _ = http.body(capture, content_type);
131 });
132 session.finish()
133 }
134
135 /// Inspects one captured HTTP body without rendering it.
136 ///
137 /// # Errors
138 ///
139 /// Returns [`RedactionInspectionError`] when capture metadata, content
140 /// type, body syntax, or a shared resource limit makes inspection
141 /// inconclusive.
142 ///
143 /// # Parameters
144 ///
145 /// - `capture`: Captured bytes and completeness metadata used before body
146 /// parsing.
147 /// - `content_type`: Optional media-type metadata; None selects the
148 /// missing-type handling policy.
149 ///
150 /// # Returns
151 ///
152 /// A conclusive sensitivity inspection, or a value-free error containing
153 /// resource usage and reasons why complete classification was unavailable.
154 #[inline]
155 pub fn inspect_http_body(
156 &self,
157 capture: BodyCapture<'_>,
158 content_type: Option<&HeaderValue>,
159 ) -> Result<RedactionInspection, RedactionInspectionError> {
160 let mut session = self.inspection_runtime();
161 inspection::inspect_body(&mut session, capture, content_type);
162 session.finish()
163 }
164
165 /// Redacts one captured HTTP body using textual Content-Type metadata.
166 ///
167 /// # Parameters
168 ///
169 /// - `capture`: Captured bytes and completeness metadata used before body
170 /// parsing.
171 /// - `content_type`: Optional media-type metadata; None selects the
172 /// missing-type handling policy.
173 ///
174 /// # Returns
175 ///
176 /// Final redacted text and its execution summary, including any truncation
177 /// or admission failure recorded while processing the value.
178 #[must_use]
179 #[inline]
180 pub fn redact_http_body_with_content_type_text(
181 &self,
182 capture: BodyCapture<'_>,
183 content_type: Option<&str>,
184 ) -> RedactionTextOutput {
185 let mut session = self.text_runtime();
186 session.http(|http| {
187 let _ = http.body_with_content_type_text(capture, content_type);
188 });
189 session.finish()
190 }
191
192 /// Inspects one captured HTTP body using textual Content-Type metadata.
193 ///
194 /// # Errors
195 ///
196 /// Returns [`RedactionInspectionError`] when capture metadata, content
197 /// type, body syntax, or a shared resource limit makes inspection
198 /// inconclusive.
199 ///
200 /// # Parameters
201 ///
202 /// - `capture`: Captured bytes and completeness metadata used before body
203 /// parsing.
204 /// - `content_type`: Optional media-type metadata; None selects the
205 /// missing-type handling policy.
206 ///
207 /// # Returns
208 ///
209 /// A conclusive sensitivity inspection, or a value-free error containing
210 /// resource usage and reasons why complete classification was unavailable.
211 #[inline]
212 pub fn inspect_http_body_with_content_type_text(
213 &self,
214 capture: BodyCapture<'_>,
215 content_type: Option<&str>,
216 ) -> Result<RedactionInspection, RedactionInspectionError> {
217 let mut session = self.inspection_runtime();
218 inspection::inspect_body_with_content_type_text(&mut session, capture, content_type);
219 session.finish()
220 }
221}