♻️ Working on errors for file server and server
parent
c3ff3deb8e
commit
720aae2201
|
@ -23,6 +23,7 @@ use ptth_core::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use ptth_server::{
|
use ptth_server::{
|
||||||
|
errors::ServerError,
|
||||||
file_server,
|
file_server,
|
||||||
load_toml,
|
load_toml,
|
||||||
};
|
};
|
||||||
|
@ -46,7 +47,7 @@ fn status_reply <B: Into <Body>> (status: StatusCode, b: B)
|
||||||
}
|
}
|
||||||
|
|
||||||
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>, hyper::http::Error>
|
-> Result <Response <Body>, ServerError>
|
||||||
{
|
{
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
&ptth_req.uri,
|
&ptth_req.uri,
|
||||||
&ptth_req.headers,
|
&ptth_req.headers,
|
||||||
state.hidden_path.as_deref ()
|
state.hidden_path.as_deref ()
|
||||||
).await;
|
).await?;
|
||||||
|
|
||||||
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));
|
||||||
|
@ -90,7 +91,7 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
|
||||||
.unwrap_or_else (Body::empty)
|
.unwrap_or_else (Body::empty)
|
||||||
;
|
;
|
||||||
|
|
||||||
resp.body (body)
|
Ok (resp.body (body)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Deserialize)]
|
#[derive (Deserialize)]
|
||||||
|
|
|
@ -2,5 +2,15 @@ use thiserror::Error;
|
||||||
|
|
||||||
#[derive (Debug, Error)]
|
#[derive (Debug, Error)]
|
||||||
pub enum ServerError {
|
pub enum ServerError {
|
||||||
|
#[error ("File server error")]
|
||||||
|
FileServer (#[from] super::file_server::errors::FileServerError),
|
||||||
|
|
||||||
|
#[error ("Hyper HTTP error")]
|
||||||
|
Http (#[from] hyper::http::Error),
|
||||||
|
|
||||||
|
#[error ("Hyper invalid header name")]
|
||||||
|
InvalidHeaderName (#[from] hyper::header::InvalidHeaderName),
|
||||||
|
|
||||||
|
#[error ("Can't parse wrapped requests")]
|
||||||
|
CantParseWrappedRequests (rmp_serde::decode::Error),
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@ use thiserror::Error;
|
||||||
|
|
||||||
#[derive (Debug, Error)]
|
#[derive (Debug, Error)]
|
||||||
pub enum FileServerError {
|
pub enum FileServerError {
|
||||||
|
#[error ("Handlebars render error")]
|
||||||
|
Handlebars (#[from] handlebars::RenderError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,8 @@ use ptth_core::{
|
||||||
prefix_match,
|
prefix_match,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod errors;
|
pub mod errors;
|
||||||
|
use errors::FileServerError;
|
||||||
|
|
||||||
#[derive (Debug, Serialize)]
|
#[derive (Debug, Serialize)]
|
||||||
pub struct ServerInfo {
|
pub struct ServerInfo {
|
||||||
|
@ -239,11 +240,11 @@ async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
||||||
async fn serve_root (
|
async fn serve_root (
|
||||||
handlebars: &Handlebars <'static>,
|
handlebars: &Handlebars <'static>,
|
||||||
server_info: &ServerInfo
|
server_info: &ServerInfo
|
||||||
) -> Response
|
) -> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
let s = handlebars.render ("file_server_root", &server_info).unwrap ();
|
let s = handlebars.render ("file_server_root", &server_info)?;
|
||||||
|
|
||||||
serve_html (s)
|
Ok (serve_html (s))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serve_html (s: String) -> Response {
|
fn serve_html (s: String) -> Response {
|
||||||
|
@ -615,11 +616,11 @@ pub async fn serve_all (
|
||||||
headers: &HashMap <String, Vec <u8>>,
|
headers: &HashMap <String, Vec <u8>>,
|
||||||
hidden_path: Option <&Path>
|
hidden_path: Option <&Path>
|
||||||
)
|
)
|
||||||
-> Response
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
use InternalResponse::*;
|
use InternalResponse::*;
|
||||||
|
|
||||||
match internal_serve_all (root, method, uri, headers, hidden_path).await {
|
Ok (match internal_serve_all (root, method, uri, headers, hidden_path).await {
|
||||||
Favicon => serve_error (StatusCode::NotFound, ""),
|
Favicon => serve_error (StatusCode::NotFound, ""),
|
||||||
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
|
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
|
||||||
InvalidUri => serve_error (StatusCode::BadRequest, "Invalid URI"),
|
InvalidUri => serve_error (StatusCode::BadRequest, "Invalid URI"),
|
||||||
|
@ -634,7 +635,7 @@ pub async fn serve_all (
|
||||||
},
|
},
|
||||||
Redirect (location) => serve_307 (location),
|
Redirect (location) => serve_307 (location),
|
||||||
|
|
||||||
Root => serve_root (handlebars, server_info).await,
|
Root => serve_root (handlebars, server_info).await?,
|
||||||
ServeDir (ServeDirParams {
|
ServeDir (ServeDirParams {
|
||||||
path,
|
path,
|
||||||
dir,
|
dir,
|
||||||
|
@ -651,7 +652,7 @@ pub async fn serve_all (
|
||||||
MarkdownError::NotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"),
|
MarkdownError::NotUtf8 => serve_error (StatusCode::BadRequest, "File is not UTF-8"),
|
||||||
},
|
},
|
||||||
MarkdownPreview (s) => serve_html (s),
|
MarkdownPreview (s) => serve_html (s),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_templates (
|
pub fn load_templates (
|
||||||
|
|
|
@ -35,6 +35,8 @@ pub mod errors;
|
||||||
pub mod file_server;
|
pub mod file_server;
|
||||||
pub mod load_toml;
|
pub mod load_toml;
|
||||||
|
|
||||||
|
use errors::ServerError;
|
||||||
|
|
||||||
// Thanks to https://github.com/robsheldon/bad-passwords-index
|
// Thanks to https://github.com/robsheldon/bad-passwords-index
|
||||||
|
|
||||||
const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt");
|
const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt");
|
||||||
|
@ -61,7 +63,7 @@ struct ServerState {
|
||||||
async fn handle_req_resp <'a> (
|
async fn handle_req_resp <'a> (
|
||||||
state: &Arc <ServerState>,
|
state: &Arc <ServerState>,
|
||||||
req_resp: reqwest::Response
|
req_resp: reqwest::Response
|
||||||
) -> Result <(), ()> {
|
) -> Result <(), ServerError> {
|
||||||
//println! ("Step 1");
|
//println! ("Step 1");
|
||||||
|
|
||||||
let body = req_resp.bytes ().await.unwrap ();
|
let body = req_resp.bytes ().await.unwrap ();
|
||||||
|
@ -70,7 +72,7 @@ async fn handle_req_resp <'a> (
|
||||||
Ok (x) => x,
|
Ok (x) => x,
|
||||||
Err (e) => {
|
Err (e) => {
|
||||||
error! ("Can't parse wrapped requests: {:?}", e);
|
error! ("Can't parse wrapped requests: {:?}", e);
|
||||||
return Err (());
|
return Err (ServerError::CantParseWrappedRequests (e));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -97,7 +99,7 @@ async fn handle_req_resp <'a> (
|
||||||
&parts.uri,
|
&parts.uri,
|
||||||
&parts.headers,
|
&parts.headers,
|
||||||
state.hidden_path.as_deref ()
|
state.hidden_path.as_deref ()
|
||||||
).await;
|
).await.unwrap ();
|
||||||
|
|
||||||
let mut resp_req = state.client
|
let mut resp_req = state.client
|
||||||
.post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))
|
.post (&format! ("{}/http_response/{}", state.config.relay_url, req_id))
|
||||||
|
|
Loading…
Reference in New Issue