(mut conn: A)
| 81 | } |
| 82 | |
| 83 | pub async fn decode_startup<A>(mut conn: A) -> Result<Option<FrontendStartupMessage>, io::Error> |
| 84 | where |
| 85 | A: AsyncRead + Unpin, |
| 86 | { |
| 87 | let mut frame_len = [0; 4]; |
| 88 | let nread = netio::read_exact_or_eof(&mut conn, &mut frame_len).await?; |
| 89 | match nread { |
| 90 | // Complete frame length. Continue. |
| 91 | 4 => (), |
| 92 | // Connection closed cleanly. Indicate that the startup sequence has |
| 93 | // been terminated by the client. |
| 94 | 0 => return Ok(None), |
| 95 | // Partial frame length. Likely a client bug or network glitch, so |
| 96 | // surface the unexpected EOF. |
| 97 | _ => return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "early eof")), |
| 98 | }; |
| 99 | let frame_len = parse_frame_len(&frame_len)?; |
| 100 | |
| 101 | let mut buf = BytesMut::new(); |
| 102 | buf.resize(frame_len, b'0'); |
| 103 | conn.read_exact(&mut buf).await?; |
| 104 | |
| 105 | let mut buf = Cursor::new(&buf); |
| 106 | let version = buf.read_i32()?; |
| 107 | let message = match version { |
| 108 | VERSION_CANCEL => FrontendStartupMessage::CancelRequest { |
| 109 | conn_id: buf.read_u32()?, |
| 110 | secret_key: buf.read_u32()?, |
| 111 | }, |
| 112 | VERSION_SSL => FrontendStartupMessage::SslRequest, |
| 113 | VERSION_GSSENC => FrontendStartupMessage::GssEncRequest, |
| 114 | _ => { |
| 115 | let mut params = BTreeMap::new(); |
| 116 | while buf.peek_byte()? != 0 { |
| 117 | let name = buf.read_cstr()?.to_owned(); |
| 118 | let value = buf.read_cstr()?.to_owned(); |
| 119 | params.insert(name, value); |
| 120 | } |
| 121 | FrontendStartupMessage::Startup { version, params } |
| 122 | } |
| 123 | }; |
| 124 | Ok(Some(message)) |
| 125 | } |
| 126 | |
| 127 | impl FrontendStartupMessage { |
| 128 | /// Encodes self into dst. |
no test coverage detected