LineReceiver implementation.

This commit is contained in:
Mauro D 2022-12-24 16:57:50 +00:00
parent bb6437dec4
commit 3403368e71
5 changed files with 38 additions and 1 deletions

View file

@ -192,6 +192,7 @@ pub const EXT_SMTP_UTF8: u32 = 1 << 23;
pub const EXT_START_TLS: u32 = 1 << 24;
pub const EXT_VERB: u32 = 1 << 25;
pub const EXT_EXPN: u32 = 1 << 26;
pub const EXT_VRFY: u32 = 1 << 27;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MtPriority {

View file

@ -2,7 +2,7 @@ use std::slice::Iter;
use crate::{Error, Request};
const MAX_LINE_LENGTH: usize = 2048;
pub const MAX_LINE_LENGTH: usize = 2048;
#[derive(Default)]
pub struct RequestReceiver {
@ -31,6 +31,12 @@ pub struct DummyDataReceiver {
#[derive(Default)]
pub struct DummyLineReceiver {}
#[derive(Default)]
pub struct LineReceiver<T> {
pub buf: Vec<u8>,
pub state: T,
}
impl RequestReceiver {
pub fn ingest(
&mut self,
@ -180,6 +186,30 @@ impl DummyDataReceiver {
}
}
impl<T> LineReceiver<T> {
pub fn new(state: T) -> Self {
Self {
buf: Vec::with_capacity(32),
state,
}
}
pub fn ingest(&mut self, bytes: &mut Iter<'_, u8>) -> bool {
for &ch in bytes {
match ch {
b'\n' => return true,
b'\r' => (),
_ => {
if self.buf.len() < MAX_LINE_LENGTH {
self.buf.push(ch);
}
}
}
}
false
}
}
impl DummyLineReceiver {
pub fn ingest(&mut self, bytes: &mut Iter<'_, u8>) -> bool {
for &ch in bytes {

View file

@ -60,6 +60,7 @@ impl<T: Display> EhloResponse<T> {
EXT_ENHANCED_STATUS_CODES => write!(writer, "ENHANCEDSTATUSCODES\r\n"),
EXT_ETRN => write!(writer, "ETRN\r\n"),
EXT_EXPN => write!(writer, "EXPN\r\n"),
EXT_VRFY => write!(writer, "VRFY\r\n"),
EXT_FUTURE_RELEASE => write!(
writer,
"FUTURERELEASE {} {}\r\n",

View file

@ -90,6 +90,8 @@ pub(crate) const ETRN: u128 =
(b'E' as u128) | (b'T' as u128) << 8 | (b'R' as u128) << 16 | (b'N' as u128) << 24;
pub(crate) const EXPN: u128 =
(b'E' as u128) | (b'X' as u128) << 8 | (b'P' as u128) << 16 | (b'N' as u128) << 24;
pub(crate) const VRFY: u128 =
(b'V' as u128) | (b'R' as u128) << 8 | (b'F' as u128) << 16 | (b'Y' as u128) << 24;
pub(crate) const FUTURERELEASE: u128 = (b'F' as u128)
| (b'U' as u128) << 8
| (b'T' as u128) << 16

View file

@ -201,6 +201,7 @@ impl EhloResponse<String> {
}
ETRN => EXT_ETRN,
EXPN => EXT_EXPN,
VRFY => EXT_VRFY,
FUTURERELEASE => {
let max_interval = if parser.stop_char != LF {
parser.size()?
@ -327,6 +328,7 @@ mod tests {
"250-ENHANCEDSTATUSCODES\n",
"250-ETRN\n",
"250-EXPN\n",
"250-VRFY\n",
"250-FUTURERELEASE 1234 5678\n",
"250-HELP\n",
"250-MT-PRIORITY\n",
@ -355,6 +357,7 @@ mod tests {
| EXT_ENHANCED_STATUS_CODES
| EXT_ETRN
| EXT_EXPN
| EXT_VRFY
| EXT_FUTURE_RELEASE
| EXT_HELP
| EXT_MT_PRIORITY