|
| 1 | +use bytes::Bytes; |
| 2 | +use indexmap::IndexMap; |
| 3 | + |
| 4 | +use super::CommandExecution; |
| 5 | +use crate::application::server::cmd::Parse; |
| 6 | +use crate::application::server::connection::WriteConnection; |
| 7 | +use crate::application::server::context::Context; |
| 8 | +use crate::application::server::frame::Frame; |
| 9 | + |
| 10 | +/// Switch to a different protocol, optionally authenticating and setting the |
| 11 | +/// connection's name, or provide a contextual client report. |
| 12 | +/// |
| 13 | +/// HELLO always replies with a list of current server and connection |
| 14 | +/// properties, such as: versions, modules loaded, client ID, replication role |
| 15 | +/// and so forth. |
| 16 | +/// |
| 17 | +/// In Roster we only reply in RESP 3. |
| 18 | +#[derive(Debug, Default)] |
| 19 | +pub struct Hello {} |
| 20 | + |
| 21 | +impl Hello { |
| 22 | + pub fn new() -> Hello { |
| 23 | + Hello {} |
| 24 | + } |
| 25 | + |
| 26 | + pub(crate) fn parse_frames(parse: &mut Parse) -> anyhow::Result<Hello> { |
| 27 | + parse.finish()?; |
| 28 | + Ok(Hello::new()) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl CommandExecution for Hello { |
| 33 | + async fn apply( |
| 34 | + self, |
| 35 | + dst: &mut WriteConnection, |
| 36 | + ctx: Context, |
| 37 | + ) -> anyhow::Result<()> { |
| 38 | + let id = ctx.connection.id(); |
| 39 | + |
| 40 | + let map = IndexMap::from_iter([ |
| 41 | + ( |
| 42 | + Frame::Bulk(Bytes::from_static(b"server")), |
| 43 | + Frame::Bulk(Bytes::from_static(b"roster")), |
| 44 | + ), |
| 45 | + ( |
| 46 | + Frame::Bulk(Bytes::from_static(b"version")), |
| 47 | + Frame::Bulk(Bytes::from_static(crate::VERSION.as_bytes())), |
| 48 | + ), |
| 49 | + (Frame::Bulk(Bytes::from_static(b"proto")), Frame::Integer(3)), |
| 50 | + (Frame::Bulk(Bytes::from_static(b"id")), Frame::Integer(id)), |
| 51 | + ( |
| 52 | + Frame::Bulk(Bytes::from_static(b"mode")), |
| 53 | + Frame::Bulk(Bytes::from_static(b"standalone")), |
| 54 | + ), |
| 55 | + ( |
| 56 | + Frame::Bulk(Bytes::from_static(b"role")), |
| 57 | + Frame::Bulk(Bytes::from_static(b"undefined")), |
| 58 | + ), |
| 59 | + ( |
| 60 | + Frame::Bulk(Bytes::from_static(b"modules")), |
| 61 | + Frame::Array(Vec::new()), |
| 62 | + ), |
| 63 | + ]); |
| 64 | + |
| 65 | + let response = Frame::Map(map); |
| 66 | + dst.write_frame(&response).await?; |
| 67 | + |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use std::io::Cursor; |
| 75 | + |
| 76 | + use bytes::BytesMut; |
| 77 | + use redis_async::resp::{RespCodec, RespValue}; |
| 78 | + use redis_async::resp_array; |
| 79 | + use tokio_util::codec::Encoder; |
| 80 | + |
| 81 | + use crate::application::server::cmd::Command; |
| 82 | + use crate::application::server::frame::Frame; |
| 83 | + |
| 84 | + fn parse_cmd(obj: RespValue) -> anyhow::Result<Command> { |
| 85 | + let mut bytes = BytesMut::new(); |
| 86 | + let mut codec = RespCodec; |
| 87 | + codec.encode(obj, &mut bytes).unwrap(); |
| 88 | + |
| 89 | + let mut bytes = Cursor::new(bytes.freeze()); |
| 90 | + let frame = Frame::parse(&mut bytes)?; |
| 91 | + let client_list = Command::from_frame(frame)?; |
| 92 | + Ok(client_list) |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn ensure_parsing() { |
| 97 | + let entry: RespValue = resp_array!["HELLO"]; |
| 98 | + let client_cmd = parse_cmd(entry).unwrap(); |
| 99 | + insta::assert_debug_snapshot!(client_cmd, @r###" |
| 100 | + Hello( |
| 101 | + Hello, |
| 102 | + ) |
| 103 | + "###); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn ensure_parsing_too_much() { |
| 108 | + let entry: RespValue = resp_array!["HELLO", "BLBL"]; |
| 109 | + let client_cmd = parse_cmd(entry); |
| 110 | + assert!(client_cmd.is_err()); |
| 111 | + let client_cmd = client_cmd.unwrap_err(); |
| 112 | + insta::assert_debug_snapshot!(client_cmd, @r###" |
| 113 | + Other( |
| 114 | + "protocol error; expected end of frame, but there was more", |
| 115 | + ) |
| 116 | + "###); |
| 117 | + } |
| 118 | +} |
0 commit comments