From 64f88380088eb3fe75120afe49e9aa9bd8daf1c4 Mon Sep 17 00:00:00 2001 From: Horhik Date: Fri, 23 Apr 2021 09:45:54 +0300 Subject: [PATCH] add AllData response parser --- src/client/fcp_types.rs | 10 +++-- src/node/fcp_response.rs | 94 ++++++++++++++++++++++++++++++++++++++++ src/node/mod.rs | 2 +- 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 src/node/fcp_response.rs diff --git a/src/client/fcp_types.rs b/src/client/fcp_types.rs index e4135a7..17f63b3 100644 --- a/src/client/fcp_types.rs +++ b/src/client/fcp_types.rs @@ -279,7 +279,7 @@ impl SSKKeypair { } pub struct ClientPut { - uri: SSK, //TODO create key type + uri: USK, //TODO create key type data_length: usize, filename: Option, content_type: Option, @@ -409,7 +409,7 @@ impl FcpRequest for ClientPut { } impl ClientPut { - pub fn new_default_direct(uri: SSK, identifier: &str, data: &str) -> ClientPut { + pub fn new_default_direct(uri: USK, identifier: &str, data: &str) -> ClientPut { ClientPut { uri: uri, data_length: data.len(), @@ -456,10 +456,14 @@ fn is_client_put_converting() { EndMessage\n\ Hey jude\n"; let input = ClientPut::new_default_direct( - SSK { + USK{ + ssk: SSK{ sign_key: "BnHXXv3Fa43w~~iz1tNUd~cj4OpUuDjVouOWZ5XlpX0".to_string(), decrypt_key: "AwUSJG5ZS-FDZTqnt6skTzhxQe08T-fbKXj8aEHZsXM".to_string(), settings: Some("AQABAAE".to_string()), + }, + path: "myidentifier".to_string() + }, "myidentifier", "Hey jude", diff --git a/src/node/fcp_response.rs b/src/node/fcp_response.rs new file mode 100644 index 0000000..1f6161c --- /dev/null +++ b/src/node/fcp_response.rs @@ -0,0 +1,94 @@ +use crate::types::traits::FcpParser; +use regex::Regex; +use std::str::FromStr; +/* + AllData + Identifier=Request Number One + DataLength=37261 // length of data + StartupTime=1189683889 + CompletionTime=1189683889 + Metadata.ContentType=text/plain;charset=utf-8 + Data + <37261 bytes of data> + + + + +AllData\n +Identifier=get1\n +CompletionTime=1619156374827\n +StartupTime=1619156374743\n +DataLength=33\n +Global=false\n +Metadata.ContentType=application/octet-stream\n +Data\n +hello\n Yeah kek ruururun\n\n\nKE^[u + + */ + +#[derive(Debug, PartialEq)] +pub struct AllData { + pub identifier: String, + pub startup_time: String, + pub completion_time: String, + pub data_length: usize, + pub global: bool, + pub metadata_content_type: String, + pub data: String, +} + +impl FcpParser for AllData { + fn parse(plain: &str) -> Option { + let reg = Regex::new( + r"^AllData\nIdentifier=(.*)\nCompletionTime=(.*)\nStartupTime=(.*)\nDataLength=(.*)\nGlobal=(.*)\nMetadata\.ContentType=(.*)\nData\n((\n?.*)*)" + ) + .unwrap(); + println!("{:?}", reg); + let res = reg.captures(plain).unwrap(); + let identifier = res[1].to_string(); + // let completion_time = u32::from_str_radix(&res[2], 10).unwrap(); + // let startup_time = u32::from_str_radix(&res[3], 10).unwrap(); + let completion_time = res[2].to_string(); + let startup_time = res[3].to_string(); + let data_length = usize::from_str_radix(&res[4], 10).unwrap(); + let global = bool::from_str(&res[5]).unwrap(); + let metadata = res[6].to_string(); + let data = res[7].to_string(); + return Some(AllData { + identifier: identifier, + startup_time: startup_time, + completion_time: completion_time, + data_length: data_length, + global: global, + metadata_content_type: metadata, + data: data, + }); + } +} + +#[test] +fn is_all_data_parsing() { + assert_eq!( + AllData::parse( + "AllData\n\ + Identifier=get1\n\ + CompletionTime=1619156374827\n\ + StartupTime=1619156374743\n\ + DataLength=33\n\ + Global=false\n\ + Metadata.ContentType=application/octet-stream\n\ + Data\n\ + hello\n Yeah kek ruururun\n\n\nKE", + ) + .unwrap(), + AllData { + identifier: "get1".to_string(), + startup_time: "1619156374743".to_string(), + completion_time: "1619156374827".to_string(), + data_length: 33, + global: false, + metadata_content_type: "application/octet-stream".to_string(), + data:"hello\n Yeah kek ruururun\n\n\nKE".to_string() + } + ) +} diff --git a/src/node/mod.rs b/src/node/mod.rs index 8b13789..3e01ad4 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -1 +1 @@ - +pub mod fcp_response;