Skip to main content

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 super::Redactor;
11use crate::RedactionInspection;
12use crate::RedactionInspectionError;
13use crate::RedactionTextOutput;
14use crate::formats::http::BodyCapture;
15
16impl Redactor {
17    /// Redacts an HTTP URL through one completed text transaction.
18    #[must_use]
19    pub fn redact_http_url(&self, value: &str) -> RedactionTextOutput {
20        let mut session = self.text_runtime();
21        session.http(|http| {
22            let _ = http.url(value);
23        });
24        session.finish()
25    }
26
27    /// Inspects one HTTP URL without rendering it.
28    ///
29    /// # Errors
30    ///
31    /// Returns [`RedactionInspectionError`] when the URL is invalid or a
32    /// shared resource limit prevents complete inspection.
33    pub fn inspect_http_url(&self, value: &str) -> Result<RedactionInspection, RedactionInspectionError> {
34        let mut session = self.inspection_runtime();
35        crate::formats::http::inspection::inspect_url(&mut session, value);
36        session.finish()
37    }
38
39    /// Redacts an HTTP header collection through one completed transaction.
40    #[must_use]
41    pub fn redact_http_headers(&self, headers: &http::HeaderMap) -> RedactionTextOutput {
42        let mut session = self.text_runtime();
43        session.http(|http| {
44            let _ = http.headers(headers);
45        });
46        session.finish()
47    }
48
49    /// Inspects HTTP headers without rendering their values.
50    ///
51    /// # Errors
52    ///
53    /// Returns [`RedactionInspectionError`] when a header cannot be decoded
54    /// safely or a shared resource limit prevents complete inspection.
55    pub fn inspect_http_headers(
56        &self,
57        headers: &http::HeaderMap,
58    ) -> Result<RedactionInspection, RedactionInspectionError> {
59        let mut session = self.inspection_runtime();
60        crate::formats::http::inspection::inspect_headers(&mut session, headers);
61        session.finish()
62    }
63
64    /// Redacts one captured HTTP body through one completed session
65    /// transaction.
66    #[must_use]
67    pub fn redact_http_body(
68        &self,
69        capture: BodyCapture<'_>,
70        content_type: Option<&http::HeaderValue>,
71    ) -> RedactionTextOutput {
72        let mut session = self.text_runtime();
73        session.http(|http| {
74            let _ = http.body(capture, content_type);
75        });
76        session.finish()
77    }
78
79    /// Inspects one captured HTTP body without rendering it.
80    ///
81    /// # Errors
82    ///
83    /// Returns [`RedactionInspectionError`] when capture metadata, content
84    /// type, body syntax, or a shared resource limit makes inspection
85    /// inconclusive.
86    pub fn inspect_http_body(
87        &self,
88        capture: BodyCapture<'_>,
89        content_type: Option<&http::HeaderValue>,
90    ) -> Result<RedactionInspection, RedactionInspectionError> {
91        let mut session = self.inspection_runtime();
92        crate::formats::http::inspection::inspect_body(&mut session, capture, content_type);
93        session.finish()
94    }
95
96    /// Redacts one captured HTTP body using textual Content-Type metadata.
97    #[must_use]
98    pub fn redact_http_body_with_content_type_text(
99        &self,
100        capture: BodyCapture<'_>,
101        content_type: Option<&str>,
102    ) -> RedactionTextOutput {
103        let mut session = self.text_runtime();
104        session.http(|http| {
105            let _ = http.body_with_content_type_text(capture, content_type);
106        });
107        session.finish()
108    }
109
110    /// Inspects one captured HTTP body using textual Content-Type metadata.
111    ///
112    /// # Errors
113    ///
114    /// Returns [`RedactionInspectionError`] when capture metadata, content
115    /// type, body syntax, or a shared resource limit makes inspection
116    /// inconclusive.
117    pub fn inspect_http_body_with_content_type_text(
118        &self,
119        capture: BodyCapture<'_>,
120        content_type: Option<&str>,
121    ) -> Result<RedactionInspection, RedactionInspectionError> {
122        let mut session = self.inspection_runtime();
123        crate::formats::http::inspection::inspect_body_with_content_type_text(&mut session, capture, content_type);
124        session.finish()
125    }
126}