(&mut self, src: &mut BytesMut)
| 233 | type Error = io::Error; |
| 234 | |
| 235 | fn decode(&mut self, src: &mut BytesMut) -> Result<Option<FrontendMessage>, io::Error> { |
| 236 | if src.len() > MAX_REQUEST_SIZE { |
| 237 | return Err(io::Error::new( |
| 238 | io::ErrorKind::InvalidData, |
| 239 | format!( |
| 240 | "request larger than {}", |
| 241 | ByteSize::b(u64::cast_from(MAX_REQUEST_SIZE)) |
| 242 | ), |
| 243 | )); |
| 244 | } |
| 245 | loop { |
| 246 | match self.decode_state { |
| 247 | DecodeState::Head => { |
| 248 | if src.len() < 5 { |
| 249 | return Ok(None); |
| 250 | } |
| 251 | let msg_type = src[0]; |
| 252 | let frame_len = parse_frame_len(&src[1..])?; |
| 253 | src.advance(5); |
| 254 | src.reserve(frame_len); |
| 255 | self.decode_state = DecodeState::Data(msg_type, frame_len); |
| 256 | } |
| 257 | |
| 258 | DecodeState::Data(msg_type, frame_len) => { |
| 259 | if src.len() < frame_len { |
| 260 | return Ok(None); |
| 261 | } |
| 262 | let buf = src.split_to(frame_len).freeze(); |
| 263 | let buf = Cursor::new(&buf); |
| 264 | let msg = match msg_type { |
| 265 | // Termination. |
| 266 | b'X' => decode_terminate(buf)?, |
| 267 | |
| 268 | // Authentication. |
| 269 | b'p' => decode_password(buf)?, |
| 270 | |
| 271 | // Invalid. |
| 272 | _ => { |
| 273 | return Err(io::Error::new( |
| 274 | io::ErrorKind::InvalidData, |
| 275 | format!("unknown message type {}", msg_type), |
| 276 | )); |
| 277 | } |
| 278 | }; |
| 279 | src.reserve(5); |
| 280 | self.decode_state = DecodeState::Head; |
| 281 | return Ok(Some(msg)); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | fn decode_terminate(mut _buf: Cursor) -> Result<FrontendMessage, io::Error> { |
nothing calls this directly
no test coverage detected