Skip to main content

instant_epp/
request.rs

1//! Types for EPP requests
2
3use std::fmt::Debug;
4
5use instant_xml::ser::Context;
6use instant_xml::{FromXmlOwned, ToXml};
7
8use crate::common::EPP_XMLNS;
9
10pub const EPP_VERSION: &str = "1.0";
11pub const EPP_LANG: &str = "en";
12
13/// Trait to set correct value for xml tags when tags are being generated from generic types
14pub trait Transaction<Ext: Extension>: Command + Sized {}
15
16pub trait Command: ToXml + Debug {
17    type Response: FromXmlOwned + Debug;
18    const COMMAND: &'static str;
19}
20
21pub trait Extension: ToXml + Debug {
22    type Response: FromXmlOwned + Debug;
23}
24
25#[derive(Debug, PartialEq)]
26/// Type corresponding to the `<command>` tag in an EPP XML request
27/// with an `<extension>` tag
28pub(crate) struct CommandWrapper<'a, D, E> {
29    command: &'static str,
30    /// The instance that will be used to populate the `<command>` tag
31    data: &'a D,
32    /// The client TRID
33    extension: Option<&'a E>,
34    client_tr_id: String,
35}
36
37impl<'a, E: Extension, D: Transaction<E>> CommandWrapper<'a, D, E> {
38    pub(crate) fn new(data: &'a D, extension: Option<&'a E>, client_tr_id: &'a str) -> Self {
39        Self {
40            command: D::COMMAND,
41            data,
42            extension,
43            client_tr_id: client_tr_id.into(),
44        }
45    }
46}
47
48impl<D: ToXml, E: ToXml> ToXml for CommandWrapper<'_, D, E> {
49    fn serialize<W: std::fmt::Write + ?Sized>(
50        &self,
51        _: Option<instant_xml::Id<'_>>,
52        serializer: &mut instant_xml::Serializer<W>,
53    ) -> Result<(), instant_xml::Error> {
54        let command = serializer.write_start("command", EPP_XMLNS, None::<Context<0>>)?;
55        serializer.end_start()?;
56        self.data.serialize(None, serializer)?;
57        if let Some(extension) = self.extension {
58            Ext { inner: extension }.serialize(None, serializer)?;
59        }
60
61        let cl_tr_id = serializer.write_start("clTRID", EPP_XMLNS, None::<Context<0>>)?;
62        serializer.end_start()?;
63        self.client_tr_id.as_str().serialize(None, serializer)?;
64        serializer.write_close(cl_tr_id)?;
65
66        serializer.write_close(command)?;
67        Ok(())
68    }
69}
70
71#[derive(Debug, ToXml)]
72#[xml(rename = "extension", ns(EPP_XMLNS))]
73struct Ext<E> {
74    inner: E,
75}
76
77#[cfg(test)]
78mod tests {
79    use similar_asserts::assert_eq;
80
81    use super::CommandWrapper;
82    use crate::common::NoExtension;
83    use crate::domain::DomainCheck;
84    use crate::tests::{get_xml, CLTRID};
85    use crate::xml;
86
87    #[test]
88    fn client_transaction_id_is_escaped() {
89        let object = DomainCheck {
90            domains: &["eppdev.com", "eppdev.net"],
91        };
92        let document =
93            CommandWrapper::new(&object, None::<&NoExtension>, "x</clTRID><foo/><clTRID>a&b");
94
95        let expected = get_xml("request/domain/check.xml")
96            .unwrap()
97            .replace(CLTRID, "x&lt;/clTRID&gt;&lt;foo/&gt;&lt;clTRID&gt;a&amp;b");
98        assert_eq!(expected, xml::serialize(document).unwrap());
99    }
100}