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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//!
//! Defines everything for the 0x0001 event.
//!
use std::collections::HashMap;
use bitvec::order::Msb0;
use crate::protocol::{
client::bits::utils::{FyveImpl, OperatingCode, OperationCode},
prelude::common::{
bits::{util::BitReversible, BitDecoder, Frame},
error::Error,
event::{EventDecoder, EventEncoder},
},
};
///
/// Describe a basic HTML content.
/// It can be a text or a child tag.
///
#[derive(Clone, Debug)]
pub enum HtmlContent {
/// A text element.
Text(String),
/// A child tag.
Child(HtmlTag),
}
impl HtmlContent {
///
/// Get the text of the content.
/// If the content is a child tag, it will return an empty string.
///
pub fn get_text(&self) -> String {
match self {
HtmlContent::Text(text) => text.clone(),
_ => String::new(),
}
}
///
/// Get the child tag of the content.
/// If the content is a text element, it will return an empty tag.
///
pub fn get_child(&self) -> HtmlTag {
match self {
HtmlContent::Child(tag) => tag.clone(),
_ => HtmlTag {
name: String::new(),
attributes: HashMap::new(),
data: Vec::new(),
},
}
}
}
///
/// Describe a basic HTML tag.
/// It can have a name, attributes, and data.
///
#[derive(Clone, Debug)]
pub struct HtmlTag {
/// The name of the tag.
pub name: String,
/// The attributes of the tag.
pub attributes: HashMap<String, String>,
/// The data of the tag.
pub data: Vec<HtmlContent>,
}
impl HtmlTag {
///
/// Creates a new tag.
///
pub fn new(name: String) -> Self {
HtmlTag {
name,
attributes: HashMap::new(),
data: Vec::new(),
}
}
///
/// Adds an attribute to the tag.
///
pub fn add_attribute(&mut self, name: String, value: String) {
self.attributes.insert(name, value);
}
///
/// Adds data to the tag.
///
pub fn add_data(&mut self, data: HtmlContent) {
self.data.push(data);
}
///
/// Get the name of the tag.
///
pub fn get_name(&self) -> String {
self.name.clone()
}
}
///
/// Describe a HTML file response.
///
pub struct HtmlFileResponse {
decoder: BitDecoder<Msb0>,
/// The name of the file.
pub name: String,
/// The parent tag.
pub parent: HtmlTag,
}
impl HtmlFileResponse {
///
/// Creates a new HTML file response.
/// It will decode the data and create the tags.
///
/// # Arguments
/// * `decoder` - The [`BitDecoder<Msb0>`] to decode the response.
/// * `parent` - The parent tag of the response.
///
/// # Returns
/// * [HtmlFileResponse] - The created [HtmlFileResponse].
///
/// # Example
/// ```rust
/// use shdp::prelude::client::versions::v1::r0x0001::HtmlFileResponse;
/// use shdp::prelude::common::bits::BitDecoder;
/// use bitvec::order::Msb0;
///
/// let decoder = BitDecoder::<Msb0>::new(Vec::new());
/// let response = HtmlFileResponse::new(decoder);
///
/// // These are default values.
/// assert_eq!(response.name, String::new());
/// assert_eq!(response.parent.get_name(), String::from(""));
/// ```
pub fn new(decoder: BitDecoder<Msb0>) -> Self {
if cfg!(feature = "debug") {
println!("[\x1b[38;5;187mSHDP\x1b[0m] \x1b[38;5;21m0x0001\x1b[0m received");
}
HtmlFileResponse {
decoder,
name: String::new(),
parent: HtmlTag {
name: String::new(),
attributes: HashMap::new(),
data: Vec::new(),
},
}
}
fn read_utf8_chain(&mut self, length: u32) -> Result<String, Error> {
let mut bytes = Vec::<u8>::new();
for _ in 0..length {
bytes.push(self.decoder.read_data(8)? as u8);
}
match String::from_utf8(bytes) {
Ok(name) => Ok(name),
Err(_) => {
return Err(Error {
code: 401,
message: "Invalid UTF-8".to_string(),
kind: crate::protocol::prelude::common::error::ErrorKind::BadRequest,
});
}
}
}
}
impl EventDecoder<Msb0> for HtmlFileResponse {
fn decode(
&mut self,
frame: Frame<Msb0>,
) -> Result<(), crate::protocol::prelude::common::error::Error> {
let mut bytes = Vec::<u8>::new();
let mut temp_byte: u8;
loop {
temp_byte = self.decoder.read_data(8)? as u8;
if temp_byte == 0 {
break;
}
bytes.push(temp_byte);
}
self.name = match String::from_utf8(bytes) {
Ok(name) => name,
Err(_) => {
return Err(crate::protocol::prelude::common::error::Error {
code: 400,
message: "Invalid UTF-8".to_string(),
kind: crate::protocol::prelude::common::error::ErrorKind::BadRequest,
});
}
};
let mut is_in_tag = false;
let mut is_in_attributes = false;
let mut entered_in_attributes = false;
let mut entered_in_data = false;
let mut is_in_data = false;
let mut text = String::new();
let mut attribute_name = String::new();
let mut tag_name = String::new();
let mut tags_controlled = Vec::<HtmlTag>::new();
tags_controlled.push(self.parent.clone());
loop {
if self.decoder.position >= (frame.data_size + 56).into() {
break;
}
let op_code = FyveImpl::get_op(&mut self.decoder)?;
if op_code.kind == OperatingCode::System {
match op_code.code {
Some(OperationCode::StartOfTag) => {
is_in_tag = true;
is_in_attributes = false;
is_in_data = false;
}
Some(OperationCode::StartOfAttributes) => {
is_in_tag = false;
is_in_attributes = true;
is_in_data = false;
}
Some(OperationCode::StartOfData) => {
is_in_tag = false;
is_in_attributes = false;
is_in_data = true;
}
Some(OperationCode::EndOfData) => {
is_in_tag = false;
is_in_attributes = false;
is_in_data = false;
}
Some(OperationCode::Utf8Chain) => {
let text_len = self.decoder.read_data(15)?;
text = self.read_utf8_chain(text_len)?;
}
Some(OperationCode::Unknown) => {
return Err(Error {
code: 400,
message: String::from(format!("Unknown operation code: {:?}", op_code)),
kind: crate::protocol::prelude::common::error::ErrorKind::BadRequest,
});
}
None => {
return Err(Error {
code: 400,
message: "Invalid operation code".to_string(),
kind: crate::protocol::prelude::common::error::ErrorKind::BadRequest,
});
}
}
if is_in_tag {
text = String::new();
}
if is_in_attributes && !text.is_empty() {
let tag = tags_controlled.get_mut(0).unwrap();
tag.add_attribute(attribute_name.clone(), text.clone());
attribute_name = String::new();
text = String::new();
} else if is_in_attributes && text.is_empty() && !entered_in_attributes {
let tag = HtmlTag {
name: tag_name.clone(),
attributes: HashMap::new(),
data: Vec::new(),
};
tag_name = String::new();
tags_controlled
.get_mut(0)
.unwrap()
.data
.push(HtmlContent::Child(tag.clone()));
tags_controlled.insert(0, tag);
entered_in_attributes = true;
}
if is_in_data && !text.is_empty() {
tags_controlled
.get_mut(0)
.unwrap()
.data
.push(HtmlContent::Text(text.clone()));
entered_in_data = true;
}
if is_in_data && !entered_in_attributes && !entered_in_data {
let tag = HtmlTag {
name: tag_name.clone(),
attributes: HashMap::new(),
data: Vec::new(),
};
tag_name = String::new();
// tags_controlled
// .get_mut(0)
// .unwrap()
// .data.push(HtmlContent::Child(tag.clone()));
tags_controlled.insert(0, tag);
} else if !is_in_data {
entered_in_data = false;
}
if is_in_data && entered_in_attributes {
entered_in_attributes = false;
}
if !is_in_tag && !is_in_attributes && !is_in_data {
tag_name = String::new();
attribute_name = String::new();
is_in_data = true;
if tags_controlled.len() >= 2 {
let last_tag = tags_controlled.get(0).unwrap().clone();
tags_controlled
.get_mut(1)
.unwrap()
.add_data(HtmlContent::Child(last_tag));
}
if !tags_controlled.is_empty() {
tags_controlled.remove(0);
}
}
}
if op_code.kind == OperatingCode::Character {
let char = op_code.get_char()?;
if is_in_tag {
tag_name.push(char);
}
if is_in_attributes {
attribute_name.push(char);
}
}
}
self.parent = tags_controlled.get(0).unwrap().clone();
Ok(())
}
fn get_responses(
&self,
) -> Result<Vec<Box<dyn EventEncoder<<Msb0 as BitReversible>::Opposite>>>, Error> {
Ok(Vec::new())
}
}