[Connect] Migrate to hyper ~v12

This commit is contained in:
ashthespy 2021-01-23 22:21:42 +00:00
parent 6f5607d9ab
commit 931c8207fc
2 changed files with 36 additions and 26 deletions

View file

@ -20,7 +20,7 @@ version = "0.1.3"
[dependencies] [dependencies]
base64 = "0.13" base64 = "0.13"
futures = "0.1" futures = "0.1"
hyper = "0.11" hyper = "0.12"
log = "0.4" log = "0.4"
num-bigint = "0.3" num-bigint = "0.3"
protobuf = "~2.14.0" protobuf = "~2.14.0"

View file

@ -5,8 +5,10 @@ use base64;
use futures::sync::mpsc; use futures::sync::mpsc;
use futures::{Future, Poll, Stream}; use futures::{Future, Poll, Stream};
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
use hyper::server::{Http, Request, Response, Service};
use hyper::{self, Get, Post, StatusCode}; use hyper::{
self, server::conn::Http, service::Service, Body, Method, Request, Response, StatusCode,
};
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
#[cfg(feature = "with-dns-sd")] #[cfg(feature = "with-dns-sd")]
@ -67,7 +69,7 @@ impl Discovery {
fn handle_get_info( fn handle_get_info(
&self, &self,
_params: &BTreeMap<String, String>, _params: &BTreeMap<String, String>,
) -> ::futures::Finished<Response, hyper::Error> { ) -> ::futures::Finished<Response<hyper::Body>, hyper::Error> {
let public_key = self.0.public_key.to_bytes_be(); let public_key = self.0.public_key.to_bytes_be();
let public_key = base64::encode(&public_key); let public_key = base64::encode(&public_key);
@ -88,13 +90,13 @@ impl Discovery {
}); });
let body = result.to_string(); let body = result.to_string();
::futures::finished(Response::new().with_body(body)) ::futures::finished(Response::new(Body::from(body)))
} }
fn handle_add_user( fn handle_add_user(
&self, &self,
params: &BTreeMap<String, String>, params: &BTreeMap<String, String>,
) -> ::futures::Finished<Response, hyper::Error> { ) -> ::futures::Finished<Response<hyper::Body>, hyper::Error> {
let username = params.get("userName").unwrap(); let username = params.get("userName").unwrap();
let encrypted_blob = params.get("blob").unwrap(); let encrypted_blob = params.get("blob").unwrap();
let client_key = params.get("clientKey").unwrap(); let client_key = params.get("clientKey").unwrap();
@ -136,7 +138,7 @@ impl Discovery {
}); });
let body = result.to_string(); let body = result.to_string();
return ::futures::finished(Response::new().with_body(body)); return ::futures::finished(Response::new(Body::from(body)));
} }
let decrypted = { let decrypted = {
@ -161,30 +163,33 @@ impl Discovery {
}); });
let body = result.to_string(); let body = result.to_string();
::futures::finished(Response::new().with_body(body)) return ::futures::finished(Response::new(Body::from(body)));
} }
fn not_found(&self) -> ::futures::Finished<Response, hyper::Error> { fn not_found(&self) -> ::futures::Finished<Response<hyper::Body>, hyper::Error> {
::futures::finished(Response::new().with_status(StatusCode::NotFound)) let mut res = Response::default();
*res.status_mut() = StatusCode::NOT_FOUND;
::futures::finished(res)
} }
} }
impl Service for Discovery { impl Service for Discovery {
type Request = Request; type ReqBody = Body;
type Response = Response; type ResBody = Body;
type Error = hyper::Error; type Error = hyper::Error;
type Future = Box<dyn Future<Item = Response, Error = hyper::Error>>;
fn call(&self, request: Request) -> Self::Future { type Future = Box<dyn Future<Item = Response<(Self::ResBody)>, Error = hyper::Error> + Send>;
fn call(&mut self, request: Request<(Self::ReqBody)>) -> Self::Future {
let mut params = BTreeMap::new(); let mut params = BTreeMap::new();
let (method, uri, _, _, body) = request.deconstruct(); let (parts, body) = request.into_parts();
if let Some(query) = uri.query() {
if let Some(query) = parts.uri.query() {
params.extend(url::form_urlencoded::parse(query.as_bytes()).into_owned()); params.extend(url::form_urlencoded::parse(query.as_bytes()).into_owned());
} }
if method != Get { if parts.method != Method::GET {
debug!("{:?} {:?} {:?}", method, uri.path(), params); debug!("{:?} {:?} {:?}", parts.method, parts.uri.path(), params);
} }
let this = self.clone(); let this = self.clone();
@ -198,9 +203,9 @@ impl Service for Discovery {
params params
}) })
.and_then(move |params| { .and_then(move |params| {
match (method, params.get("action").map(AsRef::as_ref)) { match (parts.method, params.get("action").map(AsRef::as_ref)) {
(Get, Some("getInfo")) => this.handle_get_info(&params), (Method::GET, Some("getInfo")) => this.handle_get_info(&params),
(Post, Some("addUser")) => this.handle_add_user(&params), (Method::POST, Some("addUser")) => this.handle_add_user(&params),
_ => this.not_found(), _ => this.not_found(),
} }
}), }),
@ -232,7 +237,7 @@ pub fn discovery(
let http = Http::new(); let http = Http::new();
http.serve_addr_handle( http.serve_addr_handle(
&format!("0.0.0.0:{}", port).parse().unwrap(), &format!("0.0.0.0:{}", port).parse().unwrap(),
&handle, handle.new_tokio_handle(),
move || Ok(discovery.clone()), move || Ok(discovery.clone()),
) )
.unwrap() .unwrap()
@ -244,10 +249,15 @@ pub fn discovery(
let server_future = { let server_future = {
let handle = handle.clone(); let handle = handle.clone();
serve serve
.for_each(move |connection| { .for_each(
handle.spawn(connection.then(|_| Ok(()))); move |connecting: hyper::server::conn::Connecting<
hyper::server::conn::AddrStream,
futures::Failed<_, hyper::Error>,
>| {
handle.spawn(connecting.then(|_| Ok(())));
Ok(()) Ok(())
}) },
)
.then(|_| Ok(())) .then(|_| Ok(()))
}; };
handle.spawn(server_future); handle.spawn(server_future);