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
use std::io::{self, Cursor, Read, Write};
use attribute::{IppAttribute, IppAttributeList};
use consts::attribute::{ATTRIBUTES_CHARSET, ATTRIBUTES_NATURAL_LANGUAGE, PRINTER_URI};
use consts::operation::Operation;
use consts::tag::DelimiterTag;
use parser::IppParser;
use value::IppValue;
use {IppHeader, Result, IPP_VERSION};
pub struct IppRequestResponse {
header: IppHeader,
attributes: IppAttributeList,
payload: Option<Box<Read>>,
}
pub struct IppReadAdapter {
data: Box<Read>,
payload: Option<Box<Read>>,
}
unsafe impl Send for IppReadAdapter {}
impl Read for IppReadAdapter {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.data.read(buf)? {
0 => {
if let Some(ref mut payload) = self.payload {
payload.read(buf)
} else {
Ok(0)
}
}
rc => Ok(rc),
}
}
}
pub trait IppRequestTrait {
fn header(&self) -> &IppHeader;
}
impl IppRequestTrait for IppRequestResponse {
fn header(&self) -> &IppHeader {
&self.header
}
}
impl IppRequestResponse {
pub fn new(operation: Operation, uri: &str) -> IppRequestResponse {
let hdr = IppHeader::new(IPP_VERSION, operation as u16, 1);
let mut retval = IppRequestResponse {
header: hdr,
attributes: IppAttributeList::new(),
payload: None,
};
retval.set_attribute(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_CHARSET, IppValue::Charset("utf-8".to_string())),
);
retval.set_attribute(
DelimiterTag::OperationAttributes,
IppAttribute::new(
ATTRIBUTES_NATURAL_LANGUAGE,
IppValue::NaturalLanguage("en".to_string()),
),
);
retval.set_attribute(
DelimiterTag::OperationAttributes,
IppAttribute::new(
PRINTER_URI,
IppValue::Uri(uri.replace("http", "ipp").to_string()),
),
);
retval
}
pub fn new_response(status: u16, id: u32) -> IppRequestResponse {
let hdr = IppHeader::new(IPP_VERSION, status, id);
let mut retval = IppRequestResponse {
header: hdr,
attributes: IppAttributeList::new(),
payload: None,
};
retval.set_attribute(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_CHARSET, IppValue::Charset("utf-8".to_string())),
);
retval.set_attribute(
DelimiterTag::OperationAttributes,
IppAttribute::new(
ATTRIBUTES_NATURAL_LANGUAGE,
IppValue::NaturalLanguage("en".to_string()),
),
);
retval
}
pub fn from_parser(parser: &mut IppParser) -> Result<IppRequestResponse> {
let res = parser.parse()?;
Ok(IppRequestResponse {
header: res.header().clone(),
attributes: res.attributes().clone(),
payload: None,
})
}
pub fn header(&self) -> &IppHeader {
&self.header
}
pub fn header_mut(&mut self) -> &mut IppHeader {
&mut self.header
}
pub fn attributes(&self) -> &IppAttributeList {
&self.attributes
}
pub fn payload(&self) -> &Option<Box<Read>> {
&self.payload
}
pub fn set_payload(&mut self, payload: Box<Read>) {
self.payload = Some(payload)
}
pub fn set_attribute(&mut self, group: DelimiterTag, attribute: IppAttribute) {
self.attributes.add(group, attribute);
}
pub fn write(&mut self, writer: &mut Write) -> Result<usize> {
let mut retval = self.header.write(writer)?;
retval += self.attributes.write(writer)?;
debug!("Wrote {} bytes IPP stream", retval);
if let Some(ref mut payload) = self.payload {
let size = io::copy(payload, writer)? as usize;
debug!("Wrote {} bytes payload", size);
retval += size;
}
Ok(retval)
}
pub fn into_reader(self) -> IppReadAdapter {
let mut cursor = Cursor::new(Vec::with_capacity(1024));
let _ = self.header.write(&mut cursor).unwrap();
let _ = self.attributes.write(&mut cursor).unwrap();
cursor.set_position(0);
IppReadAdapter {
data: Box::new(cursor),
payload: self.payload,
}
}
}