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
use std::fs::File;
use std::io::{BufReader, Read};
use std::time::Duration;
use num_traits::FromPrimitive;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{Body, Certificate, Client, Method};
use url::Url;
use attribute::IppAttributeList;
use consts::statuscode;
use operation::IppOperation;
use parser::IppParser;
use request::IppRequestResponse;
use {IppError, Result};
const TIMEOUT: u64 = 30;
pub struct IppClient {
uri: String,
cacerts: Vec<String>,
verify_hostname: bool,
}
impl IppClient {
pub fn new(uri: &str) -> IppClient {
IppClient {
uri: uri.to_string(),
cacerts: Vec::new(),
verify_hostname: true,
}
}
pub fn with_root_certificates<T>(uri: &str, certfiles: &[T]) -> IppClient
where
T: AsRef<str>,
{
IppClient {
uri: uri.to_string(),
cacerts: certfiles
.iter()
.map(|s| s.as_ref().to_string())
.collect::<Vec<String>>(),
verify_hostname: true,
}
}
pub fn set_verify_hostname(&mut self, verify: bool) {
self.verify_hostname = verify;
}
pub fn send<T: IppOperation>(&self, operation: T) -> Result<IppAttributeList> {
match self.send_request(operation.to_ipp_request(&self.uri)) {
Ok(resp) => {
if resp.header().operation_status > 3 {
Err(IppError::StatusError(
statuscode::StatusCode::from_u16(resp.header().operation_status)
.unwrap_or(statuscode::StatusCode::ServerErrorInternalError),
))
} else {
Ok(resp.attributes().clone())
}
}
Err(err) => Err(err),
}
}
pub fn send_request(&self, request: IppRequestResponse) -> Result<IppRequestResponse> {
match Url::parse(&self.uri) {
Ok(mut url) => {
match url.scheme() {
"ipp" => {
url.set_scheme("http")
.map_err(|_| IppError::RequestError("Invalid URI".to_string()))?;
if url.port().is_none() {
url.set_port(Some(631))
.map_err(|_| IppError::RequestError("Invalid URI".to_string()))?;
}
}
"ipps" => {
url.set_scheme("https")
.map_err(|_| IppError::RequestError("Invalid URI".to_string()))?;
if url.port().is_none() {
url.set_port(Some(631))
.map_err(|_| IppError::RequestError("Invalid URI".to_string()))?;
}
}
_ => {}
}
debug!("Request URI: {}", url);
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("application/ipp"));
let mut builder = Client::builder();
for certfile in &self.cacerts {
let mut f = File::open(&certfile)?;
let mut buf = Vec::new();
let size = f.read_to_end(&mut buf)?;
let cacert = match Certificate::from_der(&buf[0..size]) {
Ok(cacert) => {
debug!("Read DER certificate from {}", certfile);
cacert
}
Err(_) => {
let cacert = Certificate::from_pem(&buf[0..size])?;
debug!("Read PEM certificate from {}", certfile);
cacert
}
};
builder = builder.add_root_certificate(cacert);
}
if !self.verify_hostname {
debug!("Disabling hostname verification!");
builder = builder.danger_accept_invalid_hostnames(true);
}
let client = builder
.gzip(false)
.timeout(Duration::from_secs(TIMEOUT))
.build()?;
let http_req = client
.request(Method::POST, url)
.headers(headers)
.body(Body::new(request.into_reader()))
.build()?;
let http_resp = client.execute(http_req)?;
if http_resp.status().is_success() {
let mut reader = BufReader::new(http_resp);
let mut parser = IppParser::new(&mut reader);
let resp = IppRequestResponse::from_parser(&mut parser)?;
Ok(resp)
} else {
error!("HTTP error: {}", http_resp.status());
Err(IppError::RequestError(
if let Some(reason) = http_resp.status().canonical_reason() {
reason.to_string()
} else {
format!("{}", http_resp.status())
},
))
}
}
Err(err) => {
error!("Invalid URI: {}", self.uri);
Err(IppError::RequestError(err.to_string()))
}
}
}
}