]> git.proxmox.com Git - proxmox-backup.git/blame - src/client/http_client.rs
avoid compiler warnings
[proxmox-backup.git] / src / client / http_client.rs
CommitLineData
597641fd
DM
1use failure::*;
2
3use http::Uri;
4use hyper::Body;
5use hyper::client::Client;
ba3a60b2
DM
6use xdg::BaseDirectories;
7use chrono::Utc;
553610b4
DM
8use std::collections::HashSet;
9use std::sync::{Arc, Mutex};
597641fd 10
b57cb264 11use http::{Request, Response};
5a2df000
DM
12use http::header::HeaderValue;
13
82ab7230 14use futures::*;
1fdb4c6f 15use futures::stream::Stream;
82ab7230
DM
16use std::sync::atomic::{AtomicUsize, Ordering};
17use tokio::sync::mpsc;
1fdb4c6f 18
ba3a60b2 19use serde_json::{json, Value};
0dffe3f9 20use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
1fdb4c6f 21
5a2df000 22use crate::tools::{self, BroadcastFuture, tty};
e3dbd41b 23use super::pipe_to_stream::*;
aa1b2e04
DM
24use super::merge_known_chunks::*;
25
5a2df000
DM
26
27#[derive(Clone)]
28struct AuthInfo {
29 username: String,
30 ticket: String,
31 token: String,
32}
56458d97 33
151c6ce2 34/// HTTP(S) API client
597641fd 35pub struct HttpClient {
5a2df000 36 client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
597641fd 37 server: String,
5a2df000 38 auth: BroadcastFuture<AuthInfo>,
597641fd
DM
39}
40
ba3a60b2
DM
41fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) -> Result<(), Error> {
42
43 let base = BaseDirectories::with_prefix("proxmox-backup")?;
44
45 // usually /run/user/<uid>/...
46 let path = base.place_runtime_file("tickets")?;
47
48 let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
49
49cf9f3d 50 let mut data = tools::file_get_json(&path, Some(json!({})))?;
ba3a60b2
DM
51
52 let now = Utc::now().timestamp();
53
54 data[server][username] = json!({ "timestamp": now, "ticket": ticket, "token": token});
55
56 let mut new_data = json!({});
57
58 let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
59
60 let empty = serde_json::map::Map::new();
61 for (server, info) in data.as_object().unwrap_or(&empty) {
62 for (_user, uinfo) in info.as_object().unwrap_or(&empty) {
63 if let Some(timestamp) = uinfo["timestamp"].as_i64() {
64 let age = now - timestamp;
65 if age < ticket_lifetime {
66 new_data[server][username] = uinfo.clone();
67 }
68 }
69 }
70 }
71
72 tools::file_set_contents(path, new_data.to_string().as_bytes(), Some(mode))?;
73
74 Ok(())
75}
76
77fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
78 let base = match BaseDirectories::with_prefix("proxmox-backup") {
79 Ok(b) => b,
80 _ => return None,
81 };
82
83 // usually /run/user/<uid>/...
84 let path = match base.place_runtime_file("tickets") {
85 Ok(p) => p,
86 _ => return None,
87 };
88
49cf9f3d
DM
89 let data = match tools::file_get_json(&path, None) {
90 Ok(v) => v,
91 _ => return None,
92 };
ba3a60b2
DM
93
94 let now = Utc::now().timestamp();
95
96 let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
97
98 if let Some(uinfo) = data[server][username].as_object() {
99 if let Some(timestamp) = uinfo["timestamp"].as_i64() {
100 let age = now - timestamp;
101 if age < ticket_lifetime {
102 let ticket = match uinfo["ticket"].as_str() {
103 Some(t) => t,
104 None => return None,
105 };
106 let token = match uinfo["token"].as_str() {
107 Some(t) => t,
108 None => return None,
21ea0158 109 };
ba3a60b2
DM
110 return Some((ticket.to_owned(), token.to_owned()));
111 }
112 }
113 }
114
115 None
116}
117
597641fd
DM
118impl HttpClient {
119
45cdce06 120 pub fn new(server: &str, username: &str) -> Result<Self, Error> {
5a2df000 121 let client = Self::build_client();
5a2df000 122
45cdce06
DM
123 let password = if let Some((ticket, _token)) = load_ticket_info(server, username) {
124 ticket
125 } else {
126 Self::get_password(&username)?
127 };
128
129 let login = Self::credentials(client.clone(), server.to_owned(), username.to_owned(), password);
130
131 Ok(Self {
5a2df000 132 client,
597641fd 133 server: String::from(server),
5a2df000 134 auth: BroadcastFuture::new(login),
45cdce06 135 })
597641fd
DM
136 }
137
5a2df000 138 fn get_password(_username: &str) -> Result<String, Error> {
56458d97
WB
139 use std::env::VarError::*;
140 match std::env::var("PBS_PASSWORD") {
141 Ok(p) => return Ok(p),
142 Err(NotUnicode(_)) => bail!("PBS_PASSWORD contains bad characters"),
143 Err(NotPresent) => {
144 // Try another method
145 }
146 }
147
148 // If we're on a TTY, query the user for a password
149 if tty::stdin_isatty() {
150 return Ok(String::from_utf8(tty::read_password("Password: ")?)?);
151 }
152
153 bail!("no password input mechanism available");
154 }
155
5a2df000 156 fn build_client() -> Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>> {
4a3f6517
WB
157 let mut builder = native_tls::TlsConnector::builder();
158 // FIXME: We need a CLI option for this!
159 builder.danger_accept_invalid_certs(true);
5a2df000 160 let tlsconnector = builder.build().unwrap();
4a3f6517 161 let mut httpc = hyper::client::HttpConnector::new(1);
9c9ad941 162 //httpc.set_nodelay(true); // not sure if this help?
4a3f6517
WB
163 httpc.enforce_http(false); // we want https...
164 let mut https = hyper_tls::HttpsConnector::from((httpc, tlsconnector));
165 https.https_only(true); // force it!
adec8ea2
DM
166 Client::builder()
167 //.http2_initial_stream_window_size( (1 << 31) - 2)
168 //.http2_initial_connection_window_size( (1 << 31) - 2)
169 .build::<_, Body>(https)
a6b75513
DM
170 }
171
5a2df000 172 pub fn request(&self, mut req: Request<Body>) -> impl Future<Item=Value, Error=Error> {
597641fd 173
5a2df000 174 let login = self.auth.listen();
597641fd 175
5a2df000 176 let client = self.client.clone();
597641fd 177
5a2df000 178 login.and_then(move |auth| {
597641fd 179
5a2df000
DM
180 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
181 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
182 req.headers_mut().insert("CSRFPreventionToken", HeaderValue::from_str(&auth.token).unwrap());
597641fd 183
5a2df000 184 let request = Self::api_request(client, req);
597641fd 185
5a2df000
DM
186 request
187 })
1fdb4c6f
DM
188 }
189
9e391bb7 190 pub fn get(&self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
a6b75513 191
9e391bb7 192 let req = Self::request_builder(&self.server, "GET", path, data).unwrap();
5a2df000 193 self.request(req)
a6b75513
DM
194 }
195
9e391bb7 196 pub fn delete(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
a6b75513 197
9e391bb7 198 let req = Self::request_builder(&self.server, "DELETE", path, data).unwrap();
5a2df000 199 self.request(req)
a6b75513
DM
200 }
201
5a2df000 202 pub fn post(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
024f11bb 203
5a2df000
DM
204 let req = Self::request_builder(&self.server, "POST", path, data).unwrap();
205 self.request(req)
024f11bb
DM
206 }
207
5a2df000 208 pub fn download(&mut self, path: &str, mut output: Box<dyn std::io::Write + Send>) -> impl Future<Item=(), Error=Error> {
024f11bb 209
5a2df000 210 let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap();
024f11bb 211
5a2df000 212 let login = self.auth.listen();
024f11bb 213
5a2df000 214 let client = self.client.clone();
1fdb4c6f 215
5a2df000 216 login.and_then(move |auth| {
81da38c1 217
5a2df000
DM
218 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
219 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
6f62c924 220
5a2df000
DM
221 client.request(req)
222 .map_err(Error::from)
223 .and_then(|resp| {
6f62c924 224
5a2df000 225 let _status = resp.status(); // fixme: ??
6f62c924 226
5a2df000
DM
227 resp.into_body()
228 .map_err(Error::from)
229 .for_each(move |chunk| {
230 output.write_all(&chunk)?;
231 Ok(())
232 })
6f62c924 233
5a2df000
DM
234 })
235 })
6f62c924
DM
236 }
237
5a2df000 238 pub fn upload(&mut self, content_type: &str, body: Body, path: &str) -> impl Future<Item=Value, Error=Error> {
81da38c1
DM
239
240 let path = path.trim_matches('/');
5a2df000 241 let url: Uri = format!("https://{}:8007/{}", &self.server, path).parse().unwrap();
81da38c1 242
5a2df000 243 let req = Request::builder()
81da38c1
DM
244 .method("POST")
245 .uri(url)
246 .header("User-Agent", "proxmox-backup-client/1.0")
5a2df000
DM
247 .header("Content-Type", content_type)
248 .body(body).unwrap();
81da38c1 249
5a2df000 250 self.request(req)
1fdb4c6f
DM
251 }
252
6ab34afa
DM
253 pub fn start_backup(
254 &self,
255 datastore: &str,
256 backup_type: &str,
257 backup_id: &str,
39e60bd6 258 debug: bool,
6ab34afa 259 ) -> impl Future<Item=BackupClient, Error=Error> {
cf639a47 260
6ab34afa 261 let path = format!("/api2/json/admin/datastore/{}/backup", datastore);
39e60bd6 262 let param = json!({"backup-type": backup_type, "backup-id": backup_id, "debug": debug});
6ab34afa 263 let mut req = Self::request_builder(&self.server, "GET", &path, Some(param)).unwrap();
cf639a47
DM
264
265 let login = self.auth.listen();
266
267 let client = self.client.clone();
268
269 login.and_then(move |auth| {
270
271 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
272 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
273 req.headers_mut().insert("UPGRADE", HeaderValue::from_str("proxmox-backup-protocol-h2").unwrap());
274
275 client.request(req)
276 .map_err(Error::from)
277 .and_then(|resp| {
278
279 let status = resp.status();
280 if status != http::StatusCode::SWITCHING_PROTOCOLS {
9af37c8f
DM
281 future::Either::A(Self::api_response(resp).and_then(|_| { bail!("unknown error"); }))
282 } else {
283 future::Either::B(resp.into_body().on_upgrade().map_err(Error::from))
cf639a47 284 }
cf639a47 285 })
cf639a47 286 .and_then(|upgraded| {
cf639a47
DM
287 h2::client::handshake(upgraded).map_err(Error::from)
288 })
289 .and_then(|(h2, connection)| {
290 let connection = connection
291 .map_err(|_| panic!("HTTP/2.0 connection failed"));
292
293 // Spawn a new task to drive the connection state
294 hyper::rt::spawn(connection);
295
296 // Wait until the `SendRequest` handle has available capacity.
850ac6d0 297 h2.ready()
6ab34afa 298 .map(BackupClient::new)
850ac6d0 299 .map_err(Error::from)
cf639a47
DM
300 })
301 })
302 }
303
5a2df000
DM
304 fn credentials(
305 client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
45cdce06
DM
306 server: String,
307 username: String,
308 password: String,
5a2df000 309 ) -> Box<Future<Item=AuthInfo, Error=Error> + Send> {
0ffbccce 310
45cdce06 311 let server2 = server.clone();
0ffbccce 312
5a2df000 313 let create_request = futures::future::lazy(move || {
45cdce06 314 let data = json!({ "username": username, "password": password });
5a2df000 315 let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
45cdce06 316 Self::api_request(client, req)
5a2df000 317 });
0dffe3f9 318
5a2df000
DM
319 let login_future = create_request
320 .and_then(move |cred| {
321 let auth = AuthInfo {
322 username: cred["data"]["username"].as_str().unwrap().to_owned(),
323 ticket: cred["data"]["ticket"].as_str().unwrap().to_owned(),
324 token: cred["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
325 };
0dffe3f9 326
5a2df000 327 let _ = store_ticket_info(&server2, &auth.username, &auth.ticket, &auth.token);
0dffe3f9 328
5a2df000
DM
329 Ok(auth)
330 });
0dffe3f9 331
5a2df000 332 Box::new(login_future)
ba3a60b2
DM
333 }
334
d2c48afc
DM
335 fn api_response(response: Response<Body>) -> impl Future<Item=Value, Error=Error> {
336
337 let status = response.status();
338
339 response
340 .into_body()
341 .concat2()
342 .map_err(Error::from)
343 .and_then(move |data| {
344
345 let text = String::from_utf8(data.to_vec()).unwrap();
346 if status.is_success() {
347 if text.len() > 0 {
348 let value: Value = serde_json::from_str(&text)?;
349 Ok(value)
350 } else {
351 Ok(Value::Null)
352 }
353 } else {
354 bail!("HTTP Error {}: {}", status, text);
355 }
356 })
357 }
358
5a2df000
DM
359 fn api_request(
360 client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
361 req: Request<Body>
362 ) -> impl Future<Item=Value, Error=Error> {
ba3a60b2 363
5a2df000
DM
364 client.request(req)
365 .map_err(Error::from)
d2c48afc 366 .and_then(Self::api_response)
0dffe3f9
DM
367 }
368
5a2df000 369 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> {
591f570b 370 let path = path.trim_matches('/');
5a2df000
DM
371 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
372
373 if let Some(data) = data {
374 if method == "POST" {
375 let request = Request::builder()
376 .method(method)
377 .uri(url)
378 .header("User-Agent", "proxmox-backup-client/1.0")
379 .header(hyper::header::CONTENT_TYPE, "application/json")
380 .body(Body::from(data.to_string()))?;
381 return Ok(request);
382 } else {
9e391bb7
DM
383 let query = tools::json_object_to_query(data)?;
384 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
385 let request = Request::builder()
386 .method(method)
387 .uri(url)
388 .header("User-Agent", "proxmox-backup-client/1.0")
389 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
390 .body(Body::empty())?;
391 return Ok(request);
5a2df000 392 }
5a2df000 393 }
0dffe3f9 394
1fdb4c6f 395 let request = Request::builder()
5a2df000 396 .method(method)
1fdb4c6f
DM
397 .uri(url)
398 .header("User-Agent", "proxmox-backup-client/1.0")
5a2df000
DM
399 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
400 .body(Body::empty())?;
1fdb4c6f 401
5a2df000 402 Ok(request)
597641fd
DM
403 }
404}
b57cb264 405
6ab34afa
DM
406//#[derive(Clone)]
407pub struct BackupClient {
9af37c8f 408 h2: H2Client,
b57cb264
DM
409}
410
91320f08 411
6ab34afa 412impl BackupClient {
b57cb264
DM
413
414 pub fn new(h2: h2::client::SendRequest<bytes::Bytes>) -> Self {
9af37c8f 415 Self { h2: H2Client::new(h2) }
b57cb264
DM
416 }
417
418 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
9af37c8f 419 self.h2.get(path, param)
b57cb264
DM
420 }
421
82ab7230 422 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
9af37c8f 423 self.h2.put(path, param)
82ab7230
DM
424 }
425
b57cb264 426 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
9af37c8f 427 self.h2.post(path, param)
97f22ce5
DM
428 }
429
d6f204ed
DM
430 pub fn finish(&self) -> impl Future<Item=(), Error=Error> {
431 self.h2.clone().post("finish", None).map(|_| ())
432 }
433
a42fa400 434 pub fn upload_stream(
d6f204ed
DM
435 &self,
436 archive_name: &str,
437 stream: impl Stream<Item=bytes::BytesMut, Error=Error>,
a42fa400
DM
438 prefix: &str,
439 fixed_size: Option<u64>,
d6f204ed
DM
440 ) -> impl Future<Item=(), Error=Error> {
441
442 let known_chunks = Arc::new(Mutex::new(HashSet::new()));
443
91320f08
DM
444 let mut stream_len = 0u64;
445
446 let stream = stream.
447 map(move |data| {
448 let digest = openssl::sha::sha256(&data);
3dc5b2a2 449 let offset = stream_len;
91320f08 450 stream_len += data.len() as u64;
3dc5b2a2 451 ChunkInfo { data, digest, offset }
91320f08
DM
452 });
453
d6f204ed
DM
454 let h2 = self.h2.clone();
455 let h2_2 = self.h2.clone();
456 let h2_3 = self.h2.clone();
457 let h2_4 = self.h2.clone();
458
a42fa400
DM
459 let mut param = json!({ "archive-name": archive_name });
460 if let Some(size) = fixed_size {
461 param["size"] = size.into();
462 }
463
464 let index_path = format!("{}_index", prefix);
a42fa400 465 let close_path = format!("{}_close", prefix);
d6f204ed 466
642322b4
DM
467 let prefix = prefix.to_owned();
468
a42fa400 469 Self::download_chunk_list(h2, &index_path, archive_name, known_chunks.clone())
d6f204ed 470 .and_then(move |_| {
a42fa400 471 h2_2.post(&index_path, Some(param))
d6f204ed
DM
472 })
473 .and_then(move |res| {
474 let wid = res.as_u64().unwrap();
642322b4 475 Self::upload_chunk_info_stream(h2_3, wid, stream, &prefix, known_chunks.clone())
417cb073 476 .and_then(move |(chunk_count, size, _speed)| {
8bea85b4
DM
477 let param = json!({
478 "wid": wid ,
479 "chunk-count": chunk_count,
480 "size": size,
481 });
a42fa400
DM
482 h2_4.post(&close_path, Some(param))
483 })
d6f204ed
DM
484 .map(|_| ())
485 })
486 }
487
6ab34afa 488 fn response_queue() -> (
82ab7230
DM
489 mpsc::Sender<h2::client::ResponseFuture>,
490 sync::oneshot::Receiver<Result<(), Error>>
491 ) {
492 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(100);
493 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
adec8ea2 494
82ab7230
DM
495 hyper::rt::spawn(
496 verify_queue_rx
497 .map_err(Error::from)
498 .for_each(|response: h2::client::ResponseFuture| {
499 response
500 .map_err(Error::from)
9af37c8f 501 .and_then(H2Client::h2api_response)
82ab7230
DM
502 .and_then(|result| {
503 println!("RESPONSE: {:?}", result);
504 Ok(())
505 })
506 .map_err(|err| format_err!("pipelined request failed: {}", err))
507 })
508 .then(|result|
509 verify_result_tx.send(result)
510 )
511 .map_err(|_| { /* ignore closed channel */ })
512 );
adec8ea2 513
82ab7230
DM
514 (verify_queue_tx, verify_result_rx)
515 }
516
642322b4 517 fn append_chunk_queue(h2: H2Client, wid: u64, path: String) -> (
174ad378 518 mpsc::Sender<(MergedChunkInfo, Option<h2::client::ResponseFuture>)>,
05cba08c
DM
519 sync::oneshot::Receiver<Result<(), Error>>
520 ) {
771953f9 521 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(64);
05cba08c
DM
522 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
523
524 let h2_2 = h2.clone();
525
526 hyper::rt::spawn(
527 verify_queue_rx
528 .map_err(Error::from)
174ad378
DM
529 .and_then(move |(merged_chunk_info, response): (MergedChunkInfo, Option<h2::client::ResponseFuture>)| {
530 match (response, merged_chunk_info) {
531 (Some(response), MergedChunkInfo::Known(list)) => {
05cba08c 532 future::Either::A(
174ad378
DM
533 response
534 .map_err(Error::from)
535 .and_then(H2Client::h2api_response)
771953f9 536 .and_then(move |_result| {
174ad378 537 Ok(MergedChunkInfo::Known(list))
05cba08c 538 })
05cba08c
DM
539 )
540 }
174ad378 541 (None, MergedChunkInfo::Known(list)) => {
05cba08c
DM
542 future::Either::B(future::ok(MergedChunkInfo::Known(list)))
543 }
174ad378 544 _ => unreachable!(),
05cba08c
DM
545 }
546 })
62436222 547 .merge_known_chunks()
05cba08c
DM
548 .and_then(move |merged_chunk_info| {
549 match merged_chunk_info {
550 MergedChunkInfo::Known(chunk_list) => {
551 let mut digest_list = vec![];
552 let mut offset_list = vec![];
553 for (offset, digest) in chunk_list {
554 //println!("append chunk {} (offset {})", tools::digest_to_hex(&digest), offset);
555 digest_list.push(tools::digest_to_hex(&digest));
556 offset_list.push(offset);
557 }
558 println!("append chunks list len ({})", digest_list.len());
559 let param = json!({ "wid": wid, "digest-list": digest_list, "offset-list": offset_list });
a42fa400 560 let mut request = H2Client::request_builder("localhost", "PUT", &path, None).unwrap();
05cba08c
DM
561 request.headers_mut().insert(hyper::header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
562 let param_data = bytes::Bytes::from(param.to_string().as_bytes());
563 let upload_data = Some(param_data);
564 h2_2.send_request(request, upload_data)
565 .and_then(move |response| {
566 response
567 .map_err(Error::from)
568 .and_then(H2Client::h2api_response)
569 .and_then(|_| Ok(()))
570 })
571 .map_err(|err| format_err!("pipelined request failed: {}", err))
572 }
573 _ => unreachable!(),
574 }
575 })
576 .for_each(|_| Ok(()))
577 .then(|result|
578 verify_result_tx.send(result)
579 )
580 .map_err(|_| { /* ignore closed channel */ })
581 );
582
583 (verify_queue_tx, verify_result_rx)
584 }
585
6ab34afa 586 fn download_chunk_list(
9af37c8f 587 h2: H2Client,
553610b4
DM
588 path: &str,
589 archive_name: &str,
590 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
591 ) -> impl Future<Item=(), Error=Error> {
592
593 let param = json!({ "archive-name": archive_name });
9af37c8f 594 let request = H2Client::request_builder("localhost", "GET", path, Some(param)).unwrap();
553610b4 595
9af37c8f 596 h2.send_request(request, None)
553610b4
DM
597 .and_then(move |response| {
598 response
599 .map_err(Error::from)
600 .and_then(move |resp| {
601 let status = resp.status();
7dd1bcac 602
553610b4 603 if !status.is_success() {
7dd1bcac
DM
604 future::Either::A(H2Client::h2api_response(resp).and_then(|_| { bail!("unknown error"); }))
605 } else {
606 future::Either::B(future::ok(resp.into_body()))
553610b4 607 }
553610b4
DM
608 })
609 .and_then(move |mut body| {
610
611 let mut release_capacity = body.release_capacity().clone();
612
613 crate::backup::DigestListDecoder::new(body.map_err(Error::from))
614 .for_each(move |chunk| {
615 let _ = release_capacity.release_capacity(chunk.len());
616 println!("GOT DOWNLOAD {}", tools::digest_to_hex(&chunk));
617 known_chunks.lock().unwrap().insert(chunk);
618 Ok(())
619 })
620 })
621 })
622 }
623
a42fa400 624 fn upload_chunk_info_stream(
9af37c8f 625 h2: H2Client,
82ab7230 626 wid: u64,
91320f08 627 stream: impl Stream<Item=ChunkInfo, Error=Error>,
642322b4 628 prefix: &str,
553610b4 629 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
8bea85b4 630 ) -> impl Future<Item=(usize, usize, usize), Error=Error> {
adec8ea2 631
82ab7230
DM
632 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
633 let repeat2 = repeat.clone();
adec8ea2 634
82ab7230
DM
635 let stream_len = std::sync::Arc::new(AtomicUsize::new(0));
636 let stream_len2 = stream_len.clone();
9c9ad941 637
642322b4
DM
638 let append_chunk_path = format!("{}_index", prefix);
639 let upload_chunk_path = format!("{}_chunk", prefix);
640
641 let (upload_queue, upload_result) = Self::append_chunk_queue(h2.clone(), wid, append_chunk_path.to_owned());
9c9ad941 642
82ab7230 643 let start_time = std::time::Instant::now();
9c9ad941 644
82ab7230 645 stream
aa1b2e04 646 .map(move |chunk_info| {
82ab7230 647 repeat.fetch_add(1, Ordering::SeqCst);
91320f08 648 stream_len.fetch_add(chunk_info.data.len(), Ordering::SeqCst);
62436222
DM
649
650 let mut known_chunks = known_chunks.lock().unwrap();
651 let chunk_is_known = known_chunks.contains(&chunk_info.digest);
652 if chunk_is_known {
653 MergedChunkInfo::Known(vec![(chunk_info.offset, chunk_info.digest)])
654 } else {
655 known_chunks.insert(chunk_info.digest);
656 MergedChunkInfo::New(chunk_info)
657 }
aa1b2e04 658 })
62436222 659 .merge_known_chunks()
aa1b2e04 660 .for_each(move |merged_chunk_info| {
174ad378
DM
661
662 if let MergedChunkInfo::New(chunk_info) = merged_chunk_info {
663 let offset = chunk_info.offset;
664 let digest = chunk_info.digest;
665 let upload_queue = upload_queue.clone();
666
667 println!("upload new chunk {} ({} bytes, offset {})", tools::digest_to_hex(&digest),
668 chunk_info.data.len(), offset);
669
642322b4
DM
670 let param = json!({ "wid": wid, "size" : chunk_info.data.len() });
671 let request = H2Client::request_builder("localhost", "POST", &upload_chunk_path, Some(param)).unwrap();
174ad378
DM
672 let upload_data = Some(chunk_info.data.freeze());
673
8de20e5c 674 let new_info = MergedChunkInfo::Known(vec![(offset, digest)]);
174ad378
DM
675
676 future::Either::A(
677 h2.send_request(request, upload_data)
678 .and_then(move |response| {
679 upload_queue.clone().send((new_info, Some(response)))
680 .map(|_| ()).map_err(Error::from)
681 })
682 )
683 } else {
684
685 future::Either::B(
686 upload_queue.clone().send((merged_chunk_info, None))
687 .map(|_| ()).map_err(Error::from)
688 )
689 }
82ab7230
DM
690 })
691 .then(move |result| {
692 println!("RESULT {:?}", result);
693 upload_result.map_err(Error::from).and_then(|upload1_result| {
694 Ok(upload1_result.and(result))
695 })
696 })
697 .flatten()
698 .and_then(move |_| {
699 let repeat = repeat2.load(Ordering::SeqCst);
700 let stream_len = stream_len2.load(Ordering::SeqCst);
701 let speed = ((stream_len*1000000)/(1024*1024))/(start_time.elapsed().as_micros() as usize);
702 println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), speed);
703 if repeat > 0 {
704 println!("Average chunk size was {} bytes.", stream_len/repeat);
705 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
706 }
8bea85b4 707 Ok((repeat, stream_len, speed))
82ab7230
DM
708 })
709 }
710
711 pub fn upload_speedtest(&self) -> impl Future<Item=usize, Error=Error> {
712
713 let mut data = vec![];
714 // generate pseudo random byte sequence
715 for i in 0..1024*1024 {
716 for j in 0..4 {
717 let byte = ((i >> (j<<3))&0xff) as u8;
718 data.push(byte);
719 }
720 }
721
722 let item_len = data.len();
723
724 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
725 let repeat2 = repeat.clone();
726
6ab34afa 727 let (upload_queue, upload_result) = Self::response_queue();
82ab7230
DM
728
729 let start_time = std::time::Instant::now();
730
6ab34afa 731 let h2 = self.h2.clone();
82ab7230
DM
732
733 futures::stream::repeat(data)
734 .take_while(move |_| {
735 repeat.fetch_add(1, Ordering::SeqCst);
736 Ok(start_time.elapsed().as_secs() < 5)
737 })
738 .for_each(move |data| {
6ab34afa 739 let h2 = h2.clone();
82ab7230
DM
740
741 let upload_queue = upload_queue.clone();
742
743 println!("send test data ({} bytes)", data.len());
9af37c8f
DM
744 let request = H2Client::request_builder("localhost", "POST", "speedtest", None).unwrap();
745 h2.send_request(request, Some(bytes::Bytes::from(data)))
82ab7230
DM
746 .and_then(move |response| {
747 upload_queue.send(response)
748 .map(|_| ()).map_err(Error::from)
adec8ea2
DM
749 })
750 })
82ab7230
DM
751 .then(move |result| {
752 println!("RESULT {:?}", result);
753 upload_result.map_err(Error::from).and_then(|upload1_result| {
754 Ok(upload1_result.and(result))
755 })
756 })
757 .flatten()
758 .and_then(move |_| {
759 let repeat = repeat2.load(Ordering::SeqCst);
760 println!("Uploaded {} chunks in {} seconds.", repeat, start_time.elapsed().as_secs());
761 let speed = ((item_len*1000000*(repeat as usize))/(1024*1024))/(start_time.elapsed().as_micros() as usize);
762 if repeat > 0 {
763 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
764 }
765 Ok(speed)
766 })
adec8ea2 767 }
9af37c8f
DM
768}
769
770#[derive(Clone)]
771pub struct H2Client {
772 h2: h2::client::SendRequest<bytes::Bytes>,
773}
774
775impl H2Client {
776
777 pub fn new(h2: h2::client::SendRequest<bytes::Bytes>) -> Self {
778 Self { h2 }
779 }
780
781 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
782 let req = Self::request_builder("localhost", "GET", path, param).unwrap();
783 self.request(req)
784 }
785
786 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
787 let req = Self::request_builder("localhost", "PUT", path, param).unwrap();
788 self.request(req)
789 }
790
791 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
792 let req = Self::request_builder("localhost", "POST", path, param).unwrap();
793 self.request(req)
794 }
795
796 pub fn upload(&self, path: &str, param: Option<Value>, data: Vec<u8>) -> impl Future<Item=Value, Error=Error> {
797 let request = Self::request_builder("localhost", "POST", path, param).unwrap();
798
799
800 self.h2.clone()
801 .ready()
802 .map_err(Error::from)
803 .and_then(move |mut send_request| {
804 let (response, stream) = send_request.send_request(request, false).unwrap();
805 PipeToSendStream::new(bytes::Bytes::from(data), stream)
806 .and_then(|_| {
807 response
808 .map_err(Error::from)
809 .and_then(Self::h2api_response)
810 })
811 })
812 }
adec8ea2 813
b57cb264 814 fn request(
9af37c8f 815 &self,
b57cb264
DM
816 request: Request<()>,
817 ) -> impl Future<Item=Value, Error=Error> {
818
9af37c8f 819 self.send_request(request, None)
82ab7230
DM
820 .and_then(move |response| {
821 response
822 .map_err(Error::from)
823 .and_then(Self::h2api_response)
824 })
825 }
826
827 fn send_request(
9af37c8f 828 &self,
82ab7230
DM
829 request: Request<()>,
830 data: Option<bytes::Bytes>,
831 ) -> impl Future<Item=h2::client::ResponseFuture, Error=Error> {
832
9af37c8f 833 self.h2.clone()
10130cf4
DM
834 .ready()
835 .map_err(Error::from)
836 .and_then(move |mut send_request| {
82ab7230
DM
837 if let Some(data) = data {
838 let (response, stream) = send_request.send_request(request, false).unwrap();
839 future::Either::A(PipeToSendStream::new(bytes::Bytes::from(data), stream)
840 .and_then(move |_| {
841 future::ok(response)
842 }))
843 } else {
844 let (response, _stream) = send_request.send_request(request, true).unwrap();
845 future::Either::B(future::ok(response))
846 }
b57cb264
DM
847 })
848 }
849
850 fn h2api_response(response: Response<h2::RecvStream>) -> impl Future<Item=Value, Error=Error> {
851
852 let status = response.status();
853
854 let (_head, mut body) = response.into_parts();
855
856 // The `release_capacity` handle allows the caller to manage
857 // flow control.
858 //
859 // Whenever data is received, the caller is responsible for
860 // releasing capacity back to the server once it has freed
861 // the data from memory.
862 let mut release_capacity = body.release_capacity().clone();
863
864 body
865 .map(move |chunk| {
b57cb264
DM
866 // Let the server send more data.
867 let _ = release_capacity.release_capacity(chunk.len());
868 chunk
869 })
870 .concat2()
871 .map_err(Error::from)
872 .and_then(move |data| {
b57cb264
DM
873 let text = String::from_utf8(data.to_vec()).unwrap();
874 if status.is_success() {
875 if text.len() > 0 {
876 let mut value: Value = serde_json::from_str(&text)?;
877 if let Some(map) = value.as_object_mut() {
878 if let Some(data) = map.remove("data") {
879 return Ok(data);
880 }
881 }
882 bail!("got result without data property");
883 } else {
884 Ok(Value::Null)
885 }
886 } else {
887 bail!("HTTP Error {}: {}", status, text);
888 }
889 })
890 }
891
eb2bdd1b 892 // Note: We always encode parameters with the url
b57cb264
DM
893 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<()>, Error> {
894 let path = path.trim_matches('/');
b57cb264
DM
895
896 if let Some(data) = data {
897 let query = tools::json_object_to_query(data)?;
eb2bdd1b
DM
898 // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
899 if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); }
b57cb264 900 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
eb2bdd1b 901 let request = Request::builder()
b57cb264
DM
902 .method(method)
903 .uri(url)
904 .header("User-Agent", "proxmox-backup-client/1.0")
905 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
906 .body(())?;
907 return Ok(request);
eb2bdd1b
DM
908 } else {
909 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
910 let request = Request::builder()
911 .method(method)
912 .uri(url)
913 .header("User-Agent", "proxmox-backup-client/1.0")
914 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
915 .body(())?;
b57cb264 916
eb2bdd1b
DM
917 Ok(request)
918 }
b57cb264
DM
919 }
920}