Response parser fix.

This commit is contained in:
Mauro D 2023-01-22 18:20:06 +00:00
parent 47dead6b13
commit 1dbb2ab8fc

View file

@ -88,7 +88,7 @@ impl ResponseReceiver {
do_restore = true; do_restore = true;
} }
} }
b'.' if self.pos < 6 && self.esc_cur[self.pos - 4] > 0 => { b'.' if self.pos < 6 => {
if let Some(restore) = self.esc_restore.get_mut(self.esc_pos) { if let Some(restore) = self.esc_restore.get_mut(self.esc_pos) {
*restore = ch; *restore = ch;
self.pos += 1; self.pos += 1;
@ -100,7 +100,6 @@ impl ResponseReceiver {
b' ' | b'\r' | b'\n' if self.pos == 6 => { b' ' | b'\r' | b'\n' if self.pos == 6 => {
self.esc = self.esc_cur; self.esc = self.esc_cur;
self.esc_cur.fill(0); self.esc_cur.fill(0);
self.esc_restore.fill(0);
self.pos = 7; self.pos = 7;
self.esc_pos = 0; self.esc_pos = 0;
} }
@ -108,21 +107,16 @@ impl ResponseReceiver {
do_restore = true; do_restore = true;
} }
} }
if do_restore { if do_restore {
// ESC parsing failed, restore parsed digits // ESC parsing failed, restore parsed digits
for &rch in &self.esc_restore { if self.esc_pos > 0 {
if rch != 0 { self.buf
self.buf.push(rch); .extend_from_slice(&self.esc_restore[..self.esc_pos]);
} else {
break;
}
} }
self.buf.push(ch); self.buf.push(ch);
self.pos = 7; self.pos = 7;
self.esc_pos = 0; self.esc_pos = 0;
self.esc_cur.fill(0); self.esc_cur.fill(0);
self.esc_restore.fill(0);
} }
} }
_ => match ch { _ => match ch {
@ -158,7 +152,10 @@ impl ResponseReceiver {
self.is_last = false; self.is_last = false;
self.code = 0; self.code = 0;
self.esc.fill(0); self.esc.fill(0);
self.esc_cur.fill(0);
self.pos = 0; self.pos = 0;
self.esc_pos = 0;
self.buf.clear();
} }
} }
@ -603,6 +600,15 @@ mod tests {
}, },
true, true,
), ),
(
concat!("250 2.0.0 Message queued for delivery.\r\n"),
Response {
code: 250,
esc: [2, 0, 0],
message: "Message queued for delivery.".to_string(),
},
true,
),
( (
concat!("250-Missing space\n", "250\n", "250 Ignore this"), concat!("250-Missing space\n", "250\n", "250 Ignore this"),
Response { Response {