From 6f9f526385527790af25a94dcf4e4ce0b2e9cd9e Mon Sep 17 00:00:00 2001 From: Ritesh Chitlangi Date: Tue, 20 Jul 2021 01:56:10 +0800 Subject: [PATCH] clean up xml serialization --- src/connection.rs | 34 +++++++++++++++++++++------------- src/epp.rs | 3 ++- src/epp/objects.rs | 0 src/epp/quick_xml.rs | 13 +++++++++++++ src/epp/request.rs | 12 +++++------- src/epp/xml.rs | 6 +++++- src/lib.rs | 21 ++++++++++----------- src/main.rs | 12 ------------ 8 files changed, 56 insertions(+), 45 deletions(-) delete mode 100644 src/epp/objects.rs create mode 100644 src/epp/quick_xml.rs diff --git a/src/connection.rs b/src/connection.rs index ba14d29..e2555f9 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -9,30 +9,29 @@ use tokio_rustls::{TlsConnector, rustls::ClientConfig, webpki::DNSNameRef, clien use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::ReadHalf, io::WriteHalf}; use crate::config::{CONFIG, EppClientConnection}; -use tokio::time::{sleep, Duration}; use crate::error; -use crate::epp::request::EppRequest; +use crate::epp::request::{EppRequest, Login}; +use crate::epp::xml::EppXml; -struct ConnectionStream { +pub struct ConnectionStream { reader: ReadHalf>, writer: WriteHalf>, } -struct EppConnection { +pub struct EppConnection { registry: String, - credentials: (String, String), stream: ConnectionStream, pub greeting: String, } pub struct EppClient { + credentials: (String, String), connection: EppConnection, } impl EppConnection { pub async fn new( registry: String, - credentials: (String, String), mut stream: ConnectionStream) -> Result> { let mut buf = vec![0u8; 4096]; stream.reader.read(&mut buf).await?; @@ -40,7 +39,6 @@ impl EppConnection { Ok(EppConnection { registry: registry, - credentials: credentials, stream: stream, greeting: greeting }) @@ -66,9 +64,6 @@ impl EppConnection { buf[..4].clone_from_slice(&len_u32); buf[4..].clone_from_slice(&content.as_bytes()); - let conv = str::from_utf8(&buf[4..])?; - println!("reconverted: {}", conv); - self.write(&buf).await } @@ -141,8 +136,22 @@ impl Drop for EppConnection { } impl EppClient { + pub async fn new(connection: EppConnection, credentials: (String, String)) -> Result> { + let mut client = EppClient { + connection: connection, + credentials: credentials + }; + + let client_trid = EppRequest::generate_client_trid(&client.credentials.0)?; + let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_trid.as_str()); + + client.transact(&login_request).await?; + + Ok(client) + } + pub async fn transact(&mut self, request: &EppRequest) -> Result> { - let epp_xml = request.to_epp_xml()?; + let epp_xml = request.serialize()?; println!("Request:\r\n{}", epp_xml); @@ -207,11 +216,10 @@ pub async fn connect(registry: &'static str) -> Result let connection = EppConnection::new( registry.to_string(), - credentials, stream ).await.unwrap(); - let client = EppClient { connection: connection }; + let client = EppClient::new(connection, credentials).await.unwrap(); tx.send(client).unwrap(); }); diff --git a/src/epp.rs b/src/epp.rs index a21b298..478b25f 100644 --- a/src/epp.rs +++ b/src/epp.rs @@ -1,2 +1,3 @@ -pub mod objects; +pub mod quick_xml; pub mod request; +pub mod xml; diff --git a/src/epp/objects.rs b/src/epp/objects.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/epp/quick_xml.rs b/src/epp/quick_xml.rs new file mode 100644 index 0000000..c0a299c --- /dev/null +++ b/src/epp/quick_xml.rs @@ -0,0 +1,13 @@ +use quick_xml::se; +use std::error::Error; + +use crate::epp::request::EppObject; +use crate::epp::xml::{EppXml, EPP_XML_HEADER}; + +impl EppXml for EppObject { + fn serialize(&self) -> Result> { + let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); + + Ok(epp_xml) + } +} diff --git a/src/epp/request.rs b/src/epp/request.rs index d2aadd5..c62d568 100644 --- a/src/epp/request.rs +++ b/src/epp/request.rs @@ -1,8 +1,7 @@ -use quick_xml::se; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::error::Error; +use std::time::SystemTime; -const EPP_XML_HEADER: &str = r#""#; const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0"; const EPP_XMLNS_XSI: &str = "http://www.w3.org/2001/XMLSchema-instance"; const EPP_XSI_SCHEMA_LOCATION: &str = "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"; @@ -61,10 +60,9 @@ impl EppObject { } } - pub fn to_epp_xml(&self) -> Result> { - let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); - - Ok(epp_xml) + pub fn generate_client_trid(username: &str) -> Result> { + let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; + Ok(format!("{}:{}", username, timestamp.as_secs())) } } diff --git a/src/epp/xml.rs b/src/epp/xml.rs index 3588cbe..e78f9ba 100644 --- a/src/epp/xml.rs +++ b/src/epp/xml.rs @@ -1,4 +1,8 @@ +use std::error::Error; + +pub const EPP_XML_HEADER: &str = r#""#; + pub trait EppXml { fn serialize(&self) -> Result>; - fn deserialize(&self) -> Result>; + // fn deserialize(&self) -> Result>; } diff --git a/src/lib.rs b/src/lib.rs index 1dc6ae0..19cc4dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,12 @@ // pub mod config; // pub mod connection; +// pub mod epp; +// pub mod error; // #[cfg(test)] // mod tests { -// use super::config; +// use super::config; // use super::connection; -// use std::str; // #[test] // fn config() { @@ -22,14 +23,12 @@ // #[test] // fn connect() { -// let mut cn = aw!(connection::connect("hexonet")).unwrap(); -// println!("lol"); -// let contents = aw!(cn.read()).unwrap(); - -// match str::from_utf8(&contents) { -// Ok(v) => println!("{}", v), -// Err(e) => panic!("Error: {}", e) -// } -// aw!(cn.close()); +// let mut client = match aw!(connection::connect("hexonet")) { +// Ok(client) => { +// println!("{}", client.greeting()); +// client +// }, +// Err(e) => panic!("Error: {}", e) +// }; // } // } diff --git a/src/main.rs b/src/main.rs index 3bca744..4dc9d89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,19 +17,7 @@ async fn main() { Err(e) => panic!("Error: {}", e) }; - // sleep(Duration::from_millis(100000)).await; - - let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); - let cl_trid = format!("eppdev:{}", timestamp.as_secs()); - let epp_login = request::Login::new("eppdev", "sh48sja#27*A", &cl_trid); - - client.transact(&epp_login).await.unwrap(); - let epp_hello = request::Hello::new(); client.transact(&epp_hello).await.unwrap(); - - //let response = client.transact(&epp_hello).await.unwrap(); - - //println!("{}", response); }