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
|
mod get;
mod set;
use log;
use std::io::{Read, Write};
use super::{ConfigError, Configuration};
use get::serialize;
use set::LineParser;
const MAX_LINE_LENGTH: usize = 256;
pub fn handle<S: Read + Write, C: Configuration>(stream: &mut S, config: &C) {
fn operation<S: Read + Write, C: Configuration>(
stream: &mut S,
config: &C,
) -> Result<(), ConfigError> {
// read string up to maximum length (why is this not in std?)
fn readline<R: Read>(reader: &mut R) -> Result<String, ConfigError> {
let mut m: [u8; 1] = [0u8];
let mut l: String = String::with_capacity(MAX_LINE_LENGTH);
while let Ok(_) = reader.read_exact(&mut m) {
let c = m[0] as char;
if c == '\n' {
log::trace!("UAPI, line: {}", l);
return Ok(l);
};
l.push(c);
if l.len() > MAX_LINE_LENGTH {
return Err(ConfigError::LineTooLong);
}
}
return Err(ConfigError::IOError);
}
// split into (key, value) pair
fn keypair<'a>(ln: &'a str) -> Result<(&'a str, &'a str), ConfigError> {
let mut split = ln.splitn(2, "=");
match (split.next(), split.next()) {
(Some(key), Some(value)) => Ok((key, value)),
_ => Err(ConfigError::LineTooLong),
}
};
// read operation line
match readline(stream)?.as_str() {
"get=1" => {
log::debug!("UAPI, Get operation");
serialize(stream, config).map_err(|_| ConfigError::IOError)
}
"set=1" => {
log::debug!("UAPI, Set operation");
let mut parser = LineParser::new(config);
loop {
let ln = readline(stream)?;
if ln == "" {
break Ok(());
};
let (k, v) = keypair(ln.as_str())?;
parser.parse_line(k, v)?;
}
}
_ => Err(ConfigError::InvalidOperation),
}
}
// process operation
let res = operation(stream, config);
log::debug!("UAPI, Result of operation: {:?}", res);
// return errno
let _ = stream.write("errno=".as_ref());
let _ = stream.write(
match res {
Err(e) => e.errno().to_string(),
Ok(()) => "0".to_owned(),
}
.as_ref(),
);
let _ = stream.write("\n\n".as_ref());
}
|