use crate::error::NetError;
use crate::module::transport::failure::{ConnectStage, ConnectionFailure};
use http::header::{HeaderName, HeaderValue, CONTENT_TYPE, RETRY_AFTER};
use http::HeaderMap;
use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor};
use std::fmt;
const MAX_ALLOWLIST_CODES: usize = 32;
const MAX_CODE_BYTES: usize = 64;
const MAX_BODY_INPUT_BYTES: usize = 16 * 1024;
const MAX_HEADER_VALUE_BYTES: usize = 128;
#[derive(Clone, Default)]
pub struct HandshakeDiagnosticOptions {
pub public_json_codes: Vec<String>,
}
impl HandshakeDiagnosticOptions {
pub fn validate(&self) -> crate::Result<()> {
if self.public_json_codes.len() > MAX_ALLOWLIST_CODES {
return Err(NetError::config(
"diagnostics.public_json_codes",
"must contain at most 32 public codes, including duplicates",
));
}
for code in &self.public_json_codes {
if code.is_empty()
|| code.len() > MAX_CODE_BYTES
|| !code
.bytes()
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_')
{
return Err(NetError::config(
"diagnostics.public_json_codes",
"each public code must contain 1–64 ASCII uppercase letters, digits or underscores",
));
}
}
Ok(())
}
}
impl fmt::Debug for HandshakeDiagnosticOptions {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("HandshakeDiagnosticOptions")
.field("public_json_code_count", &self.public_json_codes.len())
.finish()
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HandshakeDiagnosticKind {
HttpRejected,
Io(
std::io::ErrorKind,
),
TlsCertificate,
TlsProtocol,
Protocol,
Timeout,
Dns,
Local,
Other,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HandshakeBodyCaptureState {
Omitted,
Unavailable,
Incomplete,
Unsupported,
TooLarge,
Unrecognized,
Captured,
}
#[derive(Clone)]
pub struct HandshakeDiagnostic {
kind: HandshakeDiagnosticKind,
headers: HeaderMap,
body_summary: Option<String>,
body_capture_state: HandshakeBodyCaptureState,
}
impl HandshakeDiagnostic {
pub(crate) fn new(kind: HandshakeDiagnosticKind) -> Self {
Self {
kind,
headers: HeaderMap::new(),
body_summary: None,
body_capture_state: HandshakeBodyCaptureState::Unavailable,
}
}
pub(crate) fn http<'a>(
options: &HandshakeDiagnosticOptions,
headers: impl Iterator<Item = (&'a str, &'a [u8])>,
body: Option<&[u8]>,
) -> Self {
let metadata = HttpMetadata::read(headers);
let mut diagnostic = Self::new(HandshakeDiagnosticKind::HttpRejected);
if let Some(content_type) = metadata.json_content_type() {
diagnostic.retain_header(CONTENT_TYPE, content_type);
}
if let Some(seconds) = metadata.retry_after.unique().and_then(parse_decimal) {
let mut normalized = String::new();
if normalized.try_reserve_exact(20).is_ok() {
use std::fmt::Write;
if write!(&mut normalized, "{seconds}").is_ok() {
diagnostic.retain_header(RETRY_AFTER, &normalized);
} else {
log_capture_allocation_failure();
}
} else {
log_capture_allocation_failure();
}
}
let (state, summary) = capture_body(options, &metadata, body);
diagnostic.body_capture_state = state;
diagnostic.body_summary = summary;
diagnostic
}
pub(crate) fn from_failure(failure: ConnectionFailure) -> Self {
use HandshakeDiagnosticKind as Kind;
let kind = if failure.http_status().is_some() {
Kind::HttpRejected
} else if failure.error().kind() == crate::error::ErrorKind::TimedOut {
Kind::Timeout
} else {
match failure.stage() {
ConnectStage::Provider
| ConnectStage::RequestBuild
| ConnectStage::EventDelivery => Kind::Local,
ConnectStage::Dns => Kind::Dns,
ConnectStage::Tls | ConnectStage::ProxyTls => Kind::TlsProtocol,
_ if failure.error().kind() == crate::error::ErrorKind::Tls => Kind::TlsProtocol,
_ if failure.error().kind() == crate::error::ErrorKind::Protocol => Kind::Protocol,
_ => Kind::Other,
}
};
Self::new(kind)
}
fn retain_header(&mut self, name: HeaderName, value: &str) {
let Some(value) = try_owned(value) else {
return;
};
let Ok(value) = HeaderValue::from_maybe_shared(bytes::Bytes::from(value)) else {
return;
};
if self.headers.try_insert(name, value).is_err() {
log_capture_allocation_failure();
}
}
pub fn kind(&self) -> HandshakeDiagnosticKind {
self.kind
}
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
pub fn body_summary(&self) -> Option<&str> {
self.body_summary.as_deref()
}
pub fn body_capture_state(&self) -> HandshakeBodyCaptureState {
self.body_capture_state
}
}
fn try_owned(value: &str) -> Option<String> {
let mut owned = String::new();
if owned.try_reserve_exact(value.len()).is_err() {
log_capture_allocation_failure();
return None;
}
owned.push_str(value);
Some(owned)
}
fn log_capture_allocation_failure() {
crate::log_e!(crate::common::log::log_def::LogType::WSC; "handshake_diagnostic", "error", "safe_capture_allocation_failed");
}
#[derive(Default)]
struct HeaderField<'a> {
value: Option<&'a [u8]>,
duplicate: bool,
}
impl<'a> HeaderField<'a> {
fn insert(&mut self, value: &'a [u8]) {
if self.value.is_some() {
self.duplicate = true;
} else {
self.value = Some(value);
}
}
fn unique(&self) -> Option<&'a [u8]> {
if self.duplicate {
None
} else {
self.value
}
}
}
#[derive(Default)]
struct HttpMetadata<'a> {
content_type: HeaderField<'a>,
content_length: HeaderField<'a>,
content_encoding: HeaderField<'a>,
retry_after: HeaderField<'a>,
transfer_encoding: bool,
}
impl<'a> HttpMetadata<'a> {
fn read(headers: impl Iterator<Item = (&'a str, &'a [u8])>) -> Self {
let mut metadata = Self::default();
for (name, value) in headers {
if name.eq_ignore_ascii_case("content-type") {
metadata.content_type.insert(value);
} else if name.eq_ignore_ascii_case("content-length") {
metadata.content_length.insert(value);
} else if name.eq_ignore_ascii_case("content-encoding") {
metadata.content_encoding.insert(value);
} else if name.eq_ignore_ascii_case("retry-after") {
metadata.retry_after.insert(value);
} else if name.eq_ignore_ascii_case("transfer-encoding") {
metadata.transfer_encoding = true;
}
}
metadata
}
fn json_content_type(&self) -> Option<&'static str> {
let value = self.content_type.unique()?;
if value.len() > MAX_HEADER_VALUE_BYTES {
return None;
}
let media_type = trim_ows(value.split(|byte| *byte == b';').next()?);
if media_type.eq_ignore_ascii_case(b"application/json") {
Some("application/json")
} else if media_type.eq_ignore_ascii_case(b"application/problem+json") {
Some("application/problem+json")
} else {
None
}
}
fn has_supported_encoding(&self) -> bool {
if self.transfer_encoding || self.content_encoding.duplicate {
return false;
}
match self.content_encoding.value {
None => true,
Some(value) => {
value.len() <= MAX_HEADER_VALUE_BYTES
&& trim_ows(value).eq_ignore_ascii_case(b"identity")
}
}
}
}
fn trim_ows(mut value: &[u8]) -> &[u8] {
while let Some((byte, rest)) = value.split_first() {
if matches!(byte, b' ' | b'\t') {
value = rest;
} else {
break;
}
}
while let Some((byte, rest)) = value.split_last() {
if matches!(byte, b' ' | b'\t') {
value = rest;
} else {
break;
}
}
value
}
fn parse_decimal(value: &[u8]) -> Option<u64> {
if value.len() > MAX_HEADER_VALUE_BYTES {
return None;
}
let value = trim_ows(value);
if value.is_empty() || !value.iter().all(u8::is_ascii_digit) {
return None;
}
std::str::from_utf8(value).ok()?.parse().ok()
}
fn capture_body(
options: &HandshakeDiagnosticOptions,
metadata: &HttpMetadata<'_>,
body: Option<&[u8]>,
) -> (HandshakeBodyCaptureState, Option<String>) {
use HandshakeBodyCaptureState as State;
if options.public_json_codes.is_empty() {
return (State::Omitted, None);
}
let Some(body) = body.filter(|body| !body.is_empty()) else {
return (State::Unavailable, None);
};
if body.len() > MAX_BODY_INPUT_BYTES {
return (State::TooLarge, None);
}
if metadata.json_content_type().is_none()
|| !metadata.has_supported_encoding()
|| metadata.content_length.duplicate
{
return (State::Unsupported, None);
}
let Some(length) = metadata.content_length.unique() else {
return (State::Incomplete, None);
};
let Some(length) = parse_decimal(length) else {
return (State::Unsupported, None);
};
if length > MAX_BODY_INPUT_BYTES as u64 {
return (State::TooLarge, None);
}
if length != body.len() as u64 {
return (State::Incomplete, None);
}
if std::str::from_utf8(body).is_err() {
return (State::Unsupported, None);
}
let mut deserializer = serde_json::Deserializer::from_slice(body);
let selected = CodeObjectSeed {
codes: &options.public_json_codes,
}
.deserialize(&mut deserializer);
let Ok(selected) = selected else {
return (State::Unsupported, None);
};
if deserializer.end().is_err() {
return (State::Unsupported, None);
}
let Some(code) = selected.and_then(|index| options.public_json_codes.get(index)) else {
return (State::Unrecognized, None);
};
let mut summary = String::new();
if summary
.try_reserve_exact("code=".len() + code.len())
.is_err()
{
log_capture_allocation_failure();
return (State::Unavailable, None);
}
summary.push_str("code=");
summary.push_str(code);
(State::Captured, Some(summary))
}
struct CodeObjectSeed<'a> {
codes: &'a [String],
}
impl<'de> DeserializeSeed<'de> for CodeObjectSeed<'_> {
type Value = Option<usize>;
fn deserialize<D: serde::Deserializer<'de>>(
self,
deserializer: D,
) -> Result<Self::Value, D::Error> {
deserializer.deserialize_map(self)
}
}
impl<'de> Visitor<'de> for CodeObjectSeed<'_> {
type Value = Option<usize>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a JSON object with at most one top-level code")
}
fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
let mut seen_code = false;
let mut selected = None;
while let Some(is_code) = map.next_key_seed(CodeKeySeed)? {
if is_code {
if seen_code {
return Err(serde::de::Error::custom("duplicate diagnostic code"));
}
seen_code = true;
selected = map.next_value_seed(CodeValueSeed { codes: self.codes })?;
} else {
map.next_value::<IgnoredAny>()?;
}
}
Ok(selected)
}
}
struct CodeKeySeed;
impl<'de> DeserializeSeed<'de> for CodeKeySeed {
type Value = bool;
fn deserialize<D: serde::Deserializer<'de>>(self, deserializer: D) -> Result<bool, D::Error> {
deserializer.deserialize_str(self)
}
}
impl Visitor<'_> for CodeKeySeed {
type Value = bool;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a JSON object key")
}
fn visit_str<E: serde::de::Error>(self, key: &str) -> Result<bool, E> {
Ok(key == "code")
}
}
struct CodeValueSeed<'a> {
codes: &'a [String],
}
impl<'de> DeserializeSeed<'de> for CodeValueSeed<'_> {
type Value = Option<usize>;
fn deserialize<D: serde::Deserializer<'de>>(
self,
deserializer: D,
) -> Result<Self::Value, D::Error> {
deserializer.deserialize_any(self)
}
}
impl<'de> Visitor<'de> for CodeValueSeed<'_> {
type Value = Option<usize>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a public diagnostic code")
}
fn visit_str<E: serde::de::Error>(self, code: &str) -> Result<Self::Value, E> {
Ok(self.codes.iter().position(|allowed| allowed == code))
}
fn visit_bool<E: serde::de::Error>(self, _value: bool) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_i64<E: serde::de::Error>(self, _value: i64) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_u64<E: serde::de::Error>(self, _value: u64) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_f64<E: serde::de::Error>(self, _value: f64) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}
fn visit_seq<S: SeqAccess<'de>>(self, mut sequence: S) -> Result<Self::Value, S::Error> {
while sequence.next_element::<IgnoredAny>()?.is_some() {}
Ok(None)
}
fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
while map.next_entry::<IgnoredAny, IgnoredAny>()?.is_some() {}
Ok(None)
}
}
impl fmt::Debug for HandshakeDiagnostic {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("HandshakeDiagnostic")
.field("kind", &self.kind)
.field("header_count", &self.headers.len())
.field(
"body_summary_bytes",
&self.body_summary.as_ref().map(String::len),
)
.field("body_capture_state", &self.body_capture_state)
.finish()
}
}
#[cfg(test)]
#[path = "handshake_diagnostic_tests.rs"]
mod tests;
#[cfg(test)]
#[path = "handshake_diagnostic_api_tests.rs"]
mod api_tests;