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
241
242
243
use crate::*;

#[derive(Eq, PartialEq)]
enum Stage {
    Text,
    Placeholder,
}

pub fn templates(data: &mut extensions::PresentData) -> RetFut<'_, ()> {
    box_fut!({
        let bytes = handle_template(data.args(), data.response().body(), data.host()).await;
        *data.response_mut().body_mut() = bytes;
    })
}

pub async fn handle_template(
    arguments: &utils::PresentArguments,
    file: &[u8],
    host: &Host,
) -> Bytes {
    let mut file_contents = Vec::with_capacity(arguments.iter().count());
    for argument in arguments.iter().rev() {
        let file = read_template_file(argument, host).await;
        if let Some(file) = file {
            file_contents.push(file);
        }
    }

    // Get templates, from cache or file
    let templates = collect_templates(file_contents.iter().map(|b| &b[..]));

    // Remove first line if it contains "tmpl-ignore", for formatting quirks.
    let mut file = file;
    {
        let limit = 48;
        let first_line_end = file
            .iter()
            .copied()
            .enumerate()
            .position(|(pos, byte)| pos >= limit || byte == LF);

        if first_line_end.unwrap_or(0) != limit {
            if let Some(first_line_end) = first_line_end {
                if let Ok(first_line) = str::from_utf8(&file[..=first_line_end]) {
                    if first_line.contains("tmpl-ignore") {
                        file = &file[first_line_end + 1..];
                    }
                }
            }
        }
    }

    let mut response = BytesMut::with_capacity(file.len() * 3 / 2);

    let mut stage = Stage::Text;
    let mut placeholder_start = 0;
    let mut escaped = 0;
    let mut start_byte = Some(0);

    for (position, byte) in file.iter().copied().enumerate() {
        let is_escape = byte == ESCAPE;

        match stage {
            Stage::Text => {
                if file[position..].starts_with(b"$[") {
                    let previous = &file[position.saturating_sub(2)..position];
                    let a = if previous.len() == 1 {
                        None
                    } else {
                        previous.first().copied()
                    };
                    let b = previous.last().copied();
                    match (a, b) {
                        (Some(b'\\'), Some(b'\\')) => {
                            response
                                .extend_from_slice(&file[start_byte.take().unwrap()..position - 1]);
                            placeholder_start = position;
                            stage = Stage::Placeholder;
                        }
                        (_, Some(b'\\')) => {
                            response
                                .extend_from_slice(&file[start_byte.take().unwrap()..position - 1]);
                            start_byte = Some(position);
                        }
                        _ => {
                            response.extend_from_slice(&file[start_byte.take().unwrap()..position]);
                            placeholder_start = position;
                            stage = Stage::Placeholder;
                        }
                    }
                }
            }
            Stage::Placeholder if escaped != 1 => {
                // If placeholder closed
                if byte == R_SQ_BRACKET {
                    // Check if name is longer than empty
                    if position.checked_sub(placeholder_start + 3).is_some() {
                        // Good; we have UTF-8
                        if let Ok(key) = str::from_utf8(&file[placeholder_start + 2..position]) {
                            // If it is a valid template?
                            if let Some(template) = templates.get(key) {
                                response.extend_from_slice(template);
                            }
                        }
                    }
                    start_byte = Some(position + 1);
                    // Set stage to accept new text
                    stage = Stage::Text;
                }
            }
            Stage::Placeholder => {}
        }

        // Do we escape?
        if is_escape {
            escaped += 1;
            if escaped == 2 {
                escaped = 0;
            }
        } else {
            escaped = 0;
        }
    }

    if let Some(start_byte) = start_byte {
        response.extend_from_slice(&file[start_byte..]);
    }

    response.freeze()
}
fn collect_templates<'a, I: Iterator<Item = &'a [u8]>>(files: I) -> HashMap<&'a str, &'a [u8]> {
    let mut templates = HashMap::with_capacity(32);

    for file in files {
        for (key, value) in extract_templates(file).into_iter() {
            templates.insert(key, value);
        }
    }

    templates
}
async fn read_template_file<'a>(file: &str, host: &'a Host) -> Option<Bytes> {
    let path = utils::make_path(&host.path, "templates", file, None);

    // The template file will be access several times.
    match read_file_cached(&path, host.file_cache.as_ref()).await {
        Some(file) => Some(file),
        None => {
            warn!("Requested template file {:?} doesn't exist.", file);
            None
        }
    }
}
fn extract_templates(file: &[u8]) -> HashMap<&str, &[u8]> {
    let mut templates = HashMap::with_capacity(16);

    let mut stage = Stage::Text;
    let mut placeholder_start = 0;
    let mut escaped = 0;
    let mut start_byte = Some(0);
    let mut name = None;
    let mut newline_size = 1;

    for (position, byte) in file.iter().copied().enumerate() {
        if byte == CR {
            newline_size = 2;
        }
        let is_escape = byte == ESCAPE;

        match stage {
            Stage::Text => {
                if file[position..].starts_with(b"$[") {
                    let previous = &file[position.saturating_sub(2)..position];
                    let a = if previous.len() == 1 {
                        None
                    } else {
                        previous.first().copied()
                    };
                    let b = previous.last().copied();
                    let end = match (a, b) {
                        (Some(b'\\'), Some(b'\\')) => Some(position - 1),
                        (_, Some(b'\\')) => None,
                        _ => Some(position),
                    };
                    if let Some(end) = end {
                        let end = end.saturating_sub(newline_size);
                        if let Some(name) = name.take() {
                            let start = start_byte.take().unwrap();
                            templates.insert(name, &file[start..end.max(start)]);
                        }
                        placeholder_start = position;
                        stage = Stage::Placeholder;
                    }
                }
            }
            Stage::Placeholder if escaped != 1 => {
                // If placeholder closed
                if byte == R_SQ_BRACKET {
                    // Check if name is longer than empty
                    if position.checked_sub(placeholder_start + 3).is_some() {
                        // Good; we have UTF-8
                        if let Ok(key) = str::from_utf8(&file[placeholder_start + 2..position]) {
                            name = Some(key);
                        }
                    }
                    let ignore_after_name = if file.get(position + newline_size) == Some(&LF) {
                        newline_size
                    } else if file.get(position + 1) == Some(&SPACE) {
                        1
                    } else {
                        0
                    };
                    start_byte = Some(position + 1 + ignore_after_name);
                    // Set stage to accept new text
                    stage = Stage::Text;
                }
            }
            Stage::Placeholder => {}
        }

        // Do we escape?
        if is_escape {
            escaped += 1;
            if escaped == 2 {
                escaped = 0;
            }
        } else {
            escaped = 0;
        }
    }
    if let Some(name) = name.take() {
        let mut trim = 0;
        if file.get(file.len().saturating_sub(2)) == Some(&CR) {
            trim += 1;
        }
        if file.get(file.len().saturating_sub(1)) == Some(&LF) {
            trim += 1;
        }
        templates.insert(name, &file[start_byte.take().unwrap()..file.len() - trim]);
    }

    templates
}