5 use hyper
::client
::Client
;
6 use xdg
::BaseDirectories
;
8 use std
::collections
::HashSet
;
9 use std
::sync
::{Arc, Mutex}
;
11 use http
::{Request, Response}
;
12 use http
::header
::HeaderValue
;
15 use futures
::stream
::Stream
;
16 use std
::sync
::atomic
::{AtomicUsize, Ordering}
;
17 use tokio
::sync
::mpsc
;
19 use serde_json
::{json, Value}
;
20 use url
::percent_encoding
::{percent_encode, DEFAULT_ENCODE_SET}
;
22 use crate::tools
::{self, BroadcastFuture, tty}
;
23 use super::pipe_to_stream
::*;
24 use super::merge_known_chunks
::*;
36 /// HTTP(S) API client
37 pub struct HttpClient
{
38 client
: Client
<hyper_tls
::HttpsConnector
<hyper
::client
::HttpConnector
>>,
40 auth
: BroadcastFuture
<AuthInfo
>,
43 fn store_ticket_info(server
: &str, username
: &str, ticket
: &str, token
: &str) -> Result
<(), Error
> {
45 let base
= BaseDirectories
::with_prefix("proxmox-backup")?
;
47 // usually /run/user/<uid>/...
48 let path
= base
.place_runtime_file("tickets")?
;
50 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
52 let mut data
= tools
::file_get_json(&path
, Some(json
!({}
)))?
;
54 let now
= Utc
::now().timestamp();
56 data
[server
][username
] = json
!({ "timestamp": now, "ticket": ticket, "token": token}
);
58 let mut new_data
= json
!({}
);
60 let ticket_lifetime
= tools
::ticket
::TICKET_LIFETIME
- 60;
62 let empty
= serde_json
::map
::Map
::new();
63 for (server
, info
) in data
.as_object().unwrap_or(&empty
) {
64 for (_user
, uinfo
) in info
.as_object().unwrap_or(&empty
) {
65 if let Some(timestamp
) = uinfo
["timestamp"].as_i64() {
66 let age
= now
- timestamp
;
67 if age
< ticket_lifetime
{
68 new_data
[server
][username
] = uinfo
.clone();
74 tools
::file_set_contents(path
, new_data
.to_string().as_bytes(), Some(mode
))?
;
79 fn load_ticket_info(server
: &str, username
: &str) -> Option
<(String
, String
)> {
80 let base
= match BaseDirectories
::with_prefix("proxmox-backup") {
85 // usually /run/user/<uid>/...
86 let path
= match base
.place_runtime_file("tickets") {
91 let data
= match tools
::file_get_json(&path
, None
) {
96 let now
= Utc
::now().timestamp();
98 let ticket_lifetime
= tools
::ticket
::TICKET_LIFETIME
- 60;
100 if let Some(uinfo
) = data
[server
][username
].as_object() {
101 if let Some(timestamp
) = uinfo
["timestamp"].as_i64() {
102 let age
= now
- timestamp
;
103 if age
< ticket_lifetime
{
104 let ticket
= match uinfo
["ticket"].as_str() {
108 let token
= match uinfo
["token"].as_str() {
112 return Some((ticket
.to_owned(), token
.to_owned()));
122 pub fn new(server
: &str, username
: &str) -> Result
<Self, Error
> {
123 let client
= Self::build_client();
125 let password
= if let Some((ticket
, _token
)) = load_ticket_info(server
, username
) {
128 Self::get_password(&username
)?
131 let login
= Self::credentials(client
.clone(), server
.to_owned(), username
.to_owned(), password
);
135 server
: String
::from(server
),
136 auth
: BroadcastFuture
::new(login
),
140 fn get_password(_username
: &str) -> Result
<String
, Error
> {
141 use std
::env
::VarError
::*;
142 match std
::env
::var("PBS_PASSWORD") {
143 Ok(p
) => return Ok(p
),
144 Err(NotUnicode(_
)) => bail
!("PBS_PASSWORD contains bad characters"),
146 // Try another method
150 // If we're on a TTY, query the user for a password
151 if tty
::stdin_isatty() {
152 return Ok(String
::from_utf8(tty
::read_password("Password: ")?
)?
);
155 bail
!("no password input mechanism available");
158 fn build_client() -> Client
<hyper_tls
::HttpsConnector
<hyper
::client
::HttpConnector
>> {
159 let mut builder
= native_tls
::TlsConnector
::builder();
160 // FIXME: We need a CLI option for this!
161 builder
.danger_accept_invalid_certs(true);
162 let tlsconnector
= builder
.build().unwrap();
163 let mut httpc
= hyper
::client
::HttpConnector
::new(1);
164 //httpc.set_nodelay(true); // not sure if this help?
165 httpc
.enforce_http(false); // we want https...
166 let mut https
= hyper_tls
::HttpsConnector
::from((httpc
, tlsconnector
));
167 https
.https_only(true); // force it!
169 //.http2_initial_stream_window_size( (1 << 31) - 2)
170 //.http2_initial_connection_window_size( (1 << 31) - 2)
171 .build
::<_
, Body
>(https
)
174 pub fn request(&self, mut req
: Request
<Body
>) -> impl Future
<Item
=Value
, Error
=Error
> {
176 let login
= self.auth
.listen();
178 let client
= self.client
.clone();
180 login
.and_then(move |auth
| {
182 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
183 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
184 req
.headers_mut().insert("CSRFPreventionToken", HeaderValue
::from_str(&auth
.token
).unwrap());
186 let request
= Self::api_request(client
, req
);
192 pub fn get(&self, path
: &str, data
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
194 let req
= Self::request_builder(&self.server
, "GET", path
, data
).unwrap();
198 pub fn delete(&mut self, path
: &str, data
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
200 let req
= Self::request_builder(&self.server
, "DELETE", path
, data
).unwrap();
204 pub fn post(&mut self, path
: &str, data
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
206 let req
= Self::request_builder(&self.server
, "POST", path
, data
).unwrap();
210 pub fn download(&mut self, path
: &str, mut output
: Box
<dyn std
::io
::Write
+ Send
>) -> impl Future
<Item
=(), Error
=Error
> {
212 let mut req
= Self::request_builder(&self.server
, "GET", path
, None
).unwrap();
214 let login
= self.auth
.listen();
216 let client
= self.client
.clone();
218 login
.and_then(move |auth
| {
220 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
221 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
224 .map_err(Error
::from
)
227 let _status
= resp
.status(); // fixme: ??
230 .map_err(Error
::from
)
231 .for_each(move |chunk
| {
232 output
.write_all(&chunk
)?
;
240 pub fn upload(&mut self, content_type
: &str, body
: Body
, path
: &str) -> impl Future
<Item
=Value
, Error
=Error
> {
242 let path
= path
.trim_matches('
/'
);
243 let url
: Uri
= format
!("https://{}:8007/{}", &self.server
, path
).parse().unwrap();
245 let req
= Request
::builder()
248 .header("User-Agent", "proxmox-backup-client/1.0")
249 .header("Content-Type", content_type
)
250 .body(body
).unwrap();
261 ) -> impl Future
<Item
=BackupClient
, Error
=Error
> {
263 let param
= json
!({"backup-type": backup_type, "backup-id": backup_id, "store": datastore, "debug": debug}
);
264 let mut req
= Self::request_builder(&self.server
, "GET", "/api2/json/backup", Some(param
)).unwrap();
266 let login
= self.auth
.listen();
268 let client
= self.client
.clone();
270 login
.and_then(move |auth
| {
272 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
273 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
274 req
.headers_mut().insert("UPGRADE", HeaderValue
::from_str(PROXMOX_BACKUP_PROTOCOL_ID_V1
!()).unwrap());
277 .map_err(Error
::from
)
280 let status
= resp
.status();
281 if status
!= http
::StatusCode
::SWITCHING_PROTOCOLS
{
282 future
::Either
::A(Self::api_response(resp
).and_then(|_
| { bail!("unknown error"); }
))
284 future
::Either
::B(resp
.into_body().on_upgrade().map_err(Error
::from
))
287 .and_then(|upgraded
| {
288 h2
::client
::handshake(upgraded
).map_err(Error
::from
)
290 .and_then(|(h2
, connection
)| {
291 let connection
= connection
292 .map_err(|_
| panic
!("HTTP/2.0 connection failed"));
294 // Spawn a new task to drive the connection state
295 hyper
::rt
::spawn(connection
);
297 // Wait until the `SendRequest` handle has available capacity.
299 .map(BackupClient
::new
)
300 .map_err(Error
::from
)
306 client
: Client
<hyper_tls
::HttpsConnector
<hyper
::client
::HttpConnector
>>,
310 ) -> Box
<Future
<Item
=AuthInfo
, Error
=Error
> + Send
> {
312 let server2
= server
.clone();
314 let create_request
= futures
::future
::lazy(move || {
315 let data
= json
!({ "username": username, "password": password }
);
316 let req
= Self::request_builder(&server
, "POST", "/api2/json/access/ticket", Some(data
)).unwrap();
317 Self::api_request(client
, req
)
320 let login_future
= create_request
321 .and_then(move |cred
| {
322 let auth
= AuthInfo
{
323 username
: cred
["data"]["username"].as_str().unwrap().to_owned(),
324 ticket
: cred
["data"]["ticket"].as_str().unwrap().to_owned(),
325 token
: cred
["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
328 let _
= store_ticket_info(&server2
, &auth
.username
, &auth
.ticket
, &auth
.token
);
333 Box
::new(login_future
)
336 fn api_response(response
: Response
<Body
>) -> impl Future
<Item
=Value
, Error
=Error
> {
338 let status
= response
.status();
343 .map_err(Error
::from
)
344 .and_then(move |data
| {
346 let text
= String
::from_utf8(data
.to_vec()).unwrap();
347 if status
.is_success() {
349 let value
: Value
= serde_json
::from_str(&text
)?
;
355 bail
!("HTTP Error {}: {}", status
, text
);
361 client
: Client
<hyper_tls
::HttpsConnector
<hyper
::client
::HttpConnector
>>,
363 ) -> impl Future
<Item
=Value
, Error
=Error
> {
366 .map_err(Error
::from
)
367 .and_then(Self::api_response
)
370 pub fn request_builder(server
: &str, method
: &str, path
: &str, data
: Option
<Value
>) -> Result
<Request
<Body
>, Error
> {
371 let path
= path
.trim_matches('
/'
);
372 let url
: Uri
= format
!("https://{}:8007/{}", server
, path
).parse()?
;
374 if let Some(data
) = data
{
375 if method
== "POST" {
376 let request
= Request
::builder()
379 .header("User-Agent", "proxmox-backup-client/1.0")
380 .header(hyper
::header
::CONTENT_TYPE
, "application/json")
381 .body(Body
::from(data
.to_string()))?
;
384 let query
= tools
::json_object_to_query(data
)?
;
385 let url
: Uri
= format
!("https://{}:8007/{}?{}", server
, path
, query
).parse()?
;
386 let request
= Request
::builder()
389 .header("User-Agent", "proxmox-backup-client/1.0")
390 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
391 .body(Body
::empty())?
;
396 let request
= Request
::builder()
399 .header("User-Agent", "proxmox-backup-client/1.0")
400 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
401 .body(Body
::empty())?
;
408 pub struct BackupClient
{
415 pub fn new(h2
: h2
::client
::SendRequest
<bytes
::Bytes
>) -> Self {
416 Self { h2: H2Client::new(h2) }
419 pub fn get(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
420 self.h2
.get(path
, param
)
423 pub fn put(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
424 self.h2
.put(path
, param
)
427 pub fn post(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
428 self.h2
.post(path
, param
)
431 pub fn finish(&self) -> impl Future
<Item
=(), Error
=Error
> {
432 self.h2
.clone().post("finish", None
).map(|_
| ())
435 pub fn upload_config
<P
: AsRef
<std
::path
::Path
>>(
439 ) -> impl Future
<Item
=(), Error
=Error
> {
441 let h2
= self.h2
.clone();
442 let file_name
= file_name
.to_owned();
443 let src_path
= src_path
.as_ref().to_owned();
445 let task
= tokio
::fs
::File
::open(src_path
.clone())
446 .map_err(move |err
| format_err
!("unable to open file {:?} - {}", src_path
, err
))
448 let contents
= vec
![];
449 tokio
::io
::read_to_end(file
, contents
)
450 .map_err(Error
::from
)
451 .and_then(move |(_
, contents
)| {
452 let param
= json
!({"size": contents.len(), "file-name": file_name }
);
453 h2
.upload("config", Some(param
), contents
)
461 pub fn upload_stream(
464 stream
: impl Stream
<Item
=bytes
::BytesMut
, Error
=Error
>,
466 fixed_size
: Option
<u64>,
467 ) -> impl Future
<Item
=(), Error
=Error
> {
469 let known_chunks
= Arc
::new(Mutex
::new(HashSet
::new()));
471 let mut stream_len
= 0u64;
475 let digest
= openssl
::sha
::sha256(&data
);
476 let offset
= stream_len
;
477 stream_len
+= data
.len() as u64;
478 ChunkInfo { data, digest, offset }
481 let h2
= self.h2
.clone();
482 let h2_2
= self.h2
.clone();
483 let h2_3
= self.h2
.clone();
484 let h2_4
= self.h2
.clone();
486 let mut param
= json
!({ "archive-name": archive_name }
);
487 if let Some(size
) = fixed_size
{
488 param
["size"] = size
.into();
491 let index_path
= format
!("{}_index", prefix
);
492 let close_path
= format
!("{}_close", prefix
);
494 let prefix
= prefix
.to_owned();
496 Self::download_chunk_list(h2
, &index_path
, archive_name
, known_chunks
.clone())
498 h2_2
.post(&index_path
, Some(param
))
500 .and_then(move |res
| {
501 let wid
= res
.as_u64().unwrap();
502 Self::upload_chunk_info_stream(h2_3
, wid
, stream
, &prefix
, known_chunks
.clone())
503 .and_then(move |(chunk_count
, size
, _speed
)| {
506 "chunk-count": chunk_count
,
509 h2_4
.post(&close_path
, Some(param
))
515 fn response_queue() -> (
516 mpsc
::Sender
<h2
::client
::ResponseFuture
>,
517 sync
::oneshot
::Receiver
<Result
<(), Error
>>
519 let (verify_queue_tx
, verify_queue_rx
) = mpsc
::channel(100);
520 let (verify_result_tx
, verify_result_rx
) = sync
::oneshot
::channel();
524 .map_err(Error
::from
)
525 .for_each(|response
: h2
::client
::ResponseFuture
| {
527 .map_err(Error
::from
)
528 .and_then(H2Client
::h2api_response
)
530 println
!("RESPONSE: {:?}", result
);
533 .map_err(|err
| format_err
!("pipelined request failed: {}", err
))
536 verify_result_tx
.send(result
)
538 .map_err(|_
| { /* ignore closed channel */ }
)
541 (verify_queue_tx
, verify_result_rx
)
544 fn append_chunk_queue(h2
: H2Client
, wid
: u64, path
: String
) -> (
545 mpsc
::Sender
<(MergedChunkInfo
, Option
<h2
::client
::ResponseFuture
>)>,
546 sync
::oneshot
::Receiver
<Result
<(), Error
>>
548 let (verify_queue_tx
, verify_queue_rx
) = mpsc
::channel(64);
549 let (verify_result_tx
, verify_result_rx
) = sync
::oneshot
::channel();
551 let h2_2
= h2
.clone();
555 .map_err(Error
::from
)
556 .and_then(move |(merged_chunk_info
, response
): (MergedChunkInfo
, Option
<h2
::client
::ResponseFuture
>)| {
557 match (response
, merged_chunk_info
) {
558 (Some(response
), MergedChunkInfo
::Known(list
)) => {
561 .map_err(Error
::from
)
562 .and_then(H2Client
::h2api_response
)
563 .and_then(move |_result
| {
564 Ok(MergedChunkInfo
::Known(list
))
568 (None
, MergedChunkInfo
::Known(list
)) => {
569 future
::Either
::B(future
::ok(MergedChunkInfo
::Known(list
)))
574 .merge_known_chunks()
575 .and_then(move |merged_chunk_info
| {
576 match merged_chunk_info
{
577 MergedChunkInfo
::Known(chunk_list
) => {
578 let mut digest_list
= vec
![];
579 let mut offset_list
= vec
![];
580 for (offset
, digest
) in chunk_list
{
581 //println!("append chunk {} (offset {})", tools::digest_to_hex(&digest), offset);
582 digest_list
.push(tools
::digest_to_hex(&digest
));
583 offset_list
.push(offset
);
585 println
!("append chunks list len ({})", digest_list
.len());
586 let param
= json
!({ "wid": wid, "digest-list": digest_list, "offset-list": offset_list }
);
587 let mut request
= H2Client
::request_builder("localhost", "PUT", &path
, None
).unwrap();
588 request
.headers_mut().insert(hyper
::header
::CONTENT_TYPE
, HeaderValue
::from_static("application/json"));
589 let param_data
= bytes
::Bytes
::from(param
.to_string().as_bytes());
590 let upload_data
= Some(param_data
);
591 h2_2
.send_request(request
, upload_data
)
592 .and_then(move |response
| {
594 .map_err(Error
::from
)
595 .and_then(H2Client
::h2api_response
)
596 .and_then(|_
| Ok(()))
598 .map_err(|err
| format_err
!("pipelined request failed: {}", err
))
603 .for_each(|_
| Ok(()))
605 verify_result_tx
.send(result
)
607 .map_err(|_
| { /* ignore closed channel */ }
)
610 (verify_queue_tx
, verify_result_rx
)
613 fn download_chunk_list(
617 known_chunks
: Arc
<Mutex
<HashSet
<[u8;32]>>>,
618 ) -> impl Future
<Item
=(), Error
=Error
> {
620 let param
= json
!({ "archive-name": archive_name }
);
621 let request
= H2Client
::request_builder("localhost", "GET", path
, Some(param
)).unwrap();
623 h2
.send_request(request
, None
)
624 .and_then(move |response
| {
626 .map_err(Error
::from
)
627 .and_then(move |resp
| {
628 let status
= resp
.status();
630 if !status
.is_success() {
631 future
::Either
::A(H2Client
::h2api_response(resp
).and_then(|_
| { bail!("unknown error"); }
))
633 future
::Either
::B(future
::ok(resp
.into_body()))
636 .and_then(move |mut body
| {
638 let mut release_capacity
= body
.release_capacity().clone();
640 DigestListDecoder
::new(body
.map_err(Error
::from
))
641 .for_each(move |chunk
| {
642 let _
= release_capacity
.release_capacity(chunk
.len());
643 println
!("GOT DOWNLOAD {}", tools
::digest_to_hex(&chunk
));
644 known_chunks
.lock().unwrap().insert(chunk
);
651 fn upload_chunk_info_stream(
654 stream
: impl Stream
<Item
=ChunkInfo
, Error
=Error
>,
656 known_chunks
: Arc
<Mutex
<HashSet
<[u8;32]>>>,
657 ) -> impl Future
<Item
=(usize, usize, usize), Error
=Error
> {
659 let repeat
= std
::sync
::Arc
::new(AtomicUsize
::new(0));
660 let repeat2
= repeat
.clone();
662 let stream_len
= std
::sync
::Arc
::new(AtomicUsize
::new(0));
663 let stream_len2
= stream_len
.clone();
665 let append_chunk_path
= format
!("{}_index", prefix
);
666 let upload_chunk_path
= format
!("{}_chunk", prefix
);
668 let (upload_queue
, upload_result
) = Self::append_chunk_queue(h2
.clone(), wid
, append_chunk_path
.to_owned());
670 let start_time
= std
::time
::Instant
::now();
673 .map(move |chunk_info
| {
674 repeat
.fetch_add(1, Ordering
::SeqCst
);
675 stream_len
.fetch_add(chunk_info
.data
.len(), Ordering
::SeqCst
);
677 let mut known_chunks
= known_chunks
.lock().unwrap();
678 let chunk_is_known
= known_chunks
.contains(&chunk_info
.digest
);
680 MergedChunkInfo
::Known(vec
![(chunk_info
.offset
, chunk_info
.digest
)])
682 known_chunks
.insert(chunk_info
.digest
);
683 MergedChunkInfo
::New(chunk_info
)
686 .merge_known_chunks()
687 .for_each(move |merged_chunk_info
| {
689 if let MergedChunkInfo
::New(chunk_info
) = merged_chunk_info
{
690 let offset
= chunk_info
.offset
;
691 let digest
= chunk_info
.digest
;
692 let upload_queue
= upload_queue
.clone();
694 println
!("upload new chunk {} ({} bytes, offset {})", tools
::digest_to_hex(&digest
),
695 chunk_info
.data
.len(), offset
);
697 let param
= json
!({ "wid": wid, "size" : chunk_info.data.len() }
);
698 let request
= H2Client
::request_builder("localhost", "POST", &upload_chunk_path
, Some(param
)).unwrap();
699 let upload_data
= Some(chunk_info
.data
.freeze());
701 let new_info
= MergedChunkInfo
::Known(vec
![(offset
, digest
)]);
704 h2
.send_request(request
, upload_data
)
705 .and_then(move |response
| {
706 upload_queue
.clone().send((new_info
, Some(response
)))
707 .map(|_
| ()).map_err(Error
::from
)
713 upload_queue
.clone().send((merged_chunk_info
, None
))
714 .map(|_
| ()).map_err(Error
::from
)
718 .then(move |result
| {
719 println
!("RESULT {:?}", result
);
720 upload_result
.map_err(Error
::from
).and_then(|upload1_result
| {
721 Ok(upload1_result
.and(result
))
726 let repeat
= repeat2
.load(Ordering
::SeqCst
);
727 let stream_len
= stream_len2
.load(Ordering
::SeqCst
);
728 let speed
= ((stream_len
*1000000)/(1024*1024))/(start_time
.elapsed().as_micros() as usize);
729 println
!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat
, start_time
.elapsed().as_secs(), speed
);
731 println
!("Average chunk size was {} bytes.", stream_len
/repeat
);
732 println
!("Time per request: {} microseconds.", (start_time
.elapsed().as_micros())/(repeat
as u128
));
734 Ok((repeat
, stream_len
, speed
))
738 pub fn upload_speedtest(&self) -> impl Future
<Item
=usize, Error
=Error
> {
740 let mut data
= vec
![];
741 // generate pseudo random byte sequence
742 for i
in 0..1024*1024 {
744 let byte
= ((i
>> (j
<<3))&0xff) as u8;
749 let item_len
= data
.len();
751 let repeat
= std
::sync
::Arc
::new(AtomicUsize
::new(0));
752 let repeat2
= repeat
.clone();
754 let (upload_queue
, upload_result
) = Self::response_queue();
756 let start_time
= std
::time
::Instant
::now();
758 let h2
= self.h2
.clone();
760 futures
::stream
::repeat(data
)
761 .take_while(move |_
| {
762 repeat
.fetch_add(1, Ordering
::SeqCst
);
763 Ok(start_time
.elapsed().as_secs() < 5)
765 .for_each(move |data
| {
768 let upload_queue
= upload_queue
.clone();
770 println
!("send test data ({} bytes)", data
.len());
771 let request
= H2Client
::request_builder("localhost", "POST", "speedtest", None
).unwrap();
772 h2
.send_request(request
, Some(bytes
::Bytes
::from(data
)))
773 .and_then(move |response
| {
774 upload_queue
.send(response
)
775 .map(|_
| ()).map_err(Error
::from
)
778 .then(move |result
| {
779 println
!("RESULT {:?}", result
);
780 upload_result
.map_err(Error
::from
).and_then(|upload1_result
| {
781 Ok(upload1_result
.and(result
))
786 let repeat
= repeat2
.load(Ordering
::SeqCst
);
787 println
!("Uploaded {} chunks in {} seconds.", repeat
, start_time
.elapsed().as_secs());
788 let speed
= ((item_len
*1000000*(repeat
as usize))/(1024*1024))/(start_time
.elapsed().as_micros() as usize);
790 println
!("Time per request: {} microseconds.", (start_time
.elapsed().as_micros())/(repeat
as u128
));
798 pub struct H2Client
{
799 h2
: h2
::client
::SendRequest
<bytes
::Bytes
>,
804 pub fn new(h2
: h2
::client
::SendRequest
<bytes
::Bytes
>) -> Self {
808 pub fn get(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
809 let req
= Self::request_builder("localhost", "GET", path
, param
).unwrap();
813 pub fn put(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
814 let req
= Self::request_builder("localhost", "PUT", path
, param
).unwrap();
818 pub fn post(&self, path
: &str, param
: Option
<Value
>) -> impl Future
<Item
=Value
, Error
=Error
> {
819 let req
= Self::request_builder("localhost", "POST", path
, param
).unwrap();
823 pub fn upload(&self, path
: &str, param
: Option
<Value
>, data
: Vec
<u8>) -> impl Future
<Item
=Value
, Error
=Error
> {
824 let request
= Self::request_builder("localhost", "POST", path
, param
).unwrap();
829 .map_err(Error
::from
)
830 .and_then(move |mut send_request
| {
831 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
832 PipeToSendStream
::new(bytes
::Bytes
::from(data
), stream
)
835 .map_err(Error
::from
)
836 .and_then(Self::h2api_response
)
843 request
: Request
<()>,
844 ) -> impl Future
<Item
=Value
, Error
=Error
> {
846 self.send_request(request
, None
)
847 .and_then(move |response
| {
849 .map_err(Error
::from
)
850 .and_then(Self::h2api_response
)
856 request
: Request
<()>,
857 data
: Option
<bytes
::Bytes
>,
858 ) -> impl Future
<Item
=h2
::client
::ResponseFuture
, Error
=Error
> {
862 .map_err(Error
::from
)
863 .and_then(move |mut send_request
| {
864 if let Some(data
) = data
{
865 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
866 future
::Either
::A(PipeToSendStream
::new(bytes
::Bytes
::from(data
), stream
)
871 let (response
, _stream
) = send_request
.send_request(request
, true).unwrap();
872 future
::Either
::B(future
::ok(response
))
877 fn h2api_response(response
: Response
<h2
::RecvStream
>) -> impl Future
<Item
=Value
, Error
=Error
> {
879 let status
= response
.status();
881 let (_head
, mut body
) = response
.into_parts();
883 // The `release_capacity` handle allows the caller to manage
886 // Whenever data is received, the caller is responsible for
887 // releasing capacity back to the server once it has freed
888 // the data from memory.
889 let mut release_capacity
= body
.release_capacity().clone();
893 // Let the server send more data.
894 let _
= release_capacity
.release_capacity(chunk
.len());
898 .map_err(Error
::from
)
899 .and_then(move |data
| {
900 let text
= String
::from_utf8(data
.to_vec()).unwrap();
901 if status
.is_success() {
903 let mut value
: Value
= serde_json
::from_str(&text
)?
;
904 if let Some(map
) = value
.as_object_mut() {
905 if let Some(data
) = map
.remove("data") {
909 bail
!("got result without data property");
914 bail
!("HTTP Error {}: {}", status
, text
);
919 // Note: We always encode parameters with the url
920 pub fn request_builder(server
: &str, method
: &str, path
: &str, data
: Option
<Value
>) -> Result
<Request
<()>, Error
> {
921 let path
= path
.trim_matches('
/'
);
923 if let Some(data
) = data
{
924 let query
= tools
::json_object_to_query(data
)?
;
925 // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
926 if query
.len() > 4096 { bail!("h2 query data too large ({} bytes
) - please encode data inside body
", query.len()); }
927 let url: Uri = format!("https
://{}:8007/{}?{}", server, path, query).parse()?;
928 let request
= Request
::builder()
931 .header("User-Agent", "proxmox-backup-client/1.0")
932 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
936 let url
: Uri
= format
!("https://{}:8007/{}", server
, path
).parse()?
;
937 let request
= Request
::builder()
940 .header("User-Agent", "proxmox-backup-client/1.0")
941 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")