use super::HttpPolicyExecutor;
use crate::RedactionReason;
use crate::formats::http::internal::BoundedLogWriter;
use crate::formats::http::internal::markers;
use crate::runtime::OperationSink;
impl HttpPolicyExecutor<'_> {
#[must_use]
pub(super) fn finish_diagnostic_with_limit(
&self,
text: String,
max_bytes: usize,
provenance: Option<RedactionReason>,
) -> super::HttpRendered {
let mut writer = BoundedLogWriter::new(max_bytes, false);
let _ = writer.write_str(&text);
let mut operation = writer.finish_operation(RedactionReason::OutputLimitReached);
if let Some(reason) = provenance {
operation = operation.with_reason(reason);
}
super::HttpRendered::new(operation)
}
#[must_use]
#[inline]
pub(super) fn finish_rendered_url(&self, text: String, truncated: bool) -> super::HttpRendered {
let operation = if truncated {
OperationSink::truncated(text, RedactionReason::OutputLimitReached)
} else {
OperationSink::complete(text)
};
super::HttpRendered::new(operation.finish())
}
}
#[must_use]
pub(in crate::formats::http) fn bound_safe_text(text: &str, max_bytes: usize) -> (String, bool) {
if text.len() <= max_bytes {
return (text.to_owned(), false);
}
let marker = markers::TRUNCATED;
if max_bytes < marker.len() {
return (String::new(), true);
}
let payload_limit = max_bytes.saturating_sub(marker.len());
let mut output = String::new();
for character in text.chars() {
if output.len().saturating_add(character.len_utf8()) > payload_limit {
break;
}
output.push(character);
}
output.push_str(marker);
(output, true)
}