[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]
base64 = "0.13"
futures = "0.1"
hyper = "0.11"
hyper = "0.12"
log = "0.4"
num-bigint = "0.3"
protobuf = "~2.14.0"

View file

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