1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::{
    collections::HashMap,
    io::{BufRead, BufReader, Read, Write},
    net::TcpStream,
    path::{Path, PathBuf},
};

pub struct WildDocResult {
    body: Vec<u8>,
    options_json: String,
}
impl WildDocResult {
    pub fn body(&self) -> &[u8] {
        &self.body
    }
    pub fn options_json(&self) -> &str {
        &self.options_json
    }
}

pub struct WildDocClient {
    document_root: PathBuf,
    sock: TcpStream,
}
impl WildDocClient {
    pub fn new<P: AsRef<Path>>(host: &str, port: &str, document_root: P, dbname: &str) -> Self {
        let mut sock =
            TcpStream::connect(&(host.to_owned() + ":" + port)).expect("failed to connect server");
        sock.set_nonblocking(false).expect("out of service");
        sock.write_all(dbname.as_bytes()).unwrap();
        sock.write_all(&[0]).unwrap();

        let mut sig = Vec::new();
        let mut reader = BufReader::new(&sock);
        reader.read_until(0, &mut sig).unwrap();

        Self {
            document_root: {
                let mut path = document_root.as_ref().to_path_buf();
                path.push(dbname);
                path
            },
            sock,
        }
    }
    pub fn exec(&mut self, xml: &str, input_json: &str) -> std::io::Result<WildDocResult> {
        let mut include_cache = HashMap::new();

        if input_json.len() > 0 {
            self.sock.write_all(input_json.as_bytes())?;
        }
        self.sock.write_all(&[0])?;

        self.sock.write_all(xml.as_bytes())?;
        self.sock.write_all(&[0])?;

        let mut reader = BufReader::new(self.sock.try_clone().unwrap());
        loop {
            let mut recv_include = Vec::new();
            if reader.read_until(0, &mut recv_include)? > 0 {
                if recv_include.starts_with(b"include:") {
                    recv_include.remove(recv_include.len() - 1);
                    let mut exists = false;
                    if let Ok(str) = std::str::from_utf8(&recv_include) {
                        let s: Vec<&str> = str.split("include:/").collect();
                        if s.len() >= 2 {
                            let mut path = self.document_root.clone();
                            path.push(s[1]);
                            if let Some(include_xml) =
                                include_cache.entry(path).or_insert_with_key(|path| {
                                    match std::fs::File::open(path) {
                                        Ok(mut f) => {
                                            let mut contents = Vec::new();
                                            let _ = f.read_to_end(&mut contents);
                                            Some(contents)
                                        }
                                        _ => None,
                                    }
                                })
                            {
                                exists = true;
                                let exists: [u8; 1] = [1];
                                self.sock.write_all(&exists)?;

                                let len = include_xml.len() as u64;
                                self.sock.write_all(&len.to_be_bytes())?;
                                self.sock.write_all(&include_xml)?;
                            }
                        }
                    }
                    if !exists {
                        let exists: [u8; 1] = [0];
                        self.sock.write_all(&exists)?;
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        let mut len: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
        reader.read_exact(&mut len)?;
        let len = u64::from_be_bytes(len) as usize;

        let mut recv_body = Vec::<u8>::with_capacity(len);
        unsafe {
            recv_body.set_len(len);
        }
        reader.read_exact(recv_body.as_mut_slice())?;

        let mut recv_options = Vec::new();
        reader.read_until(0, &mut recv_options)?;
        recv_options.remove(recv_options.len() - 1);

        Ok(WildDocResult {
            body: recv_body,
            options_json: String::from_utf8(recv_options).unwrap_or("".to_owned()),
        })
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut client = WildDocClient::new("localhost", "51818", "./test/", "test");
        client
            .exec(
                r#"<wd:session name="hoge">
            <wd:update commit="1">
                <collection name="person">
                    <field name="name">Noah</field>
                    <field name="country">US</field>
                </collection>
                <collection name="person">
                    <field name="name">Liam</field>
                    <field name="country">US</field>
                </collection>
                <collection name="person">
                    <field name="name">Olivia</field>
                    <field name="country">UK</field>
                </collection>
            </wd:update>
        </wd:session>"#,
                "",
            )
            .unwrap();

        /*
        client.exec(r#"
            include-test:<wd:include src="hoge.xml" />
            <wd:search name="p" collection="person">
            </wd:search>
            OK
            <wd:result var="q" search="p">
                <div>
                    find <wd:print value:var="q.len" /> persons.
                </div>
                <ul>
                    <wd:for var="r" key="i" in:var="q.rows"><li>
                        <wd:print value:var="r.row" /> : <wd:print value:var="r.field.name" /> : <wd:print value:var="r.field.country" />
                    </li></wd:for>
                </ul>
            </wd:result>
        "#);


        client.exec(r#"
            <wd:search name="p" collection="person">
                <field name="country" method="match" value="US" />
            </wd:search>
            <wd:result var="q" search="p">
                <div>
                    find <wd:print value:var="q.len" /> persons from the US.
                </div>
                <ul>
                    <wd:for var="r" key="i" in:var="q.rows"><li>
                        <wd:print value:var="r.row" /> : <wd:print value:var="r.field.name" /> : <wd:print value:var="r.field.country" />
                    </li></wd:for>
                </ul>
            </wd:result>
        "#);
        client.exec(r#"
            <?script
                const ymd=function(){
                    const now=new Date();
                    return now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
                };
                const uk="UK";
            ?>
            <wd:search name="p" collection="person">
                <field name="country" method="match" value="uk" />
            </wd:search>
            <wd:result var="q" search="p">
                <div>
                    <wd:print value:script="ymd()" />
                </div>
                <div>
                    find <wd:print value:var="q.len" /> persons from the <wd:print value="uk" />.
                </div>
                <ul>
                    <wd:for var="r" key="i" in:var="q.rows"><li>
                        <wd:print value:var="r.row" /> : <wd:print value:var="'.field.name" /> : <wd:print value:var="r.field.country" />
                    </li></wd:for>
                </ul>
            </wd:result>
        "#);
        */
        client.exec(r#"<wd:session name="hoge">
            <wd:update commit="1">
                <wd:search name="person" collection="person"></wd:search>
                <wd:result var="q" search="person">
                    <wd:for var="r" key="i" in:var="q.rows">
                        <collection name="person" row:var="r.row">
                            <field name="name">Renamed <wd:print value:var="r.field.name" /></field>
                            <field name="country"><wd:print value:var="r.field.country" /></field>
                        </collection>
                    </wd:for>
                </wd:result>
            </wd:update>
        </wd:session>"#,"").unwrap();
        let r=client.exec(r#"
            <wd:search name="p" collection="person"></wd:search>
            <wd:result var="q" search="p">
                <div>
                    find <wd:print value:var="q.len" /> persons.
                </div>
                <ul>
                    <wd:for var="r" key="i" in:var="q.rows"><li>
                        <wd:print value:var="r.row" /> : <wd:print value:var="'r.field.name" /> : <wd:print value:var="r.field.country" />
                    </li></wd:for>
                </ul>
            </wd:result>
        "#,"").unwrap();
        println!("{}", std::str::from_utf8(&r.body()).unwrap());
    }
}