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
use std::io::{self, Cursor, Write};
use bytes::Bytes;
use enum_as_inner::EnumAsInner;
use futures::Stream;
use log::debug;
use tempfile::NamedTempFile;
use crate::{
attribute::*,
ipp::{DelimiterTag, IppVersion, Operation},
parser::IppParseResult,
value::*,
IppHeader, IppJobSource, IppWriter, StatusCode,
};
#[derive(EnumAsInner)]
pub enum PayloadKind {
JobSource(IppJobSource),
ReceivedData(NamedTempFile),
}
pub struct IppRequestResponse {
header: IppHeader,
attributes: IppAttributes,
payload: Option<PayloadKind>,
}
impl IppRequestResponse {
pub fn new(version: IppVersion, operation: Operation, uri: Option<&str>) -> IppRequestResponse {
let hdr = IppHeader::new(version, operation as u16, 1);
let mut retval = IppRequestResponse {
header: hdr,
attributes: IppAttributes::new(),
payload: None,
};
retval.attributes_mut().add(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_CHARSET, IppValue::Charset("utf-8".to_string())),
);
retval.attributes_mut().add(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_NATURAL_LANGUAGE, IppValue::NaturalLanguage("en".to_string())),
);
if let Some(uri) = uri {
retval.attributes_mut().add(
DelimiterTag::OperationAttributes,
IppAttribute::new(PRINTER_URI, IppValue::Uri(uri.replace("http", "ipp").to_string())),
);
}
retval
}
pub fn new_response(version: IppVersion, status: StatusCode, id: u32) -> IppRequestResponse {
let hdr = IppHeader::new(version, status as u16, id);
let mut retval = IppRequestResponse {
header: hdr,
attributes: IppAttributes::new(),
payload: None,
};
retval.attributes_mut().add(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_CHARSET, IppValue::Charset("utf-8".to_string())),
);
retval.attributes_mut().add(
DelimiterTag::OperationAttributes,
IppAttribute::new(ATTRIBUTES_NATURAL_LANGUAGE, IppValue::NaturalLanguage("en".to_string())),
);
retval
}
pub fn from_parse_result(result: IppParseResult) -> IppRequestResponse {
IppRequestResponse {
header: result.header,
attributes: result.attributes,
payload: result.payload,
}
}
pub fn header(&self) -> &IppHeader {
&self.header
}
pub fn header_mut(&mut self) -> &mut IppHeader {
&mut self.header
}
pub fn attributes(&self) -> &IppAttributes {
&self.attributes
}
pub fn attributes_mut(&mut self) -> &mut IppAttributes {
&mut self.attributes
}
pub fn payload(&self) -> &Option<PayloadKind> {
&self.payload
}
pub fn payload_mut(&mut self) -> &mut Option<PayloadKind> {
&mut self.payload
}
pub fn add_payload(&mut self, payload: IppJobSource) {
self.payload = Some(PayloadKind::JobSource(payload))
}
pub fn write(&mut self, writer: &mut Write) -> io::Result<usize> {
let mut retval = self.header.write(writer)?;
retval += self.attributes.write(writer)?;
debug!("Wrote {} bytes IPP stream", retval);
Ok(retval)
}
pub fn into_stream(self) -> Box<dyn Stream<Item = Bytes, Error = io::Error> + Send + 'static> {
let mut cursor = Cursor::new(Vec::with_capacity(1024));
let _ = self
.header
.write(&mut cursor)
.and_then(|_| self.attributes.write(&mut cursor));
let headers = futures::stream::once(Ok(cursor.into_inner().into()));
match self.payload {
Some(PayloadKind::JobSource(payload)) => Box::new(headers.chain(payload)),
_ => Box::new(headers),
}
}
}