2019-01-23 11:49:10 +00:00
|
|
|
use crate::tools;
|
2018-11-15 16:07:10 +00:00
|
|
|
use crate::api::schema::*;
|
|
|
|
use crate::api::router::*;
|
|
|
|
use crate::api::config::*;
|
2019-01-31 11:22:00 +00:00
|
|
|
use crate::auth_helpers::*;
|
2019-01-26 14:08:02 +00:00
|
|
|
use super::environment::RestEnvironment;
|
2018-12-05 11:42:25 +00:00
|
|
|
use super::formatter::*;
|
2018-11-15 09:14:08 +00:00
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-11-15 07:18:48 +00:00
|
|
|
use std::sync::Arc;
|
2018-11-16 08:15:33 +00:00
|
|
|
use std::collections::HashMap;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
|
|
|
use failure::*;
|
2018-12-01 12:37:49 +00:00
|
|
|
use serde_json::{json, Value};
|
2018-11-16 08:15:33 +00:00
|
|
|
use url::form_urlencoded;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
|
|
|
use futures::future::{self, Either};
|
|
|
|
//use tokio::prelude::*;
|
|
|
|
//use tokio::timer::Delay;
|
|
|
|
use tokio::fs::File;
|
|
|
|
//use bytes::{BytesMut, BufMut};
|
|
|
|
|
|
|
|
//use hyper::body::Payload;
|
|
|
|
use hyper::http::request::Parts;
|
2018-11-15 09:57:52 +00:00
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
2018-11-15 07:18:48 +00:00
|
|
|
use hyper::service::{Service, NewService};
|
|
|
|
use hyper::rt::{Future, Stream};
|
|
|
|
use hyper::header;
|
|
|
|
|
2019-02-01 08:54:56 +00:00
|
|
|
extern "C" { fn tzset(); }
|
|
|
|
|
2018-11-15 09:18:01 +00:00
|
|
|
pub struct RestServer {
|
|
|
|
pub api_config: Arc<ApiConfig>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RestServer {
|
|
|
|
|
|
|
|
pub fn new(api_config: ApiConfig) -> Self {
|
|
|
|
Self { api_config: Arc::new(api_config) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NewService for RestServer
|
|
|
|
{
|
|
|
|
type ReqBody = Body;
|
|
|
|
type ResBody = Body;
|
|
|
|
type Error = hyper::Error;
|
|
|
|
type InitError = hyper::Error;
|
|
|
|
type Service = ApiService;
|
|
|
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError> + Send>;
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
Box::new(future::ok(ApiService { api_config: self.api_config.clone() }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ApiService {
|
|
|
|
pub api_config: Arc<ApiConfig>,
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:07:34 +00:00
|
|
|
impl ApiService {
|
|
|
|
fn log_response(path: &str, resp: &Response<Body>) {
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-02-14 15:04:24 +00:00
|
|
|
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-02-14 12:07:34 +00:00
|
|
|
let status = resp.status();
|
2019-02-14 12:28:41 +00:00
|
|
|
|
2019-02-14 12:07:34 +00:00
|
|
|
if !status.is_success() {
|
|
|
|
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
|
|
|
let client = "unknown"; // fixme: howto get peer_addr ?
|
|
|
|
let message = "request failed";
|
|
|
|
|
|
|
|
log::error!("{}: {} {}: [client {}] {}", path, status.as_str(), reason, client, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 09:18:01 +00:00
|
|
|
|
|
|
|
impl Service for ApiService {
|
|
|
|
type ReqBody = Body;
|
|
|
|
type ResBody = Body;
|
|
|
|
type Error = hyper::Error;
|
|
|
|
type Future = Box<Future<Item = Response<Body>, Error = Self::Error> + Send>;
|
|
|
|
|
|
|
|
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
|
2019-02-14 12:07:34 +00:00
|
|
|
let path = req.uri().path().to_owned();
|
|
|
|
Box::new(handle_request(self.api_config.clone(), req).then(move |result| {
|
2018-11-15 09:18:01 +00:00
|
|
|
match result {
|
2019-02-14 12:07:34 +00:00
|
|
|
Ok(res) => {
|
|
|
|
Self::log_response(&path, &res);
|
|
|
|
Ok::<_, hyper::Error>(res)
|
|
|
|
}
|
2018-11-15 09:18:01 +00:00
|
|
|
Err(err) => {
|
2019-02-13 13:31:43 +00:00
|
|
|
if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
2018-11-15 09:18:01 +00:00
|
|
|
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
|
|
|
*resp.status_mut() = apierr.code;
|
2019-02-14 12:07:34 +00:00
|
|
|
Self::log_response(&path, &resp);
|
2018-11-15 09:18:01 +00:00
|
|
|
Ok(resp)
|
|
|
|
} else {
|
|
|
|
let mut resp = Response::new(Body::from(err.to_string()));
|
|
|
|
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
2019-02-14 12:07:34 +00:00
|
|
|
Self::log_response(&path, &resp);
|
2018-11-15 09:18:01 +00:00
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 09:25:59 +00:00
|
|
|
fn get_request_parameters_async(
|
|
|
|
info: &'static ApiMethod,
|
2018-11-15 07:18:48 +00:00
|
|
|
parts: Parts,
|
|
|
|
req_body: Body,
|
2018-11-16 08:15:33 +00:00
|
|
|
uri_param: HashMap<String, String>,
|
2018-11-15 09:25:59 +00:00
|
|
|
) -> Box<Future<Item = Value, Error = failure::Error> + Send>
|
2018-11-15 07:18:48 +00:00
|
|
|
{
|
|
|
|
let resp = req_body
|
2018-11-15 09:42:01 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err)))
|
2018-11-15 07:18:48 +00:00
|
|
|
.fold(Vec::new(), |mut acc, chunk| {
|
|
|
|
if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
|
|
|
|
acc.extend_from_slice(&*chunk);
|
|
|
|
Ok(acc)
|
|
|
|
}
|
2018-11-15 09:42:01 +00:00
|
|
|
else { Err(http_err!(BAD_REQUEST, format!("Request body too large"))) }
|
2018-11-15 07:18:48 +00:00
|
|
|
})
|
|
|
|
.and_then(move |body| {
|
|
|
|
|
2019-01-08 13:22:43 +00:00
|
|
|
let utf8 = std::str::from_utf8(&body)?;
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2018-11-16 08:15:33 +00:00
|
|
|
let mut param_list: Vec<(String, String)> = vec![];
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-01-08 13:22:43 +00:00
|
|
|
if utf8.len() > 0 {
|
|
|
|
for (k, v) in form_urlencoded::parse(utf8.as_bytes()).into_owned() {
|
2018-11-16 08:15:33 +00:00
|
|
|
param_list.push((k, v));
|
|
|
|
}
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(query_str) = parts.uri.query() {
|
2018-11-16 08:15:33 +00:00
|
|
|
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
|
2018-12-05 11:42:25 +00:00
|
|
|
if k == "_dc" { continue; } // skip extjs "disable cache" parameter
|
2018-11-16 08:15:33 +00:00
|
|
|
param_list.push((k, v));
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 08:15:33 +00:00
|
|
|
for (k, v) in uri_param {
|
|
|
|
param_list.push((k.clone(), v.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = parse_parameter_strings(¶m_list, &info.parameters, true)?;
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
Ok(params)
|
|
|
|
});
|
|
|
|
|
|
|
|
Box::new(resp)
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:04:24 +00:00
|
|
|
struct NoLogExtension();
|
|
|
|
|
2019-01-28 17:22:16 +00:00
|
|
|
fn proxy_protected_request(
|
2019-02-01 08:54:56 +00:00
|
|
|
info: &'static ApiMethod,
|
2019-01-28 17:06:42 +00:00
|
|
|
mut parts: Parts,
|
2019-01-28 16:30:39 +00:00
|
|
|
req_body: Body,
|
|
|
|
) -> BoxFut
|
|
|
|
{
|
|
|
|
|
2019-01-28 17:06:42 +00:00
|
|
|
let mut uri_parts = parts.uri.clone().into_parts();
|
|
|
|
|
|
|
|
uri_parts.scheme = Some(http::uri::Scheme::HTTP);
|
|
|
|
uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
|
|
|
|
let new_uri = http::Uri::from_parts(uri_parts).unwrap();
|
|
|
|
|
|
|
|
parts.uri = new_uri;
|
|
|
|
|
|
|
|
let request = Request::from_parts(parts, req_body);
|
|
|
|
|
|
|
|
let resp = hyper::client::Client::new()
|
|
|
|
.request(request)
|
2019-02-14 12:28:41 +00:00
|
|
|
.map_err(|e| Error::from(e))
|
|
|
|
.map(|mut resp| {
|
2019-02-14 15:04:24 +00:00
|
|
|
resp.extensions_mut().insert(NoLogExtension);
|
2019-02-14 12:28:41 +00:00
|
|
|
resp
|
|
|
|
});
|
2019-01-28 17:06:42 +00:00
|
|
|
|
2019-02-01 08:54:56 +00:00
|
|
|
let resp = if info.reload_timezone {
|
|
|
|
Either::A(resp.then(|resp| {unsafe { tzset() }; resp }))
|
|
|
|
} else {
|
|
|
|
Either::B(resp)
|
|
|
|
};
|
|
|
|
|
2019-01-28 17:06:42 +00:00
|
|
|
return Box::new(resp);
|
2019-01-28 16:30:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 09:25:59 +00:00
|
|
|
fn handle_sync_api_request(
|
2019-01-27 09:18:52 +00:00
|
|
|
mut rpcenv: RestEnvironment,
|
2018-11-15 09:25:59 +00:00
|
|
|
info: &'static ApiMethod,
|
2018-12-05 11:42:25 +00:00
|
|
|
formatter: &'static OutputFormatter,
|
2018-11-15 07:18:48 +00:00
|
|
|
parts: Parts,
|
|
|
|
req_body: Body,
|
2018-11-16 08:15:33 +00:00
|
|
|
uri_param: HashMap<String, String>,
|
2018-11-15 09:25:59 +00:00
|
|
|
) -> BoxFut
|
2018-11-15 07:18:48 +00:00
|
|
|
{
|
2018-11-16 08:15:33 +00:00
|
|
|
let params = get_request_parameters_async(info, parts, req_body, uri_param);
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2019-01-31 13:34:21 +00:00
|
|
|
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
let resp = params
|
|
|
|
.and_then(move |params| {
|
2019-01-31 13:34:21 +00:00
|
|
|
let mut delay = false;
|
2019-01-26 13:50:37 +00:00
|
|
|
let resp = match (info.handler)(params, info, &mut rpcenv) {
|
|
|
|
Ok(data) => (formatter.format_result)(data, &rpcenv),
|
2019-01-31 13:34:21 +00:00
|
|
|
Err(err) => {
|
|
|
|
if let Some(httperr) = err.downcast_ref::<HttpError>() {
|
|
|
|
if httperr.code == StatusCode::UNAUTHORIZED { delay = true; }
|
|
|
|
}
|
|
|
|
(formatter.format_error)(err)
|
|
|
|
}
|
2019-01-26 13:50:37 +00:00
|
|
|
};
|
2019-01-31 13:34:21 +00:00
|
|
|
|
2019-02-01 08:54:56 +00:00
|
|
|
if info.reload_timezone {
|
|
|
|
unsafe { tzset() };
|
|
|
|
}
|
|
|
|
|
2019-01-31 13:34:21 +00:00
|
|
|
if delay {
|
|
|
|
let delayed_response = tokio::timer::Delay::new(delay_unauth_time)
|
|
|
|
.map_err(|err| http_err!(INTERNAL_SERVER_ERROR, format!("tokio timer delay error: {}", err)))
|
|
|
|
.and_then(|_| Ok(resp));
|
|
|
|
|
|
|
|
Either::A(delayed_response)
|
|
|
|
} else {
|
|
|
|
Either::B(future::ok(resp))
|
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Box::new(resp)
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:42:43 +00:00
|
|
|
fn handle_async_api_request(
|
2019-01-27 09:18:52 +00:00
|
|
|
mut rpcenv: RestEnvironment,
|
2019-01-19 15:42:43 +00:00
|
|
|
info: &'static ApiAsyncMethod,
|
2019-01-16 12:58:36 +00:00
|
|
|
formatter: &'static OutputFormatter,
|
|
|
|
parts: Parts,
|
2019-01-14 11:26:04 +00:00
|
|
|
req_body: Body,
|
|
|
|
uri_param: HashMap<String, String>,
|
|
|
|
) -> BoxFut
|
|
|
|
{
|
|
|
|
// fixme: convert parameters to Json
|
|
|
|
let mut param_list: Vec<(String, String)> = vec![];
|
|
|
|
|
2019-01-16 12:58:36 +00:00
|
|
|
if let Some(query_str) = parts.uri.query() {
|
|
|
|
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
|
|
|
|
if k == "_dc" { continue; } // skip extjs "disable cache" parameter
|
|
|
|
param_list.push((k, v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
for (k, v) in uri_param {
|
|
|
|
param_list.push((k.clone(), v.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = match parse_parameter_strings(¶m_list, &info.parameters, true) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => {
|
2019-01-26 13:50:37 +00:00
|
|
|
let resp = (formatter.format_error)(Error::from(err));
|
2019-01-16 12:58:36 +00:00
|
|
|
return Box::new(future::ok(resp));
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-27 09:18:52 +00:00
|
|
|
match (info.handler)(parts, req_body, params, info, &mut rpcenv) {
|
2019-01-17 11:03:38 +00:00
|
|
|
Ok(future) => future,
|
|
|
|
Err(err) => {
|
2019-01-26 13:50:37 +00:00
|
|
|
let resp = (formatter.format_error)(Error::from(err));
|
2019-01-17 11:03:38 +00:00
|
|
|
Box::new(future::ok(resp))
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 12:37:49 +00:00
|
|
|
fn get_index() -> BoxFut {
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
let nodename = tools::nodename();
|
2019-01-30 14:14:20 +00:00
|
|
|
let username = ""; // fixme: implement real auth
|
|
|
|
let token = "";
|
2018-12-01 12:37:49 +00:00
|
|
|
|
|
|
|
let setup = json!({
|
|
|
|
"Setup": { "auth_cookie_name": "PBSAuthCookie" },
|
|
|
|
"NodeName": nodename,
|
|
|
|
"UserName": username,
|
2019-01-23 11:49:10 +00:00
|
|
|
"CSRFPreventionToken": token,
|
2018-12-01 12:37:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let index = format!(r###"
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
|
|
|
|
<title>Proxmox Backup Server</title>
|
2018-12-01 14:21:25 +00:00
|
|
|
<link rel="icon" sizes="128x128" href="/images/logo-128.png" />
|
2018-12-01 12:37:49 +00:00
|
|
|
<link rel="apple-touch-icon" sizes="128x128" href="/pve2/images/logo-128.png" />
|
2018-12-01 14:21:25 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="/extjs/theme-crisp/resources/theme-crisp-all.css" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="/extjs/crisp/resources/charts-all.css" />
|
2018-12-01 12:37:49 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="/fontawesome/css/font-awesome.css" />
|
|
|
|
<script type='text/javascript'> function gettext(buf) {{ return buf; }} </script>
|
2018-12-01 14:21:25 +00:00
|
|
|
<script type="text/javascript" src="/extjs/ext-all-debug.js"></script>
|
|
|
|
<script type="text/javascript" src="/extjs/charts-debug.js"></script>
|
2018-12-01 12:37:49 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
Proxmox = {};
|
|
|
|
</script>
|
2018-12-01 14:21:25 +00:00
|
|
|
<script type="text/javascript" src="/widgettoolkit/proxmoxlib.js"></script>
|
|
|
|
<script type="text/javascript" src="/extjs/locale/locale-en.js"></script>
|
2018-12-01 12:37:49 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
Ext.History.fieldid = 'x-history-field';
|
|
|
|
</script>
|
2018-12-04 16:53:10 +00:00
|
|
|
<script type="text/javascript" src="/js/proxmox-backup-gui.js"></script>
|
2018-12-01 12:37:49 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<!-- Fields required for history management -->
|
|
|
|
<form id="history-form" class="x-hidden">
|
|
|
|
<input type="hidden" id="x-history-field"/>
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"###, setup.to_string());
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
let resp = Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, "text/html")
|
|
|
|
.body(index.into())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Box::new(future::ok(resp))
|
2018-12-01 12:37:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
|
|
|
|
|
|
|
|
if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
|
|
|
|
return match ext {
|
|
|
|
"css" => ("text/css", false),
|
|
|
|
"html" => ("text/html", false),
|
|
|
|
"js" => ("application/javascript", false),
|
|
|
|
"json" => ("application/json", false),
|
|
|
|
"map" => ("application/json", false),
|
|
|
|
"png" => ("image/png", true),
|
|
|
|
"ico" => ("image/x-icon", true),
|
|
|
|
"gif" => ("image/gif", true),
|
|
|
|
"svg" => ("image/svg+xml", false),
|
|
|
|
"jar" => ("application/java-archive", true),
|
|
|
|
"woff" => ("application/font-woff", true),
|
|
|
|
"woff2" => ("application/font-woff2", true),
|
|
|
|
"ttf" => ("application/font-snft", true),
|
|
|
|
"pdf" => ("application/pdf", true),
|
|
|
|
"epub" => ("application/epub+zip", true),
|
|
|
|
"mp3" => ("audio/mpeg", true),
|
|
|
|
"oga" => ("audio/ogg", true),
|
|
|
|
"tgz" => ("application/x-compressed-tar", true),
|
|
|
|
_ => ("application/octet-stream", false),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
("application/octet-stream", false)
|
|
|
|
}
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
fn simple_static_file_download(filename: PathBuf) -> BoxFut {
|
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
let (content_type, _nocomp) = extension_to_content_type(&filename);
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
Box::new(File::open(filename)
|
2018-11-15 09:42:01 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))
|
2018-12-02 10:00:52 +00:00
|
|
|
.and_then(move |file| {
|
2018-11-15 07:18:48 +00:00
|
|
|
let buf: Vec<u8> = Vec::new();
|
|
|
|
tokio::io::read_to_end(file, buf)
|
2018-11-15 09:42:01 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))
|
2018-12-02 10:00:52 +00:00
|
|
|
.and_then(move |data| {
|
|
|
|
let mut response = Response::new(data.1.into());
|
|
|
|
response.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static(content_type));
|
|
|
|
Ok(response)
|
|
|
|
})
|
2018-11-15 07:18:48 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chuncked_static_file_download(filename: PathBuf) -> BoxFut {
|
|
|
|
|
2018-12-02 10:00:52 +00:00
|
|
|
let (content_type, _nocomp) = extension_to_content_type(&filename);
|
|
|
|
|
2018-11-15 07:18:48 +00:00
|
|
|
Box::new(File::open(filename)
|
2018-11-15 09:42:01 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))
|
2018-12-02 10:00:52 +00:00
|
|
|
.and_then(move |file| {
|
2019-01-20 13:28:06 +00:00
|
|
|
let payload = tokio::codec::FramedRead::new(file, tokio::codec::BytesCodec::new()).
|
2018-11-15 07:18:48 +00:00
|
|
|
map(|bytes| {
|
|
|
|
//sigh - howto avoid copy here? or the whole map() ??
|
|
|
|
hyper::Chunk::from(bytes.to_vec())
|
|
|
|
});
|
|
|
|
let body = Body::wrap_stream(payload);
|
2018-12-02 10:00:52 +00:00
|
|
|
|
|
|
|
// fixme: set other headers ?
|
2018-11-15 07:18:48 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
2018-12-02 10:00:52 +00:00
|
|
|
.header(header::CONTENT_TYPE, content_type)
|
2018-11-15 07:18:48 +00:00
|
|
|
.body(body)
|
|
|
|
.unwrap())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_static_file_download(filename: PathBuf) -> BoxFut {
|
|
|
|
|
|
|
|
let response = tokio::fs::metadata(filename.clone())
|
2018-11-15 09:42:01 +00:00
|
|
|
.map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err)))
|
2018-11-15 07:18:48 +00:00
|
|
|
.and_then(|metadata| {
|
|
|
|
if metadata.len() < 1024*32 {
|
|
|
|
Either::A(simple_static_file_download(filename))
|
|
|
|
} else {
|
|
|
|
Either::B(chuncked_static_file_download(filename))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Box::new(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
|
|
|
|
|
|
|
|
let (parts, body) = req.into_parts();
|
|
|
|
|
|
|
|
let method = parts.method.clone();
|
|
|
|
let path = parts.uri.path();
|
|
|
|
|
|
|
|
// normalize path
|
|
|
|
// do not allow ".", "..", or hidden files ".XXXX"
|
|
|
|
// also remove empty path components
|
|
|
|
|
|
|
|
let items = path.split('/');
|
|
|
|
let mut path = String::new();
|
|
|
|
let mut components = vec![];
|
|
|
|
|
|
|
|
for name in items {
|
|
|
|
if name.is_empty() { continue; }
|
|
|
|
if name.starts_with(".") {
|
2018-11-15 09:57:52 +00:00
|
|
|
return Box::new(future::err(http_err!(BAD_REQUEST, "Path contains illegal components.".to_string())));
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
path.push('/');
|
|
|
|
path.push_str(name);
|
|
|
|
components.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let comp_len = components.len();
|
|
|
|
|
|
|
|
println!("REQUEST {} {}", method, path);
|
|
|
|
println!("COMPO {:?}", components);
|
|
|
|
|
2019-01-28 16:30:39 +00:00
|
|
|
let env_type = api.env_type();
|
|
|
|
let mut rpcenv = RestEnvironment::new(env_type);
|
2019-01-27 09:18:52 +00:00
|
|
|
|
2019-01-31 11:22:00 +00:00
|
|
|
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
|
|
|
|
|
|
|
|
if let Some(raw_cookie) = parts.headers.get("COOKIE") {
|
|
|
|
if let Ok(cookie) = raw_cookie.to_str() {
|
|
|
|
if let Some(ticket) = tools::extract_auth_cookie(cookie, "PBSAuthCookie") {
|
|
|
|
if let Ok((_, Some(username))) = tools::ticket::verify_rsa_ticket(
|
|
|
|
public_auth_key(), "PBS", &ticket, None, -300, 3600*2) {
|
|
|
|
rpcenv.set_user(Some(username));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 11:10:38 +00:00
|
|
|
if comp_len >= 1 && components[0] == "api2" {
|
2018-11-15 07:18:48 +00:00
|
|
|
println!("GOT API REQUEST");
|
|
|
|
if comp_len >= 2 {
|
|
|
|
let format = components[1];
|
2018-12-05 11:42:25 +00:00
|
|
|
let formatter = match format {
|
|
|
|
"json" => &JSON_FORMATTER,
|
|
|
|
"extjs" => &EXTJS_FORMATTER,
|
|
|
|
_ => {
|
|
|
|
return Box::new(future::err(http_err!(BAD_REQUEST, format!("Unsupported output format '{}'.", format))));
|
|
|
|
}
|
|
|
|
};
|
2018-11-15 07:18:48 +00:00
|
|
|
|
2018-11-16 08:15:33 +00:00
|
|
|
let mut uri_param = HashMap::new();
|
|
|
|
|
2019-01-31 11:22:00 +00:00
|
|
|
if comp_len == 4 && components[2] == "access" && components[3] == "ticket" {
|
|
|
|
// explicitly allow those calls without auth
|
|
|
|
} else {
|
|
|
|
if let Some(_username) = rpcenv.get_user() {
|
|
|
|
// fixme: check permissions
|
|
|
|
} else {
|
|
|
|
// always delay unauthorized calls by 3 seconds (from start of request)
|
|
|
|
let resp = (formatter.format_error)(http_err!(UNAUTHORIZED, "permission check failed.".into()));
|
|
|
|
let delayed_response = tokio::timer::Delay::new(delay_unauth_time)
|
|
|
|
.map_err(|err| http_err!(INTERNAL_SERVER_ERROR, format!("tokio timer delay error: {}", err)))
|
|
|
|
.and_then(|_| Ok(resp));
|
|
|
|
|
|
|
|
return Box::new(delayed_response);
|
|
|
|
}
|
|
|
|
}
|
2019-01-27 09:42:45 +00:00
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
match api.find_method(&components[2..], method, &mut uri_param) {
|
|
|
|
MethodDefinition::None => {}
|
|
|
|
MethodDefinition::Simple(api_method) => {
|
2019-01-28 16:30:39 +00:00
|
|
|
if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
|
2019-02-01 08:54:56 +00:00
|
|
|
return proxy_protected_request(api_method, parts, body);
|
2019-01-28 16:30:39 +00:00
|
|
|
} else {
|
|
|
|
return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param);
|
|
|
|
}
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
2019-01-19 15:42:43 +00:00
|
|
|
MethodDefinition::Async(async_method) => {
|
2019-01-27 09:18:52 +00:00
|
|
|
return handle_async_api_request(rpcenv, async_method, formatter, parts, body, uri_param);
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// not Auth for accessing files!
|
|
|
|
|
2018-12-01 12:37:49 +00:00
|
|
|
if comp_len == 0 {
|
|
|
|
return get_index();
|
|
|
|
} else {
|
|
|
|
let filename = api.find_alias(&components);
|
|
|
|
return handle_static_file_download(filename);
|
|
|
|
}
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 09:57:52 +00:00
|
|
|
Box::new(future::err(http_err!(NOT_FOUND, "Path not found.".to_string())))
|
2018-11-15 07:18:48 +00:00
|
|
|
}
|