2 use std
::sync
::{Arc, Mutex, RwLock}
;
3 use std
::time
::Duration
;
5 use anyhow
::{bail, format_err, Error}
;
8 use http
::header
::HeaderValue
;
9 use http
::{Request, Response}
;
11 use hyper
::client
::{Client, HttpConnector}
;
12 use openssl
::{ssl::{SslConnector, SslMethod}
, x509
::X509StoreContextRef
};
13 use serde_json
::{json, Value}
;
14 use percent_encoding
::percent_encode
;
15 use xdg
::BaseDirectories
;
18 api
::error
::HttpError
,
20 tools
::fs
::{file_get_json, replace_file, CreateOptions}
,
23 use proxmox_http
::client
::HttpsConnector
;
24 use proxmox_http
::uri
::build_authority
;
26 use pbs_api_types
::{Authid, Userid}
;
27 use pbs_tools
::json
::json_object_to_query
;
28 use pbs_tools
::ticket
;
30 use super::pipe_to_stream
::PipeToSendStream
;
34 PROXMOX_BACKUP_TCP_KEEPALIVE_TIME
,
37 /// Timeout used for several HTTP operations that are expected to finish quickly but may block in
38 /// certain error conditions. Keep it generous, to avoid false-positive under high load.
39 const HTTP_TIMEOUT
: Duration
= Duration
::from_secs(2 * 60);
48 pub struct HttpClientOptions
{
49 prefix
: Option
<String
>,
50 password
: Option
<String
>,
51 fingerprint
: Option
<String
>,
54 fingerprint_cache
: bool
,
58 impl HttpClientOptions
{
60 pub fn new_interactive(password
: Option
<String
>, fingerprint
: Option
<String
>) -> Self {
64 fingerprint_cache
: true,
67 prefix
: Some("proxmox-backup".to_string()),
72 pub fn new_non_interactive(password
: String
, fingerprint
: Option
<String
>) -> Self {
74 password
: Some(password
),
80 pub fn prefix(mut self, prefix
: Option
<String
>) -> Self {
85 pub fn password(mut self, password
: Option
<String
>) -> Self {
86 self.password
= password
;
90 pub fn fingerprint(mut self, fingerprint
: Option
<String
>) -> Self {
91 self.fingerprint
= fingerprint
;
95 pub fn interactive(mut self, interactive
: bool
) -> Self {
96 self.interactive
= interactive
;
100 pub fn ticket_cache(mut self, ticket_cache
: bool
) -> Self {
101 self.ticket_cache
= ticket_cache
;
105 pub fn fingerprint_cache(mut self, fingerprint_cache
: bool
) -> Self {
106 self.fingerprint_cache
= fingerprint_cache
;
110 pub fn verify_cert(mut self, verify_cert
: bool
) -> Self {
111 self.verify_cert
= verify_cert
;
116 impl Default
for HttpClientOptions
{
117 fn default() -> Self {
124 fingerprint_cache
: false,
130 /// HTTP(S) API client
131 pub struct HttpClient
{
132 client
: Client
<HttpsConnector
>,
135 fingerprint
: Arc
<Mutex
<Option
<String
>>>,
136 first_auth
: Option
<BroadcastFuture
<()>>,
137 auth
: Arc
<RwLock
<AuthInfo
>>,
138 ticket_abort
: futures
::future
::AbortHandle
,
139 _options
: HttpClientOptions
,
142 /// Delete stored ticket data (logout)
143 pub fn delete_ticket_info(prefix
: &str, server
: &str, username
: &Userid
) -> Result
<(), Error
> {
145 let base
= BaseDirectories
::with_prefix(prefix
)?
;
147 // usually /run/user/<uid>/...
148 let path
= base
.place_runtime_file("tickets")?
;
150 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
152 let mut data
= file_get_json(&path
, Some(json
!({}
)))?
;
154 if let Some(map
) = data
[server
].as_object_mut() {
155 map
.remove(username
.as_str());
158 replace_file(path
, data
.to_string().as_bytes(), CreateOptions
::new().perm(mode
))?
;
163 fn store_fingerprint(prefix
: &str, server
: &str, fingerprint
: &str) -> Result
<(), Error
> {
165 let base
= BaseDirectories
::with_prefix(prefix
)?
;
167 // usually ~/.config/<prefix>/fingerprints
168 let path
= base
.place_config_file("fingerprints")?
;
170 let raw
= match std
::fs
::read_to_string(&path
) {
173 if err
.kind() == std
::io
::ErrorKind
::NotFound
{
176 bail
!("unable to read fingerprints from {:?} - {}", path
, err
);
181 let mut result
= String
::new();
183 raw
.split('
\n'
).for_each(|line
| {
184 let items
: Vec
<String
> = line
.split_whitespace().map(String
::from
).collect();
185 if items
.len() == 2 {
186 if items
[0] == server
{
187 // found, add later with new fingerprint
189 result
.push_str(line
);
195 result
.push_str(server
);
197 result
.push_str(fingerprint
);
200 replace_file(path
, result
.as_bytes(), CreateOptions
::new())?
;
205 fn load_fingerprint(prefix
: &str, server
: &str) -> Option
<String
> {
207 let base
= BaseDirectories
::with_prefix(prefix
).ok()?
;
209 // usually ~/.config/<prefix>/fingerprints
210 let path
= base
.place_config_file("fingerprints").ok()?
;
212 let raw
= std
::fs
::read_to_string(&path
).ok()?
;
214 for line
in raw
.split('
\n'
) {
215 let items
: Vec
<String
> = line
.split_whitespace().map(String
::from
).collect();
216 if items
.len() == 2 && items
[0] == server
{
217 return Some(items
[1].clone());
224 fn store_ticket_info(prefix
: &str, server
: &str, username
: &str, ticket
: &str, token
: &str) -> Result
<(), Error
> {
226 let base
= BaseDirectories
::with_prefix(prefix
)?
;
228 // usually /run/user/<uid>/...
229 let path
= base
.place_runtime_file("tickets")?
;
231 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
233 let mut data
= file_get_json(&path
, Some(json
!({}
)))?
;
235 let now
= proxmox
::tools
::time
::epoch_i64();
237 data
[server
][username
] = json
!({ "timestamp": now, "ticket": ticket, "token": token}
);
239 let mut new_data
= json
!({}
);
241 let ticket_lifetime
= ticket
::TICKET_LIFETIME
- 60;
243 let empty
= serde_json
::map
::Map
::new();
244 for (server
, info
) in data
.as_object().unwrap_or(&empty
) {
245 for (user
, uinfo
) in info
.as_object().unwrap_or(&empty
) {
246 if let Some(timestamp
) = uinfo
["timestamp"].as_i64() {
247 let age
= now
- timestamp
;
248 if age
< ticket_lifetime
{
249 new_data
[server
][user
] = uinfo
.clone();
255 replace_file(path
, new_data
.to_string().as_bytes(), CreateOptions
::new().perm(mode
))?
;
260 fn load_ticket_info(prefix
: &str, server
: &str, userid
: &Userid
) -> Option
<(String
, String
)> {
261 let base
= BaseDirectories
::with_prefix(prefix
).ok()?
;
263 // usually /run/user/<uid>/...
264 let path
= base
.place_runtime_file("tickets").ok()?
;
265 let data
= file_get_json(&path
, None
).ok()?
;
266 let now
= proxmox
::tools
::time
::epoch_i64();
267 let ticket_lifetime
= ticket
::TICKET_LIFETIME
- 60;
268 let uinfo
= data
[server
][userid
.as_str()].as_object()?
;
269 let timestamp
= uinfo
["timestamp"].as_i64()?
;
270 let age
= now
- timestamp
;
272 if age
< ticket_lifetime
{
273 let ticket
= uinfo
["ticket"].as_str()?
;
274 let token
= uinfo
["token"].as_str()?
;
275 Some((ticket
.to_owned(), token
.to_owned()))
281 fn build_uri(server
: &str, port
: u16, path
: &str, query
: Option
<String
>) -> Result
<Uri
, Error
> {
284 .authority(build_authority(server
, port
)?
)
285 .path_and_query(match query
{
286 Some(query
) => format
!("/{}?{}", path
, query
),
287 None
=> format
!("/{}", path
),
290 .map_err(|err
| format_err
!("error building uri - {}", err
))
298 mut options
: HttpClientOptions
,
299 ) -> Result
<Self, Error
> {
301 let verified_fingerprint
= Arc
::new(Mutex
::new(None
));
303 let mut expected_fingerprint
= options
.fingerprint
.take();
305 if expected_fingerprint
.is_some() {
306 // do not store fingerprints passed via options in cache
307 options
.fingerprint_cache
= false;
308 } else if options
.fingerprint_cache
&& options
.prefix
.is_some() {
309 expected_fingerprint
= load_fingerprint(options
.prefix
.as_ref().unwrap(), server
);
312 let mut ssl_connector_builder
= SslConnector
::builder(SslMethod
::tls()).unwrap();
314 if options
.verify_cert
{
315 let server
= server
.to_string();
316 let verified_fingerprint
= verified_fingerprint
.clone();
317 let interactive
= options
.interactive
;
318 let fingerprint_cache
= options
.fingerprint_cache
;
319 let prefix
= options
.prefix
.clone();
320 ssl_connector_builder
.set_verify_callback(openssl
::ssl
::SslVerifyMode
::PEER
, move |valid
, ctx
| {
321 match Self::verify_callback(valid
, ctx
, expected_fingerprint
.as_ref(), interactive
) {
323 Ok(Some(fingerprint
)) => {
324 if fingerprint_cache
&& prefix
.is_some() {
325 if let Err(err
) = store_fingerprint(
326 prefix
.as_ref().unwrap(), &server
, &fingerprint
) {
327 eprintln
!("{}", err
);
330 *verified_fingerprint
.lock().unwrap() = Some(fingerprint
);
334 eprintln
!("certificate validation failed - {}", err
);
340 ssl_connector_builder
.set_verify(openssl
::ssl
::SslVerifyMode
::NONE
);
343 let mut httpc
= HttpConnector
::new();
344 httpc
.set_nodelay(true); // important for h2 download performance!
345 httpc
.enforce_http(false); // we want https...
347 httpc
.set_connect_timeout(Some(std
::time
::Duration
::new(10, 0)));
348 let https
= HttpsConnector
::with_connector(httpc
, ssl_connector_builder
.build(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME
);
350 let client
= Client
::builder()
351 //.http2_initial_stream_window_size( (1 << 31) - 2)
352 //.http2_initial_connection_window_size( (1 << 31) - 2)
353 .build
::<_
, Body
>(https
);
355 let password
= options
.password
.take();
356 let use_ticket_cache
= options
.ticket_cache
&& options
.prefix
.is_some();
358 let password
= if let Some(password
) = password
{
361 let userid
= if auth_id
.is_token() {
362 bail
!("API token secret must be provided!");
366 let mut ticket_info
= None
;
367 if use_ticket_cache
{
368 ticket_info
= load_ticket_info(options
.prefix
.as_ref().unwrap(), server
, userid
);
370 if let Some((ticket
, _token
)) = ticket_info
{
373 Self::get_password(userid
, options
.interactive
)?
377 let auth
= Arc
::new(RwLock
::new(AuthInfo
{
378 auth_id
: auth_id
.clone(),
379 ticket
: password
.clone(),
380 token
: "".to_string(),
383 let server2
= server
.to_string();
384 let client2
= client
.clone();
385 let auth2
= auth
.clone();
386 let prefix2
= options
.prefix
.clone();
388 let renewal_future
= async
move {
390 tokio
::time
::sleep(Duration
::new(60*15, 0)).await
; // 15 minutes
391 let (auth_id
, ticket
) = {
392 let authinfo
= auth2
.read().unwrap().clone();
393 (authinfo
.auth_id
, authinfo
.ticket
)
395 match Self::credentials(client2
.clone(), server2
.clone(), port
, auth_id
.user().clone(), ticket
).await
{
397 if use_ticket_cache
&& prefix2
.is_some() {
398 let _
= store_ticket_info(prefix2
.as_ref().unwrap(), &server2
, &auth
.auth_id
.to_string(), &auth
.ticket
, &auth
.token
);
400 *auth2
.write().unwrap() = auth
;
403 eprintln
!("re-authentication failed: {}", err
);
410 let (renewal_future
, ticket_abort
) = futures
::future
::abortable(renewal_future
);
412 let login_future
= Self::credentials(
416 auth_id
.user().clone(),
419 let server
= server
.to_string();
420 let prefix
= options
.prefix
.clone();
421 let authinfo
= auth
.clone();
424 if use_ticket_cache
&& prefix
.is_some() {
425 let _
= store_ticket_info(prefix
.as_ref().unwrap(), &server
, &auth
.auth_id
.to_string(), &auth
.ticket
, &auth
.token
);
427 *authinfo
.write().unwrap() = auth
;
428 tokio
::spawn(renewal_future
);
432 let first_auth
= if auth_id
.is_token() {
433 // TODO check access here?
436 Some(BroadcastFuture
::new(Box
::new(login_future
)))
441 server
: String
::from(server
),
443 fingerprint
: verified_fingerprint
,
453 /// Login is done on demand, so this is only required if you need
454 /// access to authentication data in 'AuthInfo'.
456 /// Note: tickets a periodially re-newed, so one can use this
457 /// to query changed ticket.
458 pub async
fn login(&self) -> Result
<AuthInfo
, Error
> {
459 if let Some(future
) = &self.first_auth
{
460 future
.listen().await?
;
463 let authinfo
= self.auth
.read().unwrap();
467 /// Returns the optional fingerprint passed to the new() constructor.
468 pub fn fingerprint(&self) -> Option
<String
> {
469 (*self.fingerprint
.lock().unwrap()).clone()
472 fn get_password(username
: &Userid
, interactive
: bool
) -> Result
<String
, Error
> {
473 // If we're on a TTY, query the user for a password
474 if interactive
&& tty
::stdin_isatty() {
475 let msg
= format
!("Password for \"{}\": ", username
);
476 return Ok(String
::from_utf8(tty
::read_password(&msg
)?
)?
);
479 bail
!("no password input mechanism available");
484 ctx
: &mut X509StoreContextRef
,
485 expected_fingerprint
: Option
<&String
>,
487 ) -> Result
<Option
<String
>, Error
> {
493 let cert
= match ctx
.current_cert() {
495 None
=> bail
!("context lacks current certificate."),
498 let depth
= ctx
.error_depth();
499 if depth
!= 0 { bail!("context depth != 0") }
501 let fp
= match cert
.digest(openssl
::hash
::MessageDigest
::sha256()) {
503 Err(err
) => bail
!("failed to calculate certificate FP - {}", err
), // should not happen
505 let fp_string
= proxmox
::tools
::digest_to_hex(&fp
);
506 let fp_string
= fp_string
.as_bytes().chunks(2).map(|v
| std
::str::from_utf8(v
).unwrap())
507 .collect
::<Vec
<&str>>().join(":");
509 if let Some(expected_fingerprint
) = expected_fingerprint
{
510 let expected_fingerprint
= expected_fingerprint
.to_lowercase();
511 if expected_fingerprint
== fp_string
{
512 return Ok(Some(fp_string
));
514 eprintln
!("WARNING: certificate fingerprint does not match expected fingerprint!");
515 eprintln
!("expected: {}", expected_fingerprint
);
519 // If we're on a TTY, query the user
520 if interactive
&& tty
::stdin_isatty() {
521 eprintln
!("fingerprint: {}", fp_string
);
523 eprint
!("Are you sure you want to continue connecting? (y/n): ");
524 let _
= std
::io
::stdout().flush();
525 use std
::io
::{BufRead, BufReader}
;
526 let mut line
= String
::new();
527 match BufReader
::new(std
::io
::stdin()).read_line(&mut line
) {
529 let trimmed
= line
.trim();
530 if trimmed
== "y" || trimmed
== "Y" {
531 return Ok(Some(fp_string
));
532 } else if trimmed
== "n" || trimmed
== "N" {
533 bail
!("Certificate fingerprint was not confirmed.");
538 Err(err
) => bail
!("Certificate fingerprint was not confirmed - {}.", err
),
543 bail
!("Certificate fingerprint was not confirmed.");
546 pub async
fn request(&self, mut req
: Request
<Body
>) -> Result
<Value
, Error
> {
548 let client
= self.client
.clone();
550 let auth
= self.login().await?
;
551 if auth
.auth_id
.is_token() {
552 let enc_api_token
= format
!("PBSAPIToken {}:{}", auth
.auth_id
, percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
553 req
.headers_mut().insert("Authorization", HeaderValue
::from_str(&enc_api_token
).unwrap());
555 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
556 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
557 req
.headers_mut().insert("CSRFPreventionToken", HeaderValue
::from_str(&auth
.token
).unwrap());
560 Self::api_request(client
, req
).await
567 ) -> Result
<Value
, Error
> {
568 let req
= Self::request_builder(&self.server
, self.port
, "GET", path
, data
)?
;
569 self.request(req
).await
576 ) -> Result
<Value
, Error
> {
577 let req
= Self::request_builder(&self.server
, self.port
, "DELETE", path
, data
)?
;
578 self.request(req
).await
585 ) -> Result
<Value
, Error
> {
586 let req
= Self::request_builder(&self.server
, self.port
, "POST", path
, data
)?
;
587 self.request(req
).await
594 ) -> Result
<Value
, Error
> {
595 let req
= Self::request_builder(&self.server
, self.port
, "PUT", path
, data
)?
;
596 self.request(req
).await
599 pub async
fn download(
602 output
: &mut (dyn Write
+ Send
),
603 ) -> Result
<(), Error
> {
604 let mut req
= Self::request_builder(&self.server
, self.port
, "GET", path
, None
)?
;
606 let client
= self.client
.clone();
608 let auth
= self.login().await?
;
610 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
611 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
613 let resp
= tokio
::time
::timeout(
618 .map_err(|_
| format_err
!("http download request timed out"))??
;
619 let status
= resp
.status();
620 if !status
.is_success() {
621 HttpClient
::api_response(resp
)
622 .map(|_
| Err(format_err
!("unknown error")))
626 .map_err(Error
::from
)
627 .try_fold(output
, move |acc
, chunk
| async
move {
628 acc
.write_all(&chunk
)?
;
642 ) -> Result
<Value
, Error
> {
644 let query
= match data
{
645 Some(data
) => Some(json_object_to_query(data
)?
),
648 let url
= build_uri(&self.server
, self.port
, path
, query
)?
;
650 let req
= Request
::builder()
653 .header("User-Agent", "proxmox-backup-client/1.0")
654 .header("Content-Type", content_type
)
655 .body(body
).unwrap();
657 self.request(req
).await
660 pub async
fn start_h2_connection(
662 mut req
: Request
<Body
>,
663 protocol_name
: String
,
664 ) -> Result
<(H2Client
, futures
::future
::AbortHandle
), Error
> {
666 let client
= self.client
.clone();
667 let auth
= self.login().await?
;
669 if auth
.auth_id
.is_token() {
670 let enc_api_token
= format
!("PBSAPIToken {}:{}", auth
.auth_id
, percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
671 req
.headers_mut().insert("Authorization", HeaderValue
::from_str(&enc_api_token
).unwrap());
673 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
674 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
675 req
.headers_mut().insert("CSRFPreventionToken", HeaderValue
::from_str(&auth
.token
).unwrap());
678 req
.headers_mut().insert("UPGRADE", HeaderValue
::from_str(&protocol_name
).unwrap());
680 let resp
= tokio
::time
::timeout(
685 .map_err(|_
| format_err
!("http upgrade request timed out"))??
;
686 let status
= resp
.status();
688 if status
!= http
::StatusCode
::SWITCHING_PROTOCOLS
{
689 Self::api_response(resp
).await?
;
690 bail
!("unknown error");
693 let upgraded
= hyper
::upgrade
::on(resp
).await?
;
695 let max_window_size
= (1 << 31) - 2;
697 let (h2
, connection
) = h2
::client
::Builder
::new()
698 .initial_connection_window_size(max_window_size
)
699 .initial_window_size(max_window_size
)
700 .max_frame_size(4*1024*1024)
704 let connection
= connection
705 .map_err(|_
| eprintln
!("HTTP/2.0 connection failed"));
707 let (connection
, abort
) = futures
::future
::abortable(connection
);
708 // A cancellable future returns an Option which is None when cancelled and
709 // Some when it finished instead, since we don't care about the return type we
710 // need to map it away:
711 let connection
= connection
.map(|_
| ());
713 // Spawn a new task to drive the connection state
714 tokio
::spawn(connection
);
716 // Wait until the `SendRequest` handle has available capacity.
717 let c
= h2
.ready().await?
;
718 Ok((H2Client
::new(c
), abort
))
721 async
fn credentials(
722 client
: Client
<HttpsConnector
>,
727 ) -> Result
<AuthInfo
, Error
> {
728 let data
= json
!({ "username": username, "password": password }
);
729 let req
= Self::request_builder(&server
, port
, "POST", "/api2/json/access/ticket", Some(data
))?
;
730 let cred
= Self::api_request(client
, req
).await?
;
731 let auth
= AuthInfo
{
732 auth_id
: cred
["data"]["username"].as_str().unwrap().parse()?
,
733 ticket
: cred
["data"]["ticket"].as_str().unwrap().to_owned(),
734 token
: cred
["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
740 async
fn api_response(response
: Response
<Body
>) -> Result
<Value
, Error
> {
741 let status
= response
.status();
742 let data
= hyper
::body
::to_bytes(response
.into_body()).await?
;
744 let text
= String
::from_utf8(data
.to_vec()).unwrap();
745 if status
.is_success() {
749 let value
: Value
= serde_json
::from_str(&text
)?
;
753 Err(Error
::from(HttpError
::new(status
, text
)))
757 async
fn api_request(
758 client
: Client
<HttpsConnector
>,
760 ) -> Result
<Value
, Error
> {
763 tokio
::time
::timeout(
768 .map_err(|_
| format_err
!("http request timed out"))??
772 // Read-only access to server property
773 pub fn server(&self) -> &str {
777 pub fn port(&self) -> u16 {
781 pub fn request_builder(server
: &str, port
: u16, method
: &str, path
: &str, data
: Option
<Value
>) -> Result
<Request
<Body
>, Error
> {
782 if let Some(data
) = data
{
783 if method
== "POST" {
784 let url
= build_uri(server
, port
, path
, None
)?
;
785 let request
= Request
::builder()
788 .header("User-Agent", "proxmox-backup-client/1.0")
789 .header(hyper
::header
::CONTENT_TYPE
, "application/json")
790 .body(Body
::from(data
.to_string()))?
;
793 let query
= json_object_to_query(data
)?
;
794 let url
= build_uri(server
, port
, path
, Some(query
))?
;
795 let request
= Request
::builder()
798 .header("User-Agent", "proxmox-backup-client/1.0")
799 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
800 .body(Body
::empty())?
;
804 let url
= build_uri(server
, port
, path
, None
)?
;
805 let request
= Request
::builder()
808 .header("User-Agent", "proxmox-backup-client/1.0")
809 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
810 .body(Body
::empty())?
;
817 impl Drop
for HttpClient
{
819 self.ticket_abort
.abort();
825 pub struct H2Client
{
826 h2
: h2
::client
::SendRequest
<bytes
::Bytes
>,
831 pub fn new(h2
: h2
::client
::SendRequest
<bytes
::Bytes
>) -> Self {
839 ) -> Result
<Value
, Error
> {
840 let req
= Self::request_builder("localhost", "GET", path
, param
, None
).unwrap();
841 self.request(req
).await
848 ) -> Result
<Value
, Error
> {
849 let req
= Self::request_builder("localhost", "PUT", path
, param
, None
).unwrap();
850 self.request(req
).await
857 ) -> Result
<Value
, Error
> {
858 let req
= Self::request_builder("localhost", "POST", path
, param
, None
).unwrap();
859 self.request(req
).await
862 pub async
fn download
<W
: Write
+ Send
>(
865 param
: Option
<Value
>,
867 ) -> Result
<(), Error
> {
868 let request
= Self::request_builder("localhost", "GET", path
, param
, None
).unwrap();
870 let response_future
= self.send_request(request
, None
).await?
;
872 let resp
= response_future
.await?
;
874 let status
= resp
.status();
875 if !status
.is_success() {
876 H2Client
::h2api_response(resp
).await?
; // raise error
880 let mut body
= resp
.into_body();
881 while let Some(chunk
) = body
.data().await
{
883 body
.flow_control().release_capacity(chunk
.len())?
;
884 output
.write_all(&chunk
)?
;
892 method
: &str, // POST or PUT
894 param
: Option
<Value
>,
897 ) -> Result
<Value
, Error
> {
898 let request
= Self::request_builder("localhost", method
, path
, param
, Some(content_type
)).unwrap();
900 let mut send_request
= self.h2
.clone().ready().await?
;
902 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
904 PipeToSendStream
::new(bytes
::Bytes
::from(data
), stream
).await?
;
907 .map_err(Error
::from
)
908 .and_then(Self::h2api_response
)
914 request
: Request
<()>,
915 ) -> Result
<Value
, Error
> {
917 self.send_request(request
, None
)
918 .and_then(move |response
| {
920 .map_err(Error
::from
)
921 .and_then(Self::h2api_response
)
928 request
: Request
<()>,
929 data
: Option
<bytes
::Bytes
>,
930 ) -> impl Future
<Output
= Result
<h2
::client
::ResponseFuture
, Error
>> {
934 .map_err(Error
::from
)
935 .and_then(move |mut send_request
| async
move {
936 if let Some(data
) = data
{
937 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
938 PipeToSendStream
::new(data
, stream
).await?
;
941 let (response
, _stream
) = send_request
.send_request(request
, true).unwrap();
947 pub async
fn h2api_response(
948 response
: Response
<h2
::RecvStream
>,
949 ) -> Result
<Value
, Error
> {
950 let status
= response
.status();
952 let (_head
, mut body
) = response
.into_parts();
954 let mut data
= Vec
::new();
955 while let Some(chunk
) = body
.data().await
{
957 // Whenever data is received, the caller is responsible for
958 // releasing capacity back to the server once it has freed
959 // the data from memory.
960 // Let the server send more data.
961 body
.flow_control().release_capacity(chunk
.len())?
;
965 let text
= String
::from_utf8(data
.to_vec()).unwrap();
966 if status
.is_success() {
970 let mut value
: Value
= serde_json
::from_str(&text
)?
;
971 if let Some(map
) = value
.as_object_mut() {
972 if let Some(data
) = map
.remove("data") {
976 bail
!("got result without data property");
979 Err(Error
::from(HttpError
::new(status
, text
)))
983 // Note: We always encode parameters with the url
984 pub fn request_builder(
988 param
: Option
<Value
>,
989 content_type
: Option
<&str>,
990 ) -> Result
<Request
<()>, Error
> {
991 let path
= path
.trim_matches('
/'
);
993 let content_type
= content_type
.unwrap_or("application/x-www-form-urlencoded");
994 let query
= match param
{
996 let query
= json_object_to_query(param
)?
;
997 // We detected problem with hyper around 6000 characters - so we try to keep on the safe side
998 if query
.len() > 4096 {
999 bail
!("h2 query data too large ({} bytes) - please encode data inside body", query
.len());
1006 let url
= build_uri(server
, 8007, path
, query
)?
;
1007 let request
= Request
::builder()
1010 .header("User-Agent", "proxmox-backup-client/1.0")
1011 .header(hyper
::header
::CONTENT_TYPE
, content_type
)