diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-06-05 18:47:09 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-06-05 18:47:09 +0200 |
commit | 5d2c20e2b5b93f5dd74b0faf016d67afc631b903 (patch) | |
tree | 5d8886077932770fd15afe92455f1acbd2151420 /net/src | |
parent | e475e149ec7d259bb57af24ad4b0b9b55e8116c4 (diff) |
net: Bump hyper to "0.12", hyper-tls to "0.2".
Diffstat (limited to 'net/src')
-rw-r--r-- | net/src/async.rs | 68 | ||||
-rw-r--r-- | net/src/lib.rs | 26 |
2 files changed, 57 insertions, 37 deletions
diff --git a/net/src/async.rs b/net/src/async.rs index 9ee962e2..3095c3dc 100644 --- a/net/src/async.rs +++ b/net/src/async.rs @@ -5,9 +5,9 @@ use failure; use futures::{future, Future, Stream}; -use hyper::client::{FutureResponse, HttpConnector}; -use hyper::header::{ContentLength, ContentType}; -use hyper::{self, Client, StatusCode, Request, Method}; +use hyper::client::{ResponseFuture, HttpConnector}; +use hyper::header::{CONTENT_LENGTH, CONTENT_TYPE, HeaderValue}; +use hyper::{self, Client, Body, StatusCode, Request}; use hyper_tls::HttpsConnector; use native_tls::{Certificate, TlsConnector}; use percent_encoding::{percent_encode, DEFAULT_ENCODE_SET}; @@ -40,15 +40,14 @@ const DNS_WORKER: usize = 4; impl KeyServer { /// Returns a handle for the given URI. - pub fn new(ctx: &Context, uri: &str, handle: &Handle) -> Result<Self> { + pub fn new(ctx: &Context, uri: &str, _handle: &Handle) -> Result<Self> { let uri: Url = uri.parse()?; let client: Box<AClient> = match uri.scheme() { - "hkp" => Box::new(Client::new(handle)), + "hkp" => Box::new(Client::new()), "hkps" => { - Box::new(Client::configure() - .connector(HttpsConnector::new(DNS_WORKER, handle)?) - .build(handle)) + Box::new(Client::builder() + .build(HttpsConnector::new(DNS_WORKER)?)) }, _ => return Err(Error::MalformedUri.into()), }; @@ -60,7 +59,7 @@ impl KeyServer { /// /// `cert` is used to authenticate the server. pub fn with_cert(ctx: &Context, uri: &str, cert: Certificate, - handle: &Handle) -> Result<Self> { + _handle: &Handle) -> Result<Self> { let uri: Url = uri.parse()?; let client: Box<AClient> = { @@ -68,11 +67,10 @@ impl KeyServer { ssl.add_root_certificate(cert)?; let ssl = ssl.build()?; - let mut http = HttpConnector::new(DNS_WORKER, handle); + let mut http = HttpConnector::new(DNS_WORKER); http.enforce_http(false); - Box::new(Client::configure() - .connector(HttpsConnector::from((http, ssl))) - .build(handle)) + Box::new(Client::builder() + .build(HttpsConnector::from((http, ssl)))) }; Self::make(ctx, client, uri) @@ -127,14 +125,16 @@ impl KeyServer { .from_err() .and_then(|res| { let status = res.status(); - res.body().concat2().from_err() + res.into_body().concat2().from_err() .and_then(move |body| match status { - StatusCode::Ok => { + StatusCode::OK => { let c = Cursor::new(body.as_ref()); - let r = armor::Reader::new(c, armor::Kind::PublicKey); + let r = armor::Reader::new( + c, armor::Kind::PublicKey); future::done(TPK::from_reader(r)) }, - StatusCode::NotFound => future::err(Error::NotFound.into()), + StatusCode::NOT_FOUND => + future::err(Error::NotFound.into()), n => future::err(Error::HttpStatus(n).into()), }) })) @@ -165,18 +165,28 @@ impl KeyServer { let mut post_data = b"keytext=".to_vec(); post_data.extend_from_slice(percent_encode(&armored_blob, KEYSERVER_ENCODE_SET) .collect::<String>().as_bytes()); + let length = post_data.len(); - let mut request = Request::new(Method::Post, url2uri(uri)); - request.headers_mut().set(ContentType::form_url_encoded()); - request.headers_mut().set(ContentLength(post_data.len() as u64)); - request.set_body(post_data); + 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())), + }; + request.headers_mut().insert( + CONTENT_TYPE, + HeaderValue::from_static("application/x-www-form-urlencoded")); + request.headers_mut().insert( + CONTENT_LENGTH, + 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::NotFound => future::err(Error::ProtocolViolation.into()), + StatusCode::OK => future::ok(()), + StatusCode::NOT_FOUND => future::err(Error::ProtocolViolation.into()), n => future::err(Error::HttpStatus(n).into()), } })) @@ -184,24 +194,24 @@ impl KeyServer { } trait AClient { - fn do_get(&mut self, uri: Url) -> FutureResponse; - fn do_request(&mut self, request: Request) -> FutureResponse; + fn do_get(&mut self, uri: Url) -> ResponseFuture; + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture; } impl AClient for Client<HttpConnector> { - fn do_get(&mut self, uri: Url) -> FutureResponse { + fn do_get(&mut self, uri: Url) -> ResponseFuture { self.get(url2uri(uri)) } - fn do_request(&mut self, request: Request) -> FutureResponse { + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture { self.request(request) } } impl AClient for Client<HttpsConnector<HttpConnector>> { - fn do_get(&mut self, uri: Url) -> FutureResponse { + fn do_get(&mut self, uri: Url) -> ResponseFuture { self.get(url2uri(uri)) } - fn do_request(&mut self, request: Request) -> FutureResponse { + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture { self.request(request) } } diff --git a/net/src/lib.rs b/net/src/lib.rs index d01aa93d..bb9c0bf4 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -36,6 +36,7 @@ extern crate sequoia_core; #[macro_use] extern crate failure; extern crate futures; +extern crate http; extern crate hyper; extern crate hyper_tls; extern crate native_tls; @@ -47,8 +48,8 @@ extern crate url; extern crate capnp_rpc; -use hyper::client::{FutureResponse, HttpConnector}; -use hyper::{Client, Request}; +use hyper::client::{ResponseFuture, HttpConnector}; +use hyper::{Client, Request, Body}; use hyper_tls::HttpsConnector; use native_tls::Certificate; use std::convert::From; @@ -114,24 +115,24 @@ impl KeyServer { } trait AClient { - fn do_get(&mut self, uri: Url) -> FutureResponse; - fn do_request(&mut self, request: Request) -> FutureResponse; + fn do_get(&mut self, uri: Url) -> ResponseFuture; + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture; } impl AClient for Client<HttpConnector> { - fn do_get(&mut self, uri: Url) -> FutureResponse { + fn do_get(&mut self, uri: Url) -> ResponseFuture { self.get(url2uri(uri)) } - fn do_request(&mut self, request: Request) -> FutureResponse { + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture { self.request(request) } } impl AClient for Client<HttpsConnector<HttpConnector>> { - fn do_get(&mut self, uri: Url) -> FutureResponse { + fn do_get(&mut self, uri: Url) -> ResponseFuture { self.get(url2uri(uri)) } - fn do_request(&mut self, request: Request) -> FutureResponse { + fn do_request(&mut self, request: Request<Body>) -> ResponseFuture { self.request(request) } } @@ -160,6 +161,9 @@ pub enum Error { /// A `hyper::error::UriError` occurred. #[fail(display = "URI Error")] UriError(url::ParseError), + /// A `http::Error` occurred. + #[fail(display = "http Error")] + HttpError(http::Error), /// A `hyper::Error` occurred. #[fail(display = "Hyper Error")] HyperError(hyper::Error), @@ -168,6 +172,12 @@ pub enum Error { TlsError(native_tls::Error), } +impl From<http::Error> for Error { + fn from(e: http::Error) -> Error { + Error::HttpError(e) + } +} + impl From<hyper::Error> for Error { fn from(e: hyper::Error) -> Error { Error::HyperError(e) |