use std::pin::Pin;
use std::task::{Context, Poll};
use futures::io::{AsyncRead, AsyncWrite};
use crate::output::OutputLevel;
#[derive(Clone, Copy)]
enum Direction {
Incoming,
Outgoing,
}
impl Direction {
fn prefix(self) -> &'static str {
match self {
Self::Incoming => "<-",
Self::Outgoing => "->",
}
}
}
const RESYNC_LIMIT: usize = 64 * 1024;
struct Framer {
channel: String,
dir: Direction,
buf: Vec<u8>,
}
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.is_empty() || haystack.len() < needle.len() {
return None;
}
haystack.windows(needle.len()).position(|w| w == needle)
}
fn parse_content_length(headers: &[u8]) -> Option<usize> {
let text = std::str::from_utf8(headers).ok()?;
for line in text.split("\r\n") {
if let Some((k, v)) = line.split_once(':')
&& k.trim().eq_ignore_ascii_case("content-length")
{
return v.trim().parse::<usize>().ok();
}
}
None
}
impl Framer {
fn new(server: &str, dir: Direction) -> Self {
Self {
channel: format!("{server} (trace)"),
dir,
buf: Vec::new(),
}
}
fn feed(&mut self, bytes: &[u8]) {
self.buf.extend_from_slice(bytes);
loop {
match find_subslice(&self.buf, b"Content-Length:") {
Some(0) => {}
Some(start) => {
self.buf.drain(..start);
}
None => {
if self.buf.len() > RESYNC_LIMIT {
self.buf.clear();
}
return;
}
}
let Some(hdr_end) = find_subslice(&self.buf, b"\r\n\r\n") else {
if self.buf.len() > RESYNC_LIMIT {
self.buf.clear();
}
return;
};
let Some(len) = parse_content_length(&self.buf[..hdr_end]) else {
self.buf.drain(..hdr_end + 4);
continue;
};
let body_start = hdr_end + 4;
if self.buf.len() < body_start + len {
return; }
let body = &self.buf[body_start..body_start + len];
let text = String::from_utf8_lossy(body).into_owned();
crate::output::push(
&self.channel,
OutputLevel::Trace,
&format!("{} {text}", self.dir.prefix()),
);
self.buf.drain(..body_start + len);
}
}
}
pub struct TraceRead<R> {
inner: R,
framer: Framer,
}
impl<R> TraceRead<R> {
pub fn new(inner: R, server: String) -> Self {
Self {
inner,
framer: Framer::new(&server, Direction::Incoming),
}
}
}
impl<R: AsyncRead + Unpin> AsyncRead for TraceRead<R> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
let this = self.get_mut();
let n = std::task::ready!(Pin::new(&mut this.inner).poll_read(cx, buf))?;
if n > 0 && crate::output::trace_enabled() {
this.framer.feed(&buf[..n]);
}
Poll::Ready(Ok(n))
}
}
pub struct TraceWrite<W> {
inner: W,
framer: Framer,
}
impl<W> TraceWrite<W> {
pub fn new(inner: W, server: String) -> Self {
Self {
inner,
framer: Framer::new(&server, Direction::Outgoing),
}
}
}
impl<W: AsyncWrite + Unpin> AsyncWrite for TraceWrite<W> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
let this = self.get_mut();
let n = std::task::ready!(Pin::new(&mut this.inner).poll_write(cx, buf))?;
if n > 0 && crate::output::trace_enabled() {
this.framer.feed(&buf[..n]);
}
Poll::Ready(Ok(n))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_close(cx)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn frame(json: &str) -> Vec<u8> {
format!("Content-Length: {}\r\n\r\n{json}", json.len()).into_bytes()
}
#[test]
fn framer_emits_one_line_per_complete_message() {
crate::output::set_trace_enabled(true);
let server = "trace-test-server";
let mut f = Framer::new(server, Direction::Incoming);
let body = r#"{"jsonrpc":"2.0","id":1,"result":null}"#;
let bytes = frame(body);
let (a, b) = bytes.split_at(10);
f.feed(a);
f.feed(b);
let lines = crate::output::snapshot(&format!("{server} (trace)")).unwrap_or_default();
assert_eq!(lines.len(), 1, "one complete message → one line");
assert!(lines[0].text.contains(body), "carries the JSON body");
assert!(lines[0].text.starts_with("<-"), "tagged with the direction");
crate::output::set_trace_enabled(false);
}
#[test]
fn framer_resyncs_when_started_mid_body() {
let server = "trace-test-resync";
let mut f = Framer::new(server, Direction::Outgoing);
f.feed(b"garbage-without-a-header-tail");
let body = r#"{"jsonrpc":"2.0","method":"x"}"#;
f.feed(&frame(body));
let lines = crate::output::snapshot(&format!("{server} (trace)")).unwrap_or_default();
assert_eq!(lines.len(), 1, "the pre-header junk is skipped");
assert!(lines[0].text.contains(body));
}
}