diff options
author | amesgen <amesgen@amesgen.de> | 2020-10-14 15:38:04 +0200 |
---|---|---|
committer | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-10-23 12:05:54 +0200 |
commit | eb324f60bbd4184057797f72cc3db34e6160497d (patch) | |
tree | c5c5438a2640435c419c8ed496693301dede9544 | |
parent | 731f01cf5f3699ab738333a65a541d3fb2f25e15 (diff) |
update net to futures=0.2
-rw-r--r-- | Cargo.lock | 340 | ||||
-rw-r--r-- | net/Cargo.toml | 11 | ||||
-rw-r--r-- | net/src/lib.rs | 153 | ||||
-rw-r--r-- | net/src/wkd.rs | 68 | ||||
-rw-r--r-- | net/tests/hkp.rs | 135 |
5 files changed, 394 insertions, 313 deletions
@@ -46,6 +46,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1fd36ffbb1fb7c834eac128ea8d0e310c5aeb635548f9d58861e1308d46e71c" [[package]] +name = "arc-swap" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" + +[[package]] name = "arrayref" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -250,11 +256,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" dependencies = [ "byteorder", - "either", "iovec", ] [[package]] +name = "bytes" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" + +[[package]] name = "bzip2" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -812,13 +823,61 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7e4c2612746b0df8fed4ce0c69156021b704c9aefa360311c04e6e9e002eed" [[package]] -name = "futures-cpupool" -version = "0.1.8" +name = "futures-channel" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" +checksum = "a7a4d35f7401e948629c9c3d6638fb9bf94e0b2121e96c3b428cc4e631f3eb74" dependencies = [ - "futures", - "num_cpus", + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d674eaa0056896d5ada519900dbf97ead2e46a7b6621e8160d79e2f2e1e2784b" + +[[package]] +name = "futures-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f57ed14da4603b2554682e9f2ff3c65d7567b53188db96cb71538217fc64581b" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8764258ed64ebc5d9ed185cf86a95db5cac810269c5d20ececb32e0088abbd" + +[[package]] +name = "futures-task" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dd26820a9f3637f1302da8bceba3ff33adbe53464b54ca24d4e2d4f1db30f94" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-util" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a894a0acddba51a2d49a6f4263b1e64b8c579ece8af50fa86503d52cd1eea34" +dependencies = [ + "futures-core", + "futures-macro", + "futures-task", + "pin-project", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", ] [[package]] @@ -865,20 +924,21 @@ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" [[package]] name = "h2" -version = "0.1.26" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" +checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" dependencies = [ - "byteorder", - "bytes", + "bytes 0.5.6", "fnv", - "futures", + "futures-core", + "futures-sink", + "futures-util", "http", "indexmap", - "log", "slab", - "string", - "tokio-io", + "tokio 0.2.22", + "tokio-util", + "tracing", ] [[package]] @@ -919,25 +979,23 @@ dependencies = [ [[package]] name = "http" -version = "0.1.21" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" dependencies = [ - "bytes", + "bytes 0.5.6", "fnv", "itoa", ] [[package]] name = "http-body" -version = "0.1.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" dependencies = [ - "bytes", - "futures", + "bytes 0.5.6", "http", - "tokio-buf", ] [[package]] @@ -947,46 +1005,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" [[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + +[[package]] name = "hyper" -version = "0.12.35" +version = "0.13.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dbe6ed1438e1f8ad955a4701e9a944938e9519f6888d12d8558b645e247d5f6" +checksum = "2f3afcfae8af5ad0576a31e768415edb627824129e8e5a29b8bfccb2f234e835" dependencies = [ - "bytes", - "futures", - "futures-cpupool", + "bytes 0.5.6", + "futures-channel", + "futures-core", + "futures-util", "h2", "http", "http-body", "httparse", - "iovec", + "httpdate", "itoa", - "log", - "net2", - "rustc_version", - "time", - "tokio", - "tokio-buf", - "tokio-executor", - "tokio-io", - "tokio-reactor", - "tokio-tcp", - "tokio-threadpool", - "tokio-timer", + "pin-project", + "socket2", + "tokio 0.2.22", + "tower-service", + "tracing", "want", ] [[package]] name = "hyper-tls" -version = "0.3.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f" +checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" dependencies = [ - "bytes", - "futures", + "bytes 0.5.6", "hyper", "native-tls", - "tokio-io", + "tokio 0.2.22", + "tokio-tls", ] [[package]] @@ -1382,6 +1440,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37fd5004feb2ce328a52b0b3d01dbf4ffff72583493900ed15f22d4111c51693" [[package]] +name = "once_cell" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + +[[package]] name = "opaque-debug" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1432,14 +1496,14 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e57fea504fea33f9fbb5f49f378359030e7e026a6ab849bb9e8f0787376f1bf" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "libc", "log", "mio-named-pipes", "miow 0.3.5", "rand", - "tokio", + "tokio 0.1.22", "tokio-named-pipes", "tokio-uds", "winapi 0.3.9", @@ -1503,6 +1567,38 @@ dependencies = [ ] [[package]] +name = "pin-project" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] name = "pkg-config" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1559,6 +1655,18 @@ dependencies = [ ] [[package]] +name = "proc-macro-hack" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + +[[package]] name = "proc-macro2" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1883,7 +1991,7 @@ dependencies = [ "socket2", "tempfile", "thiserror", - "tokio", + "tokio 0.1.22", "tokio-core", "tokio-io", "winapi 0.3.9", @@ -1894,7 +2002,7 @@ name = "sequoia-net" version = "0.20.0" dependencies = [ "anyhow", - "futures", + "futures-util", "http", "hyper", "hyper-tls", @@ -1906,8 +2014,7 @@ dependencies = [ "sequoia-openpgp", "tempfile", "thiserror", - "tokio-core", - "tokio-io", + "tokio 0.2.22", "url", "zbase32", ] @@ -2084,6 +2191,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" [[package]] +name = "signal-hook-registry" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" +dependencies = [ + "arc-swap", + "libc", +] + +[[package]] name = "signature" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2135,15 +2252,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] -name = "string" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d" -dependencies = [ - "bytes", -] - -[[package]] name = "string_cache" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2307,7 +2415,7 @@ version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "mio", "num_cpus", @@ -2326,14 +2434,27 @@ dependencies = [ ] [[package]] -name = "tokio-buf" -version = "0.1.1" +name = "tokio" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46" +checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ - "bytes", - "either", - "futures", + "bytes 0.5.6", + "fnv", + "futures-core", + "iovec", + "lazy_static", + "libc", + "memchr", + "mio", + "mio-named-pipes", + "mio-uds", + "num_cpus", + "pin-project-lite", + "signal-hook-registry", + "slab", + "tokio-macros", + "winapi 0.3.9", ] [[package]] @@ -2342,7 +2463,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25b2998660ba0e70d18684de5d06b70b70a3a747469af9dea7618cc59e75976b" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "tokio-io", ] @@ -2353,13 +2474,13 @@ version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "iovec", "log", "mio", "scoped-tls", - "tokio", + "tokio 0.1.22", "tokio-executor", "tokio-io", "tokio-reactor", @@ -2403,22 +2524,33 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "log", ] [[package]] +name = "tokio-macros" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "tokio-named-pipes" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d282d483052288b2308ba5ee795f5673b159c9bdf63c385a05609da782a5eae" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "mio", "mio-named-pipes", - "tokio", + "tokio 0.1.22", ] [[package]] @@ -2456,7 +2588,7 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98df18ed66e3b72e742f185882a9e201892407957e45fbff8da17ae7a7c51f72" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "iovec", "mio", @@ -2494,12 +2626,22 @@ dependencies = [ ] [[package]] +name = "tokio-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343" +dependencies = [ + "native-tls", + "tokio 0.2.22", +] + +[[package]] name = "tokio-udp" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2a0b10e610b39c38b031a2fcab08e4b82f16ece36504988dcbd81dbba650d82" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "log", "mio", @@ -2514,7 +2656,7 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab57a4ac4111c8c9dbcf70779f6fc8bc35ae4b2454809febac840ad19bd7e4e0" dependencies = [ - "bytes", + "bytes 0.4.12", "futures", "iovec", "libc", @@ -2527,6 +2669,47 @@ dependencies = [ ] [[package]] +name = "tokio-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" +dependencies = [ + "bytes 0.5.6", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio 0.2.22", +] + +[[package]] +name = "tower-service" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" + +[[package]] +name = "tracing" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0987850db3733619253fe60e17cb59b82d37c7e6c0236bb81e4d6b87c879f27" +dependencies = [ + "cfg-if 0.1.10", + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +dependencies = [ + "lazy_static", +] + +[[package]] name = "try-lock" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2611,11 +2794,10 @@ checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" [[package]] name = "want" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ - "futures", "log", "try-lock", ] diff --git a/net/Cargo.toml b/net/Cargo.toml index fc0525d8..bb546e3c 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -25,22 +25,21 @@ sequoia-openpgp = { path = "../openpgp", version = "0.20", default-features = fa sequoia-core = { path = "../core", version = "0.20" } anyhow = "1" -futures = "0.1" -http = "0.1.5" -hyper = "0.12" -hyper-tls = "0.3" +futures-util = "0.3" +http = "0.2" +hyper = "0.13" +hyper-tls = "0.4" libc = "0.2.33" native-tls = "0.2.0" percent-encoding = "2.1" tempfile = "3.1" thiserror = "1" -tokio-core = "0.1" -tokio-io = "0.1.4" url = "2.1" zbase32 = "0.1.2" [dev-dependencies] rand = { version = "0.7", default-features = false } +tokio = { version = "0.2", features = ["full"] } [features] default = ["compression"] diff --git a/net/src/lib.rs b/net/src/lib.rs index cdd05cc1..d39c62cc 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -14,37 +14,20 @@ //! [SKS keyserver]: https://www.sks-keyservers.net/overview-of-pools.php#pool_hkps //! //! ```no_run -//! # use tokio_core; -//! # use sequoia_openpgp as openpgp; -//! # use openpgp::KeyID; +//! # use sequoia_openpgp::KeyID; //! # use sequoia_core::Context; //! # use sequoia_net::{KeyServer, Result}; -//! # use tokio_core::reactor::Core; -//! # fn main() { f().unwrap(); } -//! # fn f() -> Result<()> { -//! let mut core = Core::new().unwrap(); +//! # async fn f() -> Result<()> { //! let ctx = Context::new()?; //! let mut ks = KeyServer::keys_openpgp_org(&ctx)?; -//! let keyid = "31855247603831FD".parse().unwrap(); -//! println!("{:?}", core.run(ks.get(&keyid))); -//! Ok(()) +//! let keyid = "31855247603831FD".parse()?; +//! println!("{:?}", ks.get(&keyid).await?); +//! # Ok(()) //! # } //! ``` #![warn(missing_docs)] -use sequoia_openpgp as openpgp; -use sequoia_core; - -use futures; -use http; -use hyper; -use hyper_tls; -use native_tls; -use percent_encoding; -use url; - -use futures::{future, Future, Stream}; use hyper::client::{ResponseFuture, HttpConnector}; use hyper::header::{CONTENT_LENGTH, CONTENT_TYPE, HeaderValue}; use hyper::{Client, Body, StatusCode, Request}; @@ -56,9 +39,9 @@ use std::convert::From; use std::io::Cursor; use url::Url; -use crate::openpgp::Cert; -use crate::openpgp::parse::Parse; -use crate::openpgp::{KeyID, armor, serialize::Serialize}; +use sequoia_openpgp::Cert; +use sequoia_openpgp::parse::Parse; +use sequoia_openpgp::{KeyID, armor, serialize::Serialize}; use sequoia_core::{Context, NetworkPolicy}; pub mod wkd; @@ -78,8 +61,6 @@ pub struct KeyServer { uri: Url, } -const DNS_WORKER: usize = 4; - impl KeyServer { /// Returns a handle for the given URI. pub fn new(ctx: &Context, uri: &str) -> Result<Self> { @@ -90,7 +71,7 @@ impl KeyServer { "hkp" => Box::new(Client::new()), "hkps" => { Box::new(Client::builder() - .build(HttpsConnector::new(DNS_WORKER)?)) + .build(HttpsConnector::new())) }, _ => return Err(Error::MalformedUri.into()), }; @@ -110,10 +91,10 @@ impl KeyServer { tls.add_root_certificate(cert); let tls = tls.build()?; - let mut http = HttpConnector::new(DNS_WORKER); + let mut http = HttpConnector::new(); http.enforce_http(false); Box::new(Client::builder() - .build(HttpsConnector::from((http, tls)))) + .build(HttpsConnector::from((http, tls.into())))) }; Self::make(ctx, client, uri) @@ -150,78 +131,40 @@ impl KeyServer { } /// Retrieves the key with the given `keyid`. - pub fn get(&mut self, keyid: &KeyID) - -> Box<dyn Future<Item=Cert, Error=anyhow::Error> + 'static> { + pub async fn get(&mut self, keyid: &KeyID) -> Result<Cert> { let keyid_want = keyid.clone(); let uri = self.uri.join( - &format!("pks/lookup?op=get&options=mr&search=0x{:X}", keyid)); - if let Err(e) = uri { - // This shouldn't happen, but better safe than sorry. - return Box::new(future::err(Error::from(e).into())); + &format!("pks/lookup?op=get&options=mr&search=0x{:X}", keyid))?; + + let res = self.client.do_get(uri).await?; + match res.status() { + StatusCode::OK => { + let body = hyper::body::to_bytes(res.into_body()).await?; + let r = armor::Reader::new( + Cursor::new(body), + armor::ReaderMode::Tolerant(Some(armor::Kind::PublicKey)), + ); + let cert = Cert::from_reader(r)?; + if cert.keys().any(|ka| KeyID::from(ka.fingerprint()) == keyid_want) { + Ok(cert) + } else { + Err(Error::MismatchedKeyID(keyid_want, cert).into()) + } + } + StatusCode::NOT_FOUND => Err(Error::NotFound.into()), + n => Err(Error::HttpStatus(n).into()), } - - Box::new(self.client.do_get(uri.unwrap()) - .from_err() - .and_then(|res| { - let status = res.status(); - res.into_body().concat2().from_err() - .and_then(move |body| match status { - StatusCode::OK => { - let c = Cursor::new(body.as_ref()); - let r = armor::Reader::new( - c, - armor::ReaderMode::Tolerant( - Some(armor::Kind::PublicKey))); - match Cert::from_reader(r) { - Ok(cert) => { - if cert.keys().any(|ka| { - KeyID::from(ka.fingerprint()) - == keyid_want - }) { - future::done(Ok(cert)) - } else { - future::err(Error::MismatchedKeyID( - keyid_want, cert).into()) - } - }, - Err(e) => { - future::err(e.into()) - } - } - }, - StatusCode::NOT_FOUND => - future::err(Error::NotFound.into()), - n => future::err(Error::HttpStatus(n).into()), - }) - })) } /// Sends the given key to the server. - pub fn send(&mut self, key: &Cert) - -> Box<dyn Future<Item=(), Error=anyhow::Error> + 'static> { - use crate::openpgp::armor::{Writer, Kind}; + pub async fn send(&mut self, key: &Cert) -> Result<()> { + use sequoia_openpgp::armor::{Writer, Kind}; - let uri = - match self.uri.join("pks/add") { - Err(e) => - // This shouldn't happen, but better safe than sorry. - return Box::new(future::err(Error::from(e).into())), - Ok(u) => u, - }; - - let mut w = match Writer::new(Vec::new(), Kind::PublicKey) { - Ok(v) => v, - Err(e) => return Box::new(future::err(e.into())), - }; + let uri = self.uri.join("pks/add")?; + let mut w = Writer::new(Vec::new(), Kind::PublicKey)?; + key.serialize(&mut w)?; - if let Err(e) = key.serialize(&mut w) { - return Box::new(future::err(e)); - } - - let armored_blob = match w.finalize() { - Ok(v) => v, - Err(e) => return Box::new(future::err(e.into())), - }; + let armored_blob = w.finalize()?; // Prepare to send url-encoded data. let mut post_data = b"keytext=".to_vec(); @@ -229,12 +172,7 @@ impl KeyServer { .collect::<String>().as_bytes()); let length = post_data.len(); - let mut request = match Request::post(url2uri(uri)) - .body(Body::from(post_data)) - { - Ok(r) => r, - Err(e) => return Box::new(future::err(Error::from(e).into())), - }; + let mut request = Request::post(url2uri(uri)).body(Body::from(post_data))?; request.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded")); @@ -243,15 +181,12 @@ impl KeyServer { HeaderValue::from_str(&format!("{}", length)) .expect("cannot fail: only ASCII characters")); - Box::new(self.client.do_request(request) - .from_err() - .and_then(|res| { - match res.status() { - StatusCode::OK => future::ok(()), - StatusCode::NOT_FOUND => future::err(Error::ProtocolViolation.into()), - n => future::err(Error::HttpStatus(n).into()), - } - })) + let res = self.client.do_request(request).await?; + match res.status() { + StatusCode::OK => Ok(()), + StatusCode::NOT_FOUND => Err(Error::ProtocolViolation.into()), + n => Err(Error::HttpStatus(n).into()), + } } } diff --git a/net/src/wkd.rs b/net/src/wkd.rs index 759f17cd..e8fd9bed 100644 --- a/net/src/wkd.rs +++ b/net/src/wkd.rs @@ -21,20 +21,19 @@ use std::fs; use std::path |