use http::{
HeaderName,
HeaderValue,
};
use url::Url;
use crate::HttpClientOptions;
use super::{
BodyLogContext,
BodyPreview,
LogSanitizePolicy,
LogSanitizer,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SanitizedLogger {
sanitizer: LogSanitizer,
body_size_limit: usize,
}
impl SanitizedLogger {
pub(crate) fn new(policy: LogSanitizePolicy, body_size_limit: usize) -> Self {
Self {
sanitizer: LogSanitizer::new(policy),
body_size_limit,
}
}
pub(crate) fn from_options(options: &HttpClientOptions) -> Self {
Self::new(options.log_sanitize_policy.clone(), options.logging.body_size_limit)
}
pub(crate) fn url(&self, url: &Url) -> String {
self.sanitizer.sanitize_url(url)
}
pub(crate) fn header_value(&self, name: &HeaderName, value: &HeaderValue) -> String {
self.sanitizer.sanitize_header_value(name, value)
}
pub(crate) fn body(&self, body: &[u8], context: BodyLogContext, content_type: Option<&str>) -> String {
let preview = BodyPreview::new(body, self.body_size_limit, context);
let preview = if let Some(content_type) = content_type {
preview.with_content_type(content_type)
} else {
preview
};
self.sanitizer.sanitize_body_preview(&preview)
}
}