use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};
use crate::errors;
use crate::proto::backend::BackendMessage;
use crate::proto::frontend::FrontendMessage;
pub struct FrontendCodec {
startup: bool,
}
impl FrontendCodec {
pub fn new() -> Self {
Self { startup: true }
}
pub fn post_startup() -> Self {
Self { startup: false }
}
}
impl Default for FrontendCodec {
fn default() -> Self {
Self::new()
}
}
impl Decoder for FrontendCodec {
type Item = FrontendMessage;
type Error = errors::ProtocolError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if self.startup {
decode_initial(src)
} else {
decode_regular(src, FrontendMessage::parse)
}
.map(|opt| {
if let Some(ref msg) = opt {
match msg {
FrontendMessage::SslRequest
| FrontendMessage::GssEncRequest
| FrontendMessage::CancelRequest { .. } => {}
_ => self.startup = false,
}
}
opt
})
}
}
impl Encoder<BackendMessage> for FrontendCodec {
type Error = errors::ProtocolError;
fn encode(&mut self, item: BackendMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
item.encode(dst);
Ok(())
}
}
pub struct BackendCodec;
impl BackendCodec {
pub fn new() -> Self {
Self
}
}
impl Default for BackendCodec {
fn default() -> Self {
Self::new()
}
}
impl Decoder for BackendCodec {
type Item = BackendMessage;
type Error = errors::ProtocolError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
decode_regular(src, BackendMessage::parse)
}
}
impl Encoder<FrontendMessage> for BackendCodec {
type Error = errors::ProtocolError;
fn encode(&mut self, item: FrontendMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
item.encode(dst);
Ok(())
}
}
fn decode_initial(src: &mut BytesMut) -> Result<Option<FrontendMessage>, errors::ProtocolError> {
if src.len() < 4 {
return Ok(None);
}
let length = u32::from_be_bytes([src[0], src[1], src[2], src[3]]) as usize;
if length < 4 {
return Err(errors::ProtocolError::InvalidValue {
position: 0,
message: format!("initial message length too small: {length}"),
});
}
if src.len() < length {
return Ok(None);
}
let frame = src.split_to(length);
FrontendMessage::parse_initial(&frame).map(Some)
}
fn decode_regular<M>(
src: &mut BytesMut,
parse: impl FnOnce(u8, &[u8]) -> Result<M, errors::ProtocolError>,
) -> Result<Option<M>, errors::ProtocolError> {
if src.len() < 5 {
return Ok(None);
}
let msg_type = src[0];
let length = u32::from_be_bytes([src[1], src[2], src[3], src[4]]) as usize;
if length < 4 {
return Err(errors::ProtocolError::InvalidValue {
position: 1,
message: format!("message length too small: {length}"),
});
}
let total = 1 + length; if src.len() < total {
return Ok(None);
}
let frame = src.split_to(total);
let payload = &frame[5..]; parse(msg_type, payload).map(Some)
}