♻️ Fix pedantic clippy warnings
parent
eada65d94b
commit
7bd2450698
|
@ -12,6 +12,7 @@ ctrlc = { version = "3.1.7", features = [ "termination" ] }
|
||||||
futures = "0.3.7"
|
futures = "0.3.7"
|
||||||
hyper = "0.13.8"
|
hyper = "0.13.8"
|
||||||
serde = {version = "1.0.117", features = ["derive"]}
|
serde = {version = "1.0.117", features = ["derive"]}
|
||||||
|
thiserror = "1.0.22"
|
||||||
tokio = { version = "0.2.22", features = ["full"] }
|
tokio = { version = "0.2.22", features = ["full"] }
|
||||||
tracing = "0.1.21"
|
tracing = "0.1.21"
|
||||||
tracing-futures = "0.2.4"
|
tracing-futures = "0.2.4"
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
// False positive on futures::select! macro
|
||||||
|
#![allow (clippy::mut_mut)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::Cell,
|
cell::Cell,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
|
@ -11,6 +14,7 @@ use tokio::{
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn init () -> oneshot::Receiver <()> {
|
pub fn init () -> oneshot::Receiver <()> {
|
||||||
let (tx, rx) = oneshot::channel::<()> ();
|
let (tx, rx) = oneshot::channel::<()> ();
|
||||||
|
|
||||||
|
@ -44,10 +48,8 @@ use std::{
|
||||||
|
|
||||||
impl fmt::Display for ShutdownError {
|
impl fmt::Display for ShutdownError {
|
||||||
fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result {
|
fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result {
|
||||||
use ShutdownError::*;
|
|
||||||
|
|
||||||
let desc = match self {
|
let desc = match self {
|
||||||
ForcedShutdown => "Shutdown was forced after a timeout",
|
ShutdownError::ForcedShutdown => "Shutdown was forced after a timeout",
|
||||||
};
|
};
|
||||||
|
|
||||||
write! (f, "{}", desc)
|
write! (f, "{}", desc)
|
||||||
|
@ -64,6 +66,10 @@ pub struct ForcedShutdown {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ForcedShutdown {
|
impl ForcedShutdown {
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// `ForcedShutdown` if the graceful shutdown doesn't complete in time
|
||||||
|
|
||||||
pub async fn wrap_server <
|
pub async fn wrap_server <
|
||||||
T,
|
T,
|
||||||
F: Future <Output = T>
|
F: Future <Output = T>
|
||||||
|
@ -91,6 +97,7 @@ impl ForcedShutdown {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn init_with_force () -> (oneshot::Receiver <()>, ForcedShutdown) {
|
pub fn init_with_force () -> (oneshot::Receiver <()>, ForcedShutdown) {
|
||||||
let (tx, rx) = oneshot::channel ();
|
let (tx, rx) = oneshot::channel ();
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,19 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::*,
|
collections::HashMap,
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
};
|
};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use thiserror::Error;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
// Hyper doesn't seem to make it easy to de/ser requests
|
// Hyper doesn't seem to make it easy to de/ser requests
|
||||||
// and responses and stuff like that, so I do it by hand here.
|
// and responses and stuff like that, so I do it by hand here.
|
||||||
|
|
||||||
|
#[derive (Debug, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error ("Unsupported method")]
|
||||||
UnsupportedMethod,
|
UnsupportedMethod,
|
||||||
InvalidHeaderName,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From <hyper::header::InvalidHeaderName> for Error {
|
|
||||||
fn from (_x: hyper::header::InvalidHeaderName) -> Self {
|
|
||||||
Self::InvalidHeaderName
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug, Deserialize, Serialize)]
|
#[derive (Debug, Deserialize, Serialize)]
|
||||||
|
@ -56,6 +52,10 @@ pub struct RequestParts {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RequestParts {
|
impl RequestParts {
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// `UnsupportedMethod` if PTTH doesn't support the method
|
||||||
|
|
||||||
pub fn from_hyper (
|
pub fn from_hyper (
|
||||||
method: hyper::Method,
|
method: hyper::Method,
|
||||||
uri: String,
|
uri: String,
|
||||||
|
@ -67,8 +67,10 @@ impl RequestParts {
|
||||||
let method = Method::try_from (method)?;
|
let method = Method::try_from (method)?;
|
||||||
let headers = HashMap::from_iter (
|
let headers = HashMap::from_iter (
|
||||||
headers.into_iter ()
|
headers.into_iter ()
|
||||||
.filter_map (|(k, v)| k.map (|k| (k, v)))
|
.filter_map (|(k, v)| {
|
||||||
.map (|(k, v)| (String::from (k.as_str ()), v.as_bytes ().to_vec ()))
|
let (k, v) = k.map (|k| (k, v))?;
|
||||||
|
Some ((String::from (k.as_str ()), v.as_bytes ().to_vec ()))
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok (Self {
|
Ok (Self {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![warn (clippy::pedantic)]
|
||||||
|
|
||||||
pub mod graceful_shutdown;
|
pub mod graceful_shutdown;
|
||||||
pub mod http_serde;
|
pub mod http_serde;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
@ -12,6 +14,7 @@ pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4";
|
||||||
// The arguments are in order so they are in order overall:
|
// The arguments are in order so they are in order overall:
|
||||||
// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix"
|
// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix"
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str>
|
pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str>
|
||||||
{
|
{
|
||||||
if hay.starts_with (prefix) {
|
if hay.starts_with (prefix) {
|
||||||
|
|
|
@ -305,7 +305,7 @@ async fn handle_http_response (
|
||||||
futures::select! {
|
futures::select! {
|
||||||
x = body_tx.send (item).fuse () => if let Err (_) = x {
|
x = body_tx.send (item).fuse () => if let Err (_) = x {
|
||||||
info! ("Body closed while relaying. (Client hung up?)");
|
info! ("Body closed while relaying. (Client hung up?)");
|
||||||
body_finished_tx.send (ClientDisconnected).map_err (|_| LostServer).unwrap ();
|
body_finished_tx.send (ClientDisconnected).map_err (|_| LostServer)?;
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
_ = shutdown_watch_rx.recv ().fuse () => {
|
_ = shutdown_watch_rx.recv ().fuse () => {
|
||||||
|
@ -316,7 +316,7 @@ async fn handle_http_response (
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
debug! ("Finished relaying bytes");
|
debug! ("Finished relaying bytes");
|
||||||
body_finished_tx.send (StreamFinished).map_err (|_| LostServer).unwrap ();
|
body_finished_tx.send (StreamFinished).map_err (|_| LostServer)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -324,6 +324,8 @@ async fn handle_http_response (
|
||||||
else {
|
else {
|
||||||
debug! ("Can't relay bytes, relay is shutting down");
|
debug! ("Can't relay bytes, relay is shutting down");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok::<(), HandleHttpResponseError> (())
|
||||||
});
|
});
|
||||||
|
|
||||||
let body = Body::wrap_stream (body_rx);
|
let body = Body::wrap_stream (body_rx);
|
||||||
|
@ -346,7 +348,7 @@ async fn handle_http_response (
|
||||||
return Ok (error_reply (StatusCode::BAD_GATEWAY, msg)?);
|
return Ok (error_reply (StatusCode::BAD_GATEWAY, msg)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
relay_task.await?;
|
relay_task.await??;
|
||||||
|
|
||||||
debug! ("Connected server to client for streaming.");
|
debug! ("Connected server to client for streaming.");
|
||||||
match body_finished_rx.await {
|
match body_finished_rx.await {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![warn (clippy::pedantic)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
convert::TryFrom,
|
convert::TryFrom,
|
||||||
error::Error,
|
error::Error,
|
||||||
|
|
|
@ -13,6 +13,7 @@ base64 = "0.12.3"
|
||||||
blake3 = "0.3.7"
|
blake3 = "0.3.7"
|
||||||
futures = "0.3.7"
|
futures = "0.3.7"
|
||||||
handlebars = "3.5.1"
|
handlebars = "3.5.1"
|
||||||
|
http = "0.2.1"
|
||||||
hyper = "0.13.8"
|
hyper = "0.13.8"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
percent-encoding = "2.1.0"
|
percent-encoding = "2.1.0"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![warn (clippy::pedantic)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
|
@ -40,12 +42,6 @@ struct ServerState <'a> {
|
||||||
hidden_path: Option <PathBuf>,
|
hidden_path: Option <PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn status_reply <B: Into <Body>> (status: StatusCode, b: B)
|
|
||||||
-> Result <Response <Body>, hyper::http::Error>
|
|
||||||
{
|
|
||||||
Response::builder ().status (status).body (b.into ())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
-> Result <Response <Body>, ServerError>
|
-> Result <Response <Body>, ServerError>
|
||||||
{
|
{
|
||||||
|
@ -53,16 +49,13 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
|
|
||||||
debug! ("req.uri () = {:?}", req.uri ());
|
debug! ("req.uri () = {:?}", req.uri ());
|
||||||
|
|
||||||
let path_and_query = req.uri ().path_and_query ().map (|x| x.as_str ()).unwrap_or_else (|| req.uri ().path ());
|
let path_and_query = req.uri ().path_and_query ().map_or_else (|| req.uri ().path (), http::uri::PathAndQuery::as_str);
|
||||||
|
|
||||||
let path_and_query = path_and_query.into ();
|
let path_and_query = path_and_query.into ();
|
||||||
|
|
||||||
let (parts, _) = req.into_parts ();
|
let (parts, _) = req.into_parts ();
|
||||||
|
|
||||||
let ptth_req = match RequestParts::from_hyper (parts.method, path_and_query, parts.headers) {
|
let ptth_req = RequestParts::from_hyper (parts.method, path_and_query, parts.headers)?;
|
||||||
Ok (x) => x,
|
|
||||||
_ => return Ok (status_reply (StatusCode::BAD_REQUEST, "Bad request")?),
|
|
||||||
};
|
|
||||||
|
|
||||||
let default_root = PathBuf::from ("./");
|
let default_root = PathBuf::from ("./");
|
||||||
let file_server_root: &std::path::Path = state.config.file_server_root
|
let file_server_root: &std::path::Path = state.config.file_server_root
|
||||||
|
@ -82,14 +75,11 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
let mut resp = Response::builder ()
|
let mut resp = Response::builder ()
|
||||||
.status (StatusCode::from (ptth_resp.parts.status_code));
|
.status (StatusCode::from (ptth_resp.parts.status_code));
|
||||||
|
|
||||||
for (k, v) in ptth_resp.parts.headers.into_iter () {
|
for (k, v) in ptth_resp.parts.headers {
|
||||||
resp = resp.header (hyper::header::HeaderName::from_str (&k)?, v);
|
resp = resp.header (hyper::header::HeaderName::from_str (&k)?, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = ptth_resp.body
|
let body = ptth_resp.body.map_or_else (Body::empty, Body::wrap_stream);
|
||||||
.map (Body::wrap_stream)
|
|
||||||
.unwrap_or_else (Body::empty)
|
|
||||||
;
|
|
||||||
|
|
||||||
Ok (resp.body (body)?)
|
Ok (resp.body (body)?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![warn (clippy::pedantic)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
|
|
@ -13,4 +13,7 @@ pub enum ServerError {
|
||||||
|
|
||||||
#[error ("Can't parse wrapped requests")]
|
#[error ("Can't parse wrapped requests")]
|
||||||
CantParseWrappedRequests (rmp_serde::decode::Error),
|
CantParseWrappedRequests (rmp_serde::decode::Error),
|
||||||
|
|
||||||
|
#[error ("Can't convert Hyper request to PTTH request")]
|
||||||
|
CantConvertHyperToPtth (#[from] ptth_core::http_serde::Error),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue